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