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.id == child.id) return true;
167 while( (node = groupIDList[node.parent]) ) {
168 if( node.id == parent.id )
175 function getMARCItemType() {
178 copy.circ_as_type != 'undef' )
179 return copy.circ_as_type;
181 return (marcXMLDoc) ? extractFixedField(marcXMLDoc, 'Type') : "";
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;
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);
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;
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);
215 /* useful for testing */
217 log_error("die(): "+msg);
218 log_stderr("die(): "+msg);
225 /* logs a load of info */
226 function log_vars( prefix ) {
227 var str = prefix + ' : ';
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;
238 str += ', Patron_Barcode=' + patron.card.barcode;
239 str += ', Patron_Library=' + patron.home_ou.name;
243 if(requestor.id != patron.id)
244 str+= ' Requestor='+requestor.usrname;
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 : '';
253 str += ', Circ_Lib=' + copy.circ_lib.shortname;
254 str += ', Copy_location=' + copy.location.name;
259 str += ', Item_Owning_lib=' + volume.owning_lib;
260 str += ', Volume=' + volume.id;
263 if(title) str += ', Record=' + title.id;
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;
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 : '';
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
290 function findGroupConfig(name) {
291 if(!name) return null;
292 var node = groupList[name];
294 if( GROUP_CONFIG[node.name] ) {
295 debugGroupConfig(name, node.name, GROUP_CONFIG[node.name]);
296 return GROUP_CONFIG[node.name];
298 } while( (node = groupIDList[node.parent]) );
303 /** prints out the settings for the given group config **/
304 function debugGroupConfig(name, foundName, config) {
306 var str = "findGroupConfig('"+name+"'): returning config info for '"+ foundName +"': ";
307 for( var i in config )
308 str += i + '=' + config[i] + ' ';