]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/fieldmapper/OrgUtils.js
"parent" is a special variable. some browsers (opera) will complain
[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.hash");
24         dojo.require("fieldmapper.OrgTree", true);
25         dojo.require("fieldmapper.OrgLasso", true);
26
27         fieldmapper.aou.slim_ok = true;
28         fieldmapper.aou.globalOrgTree = {};
29         fieldmapper.aou.OrgCache = {};
30         fieldmapper.aou.OrgCacheSN = {};
31         fieldmapper.aout.OrgTypeCache = {};
32
33         fieldmapper.aout.LoadOrgTypes = function () {
34                 for (var i in fieldmapper.aout.OrgTypeCache) {
35                         return;
36                 }
37
38                 var types = fieldmapper.standardRequest(['open-ils.actor','open-ils.actor.org_types.retrieve']);
39
40                 for (var i in types) {
41                         fieldmapper.aout.OrgTypeCache[types[i].id()] = {
42                                 loaded : true,
43                                 type : types[i]
44                         };
45                 }
46         }
47
48         fieldmapper.aou.LoadOrg = function (id, slim_ok) {
49                 if (slim_ok == null) slim_ok = fieldmapper.aou.slim_ok;
50                 var slim_o = fieldmapper.aou.OrgCache[id];
51
52                 if (slim_o && (slim_ok || slim_o.loaded))
53                         return fieldmapper.aou.OrgCache[id].org;
54
55                 var o = fieldmapper.standardRequest(['open-ils.actor','open-ils.actor.org_unit.retrieve'],[null,id]);
56                 o.children = fieldmapper.aou.OrgCache[o.id()].children;
57                 fieldmapper.aou.OrgCache[o.id()] = { loaded : true, org : o };
58                 return o;
59         }
60         fieldmapper.aou.findOrgUnit = fieldmapper.aou.LoadOrg;
61
62         if (window._l) {
63                 for (var i in _l) {
64                         fieldmapper.aou.OrgCache[_l[i][0]] = {
65                                 loaded: false,
66                                 org : new fieldmapper.aou().fromHash({
67                                         id : _l[i][0],
68                                         ou_type : _l[i][1],
69                                         parent_ou : _l[i][2],
70                                         name : _l[i][3],
71                                         opac_visible : _l[i][4],
72                                         shortname : _l[i][5]
73                                 })
74                         };
75
76                 }
77
78                 for (var i in fieldmapper.aou.OrgCache) {
79                         var x = fieldmapper.aou.OrgCache[i].org;
80                         if (x.parent_ou() == null || x.parent_ou() == '') {
81                                 fieldmapper.aou.globalOrgTree = x;
82                                 continue;
83                         }
84
85                         var par = fieldmapper.aou.findOrgUnit(x.parent_ou(),true);
86                         if (!par.children()) par.children([]);
87                         par.children().push(x);
88                         fieldmapper.aou.OrgCache[x.id()].treePtr = x;
89                 }
90
91                 for (var i in globalOrgTypes) {
92                         fieldmapper.aout.OrgTypeCache[globalOrgTypes[i].id()] = {
93                                 loaded : true,
94                                 type : globalOrgTypes[i]
95                         };
96                 }
97         }
98
99
100    /* ---------------------------------------------------------------------- */
101
102         fieldmapper.aou.prototype.fetchOrgSettingDefault = function (name) {
103                 return this.standardRequest( fieldmapper.OpenSRF.methods.FETCH_ORG_SETTING, name ); 
104         }
105
106         fieldmapper.aout.findOrgType = function (id) {
107                 fieldmapper.aout.LoadOrgTypes();
108                 return fieldmapper.aout.OrgTypeCache[id].type;
109         }
110
111         fieldmapper.aou.prototype.findOrgDepth = function (id) {
112                 if (!id) id = this.id;
113                 if (!id) return null;
114
115                 var org = fieldmapper.aou.findOrgUnit(id);
116                 return fieldmapper.aout.findOrgType(
117                         fieldmapper.aou.findOrgUnit(id).ou_type()
118                 ).depth;
119         }
120         fieldmapper.aou.findOrgDepth = fieldmapper.aou.prototype.findOrgDepth;
121
122         fieldmapper.aout.findOrgTypeFromDepth = function (depth) {
123                 if( depth == null ) return null;
124                 fieldmapper.aout.LoadOrgTypes();
125                 for( var i in fieldmapper.aout.OrgTypeCache ) {
126                         var t = fieldmapper.aout.OrgTypeCache[i].type;
127                         if( t.depth() == depth ) return t;
128                 }
129                 return null;
130         }
131
132         fieldmapper.aou.findOrgUnitSN = function (sn, slim_ok) {
133                 if (slim_ok == null) slim_ok = fieldmapper.aou.slim_ok;
134                 var org = fieldmapper.aou.OrgCacheSN[sn];
135                 if (!org) {
136                         for (var i in fieldmapper.aou.OrgCache) {
137                                 var o = fieldmapper.aou.OrgCache[i];
138                                 if (o.org.shortname() == sn) {
139                                         fieldmapper.aou.OrgCacheSN[o.org.shortname()] = o;
140                                         org = o;
141                                 }
142                         }
143
144                         if (!slim_ok && !fieldmapper.aou.OrgCache[org.id()].loaded) {
145                                 org = fieldmapper.standardRequest(fieldmapper.OpenSRF.methods.FETCH_ORG_BY_SHORTNAME, sn);
146
147                                 org.children = fieldmapper.aou.OrgCache[org.id()].children;
148                                 fieldmapper.aou.OrgCache[org.id()] = { loaded : true, org : org };
149                                 fieldmapper.aou.OrgCacheSN[org.shortname()] = { loaded : true, org : org };
150                         }
151
152                 }
153
154                 return org;
155         }
156
157         fieldmapper.aou.prototype.orgNodeTrail = function (node) {
158                 if (!node) node = this;
159                 if (!node) return [];
160
161                 var na = [];
162
163                 while( node ) {
164                         na.push(node);
165                         node = fieldmapper.aou.findOrgUnit(node.parent_ou());
166                 }
167
168                 return na.reverse();
169         }
170         fieldmapper.aou.orgNodeTrail = fieldmapper.aou.prototype.orgNodeTrail;
171
172         fieldmapper.aou.prototype.orgIsMine = function (me, org) {
173                 if (this._isfieldmapper) {
174                         org = me;
175                         me = this;
176                 }
177
178                 if(!me || !org) return false;
179
180                 if(me.id() == org.id()) return true;
181
182                 for( var i in me.children() ) {
183                         if(me.children()[i].orgIsMine(org)) return true;
184                 }
185                 return false;
186         }
187
188     /** Given an org id, returns an array of org units including
189      * the org for the ID provided and all descendant orgs */
190     fieldmapper.aou.descendantNodeList = function(orgId) {
191         var list = [];
192         function addNode(node) {
193             if(!node) return;
194             list.push(node);
195             var children = node.children();
196             if(children) {
197                 for(var i = 0; i < children.length; i++) 
198                     addNode(children[i]);
199             }
200         }
201         addNode(fieldmapper.aou.findOrgUnit(orgId));
202         return list;
203     }
204
205         dojo.addOnUnload( function () {
206                 for (var i in fieldmapper.aou.OrgCache) {
207                         x=fieldmapper.aou.OrgCache[i].treePtr;
208                         if (!x) continue;
209
210                         x.children(null);
211                         x.parent_ou(null);
212                         fieldmapper.aou.OrgCache[i]=null;
213                 }
214                 fieldmapper.aou.globalOrgTree = null;
215                 fieldmapper.aou.OrgCache = null;
216                 fieldmapper.aou.OrgCacheSN = null;
217                 fieldmapper.aout.OrgTypeCache = null;
218         });
219 }
220
221
222