]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/backend/circ/circ_lib.js
re-organizing, testing, adding functions, adding group configs
[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
52
53
54
55
56
57 /* ----------------------------------------------------------------------------- 
58         Define all of the utility functions
59         ----------------------------------------------------------------------------- */
60
61
62
63 /* useful functions for creating wrappers around system functions */
64 var __scratchKey = 0;
65 var __SCRATCH = {};
66 function scratchKey()           { return '_' + __scratchKey++; };
67 function scratchPad(key)        { return '__SCRATCH.'+ key; }
68 function getScratch(key)        { return __SCRATCH[ key ]; }
69 function scratchClear()         { for( var o in __SCRATCH ) __SCRATCH[o] = null; }
70
71
72 /* note: returns false if the value is 'f' or 'F' ... */
73 function isTrue(d) {
74         if(     d && 
75                         d != "0" && 
76                         d != "f" &&
77                         d != "F" )
78                         return true;
79         return false;
80 }
81
82 /* Utility function for iterating over array */
83 function iterate( arr, callback ) {
84         for( var i = 0; i < arr.length; i++ ) 
85                 callback(arr[i]);
86 }
87
88
89 /**
90   * returns a list of items that when passed to the callback 
91   * 'func' returned true returns null if none were found
92   */
93 function grep( arr, func ) {
94         var results = [];
95         iterate( arr, 
96                 function(d) {
97                         if( func(d) ) 
98                                 results.push(d);
99                 }
100         );
101         if(results.length > 0)  
102                 return results;
103         return null;
104 }
105
106
107
108 function flattenGroupTree(node) {
109         if(!node) return null;
110         groupList[node.name] = node;
111         groupIDList[node.id] = node;
112         iterate( node.children,
113                 function(n) {
114                         flattenGroupTree(n);
115                 }
116         );
117 }
118
119
120
121 /**
122   * Returns true if 'child' is equal or descends from 'parent'
123   * @param parent The name of the parent group
124   * @param child The name of the child group
125   */
126 function isGroupDescendant( parent, child ) {
127         return __isGroupDescendant(
128                 groupList[parent],
129                 groupList[child]);
130 }
131
132
133
134 /**
135   * Returns true if 'child' is equal or descends from 'parent'
136   * @param parent The node of the parent group
137   * @param child The node of the child group
138   */
139 function __isGroupDescendant( parent, child ) {
140         if (parent.id == child.id) return true;
141         var node = child;
142         while( (node = groupIDList[node.parent]) ) {
143                 if( node.id == parent.id ) 
144                         return true;
145         }
146         return false;
147 }
148
149
150
151
152         
153
154
155
156 /*
157 - at some point we should add a library of objects that map 
158 codes to names (item_form, item_type, etc.)
159 load_lib('item_form_map.js');
160 var form_name = item_form_map[env.record_descriptor.item_form];
161 */
162
163
164
165 /* logs a load of info */
166 function log_vars( prefix ) {
167
168         var str = prefix + ' : ';
169
170         if(patron) {
171                 str += ' Patron=' + patron.id;
172                 str += ', Patron Barcode='      + patron.card.barcode;
173                 str += ', Patron Username='+ patron.usrname;
174                 str += ', Patron Profile Group='+ patronProfile;
175                 str += ', Patron Library='      + patron.home_ou.name;
176                 str += ', Patron Fines='        + patronFines;
177                 str += ', Patron OverdueCount=' + patronOverdueCount;
178                 str += ', Patron Items Out=' + patronItemsOut;
179         }
180
181         if(copy)        {
182                 str += ', Copy=' + copy.id;
183                 str += ', Copy Barcode=' + copy.barcode;
184                 str += ', Copy status=' + copyStatus;
185                 str += ', Copy location=' + copy.location.name;
186         }
187
188         if(volume)                      str += ', Volume='      + volume.id;
189         if(title)                       str += ', Record='      + title.id;
190
191         if(recDescriptor)       {
192                 str += ', Record Descriptor=' + recDescriptor.id;
193                 str += ', Item Type='                   + recDescriptor.item_type;
194                 str += ', Item Form='                   + recDescriptor.item_form;
195                 str += ', Item Lang='                   + recDescriptor.item_lang;
196                 str += ', Item Audience='               + recDescriptor.audience;
197         }
198
199         str += ' Is Renewal: '  + ( (isTrue(isRenewal)) ? "yes" : "no" );
200
201         log_debug(str);
202 }
203
204