]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/org_utils.js
adding bib_level filter to the advanced search
[Evergreen.git] / Open-ILS / web / opac / common / js / org_utils.js
1 /* ------------------------------------------------------------------------------------------------------ */
2 /* org tree utilities */
3 /* ------------------------------------------------------------------------------------------------------ */
4
5 function fetchOrgSettingDefault(orgId, name) {
6     var req = new Request(FETCH_ORG_SETTING, orgId, name);
7     req.send(true);
8     var res = req.result();
9     return (res) ? res.value : null;
10 }
11
12
13
14 /* takes an org unit or id and return the numeric depth */
15 function findOrgDepth(org_id_or_node) {
16         var org = findOrgUnit(org_id_or_node);
17         if(!org) return -1;
18         var type = findOrgType(org.ou_type());
19         if(type) return type.depth();
20         return -1;
21 }
22
23 function findOrgTypeFromDepth(depth) {
24         if( depth == null ) return null;
25         for( var type in globalOrgTypes ) {
26                 var t = globalOrgTypes[type];
27                 if( t.depth() == depth ) return t;
28         }
29 }
30
31 /* takes the org type id from orgunit.ou_type() field and returns
32         the org type object */
33 function findOrgType(type_id) {
34         if(typeof type_id == 'object') return type_id;
35         for(var type in globalOrgTypes) {
36                 var t =globalOrgTypes[type]; 
37                 if( t.id() == type_id || t.id() == parseInt(type_id) ) 
38                         return t;
39         }
40         return null;
41 }
42
43
44 /* returns an org unit by id.  if an object is passed in as the id,
45         then the object is assumed to be an org unit and is returned */
46 function findOrgUnit(org_id) {
47         return (typeof org_id == 'object') ? org_id : orgArraySearcher[org_id];
48 }
49
50 var orgArraySearcherSN = {};
51 function findOrgUnitSN(shortname) {
52         if (typeof shortname == 'object') return shortname;
53         if( orgArraySearcherSN[shortname] ) return orgArraySearcherSN[shortname];
54         _debug("fetching org by shortname "+shortname);
55         var req = new Request(FETCH_ORG_BY_SHORTNAME, shortname);
56         req.request.alertEvent = false;
57         req.send(true);
58         return req.result();
59 }
60
61
62 /* builds a trail from the top of the org tree to the node provide.
63         basically fleshes out 'my orgs' 
64         Returns an array of [org0, org1, ..., myorg] */
65 function orgNodeTrail(node) {
66         var na = new Array();
67         while( node ) {
68                 na.push(node);
69                 node = findOrgUnit(node.parent_ou());
70         }
71         return na.reverse();
72 }
73
74 function findSiblingOrgs(node) { return findOrgUnit(node.parent_ou()).children(); }
75
76 /* true if 'org' is 'me' or a child of mine */
77 function orgIsMine(me, org) {
78         if(!me || !org) return false;
79         if(me.id() == org.id()) return true;
80         for( var i in me.children() ) {
81                 if(orgIsMine(me.children()[i], org))
82                         return true;
83         }
84         return false;
85 }
86
87
88
89 var orgArraySearcher = {};
90 var globalOrgTree;
91 for (var i in _l) {
92         var x = new aou();
93         x.id(_l[i][0]);
94         x.ou_type(_l[i][1]);
95         x.parent_ou(_l[i][2]);
96         x.name(_l[i][3]);
97     x.opac_visible(_l[i][4]);
98         orgArraySearcher[x.id()] = x;
99 }
100 for (var i in orgArraySearcher) {
101         var x = orgArraySearcher[i];
102         if (x.parent_ou() == null || x.parent_ou() == '') {
103                 globalOrgTree = x;
104                 continue;
105         } 
106
107         var parent = findOrgUnit(x.parent_ou());
108         if (!parent.children()) parent.children(new Array());
109         parent.children().push(x);
110 }
111
112 function _tree_killer () {
113         for (var i in orgArraySearcher) {
114                 x=orgArraySearcher[i];
115                 x.children(null);
116                 x.parent_ou(null);
117                 orgArraySearcher[i]=null;
118         }
119         globalOrgTree = null;
120         orgArraySearcher = null;
121         globalOrgTypes = null;
122 }
123
124
125