1 /* ------------------------------------------------------------------------------------------------------ */
2 /* org tree utilities */
3 /* ------------------------------------------------------------------------------------------------------ */
5 function fetchOrgSettingDefault(orgId, name) {
6 var req = new Request(FETCH_ORG_SETTING, orgId, name);
8 var res = req.result();
9 return (res) ? res.value : null;
12 function fetchBatchOrgSetting(orgId, nameList, onload) {
13 var req = new Request(
14 'open-ils.actor:open-ils.actor.ou_setting.ancestor_default.batch', orgId, nameList);
16 req.callback(function(r) { onload(r.getResultObject()); });
25 /* takes an org unit or id and return the numeric depth */
26 function findOrgDepth(org_id_or_node) {
27 var org = findOrgUnit(org_id_or_node);
29 var type = findOrgType(org.ou_type());
30 if(type) return type.depth();
34 function findOrgTypeFromDepth(depth) {
35 if( depth == null ) return null;
36 for( var type = 0; type < globalOrgTypes.length; type++ ) {
37 var t = globalOrgTypes[type];
38 if( t.depth() == depth ) return t;
42 /* takes the org type id from orgunit.ou_type() field and returns
43 the org type object */
44 function findOrgType(type_id) {
45 if(typeof type_id == 'object') return type_id;
46 for(var type = 0; type < globalOrgTypes.length; type++) {
47 var t =globalOrgTypes[type];
48 if( t.id() == type_id || t.id() == parseInt(type_id) )
55 /* returns an org unit by id. if an object is passed in as the id,
56 then the object is assumed to be an org unit and is returned */
57 function findOrgUnit(org_id) {
58 return (typeof org_id == 'object') ? org_id : orgArraySearcher[org_id];
61 function findOrgLasso(lasso_id) {
62 if (typeof lasso_id == 'object') return lasso_id;
63 for (var i = 0; i < _lasso.length; i++) {
64 if (_lasso[i].id() == lasso_id) return _lasso[i];
69 var orgArraySearcherSN = {};
70 function findOrgUnitSN(shortname) {
71 if (typeof shortname == 'object') return shortname;
72 if( orgArraySearcherSN[shortname] ) return orgArraySearcherSN[shortname];
73 _debug("fetching org by shortname "+shortname);
74 var req = new Request(FETCH_ORG_BY_SHORTNAME, shortname);
75 req.request.alertEvent = false;
81 /* builds a trail from the top of the org tree to the node provide.
82 basically fleshes out 'my orgs'
83 Returns an array of [org0, org1, ..., myorg] */
84 function orgNodeTrail(node) {
88 node = findOrgUnit(node.parent_ou());
93 function findSiblingOrgs(node) { return findOrgUnit(node.parent_ou()).children(); }
95 /* true if 'org' is 'me' or a child of mine, or optionally, a child of an ancestor org within the specified depth */
96 function orgIsMine(me, org, depth) {
100 if(me.id() == org.id()) {
103 if (depth !== undefined) {
104 while (depth < findOrgDepth(me)) {
105 me = findOrgUnit( me.parent_ou() );
107 if(me.id() == org.id()) {
111 var kids = me.children();
112 for( var i = 0; kids && i < kids.length; i++ ) {
113 if(orgIsMine(kids[i], org /* intentional lack of 3rd arg */)) {
121 function orgIsMineFromSet(meList, org) {
122 org = findOrgUnit(org);
123 for(var i = 0; i < meList.length; i++) {
124 if(orgIsMine(findOrgUnit(meList[i]), org))
130 var orgArraySearcher = {};
131 var orgArraySearcherOrder = [];
133 for (var i = 0; i < _l.length; i++) {
137 x.parent_ou(_l[i][2]);
139 x.opac_visible(_l[i][4]);
140 x.shortname(_l[i][5]);
141 orgArraySearcher[x.id()] = x;
142 orgArraySearcherOrder.push(x.id());
144 for (var i = 0; i < orgArraySearcherOrder.length; i++) {
145 var x = orgArraySearcher[orgArraySearcherOrder[i]];
146 if (x.parent_ou() == null || x.parent_ou() == '') {
151 var par = findOrgUnit(x.parent_ou());
152 if (!par.children()) par.children(new Array());
153 par.children().push(x);
156 function _tree_killer () {
157 for (var i in orgArraySearcher) {
158 x=orgArraySearcher[i];
161 orgArraySearcher[i]=null;
163 globalOrgTree = null;
164 orgArraySearcher = null;
165 globalOrgTypes = null;