|
4 | 4 |
|
5 | 5 | namespace Icinga\Module\Reporting\Web\Forms;
|
6 | 6 |
|
| 7 | +use DateTime; |
| 8 | +use Exception; |
7 | 9 | use Icinga\Module\Reporting\Database;
|
8 |
| -use Icinga\Module\Reporting\Web\Flatpickr; |
9 |
| -use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator; |
10 | 10 | use ipl\Html\Contract\FormSubmitElement;
|
| 11 | +use ipl\Html\FormElement\LocalDateTimeElement; |
| 12 | +use ipl\Validator\CallbackValidator; |
11 | 13 | use ipl\Web\Compat\CompatForm;
|
12 | 14 |
|
13 | 15 | class TimeframeForm extends CompatForm
|
14 | 16 | {
|
15 | 17 | use Database;
|
16 |
| - use DecoratedElement; |
17 | 18 |
|
18 | 19 | /** @var int */
|
19 | 20 | protected $id;
|
@@ -41,34 +42,123 @@ public function hasBeenSubmitted(): bool
|
41 | 42 |
|
42 | 43 | protected function assemble()
|
43 | 44 | {
|
44 |
| - $this->setDefaultElementDecorator(new CompatDecorator()); |
45 |
| - |
46 | 45 | $this->addElement('text', 'name', [
|
47 |
| - 'required' => true, |
48 |
| - 'label' => $this->translate('Name') |
| 46 | + 'required' => true, |
| 47 | + 'label' => $this->translate('Name'), |
| 48 | + 'description' => $this->translate('A unique name of this timeframe') |
49 | 49 | ]);
|
50 | 50 |
|
51 |
| - $flatpickr = new Flatpickr(); |
| 51 | + $default = new DateTime('00:00:00'); |
| 52 | + $start = $this->getPopulatedValue('start', $default); |
| 53 | + if (! $start instanceof DateTime) { |
| 54 | + $datetime = DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $start); |
| 55 | + if ($datetime) { |
| 56 | + $start = $datetime; |
| 57 | + } |
| 58 | + } |
52 | 59 |
|
53 |
| - $this->addDecoratedElement($flatpickr, 'text', 'start', [ |
54 |
| - 'required' => true, |
55 |
| - 'label' => $this->translate('Start'), |
56 |
| - 'data-flatpickr-default-hour' => '00', |
57 |
| - 'placeholder' => $this->translate( |
58 |
| - 'Select a start date or provide a textual datetime description' |
59 |
| - ), |
| 60 | + $relativeStart = $this->getPopulatedValue('relative-start', $start instanceof DateTime ? 'n' : 'y'); |
| 61 | + $this->addElement('checkbox', 'relative-start', [ |
| 62 | + 'required' => false, |
| 63 | + 'class' => 'autosubmit', |
| 64 | + 'value' => $relativeStart, |
| 65 | + 'label' => $this->translate('Relative Start') |
60 | 66 | ]);
|
61 | 67 |
|
62 |
| - $this->addDecoratedElement($flatpickr, 'text', 'end', [ |
63 |
| - 'required' => true, |
64 |
| - 'label' => $this->translate('End'), |
65 |
| - 'data-flatpickrDefaultHour' => '23', |
66 |
| - 'data-flatpickrDefaultMinute' => '59', |
67 |
| - 'data-flatpickrDefaultSeconds' => '59', |
68 |
| - 'placeholder' => $this->translate( |
69 |
| - 'Select a end date or provide a textual datetime description' |
70 |
| - ), |
71 |
| - ]); |
| 68 | + if ($relativeStart === 'n') { |
| 69 | + if (! $start instanceof DateTime) { |
| 70 | + $start = $default; |
| 71 | + $this->clearPopulatedValue('start'); |
| 72 | + } |
| 73 | + |
| 74 | + $this->addElement( |
| 75 | + new LocalDateTimeElement('start', [ |
| 76 | + 'required' => true, |
| 77 | + 'value' => $start, |
| 78 | + 'label' => $this->translate('Start'), |
| 79 | + 'description' => $this->translate('Specifies the start time of this timeframe') |
| 80 | + ]) |
| 81 | + ); |
| 82 | + } else { |
| 83 | + $this->addElement('text', 'start', [ |
| 84 | + 'required' => true, |
| 85 | + 'label' => $this->translate('Start'), |
| 86 | + 'placeholder' => $this->translate('First day of this month'), |
| 87 | + 'description' => $this->translate('Specifies the start time of this timeframe'), |
| 88 | + 'validators' => [ |
| 89 | + new CallbackValidator(function ($value, CallbackValidator $validator) { |
| 90 | + if ($value !== null) { |
| 91 | + try { |
| 92 | + new DateTime($value); |
| 93 | + } catch (Exception $_) { |
| 94 | + $validator->addMessage($this->translate('Invalid textual date time')); |
| 95 | + |
| 96 | + return false; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return true; |
| 101 | + }) |
| 102 | + ] |
| 103 | + ]); |
| 104 | + } |
| 105 | + |
| 106 | + $default = new DateTime('23:59:59'); |
| 107 | + $end = $this->getPopulatedValue('end', $default); |
| 108 | + if (! $end instanceof DateTime) { |
| 109 | + $datetime = DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $end); |
| 110 | + if ($datetime) { |
| 111 | + $end = $datetime; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + $relativeEnd = $this->getPopulatedValue('relative-end', $end instanceof DateTime ? 'n' : 'y'); |
| 116 | + if ($relativeStart === 'y') { |
| 117 | + $this->addElement('checkbox', 'relative-end', [ |
| 118 | + 'required' => false, |
| 119 | + 'class' => 'autosubmit', |
| 120 | + 'value' => $relativeEnd, |
| 121 | + 'label' => $this->translate('Relative End') |
| 122 | + ]); |
| 123 | + } |
| 124 | + |
| 125 | + if ($relativeEnd === 'n' || $relativeStart === 'n') { |
| 126 | + if (! $end instanceof DateTime) { |
| 127 | + $end = $default; |
| 128 | + $this->clearPopulatedValue('end'); |
| 129 | + } |
| 130 | + |
| 131 | + $this->addElement( |
| 132 | + new LocalDateTimeElement('end', [ |
| 133 | + 'required' => true, |
| 134 | + 'value' => $end, |
| 135 | + 'label' => $this->translate('End'), |
| 136 | + 'description' => $this->translate('Specifies the end time of this timeframe') |
| 137 | + ]) |
| 138 | + ); |
| 139 | + } else { |
| 140 | + $this->addElement('text', 'end', [ |
| 141 | + 'required' => true, |
| 142 | + 'label' => $this->translate('End'), |
| 143 | + 'placeholder' => $this->translate('Last day of this month'), |
| 144 | + 'description' => $this->translate('Specifies the end time of this timeframe'), |
| 145 | + 'validators' => [ |
| 146 | + new CallbackValidator(function ($value, CallbackValidator $validator) { |
| 147 | + if ($value !== null) { |
| 148 | + try { |
| 149 | + new DateTime($value); |
| 150 | + } catch (Exception $_) { |
| 151 | + $validator->addMessage($this->translate('Invalid textual date time')); |
| 152 | + |
| 153 | + return false; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return true; |
| 158 | + }) |
| 159 | + ] |
| 160 | + ]); |
| 161 | + } |
72 | 162 |
|
73 | 163 | $this->addElement('submit', 'submit', [
|
74 | 164 | 'label' => $this->id === null
|
@@ -99,6 +189,13 @@ public function onSuccess()
|
99 | 189 | }
|
100 | 190 |
|
101 | 191 | $values = $this->getValues();
|
| 192 | + if ($values['start'] instanceof DateTime) { |
| 193 | + $values['start'] = $values['start']->format(LocalDateTimeElement::FORMAT); |
| 194 | + } |
| 195 | + |
| 196 | + if ($values['end'] instanceof DateTime) { |
| 197 | + $values['end'] = $values['end']->format(LocalDateTimeElement::FORMAT); |
| 198 | + } |
102 | 199 |
|
103 | 200 | $now = time() * 1000;
|
104 | 201 |
|
|
0 commit comments