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