]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/backend/circ/circ_lib.js
more cleanup
[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 var holdRequestLib      = environment.requestLib;
43 var holdPickupLib       = environment.pickupLib; /* hold pickup lib */
44 var requestor = environment.requestor || patron;
45
46
47
48 /* create some new vars based on the data we have wherever possible */
49 var patronProfile;
50 var copyStatus;
51 var marcXMLDoc;
52
53 if( patron && patron.profile ) 
54         patronProfile = patron.profile.name;
55 if( copy && copy.status ) 
56         copyStatus = copy.status.name;
57 if( title && title.marc )
58         marcXMLDoc = new XML(title.marc);
59
60
61
62 /* copy the group tree into some other useful data structures */
63 var groupTree           = environment.groupTree;
64 var groupList           = {};
65 var groupIDList = {};
66 flattenGroupTree(groupTree);
67
68
69
70
71
72
73 /* ----------------------------------------------------------------------------- 
74         Define all of the utility functions
75         ----------------------------------------------------------------------------- */
76
77
78
79 /* useful functions for creating wrappers around system functions */
80 var __scratchKey = 0;
81 var __SCRATCH = {};
82 function scratchKey()           { return '_' + __scratchKey++; };
83 function scratchPad(key)        { return '__SCRATCH.'+ key; }
84 function getScratch(key)        { return __SCRATCH[ key ]; }
85 function scratchClear()         { for( var o in __SCRATCH ) __SCRATCH[o] = null; }
86
87
88 /* note: returns false if the value is 'f' or 'F' ... */
89 function isTrue(d) {
90         if(     d && 
91                         d != "0" && 
92                         d != "f" &&
93                         d != "F" )
94                         return true;
95         return false;
96 }
97
98 /* Utility function for iterating over array */
99 function iterate( arr, callback ) {
100         for( var i = 0; i < arr.length; i++ ) 
101                 callback(arr[i]);
102 }
103
104
105 /**
106   * returns a list of items that when passed to the callback 
107   * 'func' returned true returns null if none were found
108   */
109 function grep( arr, func ) {
110         var results = [];
111         iterate( arr, 
112                 function(d) {
113                         if( func(d) ) 
114                                 results.push(d);
115                 }
116         );
117         if(results.length > 0)  
118                 return results;
119         return null;
120 }
121
122
123
124 function flattenGroupTree(node) {
125         if(!node) return null;
126         groupList[node.name] = node;
127         groupIDList[node.id] = node;
128         iterate( node.children,
129                 function(n) {
130                         flattenGroupTree(n);
131                 }
132         );
133 }
134
135
136
137 /**
138   * Returns true if 'child' is equal or descends from 'parent'
139   * @param parent The name of the parent group
140   * @param child The name of the child group
141   */
142 function isGroupDescendant( parent, child ) {
143         log_debug("checking descendant p="+parent + " c=" + child);
144         return __isGroupDescendant(
145                 groupList[parent],
146                 groupList[child]);
147 }
148
149 function isGroupDescendantId( parentName, childId ) {
150         log_debug("checking descendant ID p="+parentName + " c=" + childId);
151         return __isGroupDescendant(
152                 groupList[parentName],
153                 groupIDList[childId]);
154 }
155
156
157 /**
158   * Returns true if 'child' is equal or descends from 'parent'
159   * @param parent The node of the parent group
160   * @param child The node of the child group
161   */
162 function __isGroupDescendant( parent, child ) {
163         if (parent.id == child.id) return true;
164         var node = child;
165         while( (node = groupIDList[node.parent]) ) {
166                 if( node.id == parent.id ) 
167                         return true;
168         }
169         return false;
170 }
171
172
173 function getMARCItemType() {
174         if(     copy &&
175                         copy.circ_as_type &&
176                         copy.circ_as_type != 'undef' )
177                 return copy.circ_as_type;
178         
179         return (marcXMLDoc) ? extractFixedField(marcXMLDoc, 'Type') : "";
180 }
181
182
183 function isOrgDescendent( parentName, childId ) {
184         var key = scratchKey();
185         __OILS_FUNC_isOrgDescendent(scratchPad(key), parentName, childId);
186         var val = getScratch(key);
187         if( val == '1' ) return true;
188         return false;
189 }
190
191 /* returns the number of unfulfilled holds open on this user */
192 function userHoldCount(userid) {
193    var key = scratchKey();
194    __OILS_FUNC_userHoldCount(scratchPad(key), userid);
195    return getScratch(key);
196 }
197
198 function hasCommonAncestor( org1, org2, depth ) {
199         var key = scratchKey();
200         __OILS_FUNC_hasCommonAncestor(scratchPad(key), org1, org2, depth);
201         var val = getScratch(key);
202         if( val == '1' ) return true;
203         return false;
204 }
205
206
207
208 /* useful for testing */
209 function die(msg) {
210         log_error("die(): "+msg);
211         log_stderr("die(): "+msg);
212         var foo = null;
213         var baz = foo.bar;
214 }
215
216
217
218 /* logs a load of info */
219 function log_vars( prefix ) {
220         var str = prefix + ' : ';
221
222         if(patron) {
223                 str += ' Patron=' + patron.id;
224                 str += ', Patron_Username='+ patron.usrname;
225                 str += ', Patron_Profile_Group='+ patronProfile;
226                 str += ', Patron_Fines='        + patronFines;
227                 str += ', Patron_OverdueCount=' + patronOverdueCount;
228                 str += ', Patron_Items_Out=' + patronItemsOut;
229
230                 try {
231                         str += ', Patron_Barcode='      + patron.card.barcode;
232                         str += ', Patron_Library='      + patron.home_ou.name;
233                 } catch(e) {}
234         }
235
236     if(requestor.id != patron.id) 
237         str+= ' Requestor='+requestor.usrname;
238
239         if(copy)        {
240                 str += ', Copy=' + copy.id;
241                 str += ', Copy_Barcode=' + copy.barcode;
242                 str += ', Copy_status=' + copyStatus;
243       str += (copy.circ_modifier) ? ', Circ_Mod=' + copy.circ_modifier : '';
244
245                 try {
246                         str += ', Circ_Lib=' + copy.circ_lib.shortname;
247                         str += ', Copy_location=' + copy.location.name;
248                 } catch(e) {}
249         }
250
251         if(volume) {
252         str += ', Item_Owning_lib=' + volume.owning_lib;
253         str += ', Volume='      + volume.id;
254     }
255
256         if(title) str += ', Record='    + title.id;
257
258         if(recDescriptor)       {
259                 str += ', Record_Descriptor=' + recDescriptor.id;
260                 str += ', Item_Type='                   + recDescriptor.item_type;
261                 str += ', Item_Form='                   + recDescriptor.item_form;
262                 str += ', Item_Lang='                   + recDescriptor.item_lang;
263                 str += ', Item_Audience='               + recDescriptor.audience;
264         }
265
266         str += ', Is_Renewal: ' + ( (isTrue(isRenewal)) ? "yes" : "no" );
267         str += ', Is_Precat: '  + ( (isTrue(isPrecat)) ? "yes" : "no" );
268         str += (holdRequestLib) ? ', Hold_request_lib=' + holdRequestLib.shortname : '';
269     str += (holdPickupLib) ? ', Hold_Pickup_Lib=' + holdPickupLib : '';
270
271         log_info(str);
272 }
273
274
275
276 /**
277   * Returns config information for the requested group.  If 
278   * no config info exists for the requested group, then this
279   * function searches up the tree to find the config info 
280   * for the nearest ancestor
281   * @param The name of the group who's config info to return
282   */
283 function findGroupConfig(name) {
284         if(!name) return null;
285         var node = groupList[name];
286         do {
287                 if( GROUP_CONFIG[node.name] ) {
288                         debugGroupConfig(name, node.name, GROUP_CONFIG[node.name]);
289                         return GROUP_CONFIG[node.name];
290                 }
291         } while( (node = groupIDList[node.parent]) );
292         return null;
293 }
294
295
296 /** prints out the settings for the given group config **/
297 function debugGroupConfig(name, foundName, config) {
298         if(!config) return;
299         var str = "findGroupConfig('"+name+"'): returning config info for '"+ foundName +"': ";
300         for( var i in config ) 
301                 str += i + '=' + config[i] + '  ';
302         log_debug(str);
303 }
304
305