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