]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/backend/circ/circ_lib.js
added die() function for debugging
[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
42
43
44 /* create some new vars based on the data we have wherever possible */
45 var patronProfile;
46 var copyStatus;
47 var marcXMLDoc;
48
49 if( patron && patron.profile ) 
50         patronProfile = patron.profile.name;
51 if( copy && copy.status ) 
52         copyStatus = copy.status.name;
53 if( title && title.marc )
54         marcXMLDoc = new XML(title.marc);
55
56
57
58 /* copy the group tree into some other useful data structures */
59 var groupTree           = environment.groupTree;
60 var groupList           = {};
61 var groupIDList = {};
62 flattenGroupTree(groupTree);
63
64
65
66
67
68
69 /* ----------------------------------------------------------------------------- 
70         Define all of the utility functions
71         ----------------------------------------------------------------------------- */
72
73
74
75 /* useful functions for creating wrappers around system functions */
76 var __scratchKey = 0;
77 var __SCRATCH = {};
78 function scratchKey()           { return '_' + __scratchKey++; };
79 function scratchPad(key)        { return '__SCRATCH.'+ key; }
80 function getScratch(key)        { return __SCRATCH[ key ]; }
81 function scratchClear()         { for( var o in __SCRATCH ) __SCRATCH[o] = null; }
82
83
84 /* note: returns false if the value is 'f' or 'F' ... */
85 function isTrue(d) {
86         if(     d && 
87                         d != "0" && 
88                         d != "f" &&
89                         d != "F" )
90                         return true;
91         return false;
92 }
93
94 /* Utility function for iterating over array */
95 function iterate( arr, callback ) {
96         for( var i = 0; i < arr.length; i++ ) 
97                 callback(arr[i]);
98 }
99
100
101 /**
102   * returns a list of items that when passed to the callback 
103   * 'func' returned true returns null if none were found
104   */
105 function grep( arr, func ) {
106         var results = [];
107         iterate( arr, 
108                 function(d) {
109                         if( func(d) ) 
110                                 results.push(d);
111                 }
112         );
113         if(results.length > 0)  
114                 return results;
115         return null;
116 }
117
118
119
120 function flattenGroupTree(node) {
121         if(!node) return null;
122         groupList[node.name] = node;
123         groupIDList[node.id] = node;
124         iterate( node.children,
125                 function(n) {
126                         flattenGroupTree(n);
127                 }
128         );
129 }
130
131
132
133 /**
134   * Returns true if 'child' is equal or descends from 'parent'
135   * @param parent The name of the parent group
136   * @param child The name of the child group
137   */
138 function isGroupDescendant( parent, child ) {
139         return __isGroupDescendant(
140                 groupList[parent],
141                 groupList[child]);
142 }
143
144
145
146 /**
147   * Returns true if 'child' is equal or descends from 'parent'
148   * @param parent The node of the parent group
149   * @param child The node of the child group
150   */
151 function __isGroupDescendant( parent, child ) {
152         if (parent.id == child.id) return true;
153         var node = child;
154         while( (node = groupIDList[node.parent]) ) {
155                 if( node.id == parent.id ) 
156                         return true;
157         }
158         return false;
159 }
160
161
162 function getMARCItemType() {
163
164         if(     copy &&
165                         copy.circ_as_type &&
166                         copy.circ_as_type != 'undef' )
167                 return copy.circ_as_type;
168         
169         return (marcXMLDoc) ? extractFixedField(marcXMLDoc, 'Type') : "";
170 }
171
172
173 function isOrgDescendent( parentName, childId ) {
174         var key = scratchKey();
175         __OILS_FUNC_isOrgDescendent(scratchPad(key), parentName, childId);
176         var val = getScratch(key);
177         if( val == '1' ) return true;
178         return false;
179 }
180
181 /* useful for testing */
182 function die(msg) {
183         log_error("die(): "+msg);
184         log_stderr("die(): "+msg);
185         var foo = null;
186         var baz = foo.bar;
187 }
188
189
190
191 /*
192 - at some point we should add a library of objects that map 
193 codes to names (item_form, item_type, etc.)
194 load_lib('item_form_map.js');
195 var form_name = item_form_map[env.record_descriptor.item_form];
196 */
197
198
199
200 /* logs a load of info */
201 function log_vars( prefix ) {
202         var str = prefix + ' : ';
203
204         if(patron) {
205                 str += ' Patron=' + patron.id;
206                 str += ', Patron Username='+ patron.usrname;
207                 str += ', Patron Profile Group='+ patronProfile;
208                 str += ', Patron Fines='        + patronFines;
209                 str += ', Patron OverdueCount=' + patronOverdueCount;
210                 str += ', Patron Items Out=' + patronItemsOut;
211
212                 try {
213                         str += ', Patron Barcode='      + patron.card.barcode;
214                         str += ', Patron Library='      + patron.home_ou.name;
215                 } catch(e) {}
216         }
217
218         if(copy)        {
219                 str += ', Copy=' + copy.id;
220                 str += ', Copy Barcode=' + copy.barcode;
221                 str += ', Copy status=' + copyStatus;
222
223                 try {
224                         str += ', Circ Lib=' + copy.circ_lib.shortname;
225                         str += ', Copy location=' + copy.location.name;
226                 } catch(e) {}
227         }
228
229         if(volume)                      str += ', Volume='      + volume.id;
230         if(title)                       str += ', Record='      + title.id;
231
232         if(recDescriptor)       {
233                 str += ', Record Descriptor=' + recDescriptor.id;
234                 str += ', Item Type='                   + recDescriptor.item_type;
235                 str += ', Item Form='                   + recDescriptor.item_form;
236                 str += ', Item Lang='                   + recDescriptor.item_lang;
237                 str += ', Item Audience='               + recDescriptor.audience;
238         }
239
240         str += ' Is Renewal: '  + ( (isTrue(isRenewal)) ? "yes" : "no" );
241
242         log_debug(str);
243 }
244
245