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