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