diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:46:47 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:46:47 +0000 |
commit | 4ada86876033fa171e2896d7e3d3c5645d8062db (patch) | |
tree | f0d1fee61877df200ccfb1c0af58a39cd551fb46 /library/Reporting/Web/Forms/TimeframeForm.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-reporting-4ada86876033fa171e2896d7e3d3c5645d8062db.tar.xz icingaweb2-module-reporting-4ada86876033fa171e2896d7e3d3c5645d8062db.zip |
Adding upstream version 0.10.0.upstream/0.10.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Reporting/Web/Forms/TimeframeForm.php')
-rw-r--r-- | library/Reporting/Web/Forms/TimeframeForm.php | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/library/Reporting/Web/Forms/TimeframeForm.php b/library/Reporting/Web/Forms/TimeframeForm.php new file mode 100644 index 0000000..3d78709 --- /dev/null +++ b/library/Reporting/Web/Forms/TimeframeForm.php @@ -0,0 +1,106 @@ +<?php +// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2 + +namespace Icinga\Module\Reporting\Web\Forms; + +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\Web\Compat\CompatForm; + +class TimeframeForm extends CompatForm +{ + use Database; + use DecoratedElement; + + protected $id; + + public function setId($id) + { + $this->id = $id; + + return $this; + } + + protected function assemble() + { + $this->setDefaultElementDecorator(new CompatDecorator()); + + $this->addElement('text', 'name', [ + 'required' => true, + 'label' => 'Name' + ]); + + $flatpickr = new Flatpickr(); + + $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' + ]); + + $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' + ]); + + $this->addElement('submit', 'submit', [ + 'label' => $this->id === null ? 'Create Time Frame' : 'Update Time Frame' + ]); + + if ($this->id !== null) { + /** @var FormSubmitElement $removeButton */ + $removeButton = $this->createElement('submit', 'remove', [ + 'label' => '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; + } + } + } + + public function onSuccess() + { + $db = $this->getDb(); + + $values = $this->getValues(); + + $now = time() * 1000; + + $end = $db->quoteIdentifier('end'); + + if ($this->id === null) { + $db->insert('timeframe', [ + 'name' => $values['name'], + 'start' => $values['start'], + $end => $values['end'], + 'ctime' => $now, + 'mtime' => $now + ]); + } else { + $db->update('timeframe', [ + 'name' => $values['name'], + 'start' => $values['start'], + $end => $values['end'], + 'mtime' => $now + ], ['id = ?' => $this->id]); + } + } +} |