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