LP#1694577 Reports template searching and paging
[Evergreen.git] / Open-ILS / web / reports / oils_rpt_folder_window.js
1 dojo.requireLocalization("openils.reports", "reports");
2
3 var rpt_strings = dojo.i18n.getLocalization("openils.reports", "reports");
4 var NG_NEW_TEMPLATE_INTERFACE = '/eg/staff/reporter/template/new';
5 var NG_CLONE_TEMPLATE_INTERFACE = '/eg/staff/reporter/template/clone';
6 var NG_CLONE_LEGACY_TEMPLATE_INTERFACE = '/eg/staff/reporter/legacy/template/clone';
7 var OILS_TEMPLATE_INTERFACE = 'xul/template_builder.xul';
8 var OILS_LEGACY_TEMPLATE_INTERFACE = 'oils_rpt_builder.xhtml';
9
10
11 /* generic folder window class */
12 oilsRptSetSubClass('oilsRptFolderWindow', 'oilsRptObject');
13 function oilsRptFolderWindow(type, folderId) { 
14         this.init();
15         var node = oilsRptCurrentFolderManager.findNode(type, folderId);
16         this.selector = DOM.oils_rpt_folder_contents_selector;
17         this.folderNode = node;
18         this.type = type;
19     this.folderId = folderId;
20     oilsRptFolderWindow.folderIdMap[folderId] = this;
21 }
22
23 // maps folder IDs to their containing oilsRptFolderWindow objects
24 oilsRptFolderWindow.folderIdMap = {};
25
26 // Here lie the contents of a specific folder
27 oilsRptFolderWindow.prototype.draw = function(viaPaging) {
28
29         _debug('drawing folder window for ' + this.folderNode.folder.name() );
30
31     // always start a new folder at the first page of results
32     if (!viaPaging) oilsRptOutputOffset = 0;
33
34     console.log('drawing folder ' + this.folderNode.folder.name() + 
35         ' ; offset=' + oilsRptOutputOffset + ' ; limit=' + oilsRptOutputLimit);
36
37         var obj = this;
38         setSelector(DOM.oils_rpt_output_limit_selector, oilsRptOutputLimit);
39         setSelector(DOM.oils_rpt_output_limit_selector_2, oilsRptOutputLimit2);
40
41         DOM.oils_rpt_output_limit_selector.onchange = function() {
42                 oilsRptOutputLimit = getSelectorVal(DOM.oils_rpt_output_limit_selector);
43                 obj.draw(); // resets offset
44         }
45
46         DOM.oils_rpt_output_limit_selector_2.onchange = function() {
47                 oilsRptOutputLimit2 = getSelectorVal(DOM.oils_rpt_output_limit_selector_2);
48                 obj.draw(); // resets offset
49         }
50
51     DOM.oils_rpt_output_next_selector.onclick = function() {
52         oilsRptOutputOffset += Number(oilsRptOutputLimit);
53         obj.draw(true);
54     }
55
56     // Enable / disable the Previous and Start links depending on
57     // current page.
58     if (oilsRptOutputOffset <= 0) {
59         DOM.oils_rpt_output_prev_selector.onclick = function(){};
60         DOM.oils_rpt_output_first_selector.onclick = function(){};
61         DOM.oils_rpt_output_prev_selector.removeAttribute('href');
62         DOM.oils_rpt_output_first_selector.removeAttribute('href');
63     } else {
64         DOM.oils_rpt_output_prev_selector.setAttribute('href', 'javascript:');
65         DOM.oils_rpt_output_first_selector.setAttribute('href', 'javascript:');
66
67         DOM.oils_rpt_output_prev_selector.onclick = function() {
68             oilsRptOutputOffset -= Number(oilsRptOutputLimit);
69             obj.draw(true);
70         }
71
72         DOM.oils_rpt_output_first_selector.onclick = function() {
73             obj.draw(); // resets offset
74         }
75     }
76
77         var mine = ( this.folderNode.folder.owner().id() == USER.id() );
78
79         _debug('drawing folder window with type '+this.type);
80         if(mine) _debug('folder is mine...');
81
82         if( mine && this.type == 'template') 
83                 unHideMe(DOM.oils_rpt_folder_window_contents_new_template.parentNode);
84         else hideMe(DOM.oils_rpt_folder_window_contents_new_template.parentNode);
85
86         unHideMe(DOM.oils_rpt_folder_window_contents_div);
87         hideMe(DOM.oils_rpt_folder_manager_div);
88         hideMe(DOM.oils_rpt_top_folder);
89
90         DOM.oils_rpt_folder_window_manage_tab.onclick = function() {
91                 unHideMe(DOM.oils_rpt_folder_window_contents_div);
92                 hideMe(DOM.oils_rpt_folder_manager_div);
93         }
94         DOM.oils_rpt_folder_window_edit_tab.onclick = function() {
95                 hideMe(DOM.oils_rpt_folder_window_contents_div);
96                 unHideMe(DOM.oils_rpt_folder_manager_div);
97         }
98
99         this.setFolderEditActions();
100
101         hideMe(DOM.oils_rpt_template_folder_new_report);
102         unHideMe(DOM.oils_rpt_folder_table_right_td);
103         hideMe(DOM.oils_rpt_folder_table_alt_td);
104         this.drawFolderDetails();
105
106         DOM.oils_rpt_folder_content_action_go.onclick = 
107                 function() {obj.doFolderAction()}
108
109         this.fetchFolderData();
110
111         var sel = DOM.oils_rpt_folder_contents_action_selector;
112         var x = true;
113         for( var i = 0; i < sel.options.length; i++ ) {
114                 var opt = sel.options[i];
115
116                 if( !mine && opt.getAttribute('value').match(/move/) ) {
117                         hideMe(opt);
118                         continue;
119                 }
120
121                 if( opt.getAttribute('type') == this.type ) {
122                         if(x && !opt.disabled) {
123                                 opt.selected = true;
124                                 x = false;
125                         }
126                         unHideMe(opt);
127                 } else hideMe(opt);
128         }
129
130         this.drawEditActions();
131
132         var porg = PERMS.SHARE_REPORT_FOLDER;
133         if( porg < 1 ) 
134                 DOM.oils_rpt_folder_manager_share_opt.disabled = true;
135 }
136
137 oilsRptFolderWindow.prototype.drawEditActions = function() {
138
139         DOM.oils_rpt_folder_window_contents_new_template.onclick = function() {
140                 var s = location.search+'';
141                 s = s.replace(/\&folder=\d+/g,'');
142                 s = s.replace(/\&ct=\d+/g,'');
143                 if (window.IAMBROWSER) {
144             window.top.location.href = NG_NEW_TEMPLATE_INTERFACE+'/'+obj.folderNode.folder.id();
145         } else {
146             goTo( OILS_TEMPLATE_INTERFACE+s+'&folder='+obj.folderNode.folder.id());
147         }
148         }
149
150
151         if( this.folderNode.folder.owner().id() != USER.id() )
152                 hideMe(DOM.oils_rpt_folder_manager_tab_table);
153         else
154                 unHideMe(DOM.oils_rpt_folder_manager_tab_table);
155
156         if( isTrue(this.folderNode.folder.shared())) {
157                 DOM.oils_rpt_folder_manager_share_opt.disabled = true;
158                 DOM.oils_rpt_folder_manager_unshare_opt.disabled = false;
159         } else {
160                 DOM.oils_rpt_folder_manager_share_opt.disabled = false;
161                 DOM.oils_rpt_folder_manager_unshare_opt.disabled = true;
162         }
163
164         this.hideFolderActions();
165         var obj = this;
166
167         DOM.oils_rpt_folder_manager_actions_submit.onclick = function() {
168                 var act = getSelectorVal(DOM.oils_rpt_folder_manager_actions);
169                 _debug("doing folder action: " + act);
170                 obj.hideFolderActions();
171                 switch(act) {
172                         case 'change_name':
173                                 unHideMe(DOM.oils_rpt_folder_manager_change_name_div);
174                                 break;
175                         case 'create_sub_folder':
176                                 var porg = PERMS.SHARE_REPORT_FOLDER;
177                                 if( porg < 1 ) 
178                                         DOM.oils_rpt_folder_manager_sub_shared.disabled = true;
179                                 removeChildren(DOM.oils_rpt_folder_manager_sub_lib_picker);
180                                 unHideMe(DOM.oils_rpt_folder_manager_create_sub);
181                                 obj.myOrgSelector = new oilsRptMyOrgsWidget(
182                                         DOM.oils_rpt_folder_manager_sub_lib_picker, USER.ws_ou(), porg)
183                                 obj.myOrgSelector.draw();
184                                 break;
185                         case 'delete':
186                                 obj.doFolderDelete();
187                                 break;
188                         case 'share':
189                                 obj.shareFolder();
190                                 break;
191                         case 'unshare':
192                                 obj.unShareFolder();
193                                 break;
194                 }
195         }
196
197 }
198
199
200 oilsRptFolderWindow.prototype.shareFolder = function() {
201         var folder = this.folderNode.folder;
202         if(isTrue(folder.shared()))
203                 return alertId('oils_rpt_folder_already_shared');
204         unHideMe(DOM.oils_rpt_folder_manager_share_div);
205
206
207         var orgsel = new oilsRptMyOrgsWidget(
208                 DOM.oils_rpt_folder_manager_share_lib_picker, 
209                 USER.ws_ou(), PERMS.SHARE_REPORT_FOLDER);
210         orgsel.draw();
211
212         var type = this.type;
213         DOM.oils_rpt_folder_manager_share_submit.onclick = function() {
214                 folder.shared('t');
215                 folder.share_with(orgsel.getValue());
216                 oilsRptUpdateFolder( folder, type, 
217                         function(success) {
218                                 if(success) {
219                                         oilsRptAlertSuccess();
220                                         oilsRptCurrentFolderManager.draw();
221                                 }
222                         }
223                 );
224         }
225 }
226
227 oilsRptFolderWindow.prototype.unShareFolder = function() {
228         var folder = this.folderNode.folder;
229         if(!isTrue(folder.shared()))
230                 return alertId('oils_rpt_folder_already_unshared');
231         if(!confirmId('oils_rpt_folder_unshare_confirm')) return;
232         folder.shared('f');
233         var type = this.type;
234         oilsRptUpdateFolder( folder, type, 
235                 function(success) {
236                         if(success) {
237                                 oilsRptAlertSuccess();
238                                 oilsRptCurrentFolderManager.draw();
239                         }
240                 }
241         );
242 }
243
244
245 oilsRptFolderWindow.prototype.hideFolderActions = function() {
246         hideMe(DOM.oils_rpt_folder_manager_change_name_div);
247         hideMe(DOM.oils_rpt_folder_manager_create_sub);
248         hideMe(DOM.oils_rpt_folder_manager_share_div);
249 }
250
251
252 oilsRptFolderWindow.prototype.doFolderAction = function() {
253         var objs = (this.fmTable) ? this.fmTable.getSelected() : [];
254
255         if( this.type == 'output' && this.fmTable2 ) 
256                 objs = objs.concat( this.fmTable2.getSelected() );
257
258         if( objs.length == 0 ) 
259                 return alert(rpt_strings.FOLDER_WINDOW_SELECT_ITEM);
260         var action = getSelectorVal(DOM.oils_rpt_folder_contents_action_selector);
261
262         var obj = this;
263         var successCallback = function(errid) {
264                 if(errid) alertId(errid)
265                 else oilsRptAlertSuccess();
266                 obj.draw();
267         };
268
269         var obj = this;
270         switch(action) {
271
272                 case 'create_report' :
273                         hideMe(DOM.oils_rpt_folder_table_right_td);
274                         unHideMe(DOM.oils_rpt_folder_table_alt_td);
275                         unHideMe(DOM.oils_rpt_editor_div);
276                         new oilsRptReportEditor(new oilsReport(objs[0]), this);
277                         break;
278                 case 'delete_report' :
279                         if(!confirmId('oils_rpt_verify_report_delete')) return;
280                         this.deleteReports(objs, 0, successCallback);
281                         break;
282
283                 case 'delete_template' :
284             if(!confirmId('oils_rpt_verify_template_delete')) return;
285                         this.deleteTemplates(objs, 0, successCallback);
286                         break;
287
288                 case 'show_output':
289                         this.showOutput(objs[0]);
290                         break;
291
292                 case 'delete_output':
293                         if(!confirmId('oils_rpt_folder_contents_confirm_delete')) return;
294                         this.deleteOutputs(objs,0, successCallback);
295                         break;
296
297                 case 'move_template':
298                         this.changeBatchFolder(objs, 'template', successCallback);
299                         break;
300
301                 case 'move_report':
302                         this.changeBatchFolder(objs, 'report', successCallback);
303                         break;
304
305                 case 'move_output':
306                         this.changeBatchFolder(objs, 'output', successCallback);
307                         break;
308
309                 case 'clone_template':
310                         this.cloneTemplate(objs[0]);
311         }
312 }
313
314 oilsRptFolderWindow.prototype.changeBatchFolder = function(objs, type, callback) {
315         hideMe(DOM.oils_rpt_folder_window_contents_table);
316         unHideMe(DOM.oils_rpt_move_folder_div)
317         var obj = this;
318         this.drawFolderOptions(type,    
319                 function(folderid) {
320                         obj.changeFolderList(objs, type, folderid, 0, callback);
321                         hideMe(DOM.oils_rpt_move_folder_div)
322                         unHideMe(DOM.oils_rpt_folder_window_contents_table);
323                 }
324         );
325 }
326
327 oilsRptFolderWindow.prototype.cloneTemplate = function(template) {
328         hideMe(DOM.oils_rpt_folder_window_contents_table);
329         unHideMe(DOM.oils_rpt_move_folder_div)
330         var obj = this;
331         this.drawFolderOptions('template',
332                 function(folderid) {
333                         var s = location.search+'';
334                         s = s.replace(/\&folder=\d+/g,'');
335                         s = s.replace(/\&ct=\d+/g,'');
336             version = JSON2js(template.data()).version;
337             if(version && version >= 5) {
338                             window.top.location.href = NG_CLONE_TEMPLATE_INTERFACE+'/'+folderid+'/'+template.id();
339             } else if(version && version >= 2) {
340                         if (window.IAMBROWSER) {
341                                 window.top.location.href = NG_CLONE_LEGACY_TEMPLATE_INTERFACE+'/'+folderid+'/'+template.id();
342                 } else {
343                     _debug('entering new template building interface with template version ' + version);
344                             goTo(OILS_TEMPLATE_INTERFACE+s+'&folder='+folderid+'&ct='+template.id());
345                 }
346             } else {
347                         if (window.IAMBROWSER) {
348                                 window.top.location.href = NG_CLONE_LEGACY_TEMPLATE_INTERFACE+'/'+folderid+'/'+template.id();
349                 } else {
350                             goTo(OILS_LEGACY_TEMPLATE_INTERFACE+s+'&folder='+folderid+'&ct='+template.id());
351                 }
352             }
353                 }
354         );
355 }
356
357
358 oilsRptFolderWindow.prototype.changeFolderList = function(list, type, folderid, idx, callback, errid) {
359         if( idx >= list.length ) return callback(errid);
360         var item = list[idx];
361         var obj = this;
362         var rcback = function(){obj.changeFolderList(list,type,folderid,++idx,callback,errid)};
363
364         item.folder(folderid);
365
366         switch(type) {
367                 case 'template':
368                         oilsRptUpdateTemplate(item,rcback);
369                         break;
370                 case 'report':
371                         oilsRptUpdateReport(item,rcback);
372                         break;
373                 case 'output':
374                         oilsRptUpdateSchedule(item,rcback);
375                         break;
376         }
377 }
378
379 oilsRptFolderWindow.prototype.drawFolderOptions = function(type, callback) {
380         //var oilsRptChangeFolderTree;
381         var selectedFolder;
382         oilsRptBuildFolder(
383                 type,
384                 DOM.oils_rpt_move_folder_picker,
385                 'tree9807897',
386                 rpt_strings.FOLDER_WINDOW_CHANGE_FOLDERS,
387                 function(node) { 
388                         appendClear(DOM.oils_rpt_move_folder_selected, node.folder.name());
389                         selectedFolder = node.folder.id();
390                 } 
391         );
392
393         DOM.oils_rpt_change_folder_submit.onclick = function() {
394                 if(selectedFolder) callback(selectedFolder);
395         }
396
397         DOM.oils_rpt_change_folder_cancel.onclick = function() {
398                 hideMe(DOM.oils_rpt_move_folder_div)
399                 unHideMe(DOM.oils_rpt_folder_window_contents_table);
400         }
401 }
402
403
404 oilsRptFolderWindow.prototype.deleteOutputs = function(list, idx, callback, errid) {
405         if( idx >= list.length ) return callback(errid);
406         var output = list[idx];
407
408         if( output.runner().id()  != USER.id() ) {
409                 this.deleteOutputs(list, ++idx, 
410                         callback, 'oils_rpt_folder_contents_no_delete');
411
412         } else {
413                 _debug('deleting output ' + output.id());
414                 var req = new Request(OILS_RPT_DELETE_SCHEDULE,SESSION,output.id());
415                 var obj = this;
416                 req.callback(function(){obj.deleteOutputs(list, ++idx, callback, errid);});
417                 req.send();
418         }
419 }
420
421 oilsRptFolderWindow.prototype.showOutput = function(sched) {
422         oilsRptFetchReport(sched.report().id(), 
423                 function(r) {
424                         var url = oilsRptBuildOutputLink(r.template(), r.id(), sched.id());
425                         _debug("launching report output view at URL: " + url);
426                         if(isXUL() && !window.IAMBROWSER) 
427                                 xulG.new_tab(xulG.url_prefix('XUL_REMOTE_BROWSER?url=') + url,
428                                         {tab_name: dojo.string.substitute( rpt_strings.FOLDER_WINDOW_REPORT_OUTPUT, [r.name()] ), browser:false},
429                                         {no_xulG:false, show_nav_buttons:true, show_print_button:true});
430                         else {
431                                 //goTo(url);
432                                 var win = window.open(url,r.name(), 'resizable,width=800,height=600,scrollbars=1'); 
433                         }
434                 }
435         );
436 }
437
438
439 oilsRptFolderWindow.prototype.deleteReports = function(list, idx, callback, errid) {
440         if( idx >= list.length ) return callback(errid);
441         var report = list[idx];
442
443         var obj = this;
444         if( report.owner().id() != USER.id() ) {
445                 this.deleteReports(list, ++idx, 
446                         callback, 'oils_rpt_folder_contents_no_delete');
447
448         } else {
449
450 //              var req0 = new Request(OILS_RPT_REPORT_HAS_OUTS, SESSION, report.id());
451 //              req0.callback(
452 //                      function(r0) {
453 //                              var r0es = r0.getResultObject();
454 //                              if( r0es != '0' ) {
455 //                                      obj.deleteReports(list, ++idx, 
456 //                                              callback, 'oils_rpt_folder_contents_report_no_delete');
457 //                              } else {
458                                         _debug('deleting report ' + report.id());
459                                         var req = new Request(OILS_RPT_DELETE_REPORT, SESSION, report.id());
460                                         req.callback(function(r) { 
461                                                 var res = r.getResultObject();
462                                                 if( res == 0 ) return oilsRptAlertFailure();
463                                                 obj.deleteReports(list, ++idx, callback, errid)
464                                         });
465                                         req.send();
466 //                              }
467 //                      }
468 //              );
469 //
470 //              req0.send();
471         }
472 }
473
474 oilsRptFolderWindow.prototype.deleteTemplates = function(list, idx, callback, errid) {
475         if( idx >= list.length ) return callback(errid);
476         var tmpl = list[idx];
477
478         var obj = this;
479         if( tmpl.owner().id() != USER.id() ) {
480                 this.deleteTemplates(list, ++idx, 
481                         callback, 'oils_rpt_folder_contents_no_delete');
482
483         } else {
484
485
486 //              var req0 = new Request( OILS_RPT_TEMPLATE_HAS_RPTS, SESSION, tmpl.id() );
487 //              req0.callback(
488 //                      function(r0) {
489 //                              var resp = r0.getResultObject();
490 //
491 //                              if( resp != '0' ) {
492 //                                      obj.deleteTemplates(list, ++idx, 
493 //                                              callback, 'oils_rpt_folder_contents_template_no_delete');
494 //
495 //                              } else {
496                                         _debug('deleting template ' + tmpl.id());
497                                         var req = new Request(OILS_RPT_DELETE_TEMPLATE, SESSION, tmpl.id());
498                                         req.callback(function(r) {
499                                                 var res = r.getResultObject();
500                                                 if( res == 0 ) return oilsRptAlertFailure();
501                                                 if( res == 1 ) errid = 'oils_rpt_folder_contents_template_no_delete';
502                                                 obj.deleteTemplates(list, ++idx, callback, errid)
503                                         });
504                                         req.send();
505 //                              }
506 //                      }
507 //              );
508 //              req0.send();
509         }
510 }
511
512
513
514 oilsRptFolderWindow.prototype.drawFolderDetails = function() {
515         appendClear(DOM.oils_rpt_folder_creator_label, 
516                 text(this.folderNode.folder.owner().usrname()));
517         appendClear(DOM.oils_rpt_folder_name_label, 
518                 text(this.folderNode.folder.name()));
519 }
520
521
522 oilsRptFolderWindow.prototype.createSearchRequest = function() {
523     var field = getSelectorVal(DOM.template_search_fields_selector);
524     var fields = field ? [field] : ['name', 'description'];
525
526     return new Request(
527         'open-ils.reporter:open-ils.reporter.search.templates.atomic', 
528         SESSION, {
529             limit  : oilsRptOutputLimit,
530             offset : oilsRptOutputOffset,
531             query  : DOM.template_search_query.value,
532             folder : getSelectorVal(DOM.template_search_folder_selector),
533             fields : fields,
534         }
535     );
536 }
537
538 oilsRptFolderWindow.prototype.fetchFolderData = function(callback) {
539
540         hideMe(DOM.oils_rpt_content_count_row_2);
541         hideMe(DOM.oils_rpt_content_row_2);
542
543         removeChildren(this.selector);
544         var req = new Request(OILS_RPT_FETCH_FOLDER_DATA, 
545                 SESSION, this.type, this.folderNode.folder.id(), 
546         oilsRptOutputLimit, oilsRptOutputOffset);
547
548         hideMe(DOM.oils_rpt_pending_output);
549
550         if(this.type == 'output') {
551                 unHideMe(DOM.oils_rpt_pending_output);
552                 /* first fetch the non-complete schedules */
553                 req = new Request(OILS_RPT_FETCH_OUTPUT, 
554                         SESSION, this.folderNode.folder.id(), oilsRptOutputLimit, 0);
555         }
556
557     // Displaying the special template "Search Results" folder means 
558     // starting a new search.
559     var hidableNodes = document.getElementsByClassName('hidden-for-search-results');
560     var showableNodes = document.getElementsByClassName('show-for-search-results');
561     if (this.type == 'template' && 
562         this.folderNode.folder.id() == oilsRptSearchResultFolderId) {
563         req = this.createSearchRequest();
564
565         // when displaying search results, hide anything that should be hidden
566         dojo.forEach(hidableNodes, function(n) { n.style.visibility = 'hidden' });
567         dojo.forEach(showableNodes, function(n) { n.style.visibility = 'visible' });
568         hideMe(DOM.real_folder_name);
569         unHideMe(DOM.search_results_folder_name);
570     } else {
571         dojo.forEach(hidableNodes, function(n) { n.style.visibility = 'visible' });
572         dojo.forEach(showableNodes, function(n) { n.style.visibility = 'hidden' });
573         unHideMe(DOM.real_folder_name);
574         hideMe(DOM.search_results_folder_name);
575     }
576
577         var obj = this;
578         removeChildren(obj.selector);
579         req.callback(
580                 function(r) {
581                         var res = r.getResultObject();
582
583                         if( res.length == 0 ) {
584                                 //hideMe(DOM.oils_rpt_content_count_row); /* this also hides the new-template link.. fix me */
585                                 hideMe(DOM.oils_rpt_content_row);
586                                 unHideMe(DOM.oils_rpt_content_row_empty);
587                         } else {
588                                 unHideMe(DOM.oils_rpt_content_count_row);
589                                 unHideMe(DOM.oils_rpt_content_row);
590                                 hideMe(DOM.oils_rpt_content_row_empty);
591                         }
592
593                         if( obj.type == 'output' ) {
594                                 obj.fleshSchedules(res, 0, obj.selector);
595                         } else {
596
597
598                                 obj.fmTable = drawFMObjectTable( 
599                                         { 
600                                                 dest : obj.selector, 
601                                                 obj : res,
602                                                 selectCol : true,
603                                                 selectColName : rpt_strings.FOLDER_WINDOW_COLNAME_SELECT,
604                                                 selectAllName : rpt_strings.FOLDER_WINDOW_COLNAME_ALL,
605                                                 selectNoneName : rpt_strings.FOLDER_WINDOW_COLNAME_NONE
606                                         }
607                                 );
608                         }
609                 }
610         );
611         req.send();
612
613         if( this.type != 'output' ) return;
614
615         /*
616         unHideMe(DOM.oils_rpt_content_count_row_2);
617         unHideMe(DOM.oils_rpt_content_row_2);
618         */
619
620         /* now fetch the completed schedules */
621         req = new Request(OILS_RPT_FETCH_OUTPUT, 
622                 SESSION, this.folderNode.folder.id(), oilsRptOutputLimit2, 1);
623
624         _debug("TRYING: fleshing finished scheds with div: " + DOM.oils_rpt_folder_contents_selector_2);
625         removeChildren(DOM.oils_rpt_folder_contents_selector_2);
626         req.callback(
627                 function(r) {
628                         var res = r.getResultObject();
629
630                         if( res.length == 0 ) {
631                                 hideMe(DOM.oils_rpt_content_count_row_2);
632                                 hideMe(DOM.oils_rpt_content_row_2);
633                         } else {
634                                 unHideMe(DOM.oils_rpt_content_count_row_2);
635                                 unHideMe(DOM.oils_rpt_content_row_2);
636                         }
637
638                         _debug("fleshing finished scheds with div: " + DOM.oils_rpt_folder_contents_selector_2);
639                         obj.fleshSchedules(res, 0, DOM.oils_rpt_folder_contents_selector_2, true);
640                 }
641         );
642         req.send();
643 }
644
645
646 oilsRptFolderWindow.prototype.fleshSchedules = function(list, idx, selector, isSecond) {
647
648         if( idx >= list.length ) return;
649
650         var sched = list[idx];
651         var obj = this;
652
653         oilsRptFetchUser(sched.runner(),
654
655                 function(user) {
656                         sched.runner(user);
657                         oilsRptFetchTemplate(sched.report().template(),
658
659                                 function(template) {
660                                         sched.report().template(template);
661                                         if( idx == 0 ) {
662                                                 _debug("drawing schedule with output: "+selector);
663                                                 var t = drawFMObjectTable( 
664                                                         { 
665                                                                 dest : selector, 
666                                                                 obj : [sched],
667                                                                 selectCol : true,
668                                                                 selectColName : rpt_strings.FOLDER_WINDOW_COLNAME_SELECT,
669                                                                 selectAllName : rpt_strings.FOLDER_WINDOW_COLNAME_ALL,
670                                                                 selectNoneName : rpt_strings.FOLDER_WINDOW_COLNAME_NONE
671                                                         }
672                                                 );
673
674                                                 if( isSecond ) obj.fmTable2 = t;
675                                                 else obj.fmTable = t;
676
677                                         } else {
678                                                 //obj.fmTable.add(sched);
679                                                 if( isSecond ) obj.fmTable2.add(sched);
680                                                 else obj.fmTable.add(sched);
681                                         }
682
683                                         obj.fleshSchedules(list, ++idx, selector, isSecond);
684                                 }
685                         );
686                 }
687         );
688 }
689
690
691 oilsRptFolderWindow.prototype.setSelected = function(folderNode) {
692         this.selectedFolder = folderNode;
693 }
694
695 oilsRptFolderWindow.prototype.setFolderEditActions = function() {
696         var folder = this.folderNode.folder;
697
698         var obj = this;
699         DOM.oils_rpt_folder_manager_name_input.value = folder.name();
700         DOM.oils_rpt_folder_manager_change_name_submit.onclick = function() {
701                 var name = DOM.oils_rpt_folder_manager_name_input.value;
702                 if(name != "") {
703                         folder.name( name );
704                         if(confirmId('oils_rpt_folder_manager_change_name_confirm')) {
705                                 oilsRptUpdateFolder(folder, obj.type,
706                                         function(success) {
707                                                 if(success) {
708                                                         oilsRptAlertSuccess();
709                                                         oilsRptCurrentFolderManager.draw();
710                                                 }
711                                         }
712                                 );
713                         }
714                 }
715         }
716
717         DOM.oils_rpt_folder_manager_sub_lib_create.onclick = function() {
718                 var folder;
719
720                 if( obj.type == 'report' ) folder = new rrf();
721                 if( obj.type == 'template' ) folder = new rtf();
722                 if( obj.type == 'output' ) folder = new rof();
723
724                 folder.owner(USER.id());
725                 folder.parent(obj.folderNode.folder.id());
726                 folder.name(DOM.oils_rpt_folder_manager_sub_name.value);
727                 var shared = getSelectorVal(DOM.oils_rpt_folder_manager_sub_shared);
728                 folder.shared( (shared == 'yes') ? 't' : 'f');
729                 if( folder.shared() == 't' )
730                         folder.share_with( obj.myOrgSelector.getValue() );
731
732                 oilsRptCreateFolder(folder, obj.type,
733                         function(success) {
734                                 if(success) {
735                                         oilsRptAlertSuccess();
736                                         oilsRptCurrentFolderManager.draw();
737                                 }
738                         }
739                 );
740         }
741 }
742
743
744 oilsRptFolderWindow.prototype.doFolderDelete = function() {
745         
746         var cache = oilsRptFolderNodeCache[this.type];
747         /* let's see if this folder has any children */
748         for( var c in cache ) 
749                 if( cache[c].folder.parent() == this.folderNode.folder.id() )
750                         return alertId('oils_rpt_folder_cannot_delete');
751
752         /* lets see if the folder has contents */
753         var req = new Request(OILS_RPT_FETCH_FOLDER_DATA, 
754                 SESSION, this.type, this.folderNode.folder.id(), 1);
755
756         if(this.type == 'output') {
757                 req = new Request(OILS_RPT_FETCH_OUTPUT, 
758                         SESSION, this.folderNode.folder.id(), 1);
759         }
760
761         var obj = this;
762         req.send();
763
764         req.callback( 
765                 function(r) {
766
767                         var contents = r.getResultObject();
768                         if( contents.length > 0 ) 
769                                 return alertId('oils_rpt_folder_cannot_delete');
770
771                         if( confirmId('oils_rpt_folder_manager_delete_confirm') ) {
772                                 var req2 = new Request(OILS_RPT_DELETE_FOLDER, 
773                                         SESSION, obj.type, obj.folderNode.folder.id());
774         
775                                 req2.callback( 
776                                         function(r2) {
777                                                 var res = r2.getResultObject();
778                                                 if( res == 1 ) {
779                                                         oilsRptAlertSuccess();
780                                                         oilsRptCurrentFolderManager.draw();
781                                                 }
782                                                 else alert('error: '+js2JSON(res));
783                                         }
784                                 );
785
786                                 req2.send();
787                         }
788                 }
789         );
790 }
791
792 function oilsRptViewEditReport(report, readonly) {
793
794
795     var folderWindow = oilsRptFolderWindow.folderIdMap[report.folder()];
796
797     var req = new Request(
798         'open-ils.reporter:open-ils.reporter.report.fleshed.retrieve',
799         SESSION, report.id()
800     );
801
802     req.callback(function(r) {
803
804         hideMe(DOM.oils_rpt_folder_table_right_td);
805         unHideMe(DOM.oils_rpt_folder_table_alt_td);
806         unHideMe(DOM.oils_rpt_editor_div);
807
808         report = r.getResultObject();
809
810         new oilsRptReportEditor(
811             new oilsReport(report.template(), report), folderWindow, readonly);
812     });
813
814     req.send();
815 }
816
817 function oilsRptViewReport(report) {
818     oilsRptViewEditReport(report, true);
819 }
820
821 function oilsRptEditReport(report) {
822     oilsRptViewEditReport(report);
823 }
824
825