1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
<?php
namespace Icinga\Module\Graphite\Forms\TimeRangePicker;
use Icinga\Module\Graphite\Util\TimeRangePickerTools;
use Icinga\Web\Form;
use Zend_Form_Element_Select;
class CommonForm extends Form
{
/**
* The selectable units with themselves in seconds
*
* One month equals 30 days and one year equals 365.25 days. This should cover enough cases.
*
* @var int[]
*/
protected $rangeFactors = [
'minutes' => 60,
'hours' => 3600,
'days' => 86400,
'weeks' => 604800,
'months' => 2592000,
'years' => 31557600
];
/**
* The elements' default values
*
* @var string[]|null
*/
protected $defaultFormData;
public function init()
{
$this->setName('form_timerangepickercommon_graphite');
$this->setAttrib('data-base-target', '_self');
$this->setAttrib('class', 'icinga-form icinga-controls inline');
}
public function createElements(array $formData)
{
$this->addElements([
$this->createSelect(
'minutes',
$this->translate('Minutes'),
$this->translate('Show the last … minutes'),
[5, 10, 15, 30, 45],
$this->translate('%d minute'),
$this->translate('%d minutes')
),
$this->createSelect(
'hours',
$this->translate('Hours'),
$this->translate('Show the last … hours'),
[1, 2, 3, 6, 12, 18],
$this->translate('%d hour'),
$this->translate('%d hours')
),
$this->createSelect(
'days',
$this->translate('Days'),
$this->translate('Show the last … days'),
range(1, 6),
$this->translate('%d day'),
$this->translate('%d days')
),
$this->createSelect(
'weeks',
$this->translate('Weeks'),
$this->translate('Show the last … weeks'),
range(1, 4),
$this->translate('%d week'),
$this->translate('%d weeks')
),
$this->createSelect(
'months',
$this->translate('Months'),
$this->translate('Show the last … months'),
[1, 2, 3, 6, 9],
$this->translate('%d month'),
$this->translate('%d months')
),
$this->createSelect(
'years',
$this->translate('Years'),
$this->translate('Show the last … years'),
range(1, 3),
$this->translate('%d year'),
$this->translate('%d years')
)
]);
$this->urlToForm();
$this->defaultFormData = $this->getValues();
}
public function onSuccess()
{
$this->formToUrl();
$this->getRedirectUrl()->remove(array_values(TimeRangePickerTools::getAbsoluteRangeParameters()));
}
/**
* Create a common range picker for a specific time unit
*
* @param string $name
* @param string $label
* @param string $description
* @param int[] $options
* @param string $singular
* @param string $plural
*
* @return Zend_Form_Element_Select
*/
protected function createSelect($name, $label, $description, array $options, $singular, $plural)
{
$multiOptions = ['' => $label];
foreach ($options as $option) {
$multiOptions[$option] = sprintf($option === 1 ? $singular : $plural, $option);
}
$element = $this->createElement('select', $name, [
'label' => $label,
'description' => $description,
'multiOptions' => $multiOptions,
'title' => $description,
'autosubmit' => true
]);
$decorators = $element->getDecorators();
$element->setDecorators([
'Zend_Form_Decorator_ViewHelper' => $decorators['Zend_Form_Decorator_ViewHelper']
]);
return $element;
}
/**
* Set this form's elements' default values based on the redirect URL's parameters
*/
protected function urlToForm()
{
$params = $this->getRedirectUrl()->getParams();
$seconds = TimeRangePickerTools::getRelativeSeconds($params);
if (
$seconds === null
&& count(array_intersect_key(
$params->toArray(false),
array_keys(TimeRangePickerTools::getAllRangeParameters())
)) === 0
) {
$seconds = TimeRangePickerTools::getDefaultRelativeTimeRange();
}
if ($seconds !== null) {
if ($seconds !== false) {
foreach ($this->rangeFactors as $unit => $factor) {
/** @var Zend_Form_Element_Select $element */
$element = $this->getElement($unit);
$options = $element->getMultiOptions();
unset($options['']);
foreach ($options as $option => $_) {
if ($seconds === (int) $option * $factor) {
$element->setValue((string) $option);
return;
}
}
}
}
$params->remove(TimeRangePickerTools::getRelativeRangeParameter());
}
}
/**
* Change the redirect URL's parameters based on this form's elements' values
*/
protected function formToUrl()
{
$formData = $this->getValues();
foreach ($this->rangeFactors as $unit => $factor) {
if ($formData[$unit] !== '' && $formData[$unit] !== $this->defaultFormData[$unit]) {
$this->getRedirectUrl()->setParam(
TimeRangePickerTools::getRelativeRangeParameter(),
(string) ((int) $formData[$unit] * $factor)
);
return;
}
}
}
}
|