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;
49 /* create some new vars based on the data we have wherever possible */
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);
63 /* copy the group tree into some other useful data structures */
64 var groupTree = environment.groupTree;
67 flattenGroupTree(groupTree);
74 /* -----------------------------------------------------------------------------
75 Define all of the utility functions
76 ----------------------------------------------------------------------------- */
80 /* useful functions for creating wrappers around system functions */
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; }
89 /* note: returns false if the value is 'f' or 'F' ... */
99 /* Utility function for iterating over array */
100 function iterate( arr, callback ) {
101 for( var i = 0; i < arr.length; i++ )
107 * returns a list of items that when passed to the callback
108 * 'func' returned true returns null if none were found
110 function grep( arr, func ) {
118 if(results.length > 0)
125 function flattenGroupTree(node) {
126 if(!node) return null;
127 groupList[node.name] = node;
128 groupIDList[node.id] = node;
129 iterate( node.children,
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
143 function isGroupDescendant( parent, child ) {
144 log_debug("checking descendant p="+parent + " c=" + child);
145 return __isGroupDescendant(
150 function isGroupDescendantId( parentName, childId ) {
151 log_debug("checking descendant ID p="+parentName + " c=" + childId);
152 return __isGroupDescendant(
153 groupList[parentName],
154 groupIDList[childId]);
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
163 function __isGroupDescendant( parent, child ) {
164 if (parent.id == child.id) return true;
166 while( (node = groupIDList[node.parent]) ) {
167 if( node.id == parent.id )
174 function getMARCItemType() {
177 copy.circ_as_type != 'undef' )
178 return copy.circ_as_type;
180 return (marcXMLDoc) ? extractFixedField(marcXMLDoc, 'Type') : "";
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;
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);
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;
209 /* useful for testing */
211 log_error("die(): "+msg);
212 log_stderr("die(): "+msg);
219 /* logs a load of info */
220 function log_vars( prefix ) {
221 var str = prefix + ' : ';
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;
232 str += ', Patron_Barcode=' + patron.card.barcode;
233 str += ', Patron_Library=' + patron.home_ou.name;
237 if(requestor.id != patron.id)
238 str+= ' Requestor='+requestor.usrname;
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 : '';
247 str += ', Circ_Lib=' + copy.circ_lib.shortname;
248 str += ', Copy_location=' + copy.location.name;
253 str += ', Item_Owning_lib=' + volume.owning_lib;
254 str += ', Volume=' + volume.id;
257 if(title) str += ', Record=' + title.id;
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;
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 : '';
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
284 function findGroupConfig(name) {
285 if(!name) return null;
286 var node = groupList[name];
288 if( GROUP_CONFIG[node.name] ) {
289 debugGroupConfig(name, node.name, GROUP_CONFIG[node.name]);
290 return GROUP_CONFIG[node.name];
292 } while( (node = groupIDList[node.parent]) );
297 /** prints out the settings for the given group config **/
298 function debugGroupConfig(name, foundName, config) {
300 var str = "findGroupConfig('"+name+"'): returning config info for '"+ foundName +"': ";
301 for( var i in config )
302 str += i + '=' + config[i] + ' ';