]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/ue.js
bug fixes, etc.
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / ue.js
1 var cgi                                 = null;
2 var advanced                    = false;
3 var SC_FETCH_ALL                = 'open-ils.circ:open-ils.circ.stat_cat.actor.retrieve.all';
4 var SC_CREATE_MAP               = 'open-ils.circ:open-ils.circ.stat_cat.actor.user_map.create';
5 var SV_FETCH_ALL                = 'open-ils.circ:open-ils.circ.survey.retrieve.all';
6 var FETCH_ID_TYPES      = 'open-ils.actor:open-ils.actor.user.ident_types.retrieve';
7 var FETCH_GROUPS                = 'open-ils.actor:open-ils.actor.groups.tree.retrieve';
8 var UPDATE_PATRON               = 'open-ils.actor:open-ils.actor.patron.update';
9 var defaultState                = 'GA';
10 var counter                             = 0;
11 var dataFields;
12 var patron;
13 var identTypesCache                     = {};
14 var statCatsCache                               = {};
15 var surveysCache                                = {};
16 var surveyQuestionsCache        = {};
17 var surveyAnswersCache          = {};
18 var groupsCache                         = {};
19
20
21 /* fetch the necessary data to start off */
22 function uEditInit() {
23
24         cgi             = new CGI();
25         session = cgi.param('ses');
26         if(cgi.param('adv')) advanced = true 
27         if(!session) throw "User session is not defined";
28
29
30         fetchUser(session);
31         $('uedit_user').appendChild(text(USER.usrname()));
32
33         setTimeout( function() { uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
34 }
35
36 /* ------------------------------------------------------------------------------ */
37 /* Fetch code
38 /* ------------------------------------------------------------------------------ */
39 function uEditFetchIdentTypes() {
40         var req = new Request(FETCH_ID_TYPES);
41         req.send(true);
42         return req.result();
43 }
44
45 function uEditFetchStatCats() {
46         var req = new Request(SC_FETCH_ALL, SESSION);
47         req.send(true);
48         return req.result();
49 }
50
51 function uEditFetchSurveys() {
52         var req = new Request(SV_FETCH_ALL, SESSION);
53         req.send(true);
54         return req.result();
55 }
56
57 function uEditFetchGroups() {
58         var req = new Request(FETCH_GROUPS);
59         req.send(true);
60         return req.result();
61 }
62 /* ------------------------------------------------------------------------------ */
63
64
65
66 /* fetches necessary and builds the UI */
67 function uEditBuild() {
68         //fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
69
70         uEditBuildLibSelector();
71         patron = fetchFleshedUser(cgi.param('usr'));
72         if(!patron) patron = uEditNewPatron(); 
73
74
75         uEditDraw( 
76                 uEditFetchIdentTypes(),
77                 uEditFetchGroups(),
78                 uEditFetchStatCats(),
79                 uEditFetchSurveys() );
80
81         if(patron.isnew()) uEditCreateNewAddr();
82 }
83
84
85 /* creates a new patron object with card attached */
86 function uEditNewPatron() {
87         var patron = new au(); 
88         patron.isnew(1);
89         patron.id(-1);
90         card = new ac();
91         card.id(-1);
92         card.isnew(1);
93         patron.card(card);
94         patron.cards([card]);
95         patron.stat_cat_entries([]);
96         patron.survey_responses([]);
97         patron.addresses([]);
98         patron.home_ou(USER.ws_ou());
99         var rand  = Math.random();
100         rand = parseInt( rand * 10000 );
101         patron.passwd(rand);
102         $('ue_password_plain').appendChild(text(rand));
103         unHideMe($('ue_password_gen'));
104         return patron;
105 }
106
107 /* Creates a new blank address, adds it to the user and the fields array */
108 var uEditVirtualAddrId = -1;
109 function uEditCreateNewAddr() {
110         var addr = new aua();
111         addr.id(uEditVirtualAddrId--);
112         addr.isnew(1);
113         addr.usr(patron.id());
114         addr.state(defaultState);
115         addr.country(defaultCountry);
116         if(patron.addresses().length == 0) {
117                 patron.mailing_address(addr);
118                 patron.billing_address(addr);
119         }
120         uEditBuildAddrFields(patron, addr);
121         patron.addresses().push(addr);
122         uEditIterateFields(function(f) { uEditCheckValid(f); });
123         uEditCheckErrors();
124 }
125
126
127 /* kicks off the UI drawing */
128 function uEditDraw(identTypes, groups, statCats, surveys ) {
129         hideMe($('uedit_loading'));
130         unHideMe($('ue_maintd'));
131
132         dataFields = [];
133         uEditDrawIDTypes(identTypes);
134         uEditDrawGroups(groups);
135         uEditDrawStatCats(statCats);
136         uEditDrawSurveys(surveys);
137         uEditDefineData(patron);
138
139         uEditIterateFields(function(f) { uEditActivateField(f) });
140         uEditIterateFields(function(f) { uEditCheckValid(f); });
141         uEditCheckErrors();
142 }
143
144
145 /** Applies the event handlers and sets the data for the field */
146 function uEditActivateField(field) {
147
148         if( field.widget.id ) {
149                 field.widget.node = $(field.widget.id);
150
151         } else {
152                 field.widget.node = 
153                         $n(field.widget.base, field.widget.name);
154         }
155
156         uEditSetOnchange(field);
157
158         if(field.widget.onblur) {
159                 field.widget.node.onblur = 
160                         function() { field.widget.onblur(field); };
161         }
162
163         var val = field.object[field.key]();
164         if(val == null) return;
165
166         if( field.widget.type == 'input' )
167                 field.widget.node.value = val;
168
169         if( field.widget.type == 'select' )
170                 setSelector(field.widget.node, val);
171
172         if( field.widget.type == 'checkbox' )
173                 field.widget.node.checked = 
174                         (val && val != 'f') ? true : false;
175
176         if( field.widget.onload ) 
177                 field.widget.onload(val);
178
179         /*
180         alert(field.key);
181         if(field.key == 'ident_value') alert(field.widget.onblur);
182         */
183
184 }
185
186
187 /* set up the onchange event for the field */
188 function uEditSetOnchange(field) {
189         var func = function() {uEditOnChange( field );}
190         field.widget.node.onchange = func;
191
192         if(field.widget.type != 'select')
193                 field.widget.node.onkeyup = func;
194 }
195
196 /* find the current value of the field object's widget */
197 function uEditNodeVal(field) {
198         if(field.widget.type == 'input')
199                 return field.widget.node.value;
200
201         if(field.widget.type == 'checkbox')
202                 return field.widget.node.checked;
203
204         if(field.widget.type == 'select')
205                 return getSelectorVal(field.widget.node);
206 }
207
208
209 /* update a field value */
210 function uEditOnChange(field) {
211
212         var newval = uEditNodeVal(field);
213         field.object[field.key](newval);
214         field.object.ischanged(1);
215
216         if(field.widget.onpostchange)
217                 field.widget.onpostchange(field, newval);
218
219
220         uEditIterateFields(function(f) { uEditCheckValid(f); });
221         uEditCheckErrors();
222 }
223
224
225 function uEditCheckValid(field) {
226         var newval = uEditNodeVal(field);
227         if(newval) {
228
229                 if(field.widget.regex) { 
230                         if(newval.match(field.widget.regex)) 
231                                 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
232                         else
233                                 addCSSClass(field.widget.node, CSS_INVALID_DATA);
234
235                 } else {
236                         removeCSSClass(field.widget.node, CSS_INVALID_DATA);
237                 }
238
239         } else {
240
241                 if(field.required) {
242                         addCSSClass(field.widget.node, CSS_INVALID_DATA);
243
244                 } else {
245                         removeCSSClass(field.widget.node, CSS_INVALID_DATA);
246                 }
247         }
248 }
249
250 /* find a field object by object key */
251 function uEditFindFieldByKey(key) {
252         var fields = grep( dataFields,
253                 function(item) { return (item.key == key); });
254         return (fields) ? fields[0] : null;
255 }
256
257 /* find a list of fields by object key */
258 function uEditFindFieldsByKey(key) {
259         return grep( dataFields,
260                 function(item) { return (item.key == key); });
261 }
262
263 /* find a field object by widget id */
264 function uEditFindFieldByWId(id) {
265         var fields = grep( dataFields,
266                 function(item) { return (item.widget.id == id); });
267         return (fields) ? fields[0] : null;
268 }
269
270
271 function uEditIterateFields(callback) {
272         for( var f in dataFields ) {
273                 callback(dataFields[f]);
274         }
275 }
276
277
278 function uEditGetErrorStrings() {
279         var errors = [];
280         uEditIterateFields(
281                 function(field) { 
282                         if(field.errkey) {
283                                 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
284                                         var str = $(field.errkey).innerHTML;
285                                         if(str) errors.push(str);
286                                 }
287                         }
288                 }
289         );
290
291         if(errors[0]) return errors;
292         return null;
293 }
294
295 function uEditAlertErrors() {
296         var errors = uEditGetErrorStrings();
297         if(!errors) return false;
298         alert(errors.join("\n"));
299         return true;
300 }
301
302
303 /* send the user to the database */
304 function uEditSaveUser() {
305
306         if(uEditGetErrorStrings()) {
307                 uEditAlertErrors();
308                 return;
309         }
310
311         var req = new Request(UPDATE_PATRON, SESSION, patron);
312         req.send(true);
313         var result = req.result();
314
315         if( checkILSEvent(result) ) 
316                 alert(js2JSON(result));
317         else 
318                 alert($('ue_success').innerHTML);
319
320         if (window.xulG && typeof window.xulG.on_save == 'function') {
321                 window.xulG.on_save(patron); 
322         } else {
323                 location.href = location.href;
324         }
325 }
326
327
328 var uEditDupHashes = {};
329 var uEditDupTemplate;
330
331 function uEditRunDupeSearch(type, search_hash) {
332
333         if(!patron.isnew()) return;
334
335         _debug('dup search: ' + js2JSON(search_hash));
336
337         var req = new Request(PATRON_SEARCH, SESSION, search_hash);
338
339         var container = $('dup_div_container');
340         if(!uEditDupTemplate)
341                 uEditDupTemplate = container.removeChild($('dup_div'));
342
343         /* clear any existing dups for this type */
344         iterate( container.getElementsByTagName('div'),
345                 function(d) {
346                         if( d.getAttribute('type') == type ) {
347                                 container.removeChild(d)
348                                 return;
349                         }
350                 }
351         );
352
353         /*req.callback(uEditHandleDupResults);*/
354         req.callback(
355                 function(r) {
356                         uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
357                 }
358         );
359         req.send();
360 }
361
362
363 function uEditHandleDupResults(ids, search_hash, type, container) {
364
365         _debug('dup search results: ' + js2JSON(ids));
366
367         if(!(ids && ids[0]))  /* no results */
368                 return uEditDupHashes[type] = null;
369
370         /* add a dup link to the UI and plug in the data */
371         var node = uEditDupTemplate.cloneNode(true);
372         container.appendChild(node);
373         node.setAttribute('type', type);
374
375         var link = $n(node, 'link');
376         link.setAttribute('type', type);
377         unHideMe(link);
378         $n(node,'count').appendChild(text(ids.length));
379
380         for( var o in search_hash ) 
381                 $n(node, 'data').appendChild(
382                         text(search_hash[o].value + ' '));
383
384         uEditDupHashes[type] = search_hash;
385
386         switch(type) {
387                 case 'ident1' :
388                         if(confirm($('ue_dup_ident1').innerHTML)) 
389                                 uEditShowSearch(type);
390                         break;
391         }
392 }
393
394
395 function uEditShowSearch(link) {
396         var type = link.getAttribute('type');
397         if(window.xulG)
398                 window.xulG.spawn_search(uEditDupHashes[type]); 
399         else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
400 }
401