]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/adminlib.js
fixes things like the stat cat editor, since that extra param now changes the return...
[working/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);
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, onload) {
63     var req = new RemoteRequest(
64         'open-ils.actor',
65         'open-ils.actor.user.has_work_perm_at.batch',
66         session, perms);
67     if(onload) {
68         req.setCompleteCallback(function(r){
69             onload(OILS_WORK_PERMS = r.getResultObject());
70         });
71         req.send()
72     } else {
73         req.send(true);
74         return OILS_WORK_PERMS = req.getResultObject();
75     }
76 }
77
78 /*
79  takes org IDs 
80  Finds the lowest relevent org unit between a context org unit and a set of
81  permission orgs.  This defines the sphere of influence for a given action
82  on a specific set of data.  if the context org shares no common nodes with
83  the set of permission orgs, null is returned.
84  returns the orgUnit object
85  */
86 function findReleventRootOrg(permOrgList, contextOrgId) {
87     var contextOrgNode = findOrgUnit(contextOrgId);
88     for(var i = 0; i < permOrgList.length; i++) {
89         var permOrg = findOrgUnit(permOrgList[i]);
90         if(orgIsMine(permOrg, contextOrgNode)) {
91             // perm org is equal to or a parent of the context org, so the context org is the highest
92             return contextOrgNode;
93         } else if(orgIsMine(contextOrgNode, permOrg)) {
94             // perm org is a child if the context org, so permOrg is the highest org
95             return permOrg;
96         }
97     }
98     return null;
99 }
100
101
102 /* offset is the depth of the highest org 
103     in the tree we're building 
104   */
105
106 /* XXX Moved to opac_utils.js */
107
108 /*
109 function buildOrgSel(selector, org, offset) { 
110     insertSelectorVal( selector, -1, 
111         org.name(), org.id(), null, findOrgDepth(org) - offset );
112     for( var c in org.children() )
113         buildOrgSel( selector, org.children()[c], offset);
114 }
115 */
116
117 /** removes all child nodes in 'tbody' that have the attribute 'key' defined */
118 function cleanTbody(tbody, key) {
119     for( var c  = 0; c < tbody.childNodes.length; c++ ) {
120         var child = tbody.childNodes[c];
121         if(child && child.getAttribute(key)) tbody.removeChild(child); 
122     }
123 }
124
125
126 /** Inserts a row into a specified place in a table
127   * tbody is the table body
128   * row is the context row after which the new row is to be inserted
129   * newRow is the new row to insert
130   */
131 function insRow( tbody, row, newRow ) {
132     if(row.nextSibling) tbody.insertBefore( newRow, row.nextSibling );
133     else{ tbody.appendChild(newRow); }
134 }
135
136
137 /** Checks to see if a given node should be enabled
138   * A node should be enabled if the itemOrg is lower in the
139   * org tree than my permissions allow editing
140   * I.e. I can edit the context item because it's "below" me
141   */
142 function checkDisabled( node, itemOrg, perm ) {
143     var itemDepth = findOrgDepth(itemOrg);
144     var mydepth = findOrgDepth(PERMS[perm]);
145     if( mydepth != -1 && mydepth <= itemDepth ) node.disabled = false;
146 }
147
148 /**
149   * If the item-related org unit (owner, etc.) is one of or
150   * or a child of any of the perm-orgs related to the
151   * provided permission, enable the requested node
152   */
153 function checkPermOrgDisabled(node, itemOrg, perm) {
154     var org_list = OILS_WORK_PERMS[perm];
155     if(org_list.length > 0) {
156         for(var i = 0; i < org_list.length; i++) {
157             var highPermOrg = findOrgUnit(org_list[i]);
158             if(orgIsMine(highPermOrg, findOrgUnit(itemOrg))) 
159                 node.disabled = false;
160         }
161     }
162 }
163
164
165 function fetchOrgUnit(id, callback) {
166
167     if(ORG_CACHE[id]) return ORG_CACHE[id];
168     var req = new Request(FETCH_ORG_UNIT, SESSION, id);    
169
170     if(callback) {
171         req.callback(
172             function(r) { 
173                 var org = r.getResultObject();
174                 ORG_CACHE[id] = org;
175                 callback(org); 
176             }
177         );
178         req.send();
179
180     } else {
181         req.send(true);
182         var org = req.result();
183         ORG_CACHE[id] = org;
184         return org;
185     }
186 }