]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/util/ils_utils.js
more web work
[Evergreen.git] / Open-ILS / src / javascript / util / ils_utils.js
1 /* */
2
3
4 /* these are the types of resource provided my MODS - used in virtual records */
5 var resourceFormats = [ 
6         "text", 
7         "moving image",
8         "sound recording",
9         "software, multimedia",
10         "still images",
11         "cartographic",
12         "mixed material",
13         "notated music",
14         "three dimensional object" ];
15
16
17
18 function findOrgDepth(type_id) {
19
20         if(type_id == null || globalOrgTypes == null)
21                 return null;
22
23         var t = findOrgType(type_id);
24         if(t != null)
25                 return t.depth();
26
27         return null;
28 }
29
30 function findOrgType(type_id) {
31
32         if(type_id == null || globalOrgTypes == null)
33                 return null;
34
35         if(typeof type_id == 'object')
36                 return type_id;
37
38         for(var type in globalOrgTypes) {
39                 var t =globalOrgTypes[type]; 
40                 if( t.id() == type_id || t.id() == parseInt(type_id) ) 
41                         return t;
42         }
43         return null;
44 }
45
46
47 /* locates a specific org unit */
48 var orgArraySearcher = null;
49
50 /* flatten the org tree for faster searching */
51 function _flattenOrgs(node) { 
52
53         if(node == null) {
54                 node = globalOrgTree;
55                 orgArraySearcher = new Object();
56         }
57
58         orgArraySearcher[node.id()] = node;
59         for(var idx in node.children()) {
60                 _flattenOrgs(node.children()[idx]);
61         }
62 }
63
64 var singleOrgCache = new Object();
65 function findOrgUnit(org_id, branch) {
66
67         if(org_id == null) return null;
68         if(typeof org_id == 'object') return org_id;
69
70         /* if we don't have the global org tree, grab the org unit from the server */
71         var tree_exists = false;
72         try{if(globalOrgTree != null) tree_exists = true;}catch(E){}
73
74         if(!tree_exists) {
75                 var org = singleOrgCache[org_id];
76                 if(org) return org;
77                 var r = new RemoteRequest(
78                         "open-ils.actor",
79                         "open-ils.actor.org_unit.retrieve", null, org_id);
80                 r.send(true);
81                 return r.getResultObject();
82         }
83
84         if(orgArraySearcher == null)
85                 _flattenOrgs();
86
87         return orgArraySearcher[org_id];
88 }
89
90
91 function getOrgById(id, node) {
92         if(node == null) node = globalOrgTree;
93         if( node.id() == id) return node;
94         for( var ind in node.children() ) {
95                 var ret = getOrgById(id, node.children()[ind] );
96                 if( ret != null )
97                         return ret;
98         }
99         return null;
100 }
101
102
103
104 function orgNodeTrail(node) {
105         var nodeArray = new Array();
106         while( node ) {
107                 nodeArray.push(node);
108                 node = findOrgUnit(node.parent_ou());
109         }
110         nodeArray = nodeArray.reverse();
111         return nodeArray;
112 }
113
114 function findSiblingOrgs(node) {
115         return findOrgUnit(node.parent_ou()).children();
116 }
117
118
119 function grabCopyLocations() {
120
121         if(globalCopyLocations != null) return;
122         debug("Grabbing copy locations");
123
124         var req = new RemoteRequest(
125                 "open-ils.search",
126                 "open-ils.search.config.copy_location.retrieve.all" );
127
128         req.send(true);
129         globalCopyLocations = req.getResultObject();
130         return globalCopyLocations;
131
132 }
133
134 function findCopyLocation(id) {
135
136         grabCopyLocations();
137         if(typeof id == 'object') return id;
138
139         if(globalCopyLocations == null) 
140                 throw new EXLogic("globalCopyLocations is NULL");
141
142         for( var x = 0; x!= globalCopyLocations.length; x++) {
143                 if(globalCopyLocations[x].id() == id)
144                         return globalCopyLocations[x];
145         }
146         return null;
147 }
148
149
150 function modsFormatToMARC(format) {
151         switch(format) {
152                 case "text":
153                         return "at";
154                 case "moving image":
155                         return "g";
156                 case "sound recording":
157                         return "ij";
158                 case "sound recording-nonmusical":
159                         return "i";
160                 case "sound recording-musical":
161                         return "j";
162                 case "software, multimedia":
163                         return "m";
164                 case "still images":
165                         return "k";
166                 case "cartographic":
167                         return "ef";
168                 case "mixed material":
169                         return "op";
170                 case "notated music":
171                         return "cd";
172                 case "three dimensional object":
173                         return "r";
174         }
175         throw new EXLogic("Invalid format provided form modsFormatToMARC: " + format);
176 }
177
178 function MARCFormatToMods(format) {
179         switch(format) {
180
181                 case "a":
182                 case "t":
183                         return "text";
184
185                 case "g":
186                         return "moving image";
187
188                 case "i":
189                         return "sound recording-nonmusical";
190
191                 case "j":
192                         return "sound recording-musical";
193
194                 case "m":
195                         return "software, multimedia";
196
197                 case "k":
198                         return "still images";
199
200                 case "e":
201                 case "f":
202                         return "cartographic";
203
204                 case "o":
205                 case "p":
206                         return "mixed material";
207
208                 case "c":
209                 case "d":
210                         return "notated music";
211
212                 case "r":
213                         return "three dimensional object";
214         }
215         throw new EXLogic("Invalid format provided for MARCFormatToMods: " + format);
216 }
217
218
219
220 /* if callback exists, call is asynchronous and 
221         the returned item is passed to the callback... */
222 function fetchRecord(id, callback) {
223
224         var req = new RemoteRequest(
225                 "open-ils.search",
226                 "open-ils.search.biblio.record.mods_slim.retrieve",
227                 id );
228
229         if(callback) {
230                 req.setCompleteCallback(
231                         function(req) {callback(req.getResultObject())});
232                 req.send();
233         } else {
234                 req.send(true);
235                 return req.getResultObject();
236         }
237 }
238
239 /* if callback exists, call is asynchronous and 
240         the returned item is passed to the callback... */
241 function fetchMetaRecord(id, callback) {
242         var req = new RemoteRequest(
243                 "open-ils.search",
244                 "open-ils.search.biblio.metarecord.mods_slim.retrieve",
245                 id );
246
247         if(callback) {
248                 req.setCompleteCallback(
249                         function(req) {callback(req.getResultObject())});
250                 req.send();
251         } else {
252                 req.send(true);
253                 return req.getResultObject();
254         }
255 }
256
257 /* if callback exists, call is asynchronous and 
258         the returned item is passed to the callback... */
259 /* XXX no method yet... */
260 function fetchVolume(id, callback) {
261         var req = new RemoteRequest(
262                 "open-ils.search",
263                 "open-ils.search.biblio.metarecord.mods_slim.retrieve",
264                 id );
265
266         if(callback) {
267                 req.setCompleteCallback(
268                         function(req) {callback(req.getResultObject())});
269                 req.send();
270         } else {
271                 req.send(true);
272                 return req.getResultObject();
273         }
274 }
275
276 /* if callback exists, call is asynchronous and 
277         the returned item is passed to the callback... */
278 function fetchCopy(id, callback) {
279         var req = new RemoteRequest(
280                 "open-ils.search",
281                 "open-ils.search.asset.copy.fleshed.retrieve",
282                 id );
283
284         if(callback) {
285                 req.setCompleteCallback(
286                         function(req) {callback(req.getResultObject())});
287                 req.send();
288         } else {
289                 req.send(true);
290                 return req.getResultObject();
291         }
292 }
293
294 function mkResourceImage(resource) {
295         var pic = elem("img");
296         pic.setAttribute("src", "/images/" + resource + ".jpg");
297         pic.setAttribute("width", "20");
298         pic.setAttribute("height", "20");
299         pic.setAttribute("title", resource);
300         return pic;
301 }
302
303
304
305 function doLogout() {
306
307         /* remove cookie so browser know's we're logged out */
308         deleteCookie("ils_ses");
309
310         var user = UserSession.instance();
311         if( user.session_id ) {
312                 var request = new RemoteRequest( "open-ils.auth",
313                         "open-ils.auth.session.delete", user.session_id );
314                 request.send(true);
315                 var response = request.getResultObject();
316                 if(! response ) {
317                         //alert("error logging out"); /* exception */
318                 }
319         }
320
321         /* completely destroy this user object */
322         user.destroy();
323 }
324
325
326 function cleanISBN(isbn) {
327         if(isbn) {
328                 isbn = isbn.replace(/^\s+/,"");
329                 var idx = isbn.indexOf(" ");
330                 if(idx > -1) {
331                         isbn = isbn.substring(0, idx);
332                 }
333
334         } else isbn = "";
335         return isbn
336
337 }
338
339
340 function grabUserByBarcode(login, barcode) {
341         if(!barcode) return null;
342         var req = new RemoteRequest(
343                 "open-ils.actor",
344                 "open-ils.actor.user.fleshed.retrieve_by_barcode",
345                 login, barcode );
346
347         req.send(true);
348         return req.getResultObject();
349 }
350
351
352