user editor V2 first commit
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / user_editor.js
1
2 var cgi                                 = null;
3 var patron                              = null;
4 var advanced                    = false;
5 var SC_FETCH_ALL     = 'open-ils.circ:open-ils.circ.stat_cat.actor.retrieve.all';
6 var SC_CREATE_MAP               = 'open-ils.circ:open-ils.circ.stat_cat.actor.user_map.create';
7 var SV_FETCH_ALL                = 'open-ils.circ:open-ils.circ.survey.retrieve.all';
8
9 var myPerms             = [ 'CREATE_USER', 'UPDATE_USER', 'CREATE_PATRON_STAT_CAT_ENTRY_MAP' ];
10
11 var pages               = [ 
12         'uedit_userid', 
13         'uedit_contact_info', 
14         'uedit_addresses', 
15         'uedit_groups', 
16         'uedit_stat_cats', 
17         'uedit_surveys',
18         'uedit_finalize',
19         ];
20
21 var pageFocus   = [
22         'ue_barcode',
23         'ue_email1',
24         'ue_addr_label',
25         'ue_barcode',
26         'ue_barcode',
27         'ue_barcode',
28         'ue_barcode'
29 ];
30
31 /* fetch the necessary data to start off */
32 function uEditInit() {
33
34         cgi             = new CGI();
35         session = cgi.param('ses');
36         if(cgi.param('adv')) advanced = true 
37         if(!session) throw "User session is not defined";
38
39         user = fetchUser(session);
40         $('uedit_user').appendChild(text(USER.usrname()));
41         uEditShowPage('uedit_userid');
42
43         setTimeout( 
44                 function() { 
45                         fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
46                         uEditDrawUser(fetchFleshedUser(cgi.param('usr')));
47                         uEditFetchStatCats();
48                         uEditFetchSurveys();
49                 }, 20 
50         );
51 }
52
53 function uEditNext() {
54         var i = _findActive();
55         if( i < (pages.length - 1)) uEditShowPage(pages[i+1]);
56 }
57
58 function uEditPrev() {
59         var i = _findActive();
60         if( i > 0 ) uEditShowPage(pages[i-1]);
61 }
62
63
64 function uEditShowPage(id) {
65         if( id == null ) return;
66
67         for( var p in pages ) {
68                 var page = pages[p];
69                 hideMe($(page));
70                 removeCSSClass($(page+'_label'), 'label_active');
71         }
72
73         unHideMe($(id));
74         addCSSClass($(id+'_label'), 'label_active');
75         var idx = _findPageIdx(id);
76         var fpage = pageFocus[idx];
77         if(fpage) { $(fpage).focus(); $(fpage).select() }
78
79         unHideMe($('ue_back'));
80         unHideMe($('ue_fwd'));
81
82         if(idx == 0) hideMe($('ue_back'));
83         if(idx == (pages.length-1)) hideMe($('ue_fwd'));
84 }
85
86
87 function _findActive() {
88         for( var p in pages ) {
89                 if(! $(pages[p]).className.match(/hide_me/) )
90                         return parseInt(p);
91         }
92         return null;
93 }
94 function _findPageIdx(name) {
95         for( var i in pages ) {
96                 var page = pages[i];
97                 if( page == name ) return i;
98         }
99         return -1;
100 }
101
102 function uEditAddrHighlight( node, type ) {
103         var tbody = $('ue_address_tbody');
104         for( var c in tbody.childNodes ) {
105                 var row = tbody.childNodes[c];
106                 if(row.nodeType != XML_ELEMENT_NODE ) continue;
107                 var div = $n(row,'ue_addr_'+type+'_yes').parentNode;
108                 removeCSSClass(div, 'addr_info_checked');
109         }
110         addCSSClass(node.parentNode, 'addr_info_checked');
111 }
112
113
114
115 function uEditDrawUser(patron) {
116         if(!patron) return 0;
117 }
118
119 function uEditFetchStatCats() {
120         var req = new Request(SC_FETCH_ALL, SESSION);
121         req.callback(uEditDrawStatCats);
122         req.send();
123 }
124
125 function uEditDrawStatCats(r) {
126         var cats = r.getResultObject();
127         var tbody = $('ue_stat_cat_tbody');
128         var templ = tbody.removeChild($('ue_stat_cat_row'));
129
130         for( var c in cats ) {
131                 var row = templ.cloneNode(true);
132                 uEditInsertCat( tbody, row, cats[c] );
133                 tbody.appendChild(row);
134         }
135 }
136
137 function uEditInsertCat( tbody, row, cat ) {
138
139         cat.entries().sort(  /* sort the entries by value */
140                 function( a, b ) { 
141                         if( a.value().toLowerCase() > b.value().toLowerCase()) return 1;
142                         if( a.value().toLowerCase() < b.value().toLowerCase()) return -1;
143                         return 0;
144                 }
145         );
146
147         var selector = $n(row, 'ue_stat_cat_selector');
148         $n(row, 'ue_stat_cat_name').appendChild(text(cat.name()));
149         $n(row, 'ue_stat_cat_owner').appendChild(text(fetchOrgUnit(cat.owner()).shortname()));
150
151         var idx = 1;
152         for( var e in cat.entries() ) {
153                 var entry = cat.entries()[e];
154                 setSelectorVal( selector, idx++, entry.value(), entry.id() );
155         }
156 }
157
158 function uEditFetchSurveys() {
159         var req = new Request(SV_FETCH_ALL, SESSION);
160         req.callback(uEditDrawSurveys);
161         req.send();
162 }
163
164 function uEditDrawSurveys(r) {
165
166         var surveys = r.getResultObject();
167         var div = $('uedit_surveys');
168         var table = div.removeChild($('ue_survey_table'));
169
170         for( var s in surveys ) {
171                 var survey = surveys[s];
172                 var clone = table.cloneNode(true);
173                 uEditInsertSurvey( div, clone, survey );
174                 div.appendChild(clone);
175         }
176 }
177
178 function uEditInsertSurvey( div, table, survey ) {
179         $n(table, 'ue_survey_name').appendChild(text(survey.name()));
180         $n(table, 'ue_survey_desc').appendChild(text(survey.description()));
181         var tbody = $n(table, 'ue_survey_tbody');
182         var templ = tbody.removeChild($n(table, 'ue_survey_row'));
183
184         var selector    = $('ue_survey_answer');
185         var polldiv             = $('ue_survey_answer_poll');
186
187         var idx = 1;
188         for( var q in survey.questions() ) {
189                 var row = templ.cloneNode(true);
190                 uEditInsertSurveyQuestion( div, table, tbody, row, survey, survey.questions()[q] );
191                 tbody.appendChild(row);
192         }
193 }
194
195 function uEditInsertSurveyQuestion( div, table, tbody, row, survey, question ) {
196
197         $n(row, 'ue_survey_question').appendChild(text(question.question()));
198
199         var selector    = $n(row, 'ue_survey_answer');
200         var polldiv             = $n(row, 'ue_survey_answer_poll');
201         var idx                 = 1;
202         var polltbody   = $n(row, 'ue_survey_answer_poll_tbody');
203         var pollrow             = polltbody.removeChild($n(polltbody, 'ue_survey_answer_poll_row'));
204
205         for( var a in question.answers() ) {
206
207                 var answer = question.answers()[a];
208
209                 if( survey.poll() ) {
210
211                         unHideMe(polldiv);
212                         var prow = pollrow.cloneNode(true);
213                         $n(prow, 'ue_survey_answer_poll_answer').appendChild(text(answer.answer()));
214
215                         $n(prow, 'ue_survey_answer_poll_radio').appendChild(
216                                 elem('input', { 
217                                         type    : 'radio', 
218                                         name    : 'survey_poll_answer_'+survey.id(),
219                                         id              :  answer.id()
220                                 }));
221
222                         polltbody.appendChild(prow);
223
224                 } else {
225
226                         unHideMe(selector.parentNode);
227                         setSelectorVal( selector, idx++, answer.answer(), answer.id() );
228                 }
229         }
230 }
231
232