edit copy context commands
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / patron / patron_display.js
1 sdump('D_TRACE','Loading patron_display.js\n');
2
3 function patron_display_init(p) {
4         sdump('D_PATRON_DISPLAY',"TESTING: patron_display.js: " + mw.G['main_test_variable'] + '\n');
5         sdump('D_CONSTRUCTOR',arg_dump(arguments));
6
7         // gives: p.clamshell, p.right_panel, p.left_panel
8         patron_display_clamshell_init(p);
9
10         // gives: p.patron_items, p.redraw_patron_items
11         patron_display_patron_items_init(p);
12
13         // gives: p.patron_checkout_items, p.redraw_patron_checkout_items
14         patron_display_patron_checkout_items_init(p);
15
16         // gives: p.patron_holds, p.redraw_patron_holds
17         patron_display_patron_holds_init(p);
18
19         // gives: p.patron_bills, p.redraw_patron_bills
20         patron_display_patron_bills_init(p);
21
22         // gives: p.patron_edit, p.redraw_patron_edit
23         patron_display_patron_edit_init(p);
24
25         //patron_display_survey_init(p);
26
27         p.set_patron = function (au) {
28                 p.patron_edit._patron = au;
29                 return p._patron = au;
30         }
31
32         p.display_patron = function (exceptions) {
33                 if (!exceptions) exceptions = {};
34                 if (!exceptions.all) {
35                         if (!exceptions.patron_checkout_items) p.redraw_patron_checkout_items();
36                         if (!exceptions.patron_items) p.redraw_patron_items();
37                         if (!exceptions.patron_holds) p.redraw_patron_holds();
38                         if (!exceptions.patron_bills) p.redraw_patron_bills();
39                         if (!exceptions.patron_display_survey) patron_display_survey_init(p);
40                         //if (!exceptions.patron_edit) p.redraw_patron_edit();
41                 }
42                 return render_fm(p.w.document, { 'au' : p._patron });
43         }
44         p.redraw = p.display_patron;
45
46         p.retrieve_patron_via_barcode = function (barcode) {
47                 if (!barcode) barcode = patron_get_barcode( p._patron );
48                 p.set_patron( retrieve_patron_by_barcode( barcode ) );
49                 return p.display_patron( {} );
50         }
51
52         p.retrieve_patron_via_id = function (id, exceptions) {
53                 p.set_patron( retrieve_patron_by_id( id ) );
54                 return p.display_patron(exceptions);
55         }
56
57         p.refresh = function(exceptions) {
58                 if (p._patron) p.retrieve_patron_via_id( p._patron.id(), exceptions );
59         }
60
61         set_patron_display_widgets(p);
62
63         if (p.patron) {
64                 if (typeof(p.patron) == 'object') {
65                         //p._patron = p.patron;
66                         p.set_patron( p.patron );
67                         p.display_patron();
68                 } else
69                         p.retrieve_patron_via_barcode( p.patron );
70         }
71
72         if (p._patron && p._patron.alert_message()) {
73                 //snd_bad(); snd_bad();
74                 s_alert(
75                         'PATRON ALERT MESSAGE\n\n\n\n' +
76                         p._patron.alert_message() +
77                         '\n\n\n\nTo remove this alert permanently, Edit the patron and erase the message in "Alert Message".\n\n'
78                 );
79         }
80
81         sdump('D_TRACE_EXIT',arg_dump(arguments));
82         return p;
83 }
84
85 function set_patron_display_widgets(p) {
86         p.commandset_node.getElementsByAttribute('id','cmd_patron_refresh')[0].addEventListener(
87                 'command',
88                 function (ev) {
89                         p.refresh();
90                 },
91                 false
92         );
93
94         function gen_func(i) {
95                 // because otherwise i would be 5 for each closure
96                 return function(ev) {
97                         p.clamshell.set_second_deck(i);
98                 };
99         }
100         var cmds = [ 'cmd_patron_checkout', 'cmd_patron_items', 'cmd_patron_holds', 
101                 'cmd_patron_bills', 'cmd_patron_edit', 'cmd_patron_info' ]
102         for (var i in cmds) {
103                 p.commandset_node.getElementsByAttribute('id',cmds[i])[0].addEventListener(
104                         'command',
105                         gen_func(i),
106                         false
107                 );
108         }
109         p.commandset_node.getElementsByAttribute('id','cmd_patron_checkout')[0].addEventListener(
110                 'command',
111                 function () {
112                         focus_widget( p.w.document, 'patron_checkout_barcode_entry_textbox' );
113                 },
114                 false
115         );
116         p.commandset_node.getElementsByAttribute('id','cmd_patron_bills')[0].addEventListener(
117                 'command',
118                 function () {
119                         focus_widget( p.w.document, 'bill_payment_amount_textbox' );
120                 },
121                 false
122         );
123         p.commandset_node.getElementsByAttribute('id','cmd_patron_edit')[0].addEventListener(
124                 'command',
125                 function () {
126                         p.redraw_patron_edit();
127                 },
128                 false
129         );
130
131
132
133
134 }
135
136
137 function patron_display_clamshell_init(p) {
138         p.clamshell = clam_shell_init( { 'w' : p.w, 'node' : p.clamshell_node, 'debug' : p.app } );
139         p.left_panel = p.clamshell.first_deck;
140         p.right_panel = p.clamshell.second_deck;
141 }
142
143 function patron_display_patron_items_init(p) {
144         p.patron_items = patron_items_init( { 'w' : p.w, 'node' : p.patron_items_node, 'debug' : p.app } );
145
146         p.w.document.getElementById('item_print').addEventListener(
147                 'command',
148                 function(ev) {
149                         var params = { 
150                                 'au' : p._patron, 
151                                 'lib' : mw.G.user_ou,
152                                 'staff' : mw.G.user,
153                                 'header' : mw.G.itemsout_header,
154                                 'line_item' : mw.G.itemsout_line_item,
155                                 'footer' : mw.G.itemsout_footer
156                         };
157                         mw.print_itemsout_receipt( params );
158                 }, false
159         );
160
161         p.redraw_patron_items = function() {
162                 p.patron_items.clear_patron_items();
163                 if (!p._patron.checkouts()) patron_get_checkouts( p._patron );
164                 for (var i = 0; i < p._patron.checkouts().length; i++) {
165                         p.patron_items.add_patron_items( [ i ] );
166                 }
167         }
168
169         p.patron_items.register_patron_items_select_callback(
170                 function (ev) {
171                         sdump('D_PATRON_DISPLAY','Firing patron_items_select_callback\n');
172                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\n');
173                         var patron_items = get_list_from_tree_selection( p.patron_items.paged_tree.tree );
174                         /* grab cover art for selected item? */
175                 }
176         );
177         p.patron_items.register_flesh_patron_items_function(
178                 function (treeitem) {
179                         sdump('D_PATRON_DISPLAY','Firing flesh_patron_items_function\n');
180                         var record_id = treeitem.getAttribute('record_id'); 
181                         p.patron_items.map_patron_items_to_cols( p._patron.checkouts()[ record_id ], treeitem );
182                 }
183         );
184         p.patron_items.register_item_context_builder(
185                 function (ev) {
186                         sdump('D_PATRON_DISPLAY','Firing context_builder for patron_items\n');
187                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\np.patron_items.paged_tree.popup = ' + p.patron_items.paged_tree.popup + '\n');
188                         empty_widget(p.patron_items.paged_tree.popup);
189                         var patron_items = get_list_from_tree_selection( p.patron_items.paged_tree.tree );
190                         sdump('D_PATRON_DISPLAY','patron_items.length = ' + patron_items.length + '\n');
191
192                         /*** RENEW ***/
193                         var menuitem_pi_r = p.patron_items.paged_tree.w.document.createElement('menuitem');
194                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_r );
195                         menuitem_pi_r.setAttribute('label',getString('circ.context_renew'));
196                         menuitem_pi_r.addEventListener(
197                                 'command',
198                                 function (ev) {
199                                         sdump('D_PATRON_DISPLAY','Firing renew context for patron_items\n');
200                                         for (var i = 0; i < patron_items.length; i++) {
201                                                 try {
202                                                         var idx = patron_items[i].getAttribute('record_id'); 
203                                                         var circ = p._patron.checkouts()[ idx ].circ;
204                                                         renew_by_circ_id( circ.id() );
205                                                         p.refresh();
206                                                 } catch(E) {
207                                                         alert(E);
208                                                 }
209                                         }
210                                 },
211                                 false
212                         );
213
214                         /*** CHECKIN ***/
215                         var menuitem_pi_c = p.patron_items.paged_tree.w.document.createElement('menuitem');
216                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_c );
217                         menuitem_pi_c.setAttribute('label',getString('circ.context_checkin'));
218                         menuitem_pi_c.addEventListener(
219                                 'command',
220                                 function (ev) {
221                                         sdump('D_PATRON_DISPLAY','Firing checkin context for patron_items\n');
222                                         for (var i = 0; i < patron_items.length; i++) {
223                                                 try {
224                                                         var idx = patron_items[i].getAttribute('record_id'); 
225                                                         var copy = p._patron.checkouts()[ idx ].copy;
226                                                         var check = checkin_by_copy_barcode( copy.barcode(), null );
227                                                         if (check != null && check.status == 0) {
228                                                                 check.route_to = mw.G.org_tree_hash[check.route_to].shortname();
229                                                                 alert('Check In: ' + check.text + '  Route To: ' + check.route_to);
230                                                         }
231                                                         p.refresh();
232                                                 } catch(E) {
233                                                         alert(E);
234                                                 }
235                                         }
236                                 },
237                                 false
238                         );
239
240                         /*** LOST ***/
241                         var menuitem_pi_r = p.patron_items.paged_tree.w.document.createElement('menuitem');
242                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_r );
243                         menuitem_pi_r.setAttribute('label',getString('circ.context_lost'));
244                         menuitem_pi_r.addEventListener(
245                                 'command',
246                                 function (ev) {
247                                         sdump('D_PATRON_DISPLAY','Firing lost context for patron_items\n');
248                                         for (var i = 0; i < patron_items.length; i++) {
249                                                 try {
250                                                         var idx = patron_items[i].getAttribute('record_id'); 
251                                                         var circ = p._patron.checkouts()[ idx ].circ;
252                                                         mark_circ_as_lost( circ ) ;
253                                                         p.refresh();
254                                                 } catch(E) {
255                                                         alert(E);
256                                                 }
257                                         }
258                                 },
259                                 false
260                         );
261
262                         /* separator */
263                         var menuitem_pi_s = p.patron_items.paged_tree.w.document.createElement('menuseparator');
264                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_s );
265                         
266
267                         /*** COPY EDITOR ***/
268                         var menuitem_pi_ce = p.patron_items.paged_tree.w.document.createElement('menuitem');
269                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_ce );
270                         menuitem_pi_ce.setAttribute('label',getString('circ.context_edit'));
271                         menuitem_pi_ce.addEventListener(
272                                 'command',
273                                 function (ev) {
274                                         sdump('D_PATRON_DISPLAY','Firing copy editor context for patron_items\n');
275                                         for (var i = 0; i < patron_items.length; i++) {
276                                                 sdump('D_PATRON_DISPLAY','Firing copy edit context\n');
277                                                 var idx = patron_items[i].getAttribute('record_id'); 
278                                                 var copy = p._patron.checkouts()[ idx ].copy;
279                                                 spawn_batch_copy_editor(
280                                                         p.w.app_shell,'new_tab','main_tabbox',{ 
281                                                                 'copy_ids' : [ copy.id() ]
282                                                         }
283                                                 );
284                                         }
285                                 },
286                                 false
287                         );
288
289                         /*** OPAC ***/
290                         var menuitem_pi_o = p.patron_items.paged_tree.w.document.createElement('menuitem');
291                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_o );
292                         menuitem_pi_o.setAttribute('label',getString('circ.context_opac'));
293                         menuitem_pi_o.addEventListener(
294                                 'command',
295                                 function (ev) {
296                                         alert('Not Yet Implemented');
297                                         sdump('D_PATRON_DISPLAY','Firing opac context for patron_items\n');
298                                         for (var i = 0; i < patron_items.length; i++) {
299                                                 sdump('D_PATRON_DISPLAY','Firing opac context\n');
300                                         }
301                                 },
302                                 false
303                         );
304                         
305                 }
306         );
307 }
308
309 function patron_display_patron_checkout_items_init(p) {
310         p.patron_checkout_items = patron_checkout_items_init( { 'w' : p.w, 'node' : p.patron_checkout_items_node, 'debug' : p.app } );
311         var tb = p.patron_checkout_items_node.getElementsByAttribute('id','patron_checkout_barcode_entry_textbox')[0];
312         var submit_button = p.patron_checkout_items_node.getElementsByAttribute('id','patron_checkout_submit_barcode_button')[0];
313
314         var checkouts = [];
315
316         function print_receipt() {
317                 p._patron._current_checkouts = checkouts;
318                 var params = { 
319                         'au' : p._patron, 
320                         'lib' : mw.G.user_ou,
321                         'staff' : mw.G.user,
322                         'header' : mw.G.checkout_header,
323                         'line_item' : mw.G.checkout_line_item,
324                         'footer' : mw.G.checkout_footer
325                 };
326                 mw.print_checkout_receipt( params );
327         }
328
329         p.w.document.getElementById('checkout_print').addEventListener( 'command',print_receipt, false);
330
331         p.w.document.getElementById('checkout_done').addEventListener(
332                 'command',
333                 function () {
334                         if (p.w.document.getElementById('checkout_auto').checked) print_receipt(); 
335                         checkouts = []; p.display_patron(); tb.focus();
336                 },
337                 false
338         );
339         p.attempt_checkout = function(barcode) {
340                 try {
341                         //if (! is_barcode_valid(barcode) ) throw('Invalid Barcode');
342                         var permit_check = checkout_permit( barcode, p._patron.id(), 0 );
343                         if (permit_check.status == 0) {
344                                 var check = checkout_by_copy_barcode( barcode, p._patron.id() );
345                                 if (check) {
346                                         checkouts.push( check );
347                                         p.patron_checkout_items.add_checkout_items( [ checkouts.length - 1 ] );
348                                         var temp = p._patron.checkouts();
349                                         temp.push( check );
350                                         p._patron.checkouts( temp );
351                                         render_fm(p.w.document, { 'au' : p._patron }); // p.display_patron();
352                                         p.redraw_patron_items();
353                                         tb.value = '';
354                                 }
355                         } else {
356                                 throw(permit_check.text);
357                         }
358                 } catch(E) {
359                         tb.select();
360                         if (typeof(E) == 'object') {
361                                 handle_error(E,true);
362                         } else {
363                                 s_alert(E);
364                         }
365                 }
366                 tb.focus();
367         }
368
369         tb.addEventListener(
370                 'keypress',
371                 function(ev) {
372                         if (ev.keyCode == 13 || ev.keyCode == 77 ) { p.attempt_checkout( tb.value ); }
373                 },
374                 false
375         );
376         submit_button.addEventListener(
377                 'command',
378                 function(ev) {
379                         p.attempt_checkout( tb.value );
380                 },
381                 false
382         );
383
384         p.redraw_patron_checkout_items = function() {
385                 p.patron_checkout_items.clear_checkout_items();
386                 for (var i = 0; i < checkouts.length; i++) {
387                         p.patron_checkout_items.add_checkout_items( [ i ] );
388                 }
389         }
390
391         p.patron_checkout_items.register_patron_checkout_items_select_callback(
392                 function (ev) {
393                         sdump('D_PATRON_DISPLAY','Firing patron_checkout_items_select_callback\n');
394                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\n');
395                         var patron_checkout_items = get_list_from_tree_selection( p.patron_checkout_items.paged_tree.tree );
396                         /* grab cover art for selected item? */
397                 }
398         );
399         p.patron_checkout_items.register_flesh_patron_checkout_items_function(
400                 function (treeitem) {
401                         sdump('D_PATRON_DISPLAY','Firing flesh_patron_checkout_items_function\n');
402                         var record_id = treeitem.getAttribute('record_id'); 
403                         p.patron_checkout_items.map_patron_checkout_items_to_cols( checkouts[ record_id ], treeitem );
404                 }
405         );
406         p.patron_checkout_items.register_checkout_context_builder(
407                 function (ev) {
408                         sdump('D_PATRON_DISPLAY','Firing context_builder for patron_checkout_items\n');
409                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\np.patron_checkout_items.paged_tree.popup = ' + p.patron_checkout_items.paged_tree.popup + '\n');
410                         empty_widget(p.patron_checkout_items.paged_tree.popup);
411                         var patron_checkout_items = get_list_from_tree_selection( p.patron_checkout_items.paged_tree.tree );
412
413                         /*** CHECKIN ***/
414                         var menuitem_pci_c = p.patron_checkout_items.paged_tree.w.document.createElement('menuitem');
415                         p.patron_checkout_items.paged_tree.popup.appendChild( menuitem_pci_c );
416                         menuitem_pci_c.setAttribute('label',getString('circ.context_checkin'));
417                         menuitem_pci_c.addEventListener(
418                                 'command',
419                                 function (ev) {
420                                         sdump('D_PATRON_DISPLAY','Firing checkin context for patron_checkout_items\n');
421                                         var keep_these = [];
422                                         for (var i = 0; i < patron_checkout_items.length; i++) {
423                                                 try {
424                                                         var idx = patron_checkout_items[i].getAttribute('record_id'); 
425                                                         var copy = checkouts[ idx ].copy;
426                                                         var check = checkin_by_copy_barcode( copy.barcode(), null );
427                                                         if (check == null) { // change this to whatever it takes
428                                                                 keep_these.push( checkouts[ idx ] );    
429                                                         }
430                                                         if (check != null && check.status == 0) {
431                                                                 check.route_to = mw.G.org_tree_hash[check.route_to].shortname();
432                                                                 alert('Check In: ' + check.text + '  Route To: ' + check.route_to);
433                                                         }
434                                                         checkouts = keep_these;
435                                                         p.refresh();
436                                                 } catch(E) {
437                                                         alert(E);
438                                                 }
439                                         }
440                                 },
441                                 false
442                         );
443
444                         /* separator */
445                         var menuitem_pci_s = p.patron_checkout_items.paged_tree.w.document.createElement('menuseparator');
446                         p.patron_checkout_items.paged_tree.popup.appendChild( menuitem_pci_s );
447                         
448
449                         /*** COPY EDITOR ***/
450                         var menuitem_pci_ce = p.patron_checkout_items.paged_tree.w.document.createElement('menuitem');
451                         p.patron_checkout_items.paged_tree.popup.appendChild( menuitem_pci_ce );
452                         menuitem_pci_ce.setAttribute('label',getString('circ.context_edit'));
453                         menuitem_pci_ce.addEventListener(
454                                 'command',
455                                 function (ev) {
456                                         sdump('D_PATRON_DISPLAY','Firing copy editor context for patron_checkout_items\n');
457                                         for (var i = 0; i < patron_checkout_items.length; i++) {
458                                                 var idx = patron_checkout_items[i].getAttribute('record_id');
459                                                 var copy = checkouts[ idx ].copy;
460                                                 sdump('D_PATRON_DISPLAY','Firing copy edit context\n');
461                                                 spawn_batch_copy_editor(
462                                                         p.w.app_shell,'new_tab','main_tabbox',{ 
463                                                                 'copy_ids' : [ copy.id() ]
464                                                         }
465                                                 );
466                                         }
467                                 },
468                                 false
469                         );
470
471                         /*** OPAC ***/
472                         var menuitem_pci_o = p.patron_checkout_items.paged_tree.w.document.createElement('menuitem');
473                         p.patron_checkout_items.paged_tree.popup.appendChild( menuitem_pci_o );
474                         menuitem_pci_o.setAttribute('label',getString('circ.context_opac'));
475                         menuitem_pci_o.addEventListener(
476                                 'command',
477                                 function (ev) {
478                                         alert('Not Yet Implemented');
479                                         sdump('D_PATRON_DISPLAY','Firing opac context for patron_checkout_items\n');
480                                         for (var i = 0; i < patron_checkout_items.length; i++) {
481                                                 var idx = patron_checkout_items[i].getAttribute('record_id');
482                                                 sdump('D_PATRON_DISPLAY','Firing opac context\n');
483                                         }
484                                 },
485                                 false
486                         );
487                         
488                 }
489         );
490 }
491
492 function patron_display_patron_holds_init(p) {
493         p.patron_holds = patron_holds_init( { 'w' : p.w, 'node' : p.patron_holds_node, 'debug' : p.app } );
494
495         p.redraw_patron_holds = function() {
496                 p.patron_holds.clear_patron_holds();
497                 if (!p._patron.hold_requests()) patron_get_holds( p._patron );
498                 for (var i = 0; i < p._patron.hold_requests().length; i++) {
499                         p.patron_holds.add_patron_holds( [ i ] );
500                 }
501         }
502
503         p.patron_holds.register_patron_holds_select_callback(
504                 function (ev) {
505                         sdump('D_PATRON_DISPLAY','Firing patron_holds_select_callback\n');
506                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\n');
507                         var patron_holds = get_list_from_tree_selection( p.patron_holds.paged_tree.tree );
508                         /* grab cover art for selected item? */
509                 }
510         );
511         p.patron_holds.register_flesh_patron_holds_function(
512                 function (treeitem) {
513                         sdump('D_PATRON_DISPLAY','Firing flesh_patron_holds_function\n');
514                         var record_id = treeitem.getAttribute('record_id'); 
515                         var hold = p._patron.hold_requests()[ record_id ];
516                         patron_get_hold_status(
517                                 hold,
518                                 function (request) {
519                                         var result = request.getResultObject();
520                                         hold.status( hold_status_as_text( result ) );
521                                         p.patron_holds.map_patron_holds_to_cols( hold, treeitem );
522                                 }
523                         );
524                 }
525         );
526         p.patron_holds.register_item_context_builder(
527                 function (ev) {
528                         sdump('D_PATRON_DISPLAY','Firing context_builder for patron_holds\n');
529                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\np.patron_holds.paged_tree.popup = ' + p.patron_holds.paged_tree.popup + '\n');
530                         empty_widget(p.patron_holds.paged_tree.popup);
531                         var patron_holds = get_list_from_tree_selection( p.patron_holds.paged_tree.tree );
532                         sdump('D_PATRON_DISPLAY','patron_holds.length = ' + patron_holds.length + '\n');
533
534                         /*** CANCEL HOLD ***/
535                         var menuitem_ph_ce = p.patron_holds.paged_tree.w.document.createElement('menuitem');
536                         p.patron_holds.paged_tree.popup.appendChild( menuitem_ph_ce );
537                         menuitem_ph_ce.setAttribute('label',getString('circ.context_cancel_hold'));
538                         menuitem_ph_ce.addEventListener(
539                                 'command',
540                                 function (ev) {
541                                         sdump('D_PATRON_DISPLAY','Firing cancel hold context for patron_holds\n');
542                                         for (var i = 0; i < patron_holds.length; i++) {
543                                                 sdump('D_PATRON_DISPLAY','Firing cancel edit context\n');
544                                                 var record_id = patron_holds[i].getAttribute('record_id');
545                                                 var hold = p._patron.hold_requests()[ record_id ];
546                                                 cancel_hold( hold );
547                                         }
548                                         p.refresh();
549                                 },
550                                 false
551                         );
552
553                         /* separator */
554                         var menuitem_ph_s = p.patron_holds.paged_tree.w.document.createElement('menuseparator');
555                         p.patron_holds.paged_tree.popup.appendChild( menuitem_ph_s );
556                         
557                         /*** COPY EDITOR ***/
558                         var menuitem_ph_ce = p.patron_holds.paged_tree.w.document.createElement('menuitem');
559                         p.patron_holds.paged_tree.popup.appendChild( menuitem_ph_ce );
560                         menuitem_ph_ce.setAttribute('label',getString('circ.context_edit'));
561                         menuitem_ph_ce.addEventListener(
562                                 'command',
563                                 function (ev) {
564                                         sdump('D_PATRON_DISPLAY','Firing copy editor context for patron_holds\n');
565                                         for (var i = 0; i < patron_holds.length; i++) {
566                                                 sdump('D_PATRON_DISPLAY','Firing copy edit context\n');
567                                                 var idx = patron_holds[i].getAttribute('record_id'); 
568                                                 var copy = p._patron.hold_requests()[ idx ].copy;
569                                                 spawn_batch_copy_editor(
570                                                         p.w.app_shell,'new_tab','main_tabbox',{ 
571                                                                 'copy_ids' : [ copy.id() ]
572                                                         }
573                                                 );
574                                         }
575                                 },
576                                 false
577                         );
578
579                         /*** OPAC ***/
580                         var menuitem_ph_o = p.patron_holds.paged_tree.w.document.createElement('menuitem');
581                         p.patron_holds.paged_tree.popup.appendChild( menuitem_ph_o );
582                         menuitem_ph_o.setAttribute('label',getString('circ.context_opac'));
583                         menuitem_ph_o.addEventListener(
584                                 'command',
585                                 function (ev) {
586                                         alert('Not Yet Implemented');
587                                         sdump('D_PATRON_DISPLAY','Firing opac context for patron_holds\n');
588                                         for (var i = 0; i < patron_holds.length; i++) {
589                                                 sdump('D_PATRON_DISPLAY','Firing opac context\n');
590                                         }
591                                 },
592                                 false
593                         );
594                         
595                 }
596         );
597 }
598
599 function patron_display_patron_bills_init(p) {
600         p.patron_bills = patron_bills_init( { 
601                 'w' : p.w, 
602                 'node' : p.patron_bills_node, 
603                 'debug' : p.app 
604         } );
605
606         p.patron_bills.refresh = function() { p.refresh(); }
607
608         p.redraw_patron_bills = function() {
609                 try {
610                         p.patron_bills.clear_patron_bills();
611                         if (!p._patron.bills) patron_get_bills( p._patron );
612                         p.patron_bills.add_patron_bills( p._patron.bills );
613                 } catch(E) {
614                         sdump('D_ERROR',js2JSON(E) + '\n');
615                 }
616         }
617 }
618
619 function patron_display_patron_edit_init(p) {
620         /*
621         p.patron_edit = patron_edit_init( { 
622                 'w' : p.w, 
623                 'node' : p.patron_edit_node, 
624                 'debug' : p.app
625         } );
626
627         p.patron_edit.redisplay = function() { p.display_patron( {'patron_edit':true} ); }
628         p.patron_edit.refresh = function() { p.refresh( {'patron_edit':true} ); }
629
630         p.redraw_patron_edit = function() {
631                 try {
632                         p.patron_edit.clear_patron_edit();
633                         p.patron_edit.add_rows( p._patron );
634                 } catch(E) {
635                         sdump('D_ERROR',js2JSON(E) + '\n');
636                 }
637         }
638         */
639         /* shoehorn in the old legacy stuff */
640         p.patron_edit = {};
641         p.redraw_patron_edit = function() { 
642                 empty_widget( p.patron_edit_node );
643                 setTimeout(
644                         function() {
645                                 var frame = p.w.document.createElement('iframe');
646                                 p.patron_edit_node.appendChild( frame );
647                                 frame.setAttribute('flex','1');
648                                 frame.setAttribute('src','chrome://evergreen/content/patron/patron_edit_legacy.xul');
649                                 frame.contentWindow.mw = mw;
650                                 frame.contentWindow.params = {};
651                                 var barcode = patron_get_barcode( p._patron );
652                                 frame.contentWindow.params.barcode = barcode;
653                                 frame.contentWindow.params._patron = p._patron;
654                                 frame.contentWindow.patron_save_callback = function ( params ) {
655                                         p._patron = params.au;
656                                         p.display_patron();
657                                 }
658                                 p.patron_edit.frame = frame;
659                         }, 0
660                 );
661         }
662 }
663
664 function patron_display_survey_init(p) {
665         var gb = p.w.document.getElementById('PatronDisplaySurvey_groupbox');
666         var count = gb.childNodes.length; for (var i = 1; i < count; i++) { gb.removeChild( gb.lastChild ); } // all but caption
667         mw.user_request(
668                 'open-ils.circ', 'open-ils.circ.survey.retrieve.required', [ mw.G.auth_ses[0] ],
669                 function (request) {
670                         var surveys = request.getResultObject();
671                         for (var i = 0; i < surveys.length; i++) {
672
673                                 var survey = a_get(surveys, i);
674                         
675                                 var hbox = document.createElement('hbox');
676                                 gb.appendChild(hbox);
677
678                                 var name_label = document.createElement('label');
679                                 name_label.setAttribute('value',survey.name());
680                                 hbox.appendChild(name_label);
681
682                                 var answer_label = document.createElement('label');
683                                 answer_label.setAttribute('value','');
684                                 hbox.appendChild(answer_label);
685
686                                 var taken_label = document.createElement('label');
687                                 taken_label.setAttribute('value','Not Taken');
688                                 hbox.appendChild(taken_label);
689
690                                 mw.user_request(
691                                         'open-ils.circ', 'open-ils.circ.survey.response.retrieve',
692                                         [ mw.G.auth_ses[0], survey.id(), p._patron.id() ],
693                                         function (request) {
694                                                 var responses = request.getResultObject();
695                                                 if (responses.length > 0) {
696                                                         var response = responses.pop(); // last response
697                                                         var date;
698                                                         if (response.effective_date()) {
699                                                                 date = formatted_date( response.effective_date(), '%D' );
700                                                         } else {
701                                                                 date = formatted_date( response.answer_date(), '%D' );
702                                                         }
703                                                         taken_label.setAttribute('value',date);
704                                                         var answer = find_id_object_in_list( // first answer
705                                                                 find_id_object_in_list(
706                                                                         survey.questions(),
707                                                                         response.question()
708                                                                 ).answers(),
709                                                                 response.answer()
710                                                         ).answer();
711                                                         answer_label.setAttribute('value',answer);
712                                                 }
713                                         }
714                                 );
715                         }
716                 }
717         );
718 }