Merge branch 'maint/7.0'
[ninja.git] / modules / reports / controllers / summary.php
blob9fa5a7f082fb20a8b96ade705d4ff4f97728d004
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[] = 'application/media/js/jquery.datePicker.js';
72 $this->template->js[] = 'application/media/js/jquery.timePicker.js';
73 $this->template->js[] = $this->add_path('reports/js/common.js');
74 $this->template->js[] = $this->add_path('summary/js/summary.js');
75 $this->template->js[] = 'application/media/js/lib.set.js';
76 $this->template->js[] = 'application/media/js/jquery.filterable.js';
78 $this->template->css[] = 'application/media/css/jquery.filterable.css';
79 $this->template->css[] = $this->add_path('reports/css/datePicker.css');
81 $this->js_strings .= reports::js_strings();
82 $this->js_strings .= "var _scheduled_label = '"._('Scheduled')."';\n";
84 $this->template->toolbar = new Toolbar_Controller(_('Summary report'));
87 $this->template->js_strings = $this->js_strings;
89 $template->scheduled_info = Scheduled_reports_Model::report_is_scheduled($this->type, $this->options['report_id']);
91 $template->type = $this->type;
92 $template->scheduled_ids = $scheduled_ids;
93 $template->scheduled_periods = $scheduled_periods;
95 $template->available_schedule_periods = Scheduled_reports_Model::get_available_report_periods();
96 $template->saved_reports = $this->options->get_all_saved();
98 $this->template->title = _("Reporting » Alert summary » Setup");
102 * Generates an alert summary report
104 public function generate($input=false)
106 $this->setup_options_obj($input);
108 $report_members = $this->options->get_report_members();
109 if (empty($report_members)) {
110 $_SESSION['report_err_msg'] = _("No objects could be found in your selected groups to base the report on");
111 return url::redirect(Router::$controller.'/index');
114 $reports_model = new Summary_Reports_Model($this->options);
116 $result = false;
117 switch ($this->options['summary_type']) {
118 case Summary_options::TOP_ALERT_PRODUCERS:
119 $result = $reports_model->top_alert_producers();
120 break;
122 case Summary_options::RECENT_ALERTS:
123 $result = $reports_model->recent_alerts();
124 break;
126 case Summary_options::ALERT_TOTALS:
127 $result = $reports_model->alert_totals();
128 break;
130 default:
131 echo Kohana::debug("Case fallthrough");
132 exit(1);
136 if ($this->options['output_format'] == 'csv') {
137 csv::csv_http_headers($this->type, $this->options);
138 $this->template = $this->add_view('summary/csv');
139 $this->template->options = $this->options;
140 $this->template->summary_type = $this->options['summary_type'];
141 $this->template->result = $result;
142 $this->template->date_format = nagstat::date_format();
143 $this->template->host_state_names = $this->host_state_names;
144 $this->template->service_state_names = $this->service_state_names;
146 return;
149 $this->template->disable_refresh = true;
150 $this->template->js[] = 'application/media/js/jquery.datePicker.js';
151 $this->template->js[] = 'application/media/js/jquery.timePicker.js';
152 $this->template->js[] = $this->add_path('reports/js/common.js');
153 $this->template->js[] = $this->add_path('summary/js/summary.js');
154 $this->template->css[] = $this->add_path('reports/css/datePicker.css');
156 $views = array(
157 Summary_options::TOP_ALERT_PRODUCERS => 'toplist',
158 Summary_options::RECENT_ALERTS => 'latest',
159 Summary_options::ALERT_TOTALS => 'alert_totals',
162 $this->template->content = $this->add_view('reports/index');
163 $this->template->content->header = $this->add_view('summary/header');
164 $this->template->content->header->standard_header = $this->add_view('reports/header');
165 $header = $this->template->content->header->standard_header;
166 $this->template->content->report_options = $this->add_view('summary/options');
167 $header->report_time_formatted = $this->format_report_time(nagstat::date_format());
168 $this->template->content->content =
169 $this->add_view("summary/" . $views[$this->options['summary_type']]);
171 $content = $this->template->content->content;
173 $content->host_state_names = $this->host_state_names;
174 $content->service_state_names = $this->service_state_names;
176 $this->js_strings .= reports::js_strings();
177 $this->js_strings .= "var _scheduled_label = '"._('Scheduled')."';\n";
178 $this->template->js_strings = $this->js_strings;
180 $content->result = $result;
181 $this->template->title = _("Reporting » Alert summary » Report");
182 $header->title = $this->options->get_value('summary_type');
184 $scheduled_info = Scheduled_reports_Model::report_is_scheduled($this->type, $this->options['report_id']);
185 if($scheduled_info) {
186 $schedule_id = $this->input->get('schedule_id', null);
187 if($schedule_id) {
188 $le_schedule = current(array_filter($scheduled_info, function($item) use ($schedule_id) {
189 return $item['id'] == $schedule_id && $item['attach_description'] && $item['description'];
190 }));
191 if($le_schedule) {
192 $header->description = $this->options['description'] ? $this->options['description']."\n".$le_schedule['description'] : $le_schedule['description'];
197 if ($this->options['output_format'] == 'pdf') {
198 return $this->generate_pdf();