1 load_lib('catalog/record_type.js');
2 load_lib('circ/circ_groups.js');
3 load_lib('JSON_v1.js');
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;
18 /* -----------------------------------------------------------------------------
19 Collect all of the global variables
20 ----------------------------------------------------------------------------- */
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';
27 result.fatalEvents = [];
28 result.infoEvents = [];
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;
50 /* create some new vars based on the data we have wherever possible */
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);
64 /* copy the group tree into some other useful data structures */
65 var groupTree = environment.groupTree;
68 flattenGroupTree(groupTree);
75 /* -----------------------------------------------------------------------------
76 Define all of the utility functions
77 ----------------------------------------------------------------------------- */
81 /* useful functions for creating wrappers around system functions */
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; }
90 /* note: returns false if the value is 'f' or 'F' ... */
100 /* Utility function for iterating over array */
101 function iterate( arr, callback ) {
102 for( var i = 0; i < arr.length; i++ )
108 * returns a list of items that when passed to the callback
109 * 'func' returned true returns null if none were found
111 function grep( arr, func ) {
119 if(results.length > 0)
126 function flattenGroupTree(node) {
127 if(!node) return null;
128 groupList[node.name] = node;
129 groupIDList[node.id] = node;
130 iterate( node.children,
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
144 function isGroupDescendant( parent, child ) {
145 log_debug("checking descendant p="+parent + " c=" + child);
146 return __isGroupDescendant(
151 function isGroupDescendantId( parentName, childId ) {
152 log_debug("checking descendant ID p="+parentName + " c=" + childId);
153 return __isGroupDescendant(
154 groupList[parentName],
155 groupIDList[childId]);
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
164 function __isGroupDescendant( parent, child ) {
165 if(!(parent && child)) return false;
166 if (parent.id == child.id) return true;
168 while( (node = groupIDList[node.parent]) ) {
169 if( node.id == parent.id )
176 function getMARCItemType() {
179 copy.circ_as_type != 'undef' )
180 return copy.circ_as_type;
182 return (marcXMLDoc) ? extractFixedField(marcXMLDoc, 'Type') : "";
186 function isOrgDescendent( parentName, childId ) {
187 var key = scratchKey();
188 __OILS_FUNC_isOrgDescendent(scratchPad(key), parentName, childId);
189 var val = getScratch(key);
190 if( val == '1' ) return true;
194 /* returns the number of unfulfilled holds open on this user */
195 function userHoldCount(userid) {
196 var key = scratchKey();
197 __OILS_FUNC_userHoldCount(scratchPad(key), userid);
198 return getScratch(key);
201 function hasCommonAncestor( org1, org2, depth ) {
202 var key = scratchKey();
203 __OILS_FUNC_hasCommonAncestor(scratchPad(key), org1, org2, depth);
204 var val = getScratch(key);
205 if( val == '1' ) return true;
209 /* returns a dictionary of circmod : count for checked out items */
210 function checkoutsByCircModifier(userid) {
211 var key = scratchKey();
212 __OILS_FUNC_userCircsByCircmod(scratchPad(key), userid);
213 var val = getScratch(key);
214 return (val) ? val : {};
217 /* useful for testing */
219 log_error("die(): "+msg);
220 log_stderr("die(): "+msg);
227 /* logs a load of info */
228 function log_vars( prefix ) {
229 var str = prefix + ' : ';
232 str += ' Patron=' + patron.id;
233 str += ', Patron_Username='+ patron.usrname;
234 str += ', Patron_Profile_Group='+ patronProfile;
235 str += ', Patron_Fines=' + patronFines;
236 str += ', Patron_OverdueCount=' + patronOverdueCount;
237 str += ', Patron_Items_Out=' + patronItemsOut;
240 str += ', Patron_Barcode=' + patron.card.barcode;
241 str += ', Patron_Library=' + patron.home_ou.name;
245 if(requestor.id != patron.id)
246 str+= ' Requestor='+requestor.usrname;
249 str += ', Copy=' + copy.id;
250 str += ', Copy_Barcode=' + copy.barcode;
251 str += ', Copy_status=' + copyStatus;
252 str += (copy.circ_modifier) ? ', Circ_Mod=' + copy.circ_modifier : '';
255 str += ', Circ_Lib=' + copy.circ_lib.shortname;
256 str += ', Copy_location=' + copy.location.name;
261 str += ', Item_Owning_lib=' + volume.owning_lib;
262 str += ', Volume=' + volume.id;
265 if(title) str += ', Record=' + title.id;
268 str += ', Record_Descriptor=' + recDescriptor.id;
269 str += ', Item_Type=' + recDescriptor.item_type;
270 str += ', Item_Form=' + recDescriptor.item_form;
271 str += ', Item_Lang=' + recDescriptor.item_lang;
272 str += ', Item_Audience=' + recDescriptor.audience;
275 str += ', Is_Renewal: ' + ( (isTrue(isRenewal)) ? "yes" : "no" );
276 str += ', Is_Precat: ' + ( (isTrue(isPrecat)) ? "yes" : "no" );
277 str += (holdRequestLib) ? ', Hold_request_lib=' + holdRequestLib.shortname : '';
278 str += (holdPickupLib) ? ', Hold_Pickup_Lib=' + holdPickupLib : '';
286 * Returns config information for the requested group. If
287 * no config info exists for the requested group, then this
288 * function searches up the tree to find the config info
289 * for the nearest ancestor
290 * @param The name of the group who's config info to return
292 function findGroupConfig(name) {
293 if(!name) return null;
294 var node = groupList[name];
296 if( GROUP_CONFIG[node.name] ) {
297 debugGroupConfig(name, node.name, GROUP_CONFIG[node.name]);
298 return GROUP_CONFIG[node.name];
300 } while( (node = groupIDList[node.parent]) );
305 /** prints out the settings for the given group config **/
306 function debugGroupConfig(name, foundName, config) {
308 var str = "findGroupConfig('"+name+"'): returning config info for '"+ foundName +"': ";
309 for( var i in config )
310 str += i + '=' + config[i] + ' ';