diff options
Diffstat (limited to 'library/Reporting/Web/Forms/ScheduleForm.php')
-rw-r--r-- | library/Reporting/Web/Forms/ScheduleForm.php | 195 |
1 files changed, 105 insertions, 90 deletions
diff --git a/library/Reporting/Web/Forms/ScheduleForm.php b/library/Reporting/Web/Forms/ScheduleForm.php index 47f3ee3..72c4767 100644 --- a/library/Reporting/Web/Forms/ScheduleForm.php +++ b/library/Reporting/Web/Forms/ScheduleForm.php @@ -1,96 +1,98 @@ <?php + // Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 namespace Icinga\Module\Reporting\Web\Forms; use DateTime; -use Icinga\Application\Version; +use Icinga\Application\Icinga; +use Icinga\Application\Web; use Icinga\Authentication\Auth; use Icinga\Module\Reporting\Database; +use Icinga\Module\Reporting\Hook\ActionHook; use Icinga\Module\Reporting\ProvidedActions; use Icinga\Module\Reporting\Report; -use Icinga\Module\Reporting\Web\Flatpickr; -use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator; +use Icinga\Util\Json; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\Form; +use ipl\Html\HtmlDocument; +use ipl\Html\HtmlElement; +use ipl\Scheduler\Contract\Frequency; use ipl\Web\Compat\CompatForm; +use ipl\Web\FormElement\ScheduleElement; + +use function ipl\Stdlib\get_php_type; class ScheduleForm extends CompatForm { - use Database; - use DecoratedElement; use ProvidedActions; /** @var Report */ protected $report; - protected $id; + /** @var ScheduleElement */ + protected $scheduleElement; - public function setReport(Report $report) + public function __construct() { - $this->report = $report; + $this->scheduleElement = new ScheduleElement('schedule_element'); + /** @var Web $app */ + $app = Icinga::app(); + $this->scheduleElement->setIdProtector([$app->getRequest(), 'protectId']); + } - $schedule = $report->getSchedule(); + public function getPartUpdates(): array + { + return $this->scheduleElement->prepareMultipartUpdate($this->getRequest()); + } + + /** + * Create a new form instance with the given report + * + * @param Report $report + * + * @return static + */ + public static function fromReport(Report $report): self + { + $form = new static(); + $form->report = $report; + $schedule = $report->getSchedule(); if ($schedule !== null) { - $this->setId($schedule->getId()); + $config = $schedule->getConfig(); + $config['action'] = $schedule->getAction(); + + /** @var Frequency $type */ + $type = $config['frequencyType']; + $config['schedule_element'] = $type::fromJson($config['frequency']); - $values = [ - 'start' => $schedule->getStart()->format('Y-m-d\\TH:i:s'), - 'frequency' => $schedule->getFrequency(), - 'action' => $schedule->getAction() - ] + $schedule->getConfig(); + unset($config['frequency']); + unset($config['frequencyType']); - $this->populate($values); + $form->populate($config); } - return $this; + return $form; } - public function setId($id) + public function hasBeenSubmitted(): bool { - $this->id = $id; - - return $this; + return $this->hasBeenSent() && ( + $this->getPopulatedValue('submit') + || $this->getPopulatedValue('remove') + || $this->getPopulatedValue('send') + ); } protected function assemble() { - $this->setDefaultElementDecorator(new CompatDecorator()); - - $frequency = [ - 'minutely' => 'Minutely', - 'hourly' => 'Hourly', - 'daily' => 'Daily', - 'weekly' => 'Weekly', - 'monthly' => 'Monthly' - ]; - - if (version_compare(Version::VERSION, '2.9.0', '>=')) { - $this->addElement('localDateTime', 'start', [ - 'required' => true, - 'label' => t('Start'), - 'placeholder' => t('Choose date and time') - ]); - } else { - $this->addDecoratedElement((new Flatpickr())->setAllowInput(false), 'text', 'start', [ - 'required' => true, - 'label' => t('Start'), - 'placeholder' => t('Choose date and time') - ]); - } - - $this->addElement('select', 'frequency', [ - 'required' => true, - 'label' => 'Frequency', - 'options' => [null => 'Please choose'] + $frequency, - ]); - $this->addElement('select', 'action', [ - 'required' => true, - 'label' => 'Action', - 'options' => [null => 'Please choose'] + $this->listActions(), - 'class' => 'autosubmit' + 'required' => true, + 'class' => 'autosubmit', + 'options' => array_merge([null => $this->translate('Please choose')], $this->listActions()), + 'label' => $this->translate('Action'), + 'description' => $this->translate('Specifies an action to be triggered by the scheduler') ]); $values = $this->getValues(); @@ -99,8 +101,8 @@ class ScheduleForm extends CompatForm $config = new Form(); // $config->populate($this->getValues()); - /** @var \Icinga\Module\Reporting\Hook\ActionHook $action */ - $action = new $values['action']; + /** @var ActionHook $action */ + $action = new $values['action'](); $action->initConfigForm($config, $this->report); @@ -109,67 +111,80 @@ class ScheduleForm extends CompatForm } } + $this->addHtml(HtmlElement::create('div', ['class' => 'schedule-element-separator'])); + $this->addElement($this->scheduleElement); + + $schedule = $this->report->getSchedule(); $this->addElement('submit', 'submit', [ - 'label' => $this->id === null ? 'Create Schedule' : 'Update Schedule' + 'label' => $schedule === null ? $this->translate('Create Schedule') : $this->translate('Update Schedule') ]); - if ($this->id !== null) { + if ($schedule !== null) { + $sendButton = $this->createElement('submit', 'send', [ + 'label' => $this->translate('Send Report Now'), + 'formnovalidate' => true + ]); + $this->registerElement($sendButton); + + /** @var HtmlDocument $wrapper */ + $wrapper = $this->getElement('submit')->getWrapper(); + $wrapper->prepend($sendButton); + /** @var FormSubmitElement $removeButton */ $removeButton = $this->createElement('submit', 'remove', [ - 'label' => 'Remove Schedule', + 'label' => $this->translate('Remove Schedule'), 'class' => 'btn-remove', 'formnovalidate' => true ]); $this->registerElement($removeButton); - $this->getElement('submit')->getWrapper()->prepend($removeButton); - - if ($removeButton->hasBeenPressed()) { - $this->getDb()->delete('schedule', ['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; - } + $wrapper->prepend($removeButton); } } public function onSuccess() { - $db = $this->getDb(); + $db = Database::get(); + $schedule = $this->report->getSchedule(); + if ($this->getPopulatedValue('remove')) { + $db->delete('schedule', ['id = ?' => $schedule->getId()]); - $values = $this->getValues(); + return; + } - $now = time() * 1000; + $values = $this->getValues(); + if ($this->getPopulatedValue('send')) { + $action = new $values['action'](); + $action->execute($this->report, $values); - if (! $values['start'] instanceof DateTime) { - $values['start'] = DateTime::createFromFormat('Y-m-d H:i:s', $values['start']); + return; } - $data = [ - 'start' => $values['start']->getTimestamp() * 1000, - 'frequency' => $values['frequency'], - 'action' => $values['action'], - 'mtime' => $now - ]; - - unset($values['start']); - unset($values['frequency']); + $action = $values['action']; unset($values['action']); + unset($values['schedule_element']); - $data['config'] = json_encode($values); + $frequency = $this->scheduleElement->getValue(); + $values['frequency'] = Json::encode($frequency); + $values['frequencyType'] = get_php_type($frequency); + $config = Json::encode($values); $db->beginTransaction(); - if ($this->id === null) { - $db->insert('schedule', $data + [ + if ($schedule === null) { + $now = (new DateTime())->getTimestamp() * 1000; + $db->insert('schedule', [ 'author' => Auth::getInstance()->getUser()->getUsername(), 'report_id' => $this->report->getId(), - 'ctime' => $now + 'ctime' => $now, + 'mtime' => $now, + 'action' => $action, + 'config' => $config ]); } else { - $db->update('schedule', $data, ['id = ?' => $this->id]); + $db->update('schedule', [ + 'action' => $action, + 'config' => $config + ], ['id = ?' => $schedule->getId()]); } $db->commitTransaction(); |