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