reports: Don't validate report names in javascript
[ninja.git] / modules / reports / controllers / summary.php
blob87482be22a4db64c489306a0792bad530eaa40c9
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Alert Summary controller
4 * Requires authentication
6 * op5, and the op5 logo are trademarks, servicemarks, registered servicemarks
7 * or registered trademarks of op5 AB.
8 * All other trademarks, servicemarks, registered trademarks, and registered
9 * servicemarks mentioned herein may be the property of their respective owner(s).
10 * The information contained herein is provided AS IS with NO WARRANTY OF ANY
11 * KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY, AND FITNESS FOR A
12 * PARTICULAR PURPOSE.
14 class Summary_Controller extends Base_reports_Controller
16 public $type = 'summary';
18 private $host_state_names = array();
19 private $service_state_names = array();
21 public function __construct()
23 parent::__construct();
24 $this->host_state_names = array(
25 Reports_Model::HOST_UP => _('UP'),
26 Reports_Model::HOST_DOWN => _('DOWN'),
27 Reports_Model::HOST_UNREACHABLE => _('UNREACHABLE')
29 $this->service_state_names = array(
30 Reports_Model::SERVICE_OK => _('OK'),
31 Reports_Model::SERVICE_WARNING => _('WARNING'),
32 Reports_Model::SERVICE_CRITICAL => _('CRITICAL'),
33 Reports_Model::SERVICE_UNKNOWN => _('UNKNOWN')
37 /**
38 * Setup options for alert summary report
40 public function index($input=false)
42 $this->setup_options_obj($input);
43 $reports_model = new Summary_Reports_Model($this->options);
45 # check if we have all required parts installed
46 if (!$reports_model->_self_check()) {
47 return url::redirect('reports/invalid_setup');
50 # what scheduled reports are there?
51 $scheduled_ids = array();
52 $scheduled_periods = null;
53 $scheduled_res = Scheduled_reports_Model::get_scheduled_reports($this->type);
54 if ($scheduled_res && count($scheduled_res)!=0) {
55 foreach ($scheduled_res as $sched_row) {
56 $scheduled_ids[] = $sched_row->report_id;
57 $scheduled_periods[$sched_row->report_id] = $sched_row->periodname;
61 $this->template->disable_refresh = true;
62 $this->template->content = $this->add_view('summary/setup');
63 $this->template->content->report_options = $this->add_view('summary/options');
64 $template = $this->template->content;
66 if(isset($_SESSION['report_err_msg'])) {
67 $template->error_msg = $_SESSION['report_err_msg'];
68 unset($_SESSION['report_err_msg']);
71 $this->template->js_header = $this->add_view('js_header');
72 $this->xtra_js[] = 'application/media/js/jquery.datePicker.js';
73 $this->xtra_js[] = 'application/media/js/jquery.timePicker.js';
74 $this->xtra_js[] = $this->add_path('reports/js/common.js');
75 $this->xtra_js[] = $this->add_path('summary/js/summary.js');
77 $this->template->css_header = $this->add_view('css_header');
78 $this->xtra_css[] = $this->add_path('reports/css/datePicker.css');
79 $this->template->css_header->css = $this->xtra_css;
81 $this->js_strings .= reports::js_strings();
82 $this->js_strings .= "var _scheduled_label = '"._('Scheduled')."';\n";
84 if ($this->options['report_id']) {
85 $this->js_strings .= "var _report_data = " . $this->options->as_json() . "\n";
87 else if (!$this->options['standardreport']) {
88 $this->inline_js .= "set_selection(document.getElementsByName('report_type').item(0).value);\n";
91 $this->template->toolbar = new Toolbar_Controller(_('Summary report'));
94 $this->template->inline_js = $this->inline_js;
95 $this->template->js_strings = $this->js_strings;
97 $template->type = $this->type;
98 $template->scheduled_ids = $scheduled_ids;
99 $template->scheduled_periods = $scheduled_periods;
101 $template->available_schedule_periods = Scheduled_reports_Model::get_available_report_periods();
103 $template->saved_reports = $saved_reports;
105 $this->template->title = _("Reporting » Alert summary » Setup");
109 * Generates an alert summary report
111 public function generate($input=false)
113 $this->setup_options_obj($input);
115 $report_members = $this->options->get_report_members();
116 if (empty($report_members)) {
117 $_SESSION['report_err_msg'] = _("No objects could be found in your selected groups to base the report on");
118 return url::redirect(Router::$controller.'/index');
121 $reports_model = new Summary_Reports_Model($this->options);
123 $result = false;
124 switch ($this->options['summary_type']) {
125 case Summary_options::TOP_ALERT_PRODUCERS:
126 $result = $reports_model->top_alert_producers();
127 break;
129 case Summary_options::RECENT_ALERTS:
130 $result = $reports_model->recent_alerts();
131 break;
133 case Summary_options::ALERT_TOTALS:
134 $result = $reports_model->alert_totals();
135 break;
137 default:
138 echo Kohana::debug("Case fallthrough");
139 exit(1);
143 if ($this->options['output_format'] == 'csv') {
144 csv::csv_http_headers($this->type, $this->options);
145 $this->template = $this->add_view('summary/csv');
146 $this->template->options = $this->options;
147 $this->template->summary_type = $this->options['summary_type'];
148 $this->template->result = $result;
149 $this->template->date_format = nagstat::date_format();
150 $this->template->host_state_names = $this->host_state_names;
151 $this->template->service_state_names = $this->service_state_names;
153 return;
156 $this->template->disable_refresh = true;
157 $this->xtra_js[] = 'application/media/js/jquery.datePicker.js';
158 $this->xtra_js[] = 'application/media/js/jquery.timePicker.js';
159 $this->xtra_js[] = $this->add_path('reports/js/common.js');
160 $this->xtra_js[] = $this->add_path('summary/js/summary.js');
161 $this->template->js_header = $this->add_view('js_header');
162 $this->template->css_header = $this->add_view('css_header');
163 $this->xtra_css[] = $this->add_path('reports/css/datePicker.css');
164 $this->template->css_header->css = $this->xtra_css;
166 if($this->options['report_period'] && $this->options['report_period'] != 'custom')
167 $report_time_formatted = $this->options->get_value('report_period');
168 else
169 $report_time_formatted = sprintf(_("%s to %s"), date(nagstat::date_format(), $this->options['start_time']), date(nagstat::date_format(), $this->options['end_time']));
171 if($this->options['rpttimeperiod'] != '')
172 $report_time_formatted .= " - {$this->options['rpttimeperiod']}";
174 $views = array(
175 Summary_options::TOP_ALERT_PRODUCERS => 'toplist',
176 Summary_options::RECENT_ALERTS => 'latest',
177 Summary_options::ALERT_TOTALS => 'alert_totals',
180 $this->template->content = $this->add_view('reports/index');
181 $this->template->content->header = $this->add_view('summary/header');
182 $this->template->content->header->standard_header = $this->add_view('reports/header');
183 $header = $this->template->content->header->standard_header;
184 $this->template->content->report_options = $this->add_view('summary/options');
185 $header->report_time_formatted = $report_time_formatted;
186 $this->template->content->content =
187 $this->add_view("summary/" . $views[$this->options['summary_type']]);
189 $content = $this->template->content->content;
191 $content->host_state_names = $this->host_state_names;
192 $content->service_state_names = $this->service_state_names;
194 $this->js_strings .= reports::js_strings();
195 $this->js_strings .= "var _scheduled_label = '"._('Scheduled')."';\n";
196 if (!$this->options['standardreport']) {
197 $this->js_strings .= "var _report_data = " . $this->options->as_json() . "\n";
199 else if (!$this->options['standardreport']) {
200 $this->inline_js .= "set_selection(document.getElementsByName('report_type').item(0).value);\n";
202 $this->template->js_strings = $this->js_strings;
203 $this->template->inline_js = $this->inline_js;
205 $content->result = $result;
206 $this->template->title = _("Reporting » Alert summary » Report");
207 $header->title = $this->options->get_value('summary_type');
209 $scheduled_info = Scheduled_reports_Model::report_is_scheduled($this->type, $this->options['report_id']);
210 if($scheduled_info) {
211 $schedule_id = $this->input->get('schedule_id', null);
212 if($schedule_id) {
213 $le_schedule = current(array_filter($scheduled_info, function($item) use ($schedule_id) {
214 return $item['id'] == $schedule_id && $item['attach_description'] && $item['description'];
215 }));
216 if($le_schedule) {
217 $header->description = $this->options['description'] ? $this->options['description']."\n".$le_schedule['description'] : $le_schedule['description'];
223 if ($this->options['output_format'] == 'pdf') {
224 return $this->generate_pdf();