]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/backend/circ/circ_lib.js
making progress toward a real circ matrix
[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 function getMARCItemType() {
151
152         if(     copy &&
153                         copy.circ_as_type &&
154                         copy.circ_as_type != 'undef' )
155                 return copy.circ_as_type;
156         
157         return extractFixedField(marcXMLDoc, 'Type');
158 }
159
160
161 function isOrgDescendent( parentName, childId ) {
162         var val = environment.__OILS_FUNC_isOrgDescendent(parentName, childId);
163         if( val == '1' ) return true;
164         return false;
165 }
166         
167
168
169
170 /*
171 - at some point we should add a library of objects that map 
172 codes to names (item_form, item_type, etc.)
173 load_lib('item_form_map.js');
174 var form_name = item_form_map[env.record_descriptor.item_form];
175 */
176
177
178
179 /* logs a load of info */
180 function log_vars( prefix ) {
181         var str = prefix + ' : ';
182
183         if(patron) {
184                 str += ' Patron=' + patron.id;
185                 str += ', Patron Barcode='      + patron.card.barcode;
186                 str += ', Patron Username='+ patron.usrname;
187                 str += ', Patron Profile Group='+ patronProfile;
188                 str += ', Patron Library='      + patron.home_ou.name;
189                 str += ', Patron Fines='        + patronFines;
190                 str += ', Patron OverdueCount=' + patronOverdueCount;
191                 str += ', Patron Items Out=' + patronItemsOut;
192         }
193
194         if(copy)        {
195                 str += ', Copy=' + copy.id;
196                 str += ', Copy Barcode=' + copy.barcode;
197                 str += ', Copy status=' + copyStatus;
198                 str += ', Copy location=' + copy.location.name;
199         }
200
201         if(volume)                      str += ', Volume='      + volume.id;
202         if(title)                       str += ', Record='      + title.id;
203
204         if(recDescriptor)       {
205                 str += ', Record Descriptor=' + recDescriptor.id;
206                 str += ', Item Type='                   + recDescriptor.item_type;
207                 str += ', Item Form='                   + recDescriptor.item_form;
208                 str += ', Item Lang='                   + recDescriptor.item_lang;
209                 str += ', Item Audience='               + recDescriptor.audience;
210         }
211
212         str += ' Is Renewal: '  + ( (isTrue(isRenewal)) ? "yes" : "no" );
213
214         log_debug(str);
215 }
216
217