Merge branch 'maint/7.0'
[ninja.git] / application / views / schedule / js / schedule.js
blob8926c050ff98e815d07e6b9f2c14a6dd891a7f37
1 function remove_scheduled_str(in_str)
3         in_str = in_str.replace(/\*/g, '');
4         in_str = in_str.replace(" ( " + _scheduled_label + " )", '');
5         return in_str;
8 function create_filename()
10         if (!$('#saved_report_id option:selected').val()) {
11                 $('input[name=filename]').val('');
12                 return false;
13         }
14         var new_filename = $('#saved_report_id option:selected').text();
15         new_filename = remove_scheduled_str(new_filename);
16         new_filename += '_' + $('#period option:selected').text() + '.pdf';
17         new_filename = new_filename.replace(/ /g, '_');
18         $('input[name=filename]').val(new_filename);
19         return true;
22 function fill_saved_reports() {
23         var report_type = this.value;
24         $.ajax(
25                 _site_domain + _index_page + "/schedule/list_by_type/"+report_type,
26                 {
27                         error: function(xhr) {
28                                 alert(xhr.responseText);
29                         },
30                         success: function(response) {
31                                 var saved_reports = document.getElementById("saved_report_id");
32                                 var default_opt = saved_reports.children[0];
33                                 saved_reports.length = 1;
34                                 var options = document.createDocumentFragment();
35                                 for(var i = 0; i < response.length; i++) {
36                                         var option = document.createElement("option");
37                                         var result = response[i];
38                                         option.appendChild(document.createTextNode(result.report_name));
39                                         option.setAttribute("value", result.id);
40                                         options.appendChild(option);
41                                 }
42                                 saved_reports.appendChild(options);
43                                 create_filename();
44                         },
45                         dataType: 'json'
46                 }
47         );
50 $(document).ready(function() {
51         $("#saved_report_id, #period").change(create_filename);
52         fill_scheduled();
53         setup_editable();
55                 // delete single schedule
56         $('body').on('click', '.delete_schedule', schedule_delete);
57         $('body').on('click', '.send_report_now', send_report_now);
59         $("#type").change(fill_saved_reports).each(fill_saved_reports);
61         $('#new_schedule_report_form').submit(function(ev) {
62                 ev.preventDefault();
64                 var rep_type_str = $('#type option:selected').val();
66                 var recipients = $.trim($('#recipients').fieldValue()[0]);
67                 if (recipients.indexOf('@') === -1) {
68                         alert(_reports_invalid_email);
69                         return false;
70                 }
72                 if(!validate_form()) {
73                         return false;
74                 }
75                 show_progress('progress', _wait_str);
76                 $.ajax({
77                         url: _site_domain + _index_page + '/schedule/schedule',
78                         type: 'POST',
79                         data: {
80                                 report_id: 0,
81                                 type: $('#type').fieldValue()[0],
82                                 saved_report_id: $('#saved_report_id').fieldValue()[0],
83                                 period: $('#period').fieldValue()[0],
84                                 recipients: recipients,
85                                 filename: $('#filename').fieldValue()[0],
86                                 description: $('#description').fieldValue()[0],
87                                 attach_description: $('#attach_description').fieldValue()[0],
88                                 local_persistent_filepath: $.trim($('#local_persistent_filepath').val())
89                         },
90                         complete: function() {
91                                 $('#progress').hide();
92                                 // make sure we hide message about no schedules and show table headers
93                                 $('#' + rep_type_str + '_no_result').hide();
94                                 $('#' + rep_type_str + '_headers').show();
95                         },
96                         error: function(data) {
97                                 jgrowl_message(data.responseText, _reports_error);
98                         },
99                         success: function(data) {
100                                 var rep_type = $('#type').attr('value');
101                                 var saved_report_id = $('#saved_report_id').attr('value');
102                                 var report_name = $('#saved_report_id option:selected').text();
103                                 var period_str = $('#period option:selected').text();
104                                 var recipients = $('#recipients').attr('value');
105                                 var filename = $('#filename').attr('value');
106                                 var local_persistent_filepath = $('#local_persistent_filepath').attr('value');
107                                 var description = $('#description').attr('value');
108                                 var attach_description = $('#attach_description').attr('value');
109                                 create_new_schedule_rows(data.id, rep_type, report_name, saved_report_id, period_str, recipients, filename, local_persistent_filepath, description, attach_description)
110                                 setup_editable();
111                                 $('#new_schedule_report_form').clearForm();
113                                 jgrowl_message(_reports_schedule_create_ok, _reports_success);
114                         },
115                         dataType: 'json'
116                 });
117         });
120 function schedule_delete(ev)
122         ev.preventDefault();
123         if (!confirm(_reports_confirm_delete_schedule)) {
124                 return false;
125         }
127         var elem = $(this);
128         var type = elem.data('type');
129         var schedule_id = elem.data('schedule');
130         var report_id = elem.data('report_id');
132         var img = $('img', elem);
133         var img_src = img.attr('src');
134         img.attr('src', loadimg_sml.src);
136         $.ajax({
137                 url:_site_domain + _index_page + '/schedule/delete_schedule',
138                 data: {'id': schedule_id},
139                 complete: function() {
140                         img.attr('src', img_src);
141                 },
142                 success: function(data) {
143                         jgrowl_message(data, _reports_success);
144                         var table = $('#'+type+'_scheduled_reports_table tbody');
145                         $('tr#report-'+schedule_id, table).detach();
146                         if (!$(':visible', table).length)
147                                 $('.no-result', table).show();
148                 },
149                 error: function(data) {
150                         jgrowl_message(data, _reports_error);
151                 },
152                 type: 'POST',
153                 dataType: 'json'
154         });
157 function send_report_now(ev)
159         ev.preventDefault();
160         var elem = $(this);
161         var type = elem.data('type');
162         var sched_id = elem.data('schedule');
163         var report_id = elem.data('report_id');
164         var img = $('img', elem);
165         var img_src = img.attr('src');
166         img.attr('src', loadimg_sml.src);
168         $.ajax({
169                 url: _site_domain + _index_page + '/schedule/send_now/' + sched_id,
170                 type: 'POST',
171                 complete: function() {
172                         img.attr('src', img_src);
173                 },
174                 success: function(data) {
175                         jgrowl_message(data, _reports_success);
176                 },
177                 error: function(data) {
178                         if(data.responseText) {
179                                 jgrowl_message(_reports_schedule_send_error + ': ' + data.responseText, _reports_error);
180                         } else {
181                                 jgrowl_message(_reports_schedule_send_error, _reports_error);
182                         }
183                         img.attr('src', img_src);
184                 },
185                 dataType: 'json'
186         });
189 function setup_editable()
191         var save_url = _site_domain + _index_page + "/schedule/save_schedule_item/";
192         $(".iseditable").editable(save_url, {
193                 id   : 'elementid',
194                 name : 'newvalue',
195                 type : 'text',
196                 event : 'dblclick',
197                 width : 'auto',
198                 height : '14px',
199                 submit : _ok_str,
200                 cancel : _cancel_str,
201                 placeholder:_reports_edit_information
202         });
203         $(".period_select").editable(save_url, {
204                 data : function(value) {
205                         var intervals = [];
206                         $('#period option').map(function() {
207                                 intervals.push("'"+$(this).val()+"': '"+$(this).text()+"' ");
208                         });
209                         intervals = "{"+intervals.join(",")+"}";
210                         return intervals;
211                 },
212                 id   : 'elementid',
213                 name : 'newvalue',
214                 event : 'dblclick',
215                 type : 'select',
216                 submit : _ok_str,
217                 cancel : _cancel_str
218         });
219         $(".iseditable_txtarea").editable(save_url, {
220                 indicator : "<img src='" + _site_domain + "application/media/images/loading.gif'>",
221                 id   : 'elementid',
222                 name : 'newvalue',
223                 type : 'textarea',
224                 event : 'dblclick',
225                 rows: '3',
226                 submit : _ok_str,
227                 cancel : _cancel_str,
228                 cssclass: "txtarea",
229                 placeholder:_reports_edit_information
230         });
231         $(".report_name").editable(save_url, {
232                 data : function (){
233                         switch (_report_types_json[this.id.split('-')[0].split('.')[0]]) {
234                                 case 'avail':
235                                         return _saved_avail_reports;
236                                 case 'sla':
237                                         return _saved_sla_reports;
238                                 case 'summary':
239                                         return _saved_summary_reports;
240                         }
241                         return false;
242                 },
243                 id   : 'elementid',
244                 name : 'newvalue',
245                 event : 'dblclick',
246                 type : 'select',
247                 submit : 'OK',
248                 cancel : 'cancel'
249         });
250         $(".attach_description").editable(save_url, {
251                 data : {0: "No", 1: "Yes"},
252                 id   : 'elementid',
253                 name : 'newvalue',
254                 event : 'dblclick',
255                 type : 'select',
256                 submit : _ok_str,
257                 cancel : _cancel_str
258         });
261 function create_new_schedule_rows(schedule_id, rep_type, report_name, report_id, report_period, recipients, filename, local_persistent_filepath, description, attach_description)
263         var template_row = $('#schedule_template tr');
265         var report_type_id = -1;
266         for (var i in _report_types_json) {
267                 if (_report_types_json[i] == rep_type) {
268                         report_type_id = i;
269                 }
270         }
272         template_row = template_row.clone()
273         $('#' + rep_type + '_scheduled_reports_table .no-result').hide();
274         template_row.attr('id', 'report-'+schedule_id);
275         $('.report_name', template_row)
276                 .attr('id', '' + report_type_id + '.report_id-'+schedule_id)
277                 .text(report_name.replace(/ \( \*Scheduled\* \)$/, ""));
278         $('.description', template_row)
279                 .attr('id', 'description-'+schedule_id)
280                 .text(description);
281         $('.period_select', template_row)
282                 .attr('id', 'period_id-'+schedule_id)
283                 .text(report_period);
284         $('.recipients', template_row)
285                 .attr('id', 'recipients-'+schedule_id)
286                 .text(recipients);
287         $('.filename', template_row)
288                 .attr('id', 'filename-'+schedule_id)
289                 .text(filename);
290         $('.local-path', template_row)
291                 .attr('id', 'local_persistent_filepath-'+schedule_id)
292                 .text(local_persistent_filepath);
293         $('.attach_description', template_row)
294                 .attr('id', 'attach_description-'+schedule_id)
295                 .text(parseInt(attach_description) ? 'Yes' : 'No');
296         var actions = $('.action', template_row);
297         $('.direct_link', actions).attr('href', _site_domain + _index_page + '/' + rep_type + '/generate?report_id=' + report_id);
298         $('.send_report_now, .delete_schedule', actions).data('schedule', schedule_id).data('report_id', report_id).data('type', rep_type);
299         var par = $('#' + rep_type + '_scheduled_reports_table tbody');
300         if (par.children().last().hasClass('odd'))
301                 template_row.attr('class', 'even');
302         else
303                 template_row.attr('class', 'odd');
304         par.append(template_row);
307 function fill_scheduled() {
308         for (var type in _scheduled_reports) {
309                 if (!_scheduled_reports[type].length) {
310                         $('#' + type + '_scheduled_reports_table .no-result').show();
311                         continue;
312                 }
313                 for (var i = 0; i < _scheduled_reports[type].length; i++) {
314                         var report = _scheduled_reports[type][i];
315                         create_new_schedule_rows(report.id, type, report.reportname, report.report_id, report.periodname, report.recipients, report.filename, report.local_persistent_filepath, report.description, report.attach_description);
316                 }
317         }
321 *       Make sure all values are properly entered
323 function validate_form(formData, jqForm, options) {
324         var interval = $('#period').val();
325         var recipients = $('input[name=recipients]').attr('value');
326         var filename = $('input[name=filename]').attr('value');
327         var description = $('input[name=description]').attr('value');
328         var saved_report_id = $('input[name=saved_report_id]').attr('value');
329         if (!saved_report_id) {
330                 saved_report_id = $('#saved_report_id').attr('value');
331         }
332         var report_id = $('input[name=report_id]').attr('value');
333         if (report_id == '' || report_id == undefined) {
334                 report_id = $('#report_id').val();
335         }
336         var fatal_err_str = _reports_fatal_err_str;// + "<br />";
337         $('.schedule_error').hide();
339         var err_str = "";
340         var errors = 0;
341         if (interval == '' || !interval) {
342                 err_str += _reports_schedule_interval_error + "<br />";
343                 errors++;
344         }
346         recipients = recipients.replace(/;/g, ',');
347         // @@@FIXME: split multiple addresses on ',' and check each one using regexp
348         if ($.trim(recipients) == '') {
349                 err_str += _reports_schedule_recipient_error + "<br />";
350                 errors++;
351         }
352         if (!saved_report_id) {
353                 alert(fatal_err_str);
354                 return false;
355         }
357         if (errors) {
358                 /*
359                 $('#response').attr("style", "");
360                 $('#response').html("<ul class=\"error\">" + err_str + "</ul>").show();
361                 */
362                 var str = _reports_errors_found + ':<br />' + err_str + '<br />' + _reports_please_correct + '<br />';
363                 $("#new_schedule_area").prepend("<div id=\"response\" class=\"schedule_err_display\"><ul class=\"error\">" + str + "</ul></div>");
364                 window.scrollTo(0,0); // make sure user sees the error message
365                 return false;
366         }
367         $('.schedule_err_display').remove();
368     return true;