1 dojo.requireLocalization("openils.reports", "reports");
3 var rpt_strings = dojo.i18n.getLocalization("openils.reports", "reports");
5 oilsRptSetSubClass('oilsRptReportEditor', 'oilsRptObject');
6 var oilsRptReportEditorFolderTree;
8 function oilsRptReportEditor(rptObject, folderWindow, readonly) {
9 var tmpl = rptObject.templateObject;
10 var rpt = rptObject.reportObject;
11 this.folderWindow = folderWindow;
12 this.readonly = readonly;
17 if (rpt && rpt.runs() && rpt.runs().length)
18 this.last_run = rpt.runs()[rpt.runs().length - 1];
20 appendClear(DOM.oils_rpt_report_editor_template_name, tmpl.name());
21 appendClear(DOM.oils_rpt_report_editor_template_creator, tmpl.owner().usrname());
22 appendClear(DOM.oils_rpt_report_editor_template_description, tmpl.description());
24 hideMe(DOM.oils_rpt_report_editor_template_doc_url_row);
25 if (rptObject.def.version >= 4) {
26 if (URL = rptObject.def.doc_url) {
27 var link = DOM.oils_rpt_report_editor_template_doc_url;
29 if (typeof xulG == 'undefined') {
30 link.setAttribute('href', URL);
31 link.setAttribute('target', '_blank');
33 link.onclick = function() {xulG.new_tab(URL); return false}
35 unHideMe(DOM.oils_rpt_report_editor_template_doc_url_row);
39 appendClear(DOM.oils_rpt_report_editor_cols,'');
40 iterate(rptObject.def.select,
43 DOM.oils_rpt_report_editor_cols.appendChild(text(i.alias));
45 DOM.oils_rpt_report_editor_cols.appendChild(
46 elem('span', {'class':'oils_rpt_field_hint'}, i.field_doc));
48 DOM.oils_rpt_report_editor_cols.appendChild(document.createElement('br'));
53 oils_rpt_editor_pivot_label
54 oils_rpt_editor_pivot_data
58 iterate(rptObject.def.select,
60 if(OILS_RPT_TRANSFORMS[i.column.transform].aggregate)
65 while(DOM.oils_rpt_editor_pivot_label.getElementsByTagName('option').length > 1)
66 DOM.oils_rpt_editor_pivot_label.removeChild(DOM.oils_rpt_editor_pivot_label.lastChild);
68 while(DOM.oils_rpt_editor_pivot_data.lastChild)
69 DOM.oils_rpt_editor_pivot_data.removeChild(DOM.oils_rpt_editor_pivot_data.lastChild);
72 unHideMe(DOM.oils_rpt_editor_pivot_label_row);
73 unHideMe(DOM.oils_rpt_editor_pivot_data_row);
75 for(var i in rptObject.def.select) {
76 var col = rptObject.def.select[i];
77 if(OILS_RPT_TRANSFORMS[col.column.transform].aggregate)
78 insertSelectorVal(DOM.oils_rpt_editor_pivot_data, -1, col.alias, parseInt(i)+1);
80 insertSelectorVal(DOM.oils_rpt_editor_pivot_label, -1, col.alias, parseInt(i)+1);
84 hideMe(DOM.oils_rpt_editor_pivot_label_row);
85 hideMe(DOM.oils_rpt_editor_pivot_data_row);
90 // populate the report edit form w/ report data
92 this.orig_rpt_name = rpt.name();
94 DOM.oils_rpt_report_editor_name.value = rpt.name();
95 DOM.oils_rpt_report_editor_name.onchange(); // validation
96 DOM.oils_rpt_report_editor_desc.value = rpt.description();
98 if (rpt.recur() == 't') {
99 DOM.oils_rpt_recur.checked = true;
100 DOM.oils_rpt_recur.onclick(); // enable recurrance selector
103 if (rpt.recurrence()) {
104 var parts = rpt.recurrence().split(/ /);
105 setSelector(DOM.oils_rpt_recur_count, parts[0]);
106 setSelector(DOM.oils_rpt_recur_interval_type, parts[1]);
109 if (run = this.last_run) {
110 DOM.oils_rpt_report_editor_name.disabled = true;
111 DOM.oils_rpt_report_editor_desc.disabled = true;
112 DOM.oils_rpt_format_csv.checked = run.csv_format() == 't';
113 DOM.oils_rpt_format_excel.checked = run.excel_format() == 't';
114 DOM.oils_rpt_format_html.checked = run.html_format() == 't';
115 DOM.oils_rpt_format_chart_bar.checked = run.chart_bar() == 't';
116 DOM.oils_rpt_format_chart_line.checked = run.chart_line() == 't';
121 DOM.oils_rpt_report_editor_name.disabled = true;
122 DOM.oils_rpt_report_editor_desc.disabled = true;
123 DOM.oils_rpt_recur.disabled = true;
124 DOM.oils_rpt_recur_count.disabled = true;
125 DOM.oils_rpt_recur_interval_type.disabled = true;
126 DOM.oils_rpt_report_editor_run_now.disabled = true;
127 DOM.oils_rpt_format_csv.disabled = true;
128 DOM.oils_rpt_format_excel.disabled = true;
129 DOM.oils_rpt_format_html.disabled = true;
130 DOM.oils_rpt_format_chart_bar.disabled = true;
131 DOM.oils_rpt_format_chart_line.disabled = true;
132 DOM.oils_rpt_param_editor_sched_email.disabled = true;
134 hideMe(DOM.oils_rpt_report_editor_save);
135 hideMe(DOM.oils_rpt_report_editor_save_new);
136 hideMe(DOM.oils_rpt_report_editor_cancel);
137 unHideMe(DOM.oils_rpt_report_editor_exit);
140 // these DOM elements are shared across instances
141 // of the UI. Re-enable everything.
142 DOM.oils_rpt_report_editor_name.disabled = false;
143 DOM.oils_rpt_report_editor_desc.disabled = false;
144 DOM.oils_rpt_recur.disabled = false;
145 DOM.oils_rpt_recur_count.disabled = false;
146 DOM.oils_rpt_recur_interval_type.disabled = false;
147 DOM.oils_rpt_report_editor_run_now.disabled = false;
148 DOM.oils_rpt_format_csv.disabled = false;
149 DOM.oils_rpt_format_excel.disabled = false;
150 DOM.oils_rpt_format_html.disabled = false;
151 DOM.oils_rpt_format_chart_bar.disabled = false;
152 DOM.oils_rpt_format_chart_line.disabled = false;
153 DOM.oils_rpt_param_editor_sched_email.disabled = false;
154 DOM.oils_rpt_report_editor_save.disabled = false;
156 unHideMe(DOM.oils_rpt_report_editor_save);
157 unHideMe(DOM.oils_rpt_report_editor_save_new);
158 unHideMe(DOM.oils_rpt_report_editor_cancel);
159 hideMe(DOM.oils_rpt_report_editor_exit);
162 this.paramEditor = new oilsRptParamEditor(
163 rptObject, DOM.oils_rpt_param_editor_tbody, this.readonly);
164 this.paramEditor.draw();
166 removeChildren(DOM.oils_rpt_report_editor_selected_folder);
167 removeChildren(DOM.oils_rpt_output_selected_folder);
172 DOM.oils_rpt_report_editor_dest_folder,
173 'oilsRptReportEditorFolderTree',
174 rpt_strings.REPORT_EDITOR_REPORT_FOLDERS,
176 appendClear(DOM.oils_rpt_report_editor_selected_folder, node.folder.name());
177 obj.selectedFolder = node;
181 // apply the previously selected report folder
182 if (rpt && rpt.folder() == node.folder.id()) {
183 appendClear(DOM.oils_rpt_report_editor_selected_folder, node.folder.name());
184 obj.selectedFolder = node;
191 DOM.oils_rpt_output_dest_folder,
192 'oilsRptReportEditorOutputTree',
193 rpt_strings.REPORT_EDITOR_OUTPUT_FOLDERS,
195 appendClear(DOM.oils_rpt_output_selected_folder, node.folder.name());
196 obj.selectedOutputFolder = node;
200 // apply the previously selected output folder
201 if (obj.last_run && obj.last_run.folder() == node.folder.id()) {
202 appendClear(DOM.oils_rpt_output_selected_folder, node.folder.name());
203 obj.selectedOutputFolder = node;
210 DOM.oils_rpt_report_editor_save.onclick = function(){obj.save();}
211 DOM.oils_rpt_report_editor_save_new.onclick = function(){obj.save({save_new : true});}
212 DOM.oils_rpt_report_editor_exit.onclick = function(){obj.exit();}
213 DOM.oils_rpt_report_editor_cancel.onclick = function(){obj.exit();}
215 DOM.oils_rpt_param_editor_sched_email.value =
216 this.last_run ? this.last_run.email() : USER.email();
218 DOM.oils_rpt_param_editor_sched_start_date.value = mkYearMonDay();
220 _debug("fleshing template:\n" + tmpl.name() + '\n' + formatJSON(tmpl.data()));
224 // options.save_new : save as a new report, even if we
225 // were editing an exitingn report.
227 // options.modify_schedule : update the pending schedule
228 // object instead of creating a new one.
229 oilsRptReportEditor.prototype.save = function(options) {
230 if (!options) options = {};
232 if(!this.selectedFolder)
233 return alert(rpt_strings.REPORT_EDITOR_PROVIDE_FOLDER_ALERT);
235 if(!DOM.oils_rpt_report_editor_name.value)
236 return alert(rpt_strings.REPORT_EDITOR_ENTER_NAME_ALERT);
238 if(!this.selectedOutputFolder)
239 return alert(rpt_strings.REPORT_EDITOR_PROVIDE_OUTPUT_ALERT);
241 var report = this.report;
243 if (report && options.save_new) {
244 // user is saving an existing report as a new report.
245 // The new report must have a different name.
246 if (DOM.oils_rpt_report_editor_name.value == this.orig_rpt_name)
247 return alert(rpt_strings.REPORT_EDITOR_ENTER_NEW_NAME_ALERT);
255 report.owner( USER.id() );
256 report.template( this.template.id() );
259 report.folder( this.selectedFolder.folder.id() );
260 report.name( DOM.oils_rpt_report_editor_name.value );
261 report.description( DOM.oils_rpt_report_editor_desc.value );
262 report.recur(this.paramEditor.recur());
263 report.recurrence(this.paramEditor.recurInterval());
265 /* collect the param data */
267 for( var p in this.paramEditor.params ) {
268 var par = this.paramEditor.params[p];
269 _debug("adding report param "+par.key+" to report data");
270 var val = par.widget.getValue();
272 if(!val || val.length == 0 )
273 return alertId('oils_rpt_empty_param');
275 if( typeof val == 'object') {
276 for( var i = i; i < val.length; i++ ) {
277 _debug("looking at widget value" + val[i]);
278 if( val[i] == '' || val[i] == null )
279 return alertId('oils_rpt_empty_param');
286 if(getSelectorVal(DOM.oils_rpt_editor_pivot_data)) {
287 data.__pivot_label = getSelectorVal(DOM.oils_rpt_editor_pivot_label);
288 data.__pivot_data = getSelectorVal(DOM.oils_rpt_editor_pivot_data);
292 data = js2JSON(data);
293 _debug("complete report data = "+data);
296 _debug("Built report:\n"+js2JSON(report));
300 if( DOM.oils_rpt_report_editor_run_now.checked ) {
301 DOM.oils_rpt_report_editor_run_now.checked = false;
306 var dt = DOM.oils_rpt_param_editor_sched_start_date.value;
307 if(!dt || !dt.match(/^\d{4}-\d{2}-\d{2}$/) ) {
308 /* for now.. make this better in the future */
309 alert(rpt_strings.REPORT_EDITOR_INVALID_DATE_ALERT);
312 var hour = getSelectorVal(DOM.oils_rpt_param_editor_sched_start_hour);
313 time = dt +'T'+hour+':00';
314 _debug("built run_time "+time);
317 // if the last run has yet to actually start, then we update it
318 // instead of creating a new one.
319 var schedule = options.save_new ? null : this.last_run;
321 if (schedule && !schedule.start_time()) {
322 if (!options.modify_schedule) {
323 // warn the user that this action will modify an existing
324 // schedule object if they continue
325 return this.showPendingScheduleDialog();
328 // no schedules exist or the most recent one has already
329 // started. Create a new one.
331 schedule.isnew(true);
334 schedule.folder(this.selectedOutputFolder.folder.id());
335 schedule.email(DOM.oils_rpt_param_editor_sched_email.value);
336 schedule.run_time(time);
337 schedule.runner(USER.id());
339 schedule.excel_format((DOM.oils_rpt_format_excel.checked) ? 't' : 'f');
340 schedule.html_format((DOM.oils_rpt_format_html.checked) ? 't' : 'f');
341 schedule.csv_format((DOM.oils_rpt_format_csv.checked) ? 't' : 'f');
342 //schedule.chart_pie((DOM.oils_rpt_format_chart_pie.checked) ? 't' : 'f');
343 schedule.chart_bar((DOM.oils_rpt_format_chart_bar.checked) ? 't' : 'f');
344 schedule.chart_line((DOM.oils_rpt_format_chart_line.checked) ? 't' : 'f');
346 debugFMObject(report);
347 debugFMObject(schedule);
349 if (report.isnew()) {
350 this.createReport(report, schedule);
352 this.updateReport(report, schedule);
356 // Modify an existing report.
357 // Modify or create the schedule depending on isnew()
358 oilsRptReportEditor.prototype.updateReport = function(report, schedule) {
362 oilsRptAlertSuccess();
366 oilsRptUpdateReport(report, function(ok) {
367 if (!ok) return oilsRptAlertFailure();
369 if (schedule.isnew()) {
371 var req = new Request(OILS_RPT_CREATE_SCHEDULE, SESSION, schedule);
372 req.callback(function(res) {
373 if(checkILSEvent(res))
374 return alertILSEvent(res);
381 oilsRptUpdateSchedule(schedule, function(ok2) {
382 if (ok2) return success();
383 _debug("schedule update failed " + js2JSON(schedule));
384 oilsRptAlertFailure();
390 oilsRptReportEditor.prototype.createReport = function(report, schedule) {
392 var folderReq = new Request(OILS_RPT_REPORT_EXISTS, SESSION, report);
395 if(r1.getResultObject() == 1) {
396 alertId('oils_rpt_report_exists');
399 var req = new Request(OILS_RPT_CREATE_REPORT, SESSION, report, schedule );
402 var res = r.getResultObject();
403 if(checkILSEvent(res)) {
406 if( res && res != '0' ) {
407 oilsRptAlertSuccess();
420 oilsRptReportEditor.prototype.showPendingScheduleDialog = function() {
421 hideMe(DOM.oils_rpt_editor_table);
422 unHideMe(DOM.oils_rpt_editor_sched_confirm);
425 unHideMe(DOM.oils_rpt_editor_table);
426 hideMe(DOM.oils_rpt_editor_sched_confirm);
430 DOM.oils_rpt_report_editor_sched_apply.onclick = function() {
432 this_.save({modify_schedule : true});
435 DOM.oils_rpt_report_editor_sched_asnew.onclick = function() {
437 if (DOM.oils_rpt_report_editor_name.value == this_.orig_rpt_name) {
438 // user is saving as new but has not yet modified the name
439 // Prompt for a new name, then udpate the name entry so save()
440 // will see it. Don't let them escape until they comply.
445 rpt_strings.REPORT_EDITOR_ENTER_NEW_NAME_ALERT,
449 if (new_name && new_name != this_.orig_rpt_name)
453 DOM.oils_rpt_report_editor_name.value = new_name;
457 this_.save({save_new : true})
459 DOM.oils_rpt_report_editor_sched_cancel.onclick = close;
463 oilsRptReportEditor.prototype.exit = function() {
464 oilsRptCurrentFolderManager.draw();
465 this.folderWindow.draw();