]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/fieldmapper/OrgUtils.js
Server-generated IDL JS
[working/Evergreen.git] / Open-ILS / web / js / dojo / fieldmapper / OrgUtils.js
1 /*
2 # ---------------------------------------------------------------------------
3 # Copyright (C) 2008  Georgia Public Library Service / Equinox Software, Inc
4 # Mike Rylander <miker@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------------------
16 */
17
18 if(!dojo._hasResource["fieldmapper.OrgUtils"]){
19
20         dojo._hasResource["fieldmapper.OrgUtils"] = true;
21         dojo.provide("fieldmapper.OrgUtils");
22         dojo.require("fieldmapper.Fieldmapper");
23         dojo.require("fieldmapper.OrgTree", true);
24         dojo.require("fieldmapper.OrgLasso", true);
25
26         fieldmapper.aou.slim_ok = true;
27         fieldmapper.aou.globalOrgTree = {};
28         fieldmapper.aou.OrgCache = {};
29         fieldmapper.aou.OrgCacheSN = {};
30         fieldmapper.aout.OrgTypeCache = {};
31
32         fieldmapper.aout.LoadOrgTypes = function () {
33                 for (var i in fieldmapper.aout.OrgTypeCache) {
34                         return;
35                 }
36
37                 var types = fieldmapper.standardRequest(['open-ils.actor','open-ils.actor.org_types.retrieve']);
38
39                 for (var i = 0; i < types.length; i++) {
40                         fieldmapper.aout.OrgTypeCache[types[i].id()] = {
41                                 loaded : true,
42                                 type : types[i]
43                         };
44                 }
45         }
46
47         fieldmapper.aou.LoadOrg = function (id, slim_ok) {
48                 if (slim_ok == null) slim_ok = fieldmapper.aou.slim_ok;
49                 var slim_o = fieldmapper.aou.OrgCache[id];
50
51                 if (slim_o && (slim_ok || slim_o.loaded))
52                         return fieldmapper.aou.OrgCache[id].org;
53
54                 var o = fieldmapper.standardRequest(['open-ils.actor','open-ils.actor.org_unit.retrieve'],[null,id]);
55         if(!(o && o.id)) {
56             throw new Error("fieldmapper.aou.LoadOrg(): No org unit found with ID " + id);
57         }
58                 o.children = fieldmapper.aou.OrgCache[o.id()].children;
59                 fieldmapper.aou.OrgCache[o.id()] = { loaded : true, org : o };
60                 return o;
61         }
62         fieldmapper.aou.findOrgUnit = fieldmapper.aou.LoadOrg;
63
64         if (window._l) {
65                 for (var i = 0; i < _l.length; i++) {
66                         fieldmapper.aou.OrgCache[_l[i][0]] = {
67                                 loaded: false,
68                                 org : new fieldmapper.aou().fromHash({
69                                         id : _l[i][0],
70                                         ou_type : _l[i][1],
71                                         parent_ou : _l[i][2],
72                                         name : _l[i][3],
73                                         opac_visible : _l[i][4],
74                                         shortname : _l[i][5]
75                                 })
76                         };
77
78                 }
79
80                 for (var i in fieldmapper.aou.OrgCache) {
81                         var x = fieldmapper.aou.OrgCache[i].org;
82                         if (x.parent_ou() == null || x.parent_ou() == '') {
83                                 fieldmapper.aou.globalOrgTree = x;
84                                 continue;
85                         }
86
87                         var par = fieldmapper.aou.findOrgUnit(x.parent_ou(),true);
88                         if (!par.children()) par.children([]);
89                         par.children().push(x);
90                         fieldmapper.aou.OrgCache[x.id()].treePtr = x;
91                 }
92
93                 for (var i = 0; i < globalOrgTypes.length; i++) {
94                         fieldmapper.aout.OrgTypeCache[globalOrgTypes[i].id()] = {
95                                 loaded : true,
96                                 type : globalOrgTypes[i]
97                         };
98                 }
99         }
100
101
102    /* ---------------------------------------------------------------------- */
103
104         fieldmapper.aou.prototype.fetchOrgSettingDefault = function (name) {
105                 return this.standardRequest( fieldmapper.OpenSRF.methods.FETCH_ORG_SETTING, [this.id(), name] ); 
106         }
107
108         fieldmapper.aou.prototype.fetchOrgSettingBatch = function (nameList) {
109                 return this.standardRequest( fieldmapper.OpenSRF.methods.FETCH_ORG_SETTING_BATCH, [this.id(), nameList] ); 
110         }
111
112         fieldmapper.aou.fetchOrgSettingDefault = function (orgId, name) {
113                 return fieldmapper.standardRequest( fieldmapper.OpenSRF.methods.FETCH_ORG_SETTING, [orgId, name] ); 
114         }
115
116         fieldmapper.aou.fetchOrgSettingBatch = function (orgId, nameList) {
117                 return fieldmapper.standardRequest( fieldmapper.OpenSRF.methods.FETCH_ORG_SETTING_BATCH, [orgId, nameList] ); 
118         }
119
120         fieldmapper.aout.findOrgType = function (id) {
121                 fieldmapper.aout.LoadOrgTypes();
122                 return fieldmapper.aout.OrgTypeCache[id].type;
123         }
124
125         fieldmapper.aou.prototype.findOrgDepth = function (id) {
126                 if (!id) id = this.id;
127                 if (!id) return null;
128
129                 var org = fieldmapper.aou.findOrgUnit(id);
130                 return fieldmapper.aout.findOrgType(
131                         fieldmapper.aou.findOrgUnit(id).ou_type()
132                 ).depth();
133         }
134         fieldmapper.aou.findOrgDepth = fieldmapper.aou.prototype.findOrgDepth;
135
136         fieldmapper.aout.findOrgTypeFromDepth = function (depth) {
137                 if( depth == null ) return null;
138                 fieldmapper.aout.LoadOrgTypes();
139                 for( var i in fieldmapper.aout.OrgTypeCache ) {
140                         var t = fieldmapper.aout.OrgTypeCache[i].type;
141                         if( t.depth() == depth ) return t;
142                 }
143                 return null;
144         }
145
146         fieldmapper.aou.findOrgUnitSN = function (sn, slim_ok) {
147                 if (slim_ok == null) slim_ok = fieldmapper.aou.slim_ok;
148                 var org = fieldmapper.aou.OrgCacheSN[sn];
149                 if (!org) {
150                         for (var i in fieldmapper.aou.OrgCache) {
151                                 var o = fieldmapper.aou.OrgCache[i];
152                                 if (o.org.shortname() == sn) {
153                                         fieldmapper.aou.OrgCacheSN[o.org.shortname()] = o;
154                                         org = o;
155                                 }
156                         }
157
158                         if (!slim_ok && !fieldmapper.aou.OrgCache[org.id()].loaded) {
159                                 org = fieldmapper.standardRequest(fieldmapper.OpenSRF.methods.FETCH_ORG_BY_SHORTNAME, sn);
160
161                                 org.children = fieldmapper.aou.OrgCache[org.id()].children;
162                                 fieldmapper.aou.OrgCache[org.id()] = { loaded : true, org : org };
163                                 fieldmapper.aou.OrgCacheSN[org.shortname()] = { loaded : true, org : org };
164                         }
165
166                 }
167
168                 return org;
169         }
170
171         fieldmapper.aou.prototype.orgNodeTrail = function(node, asId) {
172                 if (!node) node = this;
173                 if (!node) return [];
174
175                 var na = [];
176
177                 while( node ) {
178                         na.push(node);
179                         if (node.parent_ou())
180                                 node = fieldmapper.aou.findOrgUnit(node.parent_ou());
181                         else
182                                 node = null;
183                 }
184
185                 na.reverse();
186                 if (asId) return na.map(function(o) { return o.id(); });
187                 else return na;
188         }
189         fieldmapper.aou.orgNodeTrail = fieldmapper.aou.prototype.orgNodeTrail;
190
191         fieldmapper.aou.prototype.orgIsMine = function (me, org) {
192                 if (this._isfieldmapper) {
193                         org = me;
194                         me = this;
195                 }
196
197                 if(!me || !org) return false;
198
199                 if(me.id() == org.id()) return true;
200
201                 for( var i in me.children() ) {
202                         if(me.children()[i].orgIsMine(org)) return true;
203                 }
204                 return false;
205         }
206
207     /** Given an org id, returns an array of org units including
208      * the org for the ID provided and all descendant orgs */
209     fieldmapper.aou.descendantNodeList = function(orgId, asId) {
210         var list = [];
211         function addNode(node) {
212             if(!node) return;
213             if(asId) {
214                 list.push(node.id());
215             } else {
216                 list.push(node);
217             }
218             var children = node.children();
219             if(children) {
220                 for(var i = 0; i < children.length; i++) 
221                     addNode(children[i]);
222             }
223         }
224         addNode(fieldmapper.aou.findOrgUnit(orgId));
225         return list;
226     }
227
228     fieldmapper.aou.fullPath = function(orgId, asId) {
229
230         var orgList = fieldmapper.aou.orgNodeTrail(fieldmapper.aou.findOrgUnit(orgId));
231         orgList.pop(); // prevent dupes
232         orgList = orgList.concat(fieldmapper.aou.descendantNodeList(orgId))
233
234         if(!asId) return orgList;
235         return orgList.map( function(node) { return node.id(); } );
236     }
237
238         dojo.addOnUnload( function () {
239                 for (var i in fieldmapper.aou.OrgCache) {
240                         x=fieldmapper.aou.OrgCache[i].treePtr;
241                         if (!x) continue;
242
243                         x.children(null);
244                         x.parent_ou(null);
245                         fieldmapper.aou.OrgCache[i]=null;
246                 }
247                 fieldmapper.aou.globalOrgTree = null;
248                 fieldmapper.aou.OrgCache = null;
249                 fieldmapper.aou.OrgCacheSN = null;
250                 fieldmapper.aout.OrgTypeCache = null;
251         });
252 }
253
254
255