1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
4 * Report options for SLA reports
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
12 $report_info = $input;
13 } else if (!empty($_POST)) {
14 $report_info = $_POST;
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']) !== ""
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']);
30 $report_info['cal_start'],
31 $report_info['cal_end'],
32 $report_info['time_start'],
33 $report_info['time_end']
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')
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
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
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);
75 if (!is_array($months))
77 if (($end_month - $start_month == 11) ||
($end_month - $start_month == -1)) {
82 $start = $start_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]))
92 $res[$i] = $months[$i];
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)))
112 if (trim($value) == '')
114 // Because fuck locales, amiright?
115 $value = str_replace(',', '.', $value);
116 $value = (float)$value;
117 // values greater than 100 doesn't make sense
120 $this->options
['months'][$id] = $value;
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']);
135 protected function load_options($id)
137 $opts = parent
::load_options($id);
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']);