]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/backend/circ/circ_lib.js
updated hold permit to check correct lib, inserting location into script env
[Evergreen.git] / Open-ILS / src / javascript / backend / circ / circ_lib.js
1 load_lib('catalog/record_type.js');
2 load_lib('circ/circ_groups.js');
3
4
5 try {
6         if( environment.copy ) {
7                 environment.copy.fetchBestHold = function() {
8                         var key = scratchKey();
9                         environment.copy.__OILS_FUNC_fetch_best_hold(scratchPad(key));
10                         var val = getScratch(key);
11                         return (val) ? val : null;
12                 }
13         }
14 } catch(e) {}
15
16
17 /* ----------------------------------------------------------------------------- 
18         Collect all of the global variables
19         ----------------------------------------------------------------------------- */
20
21 /* the global result object.  Any data returned to the 
22         caller must be put into this object.  */
23 var result                              = environment.result = {};
24 result.event                    = 'SUCCESS';
25 result.events                   = [];
26 result.fatalEvents      = [];
27 result.infoEvents               = [];
28
29
30 /* Pull in any variables passed in from the calling process */
31 var copy                                        = environment.copy;
32 var volume                              = environment.volume;
33 var title                               = environment.title;
34 var recDescriptor               = environment.titleDescriptor;
35 var patron                              = environment.patron;
36 var patronItemsOut      = environment.patronItemsOut;
37 var patronOverdueCount  = environment.patronOverdueCount;
38 var patronFines         = environment.patronFines;
39 var isRenewal                   = environment.isRenewal;
40 var isPrecat                    = environment.isPrecat;
41 var currentLocation     = environment.location;
42
43
44
45 /* create some new vars based on the data we have wherever possible */
46 var patronProfile;
47 var copyStatus;
48 var marcXMLDoc;
49
50 if( patron && patron.profile ) 
51         patronProfile = patron.profile.name;
52 if( copy && copy.status ) 
53         copyStatus = copy.status.name;
54 if( title && title.marc )
55         marcXMLDoc = new XML(title.marc);
56
57
58
59 /* copy the group tree into some other useful data structures */
60 var groupTree           = environment.groupTree;
61 var groupList           = {};
62 var groupIDList = {};
63 flattenGroupTree(groupTree);
64
65
66
67
68
69
70 /* ----------------------------------------------------------------------------- 
71         Define all of the utility functions
72         ----------------------------------------------------------------------------- */
73
74
75
76 /* useful functions for creating wrappers around system functions */
77 var __scratchKey = 0;
78 var __SCRATCH = {};
79 function scratchKey()           { return '_' + __scratchKey++; };
80 function scratchPad(key)        { return '__SCRATCH.'+ key; }
81 function getScratch(key)        { return __SCRATCH[ key ]; }
82 function scratchClear()         { for( var o in __SCRATCH ) __SCRATCH[o] = null; }
83
84
85 /* note: returns false if the value is 'f' or 'F' ... */
86 function isTrue(d) {
87         if(     d && 
88                         d != "0" && 
89                         d != "f" &&
90                         d != "F" )
91                         return true;
92         return false;
93 }
94
95 /* Utility function for iterating over array */
96 function iterate( arr, callback ) {
97         for( var i = 0; i < arr.length; i++ ) 
98                 callback(arr[i]);
99 }
100
101
102 /**
103   * returns a list of items that when passed to the callback 
104   * 'func' returned true returns null if none were found
105   */
106 function grep( arr, func ) {
107         var results = [];
108         iterate( arr, 
109                 function(d) {
110                         if( func(d) ) 
111                                 results.push(d);
112                 }
113         );
114         if(results.length > 0)  
115                 return results;
116         return null;
117 }
118
119
120
121 function flattenGroupTree(node) {
122         if(!node) return null;
123         groupList[node.name] = node;
124         groupIDList[node.id] = node;
125         iterate( node.children,
126                 function(n) {
127                         flattenGroupTree(n);
128                 }
129         );
130 }
131
132
133
134 /**
135   * Returns true if 'child' is equal or descends from 'parent'
136   * @param parent The name of the parent group
137   * @param child The name of the child group
138   */
139 function isGroupDescendant( parent, child ) {
140         return __isGroupDescendant(
141                 groupList[parent],
142                 groupList[child]);
143 }
144
145
146
147 /**
148   * Returns true if 'child' is equal or descends from 'parent'
149   * @param parent The node of the parent group
150   * @param child The node of the child group
151   */
152 function __isGroupDescendant( parent, child ) {
153         if (parent.id == child.id) return true;
154         var node = child;
155         while( (node = groupIDList[node.parent]) ) {
156                 if( node.id == parent.id ) 
157                         return true;
158         }
159         return false;
160 }
161
162
163 function getMARCItemType() {
164
165         if(     copy &&
166                         copy.circ_as_type &&
167                         copy.circ_as_type != 'undef' )
168                 return copy.circ_as_type;
169         
170         return (marcXMLDoc) ? extractFixedField(marcXMLDoc, 'Type') : "";
171 }
172
173
174 function isOrgDescendent( parentName, childId ) {
175         var key = scratchKey();
176         __OILS_FUNC_isOrgDescendent(scratchPad(key), parentName, childId);
177         var val = getScratch(key);
178         if( val == '1' ) return true;
179         return false;
180 }
181
182 /* useful for testing */
183 function die(msg) {
184         log_error("die(): "+msg);
185         log_stderr("die(): "+msg);
186         var foo = null;
187         var baz = foo.bar;
188 }
189
190
191
192 /*
193 - at some point we should add a library of objects that map 
194 codes to names (item_form, item_type, etc.)
195 load_lib('item_form_map.js');
196 var form_name = item_form_map[env.record_descriptor.item_form];
197 */
198
199
200
201 /* logs a load of info */
202 function log_vars( prefix ) {
203         var str = prefix + ' : ';
204
205         if(patron) {
206                 str += ' Patron=' + patron.id;
207                 str += ', Patron Username='+ patron.usrname;
208                 str += ', Patron Profile Group='+ patronProfile;
209                 str += ', Patron Fines='        + patronFines;
210                 str += ', Patron OverdueCount=' + patronOverdueCount;
211                 str += ', Patron Items Out=' + patronItemsOut;
212
213                 try {
214                         str += ', Patron Barcode='      + patron.card.barcode;
215                         str += ', Patron Library='      + patron.home_ou.name;
216                 } catch(e) {}
217         }
218
219         if(copy)        {
220                 str += ', Copy=' + copy.id;
221                 str += ', Copy Barcode=' + copy.barcode;
222                 str += ', Copy status=' + copyStatus;
223
224                 try {
225                         str += ', Circ Lib=' + copy.circ_lib.shortname;
226                         str += ', Copy location=' + copy.location.name;
227                 } catch(e) {}
228         }
229
230         if(volume)                      str += ', Volume='      + volume.id;
231         if(title)                       str += ', Record='      + title.id;
232
233         if(recDescriptor)       {
234                 str += ', Record Descriptor=' + recDescriptor.id;
235                 str += ', Item Type='                   + recDescriptor.item_type;
236                 str += ', Item Form='                   + recDescriptor.item_form;
237                 str += ', Item Lang='                   + recDescriptor.item_lang;
238                 str += ', Item Audience='               + recDescriptor.audience;
239         }
240
241         str += ' Is Renewal: '  + ( (isTrue(isRenewal)) ? "yes" : "no" );
242
243         log_debug(str);
244 }
245
246