httpapievent: Set in_scheduled_downtime to null always
[ninja.git] / modules / reports / libraries / Sla_options.php
blob4d2c7ab68d204a6069cfeb7d0fcbc0887103c6b9
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 /**
4 * Report options for SLA reports
5 */
6 class Sla_options_Core extends Report_options {
7 public static $type = 'sla';
9 static function discover_options($input = false) {
10 # not using $_REQUEST, because that includes weird, scary session vars
11 if (!empty($input)) {
12 $report_info = $input;
13 } else if (!empty($_POST)) {
14 $report_info = $_POST;
15 } else {
16 $report_info = $_GET;
20 if(isset($report_info['report_period'], $report_info['start_year'], $report_info['start_month'], $report_info['end_year'], $report_info['end_month'])
21 && $report_info['report_period'] == 'custom'
22 && strval($report_info['start_year']) !== ""
23 && strval($report_info['start_month']) !== ""
24 && strval($report_info['end_year']) !== ""
25 && strval($report_info['end_month']) !== ""
26 ) {
27 $report_info['start_time'] = mktime(0, 0, 0, $report_info['start_month'], 1, $report_info['start_year']);
28 $report_info['end_time'] = mktime(0, 0, -1, $report_info['end_month'] + 1, 1, $report_info['end_year']);
29 unset(
30 $report_info['cal_start'],
31 $report_info['cal_end'],
32 $report_info['time_start'],
33 $report_info['time_end']
36 return $report_info;
39 public function setup_properties()
41 parent::setup_properties();
42 // Warning! months is 1-indexed
43 $this->properties['months'] = array('type' => 'array', 'default' => false);
45 $this->properties['report_period'] = array('type' => 'enum', 'default' => 'thisyear', 'options' => array(
46 "thisyear" => _('This year'),
47 "lastyear" => _('Last year'),
48 "lastmonth" => _('Last month'),
49 "last3months" => _('Last 3 months'),
50 "last6months" => _('Last 6 months'),
51 "lastquarter" => _('Last quarter'),
52 "last12months" => _('Last 12 months'),
53 'custom' => _('Custom')
54 ));
57 /**
58 * Because this if condition is too long for my pretty little mind to worry about,
59 * I'm'a make a predicate out of it!
61 private static function is_valid_month($month, $start_month, $end_month)
63 if ($start_month > $end_month && ($month >= $start_month || $month <= $end_month)) # e.g. report is dec(12)->feb(2), only 12, 1, 2 valid
64 return true;
65 if ($start_month <= $end_month && ($month >= $start_month && $month <= $end_month)) # e.g. report is feb(2)->apr(4), only 2, 3, 4 valid
66 return true;
67 return false;
70 private static function validate_months($start_time, $end_time, $months)
72 $start_month = date('n', $start_time);
73 $end_month = date('n', $end_time - 1);
74 $res = array();
75 if (!is_array($months))
76 $months = array();
77 if (($end_month - $start_month == 11) || ($end_month - $start_month == -1)) {
78 $start = 1;
79 $stop = 12;
81 else {
82 $start = $start_month;
83 $stop = $end_month;
85 // about the weird modulo: the valid range is 1->12, not 0->11
86 for ($i = $start; true; $i = ($i % 12) + 1)
88 if (static::is_valid_month($i, $start_month, $end_month)) {
89 if (!isset($months[$i]))
90 $res[$i] = 0.0;
91 else
92 $res[$i] = $months[$i];
94 if ($i == $stop)
95 break;
97 return $res;
100 public function set($name, $value)
102 if ($name == 'months') {
103 $value = static::validate_months($this['start_time'], $this['end_time'], $value);
105 $resp = parent::set($name, $value);
106 if ($resp === false && preg_match('/^month/', trim($name))) {
107 $id = (int)str_replace('month_', '', $name);
109 if (!$this->is_valid_month($id, date('n', $this['start_time']), date('n', $this['end_time'] - 1)))
110 return false;
112 if (trim($value) == '')
113 return;
114 // Because fuck locales, amiright?
115 $value = str_replace(',', '.', $value);
116 $value = (float)$value;
117 // values greater than 100 doesn't make sense
118 if ($value>100)
119 $value = 100;
120 $this->options['months'][$id] = $value;
121 return true;
123 return $resp;
126 protected function calculate_time($report_period)
128 $res = parent::calculate_time($report_period);
129 if ($res && isset($this->options['start_time']) && isset($this->options['end_time'])) {
130 $this->options['months'] = static::validate_months($this['start_time'], $this['end_time'], $this['months']);
132 return $res;
135 protected function load_options($id)
137 $opts = parent::load_options($id);
138 if (!$opts)
139 return false;
141 foreach (array('start_time', 'end_time', 'report_period') as $k) {
142 if (isset($opts[$k]))
143 $this->set($k, $opts[$k]);
145 $this->calculate_time($this['report_period']);
147 reset($opts['months']);
148 foreach ($this->options['months'] as $key => $_) {
149 $this->options['months'][$key] = (float)current($opts['months']);
150 next($opts['months']);
152 unset($opts['months']);
153 return $opts;