need to clear out the colums so they don't accumulate
[Evergreen.git] / Open-ILS / web / reports / oils_rpt_report_editor.js
1 oilsRptSetSubClass('oilsRptReportEditor', 'oilsRptObject');
2 var oilsRptReportEditorFolderTree;
3
4 function oilsRptReportEditor(rptObject, folderWindow) {
5         var tmpl = rptObject.templateObject;
6         var rpt = rptObject.reportObject;
7         this.folderWindow = folderWindow;
8
9         this.template = tmpl;
10         this.report = rpt;
11
12         appendClear(DOM.oils_rpt_report_editor_template_name, tmpl.name());
13         appendClear(DOM.oils_rpt_report_editor_template_creator, tmpl.owner().usrname());
14         appendClear(DOM.oils_rpt_report_editor_template_description, tmpl.description());
15     
16     appendClear(DOM.oils_rpt_report_editor_cols,'');
17         iterate(rptObject.def.select, 
18                 function(i) {
19                         if(i)
20                                 DOM.oils_rpt_report_editor_cols.appendChild(text(i.alias));
21                                 DOM.oils_rpt_report_editor_cols.appendChild(document.createElement('br'));
22                 }
23         );
24
25 /*
26 oils_rpt_editor_pivot_label
27 oils_rpt_editor_pivot_data
28 */
29
30     var hasAgg = false;
31     iterate(rptObject.def.select, 
32         function(i) {
33             if(OILS_RPT_TRANSFORMS[i.column.transform].aggregate) 
34                 hasAgg = true; 
35         }
36     );
37
38     while(DOM.oils_rpt_editor_pivot_label.getElementsByTagName('option').length > 1)
39         DOM.oils_rpt_editor_pivot_label.removeChild(DOM.oils_rpt_editor_pivot_label.lastChild);
40
41     while(DOM.oils_rpt_editor_pivot_data.lastChild)
42         DOM.oils_rpt_editor_pivot_data.removeChild(DOM.oils_rpt_editor_pivot_data.lastChild);
43
44     if(hasAgg) {
45         unHideMe(DOM.oils_rpt_editor_pivot_label_row);
46         unHideMe(DOM.oils_rpt_editor_pivot_data_row);
47
48         for(var i in rptObject.def.select) {
49             var col = rptObject.def.select[i];
50             if(OILS_RPT_TRANSFORMS[col.column.transform].aggregate) 
51                insertSelectorVal(DOM.oils_rpt_editor_pivot_data, -1, col.alias, parseInt(i)+1);
52             else
53                insertSelectorVal(DOM.oils_rpt_editor_pivot_label, -1, col.alias, parseInt(i)+1);
54         }
55
56     } else {
57         hideMe(DOM.oils_rpt_editor_pivot_label_row);
58         hideMe(DOM.oils_rpt_editor_pivot_data_row);
59     }
60  
61
62         if( rpt ) {
63                 DOM.oils_rpt_report_editor_name.value = rpt.name();
64                 DOM.oils_rpt_report_editor_description.value = rpt.description();
65         }
66
67         this.paramEditor = new oilsRptParamEditor(
68                 rptObject, DOM.oils_rpt_param_editor_tbody);
69         this.paramEditor.draw();
70
71         removeChildren(DOM.oils_rpt_report_editor_selected_folder);
72         removeChildren(DOM.oils_rpt_output_selected_folder);
73
74         var obj = this;
75         oilsRptBuildFolder(
76                 'report',
77                 DOM.oils_rpt_report_editor_dest_folder,
78                 'oilsRptReportEditorFolderTree',
79                 'Report Folders',
80                 function(node) { 
81                         appendClear(DOM.oils_rpt_report_editor_selected_folder, node.folder.name());
82                         obj.selectedFolder = node; });
83
84
85         oilsRptBuildFolder(
86                 'output',
87                 DOM.oils_rpt_output_dest_folder,
88                 'oilsRptReportEditorOutputTree',
89                 'Output Folders',
90                 function(node) { 
91                         appendClear(DOM.oils_rpt_output_selected_folder, node.folder.name());
92                         obj.selectedOutputFolder = node; });
93
94
95         var obj = this;
96         DOM.oils_rpt_report_editor_save.onclick = function(){obj.save();}
97         DOM.oils_rpt_param_editor_sched_email.value = USER.email();
98         DOM.oils_rpt_param_editor_sched_start_date.value = mkYearMonDay();
99
100         _debug("fleshing template:\n" + tmpl.name() + '\n' + formatJSON(tmpl.data()));
101 }
102
103
104 oilsRptReportEditor.prototype.save = function() {
105         var report = new rr();
106
107         if(!this.selectedFolder) 
108                 return alert('Please provide a report folder');
109
110         if(!DOM.oils_rpt_report_editor_name.value)
111                 return alert('Please enter a report name');
112
113         report.owner( USER.id() );
114         report.template( this.template.id() );
115         report.folder( this.selectedFolder.folder.id() );
116         report.name( DOM.oils_rpt_report_editor_name.value );
117         report.description( DOM.oils_rpt_report_editor_desc.value );
118         report.recur(this.paramEditor.recur());
119         report.recurance(this.paramEditor.recurInterval());
120
121
122         /* collect the param data */
123         var data = {};
124         for( var p in this.paramEditor.params ) {
125                 var par = this.paramEditor.params[p];
126                 _debug("adding report param "+par.key+" to report data");
127                 var val = par.widget.getValue();
128
129                 if(!val || val.length == 0 )
130                         return alertId('oils_rpt_empty_param');
131
132                 if( typeof val == 'object') {
133                         for( var i = i; i < val.length; i++ ) {
134                                 _debug("looking at widget value" + val[i]);
135                                 if( val[i] == '' || val[i] == null ) 
136                                         return alertId('oils_rpt_empty_param');
137                         }
138                 }
139
140                 data[par.key] = val;
141         }
142
143     if(getSelectorVal(DOM.oils_rpt_editor_pivot_data)) {
144         data.__pivot_label = getSelectorVal(DOM.oils_rpt_editor_pivot_label);
145         data.__pivot_data = getSelectorVal(DOM.oils_rpt_editor_pivot_data);
146     }
147
148
149         data = js2JSON(data);
150         _debug("complete report data = "+data);
151         report.data(data);
152
153         _debug("Built report:\n"+js2JSON(report));
154
155
156         var time;
157         if( DOM.oils_rpt_report_editor_run_now.checked ) {
158                 DOM.oils_rpt_report_editor_run_now.checked = false;
159                 time = 'now';
160
161         } else {
162
163                 var dt = DOM.oils_rpt_param_editor_sched_start_date.value;
164                 if(!dt || !dt.match(/^\d{4}-\d{2}-\d{2}$/) ) {
165                         /* for now.. make this better in the future */
166                         alert('invalid start date -  YYYY-MM-DD');
167                         return;
168                 }
169                 var hour = getSelectorVal(DOM.oils_rpt_param_editor_sched_start_hour);
170                 time = dt +'T'+hour+':00';
171                 _debug("built run_time "+time);
172         }
173
174         if(!this.selectedOutputFolder) 
175                 return alert('Please provide an output folder');
176
177         var schedule = new rs();
178         schedule.folder(this.selectedOutputFolder.folder.id());
179         schedule.email(DOM.oils_rpt_param_editor_sched_email.value);
180         schedule.run_time(time);
181         schedule.runner(USER.id());
182
183         schedule.excel_format((DOM.oils_rpt_format_excel.checked) ? 't' : 'f');
184         schedule.html_format((DOM.oils_rpt_format_html.checked) ? 't' : 'f');
185         schedule.csv_format((DOM.oils_rpt_format_csv.checked) ? 't' : 'f');
186         //schedule.chart_pie((DOM.oils_rpt_format_chart_pie.checked) ? 't' : 'f');
187         schedule.chart_bar((DOM.oils_rpt_format_chart_bar.checked) ? 't' : 'f');
188         schedule.chart_line((DOM.oils_rpt_format_chart_line.checked) ? 't' : 'f');
189
190
191         debugFMObject(report);
192         debugFMObject(schedule);
193
194         //return;
195
196         var obj = this;
197     var folderReq = new Request(OILS_RPT_REPORT_EXISTS, SESSION, report);
198     folderReq.callback(
199         function(r1) {
200             if(r1.getResultObject() == 1) {
201                 alertId('oils_rpt_report_exists');
202                 return;
203             } else {
204                 var req = new Request(OILS_RPT_CREATE_REPORT, SESSION, report, schedule );
205                 req.callback(
206                     function(r) {
207                         var res = r.getResultObject();
208                         if(checkILSEvent(res)) {
209                             alertILSEvent(res);
210                         } else {
211                             if( res && res != '0' ) {
212                                 oilsRptAlertSuccess();
213                                 oilsRptCurrentFolderManager.draw();
214                                 obj.folderWindow.draw();
215                             }
216                         }
217                     }
218                 );
219                 req.send();
220             }
221         }
222     );
223     folderReq.send();
224 }
225
226