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 var val = getScratch(key);
213 return (val) ? val : {};
216 /* useful for testing */
218 log_error("die(): "+msg);
219 log_stderr("die(): "+msg);
226 /* logs a load of info */
227 function log_vars( prefix ) {
228 var str = prefix + ' : ';
231 str += ' Patron=' + patron.id;
232 str += ', Patron_Username='+ patron.usrname;
233 str += ', Patron_Profile_Group='+ patronProfile;
234 str += ', Patron_Fines=' + patronFines;
235 str += ', Patron_OverdueCount=' + patronOverdueCount;
236 str += ', Patron_Items_Out=' + patronItemsOut;
239 str += ', Patron_Barcode=' + patron.card.barcode;
240 str += ', Patron_Library=' + patron.home_ou.name;
244 if(requestor.id != patron.id)
245 str+= ' Requestor='+requestor.usrname;
248 str += ', Copy=' + copy.id;
249 str += ', Copy_Barcode=' + copy.barcode;
250 str += ', Copy_status=' + copyStatus;
251 str += (copy.circ_modifier) ? ', Circ_Mod=' + copy.circ_modifier : '';
254 str += ', Circ_Lib=' + copy.circ_lib.shortname;
255 str += ', Copy_location=' + copy.location.name;
260 str += ', Item_Owning_lib=' + volume.owning_lib;
261 str += ', Volume=' + volume.id;
264 if(title) str += ', Record=' + title.id;
267 str += ', Record_Descriptor=' + recDescriptor.id;
268 str += ', Item_Type=' + recDescriptor.item_type;
269 str += ', Item_Form=' + recDescriptor.item_form;
270 str += ', Item_Lang=' + recDescriptor.item_lang;
271 str += ', Item_Audience=' + recDescriptor.audience;
274 str += ', Is_Renewal: ' + ( (isTrue(isRenewal)) ? "yes" : "no" );
275 str += ', Is_Precat: ' + ( (isTrue(isPrecat)) ? "yes" : "no" );
276 str += (holdRequestLib) ? ', Hold_request_lib=' + holdRequestLib.shortname : '';
277 str += (holdPickupLib) ? ', Hold_Pickup_Lib=' + holdPickupLib : '';
285 * Returns config information for the requested group. If
286 * no config info exists for the requested group, then this
287 * function searches up the tree to find the config info
288 * for the nearest ancestor
289 * @param The name of the group who's config info to return
291 function findGroupConfig(name) {
292 if(!name) return null;
293 var node = groupList[name];
295 if( GROUP_CONFIG[node.name] ) {
296 debugGroupConfig(name, node.name, GROUP_CONFIG[node.name]);
297 return GROUP_CONFIG[node.name];
299 } while( (node = groupIDList[node.parent]) );
304 /** prints out the settings for the given group config **/
305 function debugGroupConfig(name, foundName, config) {
307 var str = "findGroupConfig('"+name+"'): returning config info for '"+ foundName +"': ";
308 for( var i in config )
309 str += i + '=' + config[i] + ' ';