summaryrefslogtreecommitdiffstats
path: root/library/Reporting/Web/Forms/TimeframeForm.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Reporting/Web/Forms/TimeframeForm.php')
-rw-r--r--library/Reporting/Web/Forms/TimeframeForm.php195
1 files changed, 156 insertions, 39 deletions
diff --git a/library/Reporting/Web/Forms/TimeframeForm.php b/library/Reporting/Web/Forms/TimeframeForm.php
index 3d78709..37ea34f 100644
--- a/library/Reporting/Web/Forms/TimeframeForm.php
+++ b/library/Reporting/Web/Forms/TimeframeForm.php
@@ -1,86 +1,203 @@
<?php
+
// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2
namespace Icinga\Module\Reporting\Web\Forms;
+use DateTime;
+use Exception;
use Icinga\Module\Reporting\Database;
-use Icinga\Module\Reporting\Web\Flatpickr;
-use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator;
use ipl\Html\Contract\FormSubmitElement;
+use ipl\Html\FormElement\LocalDateTimeElement;
+use ipl\Html\HtmlDocument;
+use ipl\Validator\CallbackValidator;
use ipl\Web\Compat\CompatForm;
class TimeframeForm extends CompatForm
{
- use Database;
- use DecoratedElement;
-
+ /** @var int */
protected $id;
- public function setId($id)
+ /**
+ * Create a new form instance with the given report
+ *
+ * @param int $id
+ *
+ * @return static
+ */
+ public static function fromId(int $id): self
{
- $this->id = $id;
+ $form = new static();
+
+ $form->id = $id;
- return $this;
+ return $form;
}
- protected function assemble()
+ public function hasBeenSubmitted(): bool
{
- $this->setDefaultElementDecorator(new CompatDecorator());
+ return $this->hasBeenSent() && ($this->getPopulatedValue('submit') || $this->getPopulatedValue('remove'));
+ }
+ protected function assemble()
+ {
$this->addElement('text', 'name', [
- 'required' => true,
- 'label' => 'Name'
+ 'required' => true,
+ 'label' => $this->translate('Name'),
+ 'description' => $this->translate('A unique name of this timeframe')
]);
- $flatpickr = new Flatpickr();
+ $default = new DateTime('00:00:00');
+ $start = $this->getPopulatedValue('start', $default);
+ if (! $start instanceof DateTime) {
+ $datetime = DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $start);
+ if ($datetime) {
+ $start = $datetime;
+ }
+ }
- $this->addDecoratedElement($flatpickr, 'text', 'start', [
- 'required' => true,
- 'label' => 'Start',
- 'placeholder' => 'Select a start date or provide a textual datetime description',
- 'data-flatpickr-default-hour' => '00'
+ $relativeStart = $this->getPopulatedValue('relative-start', $start instanceof DateTime ? 'n' : 'y');
+ $this->addElement('checkbox', 'relative-start', [
+ 'required' => false,
+ 'class' => 'autosubmit',
+ 'value' => $relativeStart,
+ 'label' => $this->translate('Relative Start')
]);
- $this->addDecoratedElement($flatpickr, 'text', 'end', [
- 'required' => true,
- 'label' => 'End',
- 'placeholder' => 'Select a end date or provide a textual datetime description',
- 'data-flatpickrDefaultHour' => '23',
- 'data-flatpickrDefaultMinute' => '59',
- 'data-flatpickrDefaultSeconds' => '59'
- ]);
+ if ($relativeStart === 'n') {
+ if (! $start instanceof DateTime) {
+ $start = $default;
+ $this->clearPopulatedValue('start');
+ }
+
+ $this->addElement(
+ new LocalDateTimeElement('start', [
+ 'required' => true,
+ 'value' => $start,
+ 'label' => $this->translate('Start'),
+ 'description' => $this->translate('Specifies the start time of this timeframe')
+ ])
+ );
+ } else {
+ $this->addElement('text', 'start', [
+ 'required' => true,
+ 'label' => $this->translate('Start'),
+ 'placeholder' => $this->translate('First day of this month'),
+ 'description' => $this->translate('Specifies the start time of this timeframe'),
+ 'validators' => [
+ new CallbackValidator(function ($value, CallbackValidator $validator) {
+ if ($value !== null) {
+ try {
+ new DateTime($value);
+ } catch (Exception $_) {
+ $validator->addMessage($this->translate('Invalid textual date time'));
+
+ return false;
+ }
+ }
+
+ return true;
+ })
+ ]
+ ]);
+ }
+
+ $default = new DateTime('23:59:59');
+ $end = $this->getPopulatedValue('end', $default);
+ if (! $end instanceof DateTime) {
+ $datetime = DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $end);
+ if ($datetime) {
+ $end = $datetime;
+ }
+ }
+
+ $relativeEnd = $this->getPopulatedValue('relative-end', $end instanceof DateTime ? 'n' : 'y');
+ if ($relativeStart === 'y') {
+ $this->addElement('checkbox', 'relative-end', [
+ 'required' => false,
+ 'class' => 'autosubmit',
+ 'value' => $relativeEnd,
+ 'label' => $this->translate('Relative End')
+ ]);
+ }
+
+ if ($relativeEnd === 'n' || $relativeStart === 'n') {
+ if (! $end instanceof DateTime) {
+ $end = $default;
+ $this->clearPopulatedValue('end');
+ }
+
+ $this->addElement(
+ new LocalDateTimeElement('end', [
+ 'required' => true,
+ 'value' => $end,
+ 'label' => $this->translate('End'),
+ 'description' => $this->translate('Specifies the end time of this timeframe')
+ ])
+ );
+ } else {
+ $this->addElement('text', 'end', [
+ 'required' => true,
+ 'label' => $this->translate('End'),
+ 'placeholder' => $this->translate('Last day of this month'),
+ 'description' => $this->translate('Specifies the end time of this timeframe'),
+ 'validators' => [
+ new CallbackValidator(function ($value, CallbackValidator $validator) {
+ if ($value !== null) {
+ try {
+ new DateTime($value);
+ } catch (Exception $_) {
+ $validator->addMessage($this->translate('Invalid textual date time'));
+
+ return false;
+ }
+ }
+
+ return true;
+ })
+ ]
+ ]);
+ }
$this->addElement('submit', 'submit', [
- 'label' => $this->id === null ? 'Create Time Frame' : 'Update Time Frame'
+ 'label' => $this->id === null
+ ? $this->translate('Create Time Frame')
+ : $this->translate('Update Time Frame')
]);
if ($this->id !== null) {
/** @var FormSubmitElement $removeButton */
$removeButton = $this->createElement('submit', 'remove', [
- 'label' => 'Remove Time Frame',
+ 'label' => $this->translate('Remove Time Frame'),
'class' => 'btn-remove',
'formnovalidate' => true
]);
$this->registerElement($removeButton);
- $this->getElement('submit')->getWrapper()->prepend($removeButton);
- if ($removeButton->hasBeenPressed()) {
- $this->getDb()->delete('timeframe', ['id = ?' => $this->id]);
-
- // Stupid cheat because ipl/html is not capable of multiple submit buttons
- $this->getSubmitButton()->setValue($this->getSubmitButton()->getButtonLabel());
- $this->valid = true;
-
- return;
- }
+ /** @var HtmlDocument $wrapper */
+ $wrapper = $this->getElement('submit')->getWrapper();
+ $wrapper->prepend($removeButton);
}
}
public function onSuccess()
{
- $db = $this->getDb();
+ $db = Database::get();
+
+ if ($this->getPopulatedValue('remove')) {
+ $db->delete('timeframe', ['id = ?' => $this->id]);
+
+ return;
+ }
$values = $this->getValues();
+ if ($values['start'] instanceof DateTime) {
+ $values['start'] = $values['start']->format(LocalDateTimeElement::FORMAT);
+ }
+
+ if ($values['end'] instanceof DateTime) {
+ $values['end'] = $values['end']->format(LocalDateTimeElement::FORMAT);
+ }
$now = time() * 1000;