]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/adminlib.js
added work-org version of checkDisabled
[Evergreen.git] / Open-ILS / xul / staff_client / server / admin / adminlib.js
1 var USER;
2 var SESSION;
3 var PERMS = {};
4 var ORG_CACHE = {};
5 var OILS_WORK_PERMS = {};
6
7 var XML_ELEMENT_NODE = 1;
8 var XML_TEXT_NODE = 3;
9
10 var FETCH_ORG_UNIT = "open-ils.actor:open-ils.actor.org_unit.retrieve";
11
12 function debug(str) { try { dump(str + '\n'); } catch(e){} }
13
14 function fetchUser(session) {
15         if(session == null ) {
16                 cgi = new CGI();
17                 session = cgi.param('ses');
18         }
19         if(!session) throw "User session is not defined";
20         SESSION = session;
21         var request = new Request(FETCH_SESSION, session, 1);
22         request.send(true);
23         var user = request.result();
24         if(checkILSEvent(user)) throw user;
25         USER = user;
26         return user;
27 }
28
29 /* if defined, callback will get the user object asynchronously */
30 function fetchFleshedUser(id, callback) {
31         if(id == null) return null;
32         var req = new Request(
33                 'open-ils.actor:open-ils.actor.user.fleshed.retrieve', SESSION, id );
34
35         if( callback ) {
36                 req.callback( function(r){callback(r.getResultObject());} );
37                 req.send();
38
39         } else {
40                 req.send(true);
41                 return req.result();
42         }
43 }
44
45 /**
46   * Fetches the highest org at for each perm  and stores the value in
47   * PERMS[ permName ].  It also returns the org list to the caller
48   */
49 function fetchHighestPermOrgs( session, userId, perms ) {
50         var req = new RemoteRequest(
51                 'open-ils.actor',
52                 'open-ils.actor.user.perm.highest_org.batch', 
53                 session, userId, perms  );
54         req.send(true);
55         var orgs = req.getResultObject();
56         for( var i = 0; i != orgs.length; i++ ) 
57                 PERMS[perms[i]] = orgs[i];
58                 //PERMS[ perms[i] ] = ( orgs[i] != null ) ? orgs[i] : -1 ;
59         return orgs;
60 }
61
62 function fetchHighestWorkPermOrgs(session, userId, perms) {
63     for(var i = 0; i < perms.length; i++) {
64         var perm = perms[i];
65         var req = new RemoteRequest(
66             'open-ils.actor',
67             'open-ils.actor.user.work_perm.highest_org_set',
68             session, perm);
69         req.send(true);
70         OILS_WORK_PERMS[perm] = req.getResultObject();
71     }
72 }
73
74
75 /* offset is the depth of the highest org 
76         in the tree we're building 
77   */
78
79 /* XXX Moved to opac_utils.js */
80
81 /*
82 function buildOrgSel(selector, org, offset) { 
83         insertSelectorVal( selector, -1, 
84                 org.name(), org.id(), null, findOrgDepth(org) - offset );
85         for( var c in org.children() )
86                 buildOrgSel( selector, org.children()[c], offset);
87 }
88 */
89
90 /** removes all child nodes in 'tbody' that have the attribute 'key' defined */
91 function cleanTbody(tbody, key) {
92         for( var c  = 0; c < tbody.childNodes.length; c++ ) {
93                 var child = tbody.childNodes[c];
94                 if(child && child.getAttribute(key)) tbody.removeChild(child); 
95         }
96 }
97
98
99 /** Inserts a row into a specified place in a table
100   * tbody is the table body
101   * row is the context row after which the new row is to be inserted
102   * newRow is the new row to insert
103   */
104 function insRow( tbody, row, newRow ) {
105         if(row.nextSibling) tbody.insertBefore( newRow, row.nextSibling );
106         else{ tbody.appendChild(newRow); }
107 }
108
109
110 /** Checks to see if a given node should be enabled
111   * A node should be enabled if the itemOrg is lower in the
112   * org tree than my permissions allow editing
113   * I.e. I can edit the context item because it's "below" me
114   */
115 function checkDisabled( node, itemOrg, perm ) {
116         var itemDepth = findOrgDepth(itemOrg);
117         var mydepth = findOrgDepth(PERMS[perm]);
118         if( mydepth != -1 && mydepth <= itemDepth ) node.disabled = false;
119 }
120
121 /**
122   * If the item-related org unit (owner, etc.) is one of or
123   * or a child of any of the perm-orgs related to the
124   * provided permission, enable the requested node
125   */
126 function checkPermOrgDisabled(node, itemOrg, perm) {
127     var org_list = OILS_WORK_PERMS[perm];
128     if(org_list.length > 0) {
129         for(var i = 0; i < org_list.length; i++) {
130             var highPermOrg = findOrgUnit(org_list[i]);
131             if(orgIsMine(highPermOrg, findOrgUnit(itemOrg))) 
132                 node.disabled = false;
133         }
134     }
135 }
136
137
138 function fetchOrgUnit(id, callback) {
139
140         if(ORG_CACHE[id]) return ORG_CACHE[id];
141         var req = new Request(FETCH_ORG_UNIT, SESSION, id);     
142
143         if(callback) {
144                 req.callback(
145                         function(r) { 
146                                 var org = r.getResultObject();
147                                 ORG_CACHE[id] = org;
148                                 callback(org); 
149                         }
150                 );
151                 req.send();
152
153         } else {
154                 req.send(true);
155                 var org = req.result();
156                 ORG_CACHE[id] = org;
157                 return org;
158         }
159 }