diff options
Diffstat (limited to 'library/Reporting/Web/Forms/ReportForm.php')
-rw-r--r-- | library/Reporting/Web/Forms/ReportForm.php | 179 |
1 files changed, 137 insertions, 42 deletions
diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index 6b1e692..40e376e 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -1,4 +1,5 @@ <?php + // Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 namespace Icinga\Module\Reporting\Web\Forms; @@ -6,54 +7,143 @@ namespace Icinga\Module\Reporting\Web\Forms; use Icinga\Authentication\Auth; use Icinga\Module\Reporting\Database; use Icinga\Module\Reporting\ProvidedReports; -use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\Form; +use ipl\Html\HtmlDocument; +use ipl\Validator\CallbackValidator; use ipl\Web\Compat\CompatForm; class ReportForm extends CompatForm { - use Database; use ProvidedReports; - /** @var bool Hack to disable the {@link onSuccess()} code upon deletion of the report */ - protected $callOnSuccess; - protected $id; - public function setId($id) + /** @var string Label to use for the submit button */ + protected $submitButtonLabel; + + /** @var bool Whether to render the create and show submit button (is only used from DB Web's object detail) */ + protected $renderCreateAndShowButton = false; + + /** + * Create a new form instance with the given report id + * + * @param $id + * + * @return static + */ + public static function fromId($id): self { - $this->id = $id; + $form = new static(); + $form->id = $id; + + return $form; + } + + public function getId(): ?int + { + return $this->id; + } + + /** + * Set the label of the submit button + * + * @param string $label + * + * @return $this + */ + public function setSubmitButtonLabel(string $label): self + { + $this->submitButtonLabel = $label; return $this; } - protected function assemble() + /** + * Get the label of the submit button + * + * @return string + */ + public function getSubmitButtonLabel(): string + { + if ($this->submitButtonLabel !== null) { + return $this->submitButtonLabel; + } + + return $this->id === null ? $this->translate('Create Report') : $this->translate('Update Report'); + } + + /** + * Set whether the create and show submit button should be rendered + * + * @param bool $renderCreateAndShowButton + * + * @return $this + */ + public function setRenderCreateAndShowButton(bool $renderCreateAndShowButton): self { - $this->setDefaultElementDecorator(new CompatDecorator()); + $this->renderCreateAndShowButton = $renderCreateAndShowButton; + return $this; + } + + public function hasBeenSubmitted(): bool + { + return $this->hasBeenSent() && ( + $this->getPopulatedValue('submit') + || $this->getPopulatedValue('create_show') + || $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 report. It is used when exporting to pdf, json or csv format' + . ' and also when listing the reports in the cli' + ), + 'validators' => [ + 'Callback' => function ($value, CallbackValidator $validator) { + if ($value !== null && strpos($value, '..') !== false) { + $validator->addMessage( + $this->translate('Double dots are not allowed in the report name') + ); + + return false; + } + + return true; + } + ] ]); $this->addElement('select', 'timeframe', [ - 'required' => true, - 'label' => 'Timeframe', - 'options' => [null => 'Please choose'] + $this->listTimeframes(), - 'class' => 'autosubmit' + 'required' => true, + 'class' => 'autosubmit', + 'label' => $this->translate('Timeframe'), + 'options' => [null => $this->translate('Please choose')] + Database::listTimeframes(), + 'description' => $this->translate( + 'Specifies the time frame in which this report is to be generated' + ) ]); $this->addElement('select', 'template', [ - 'label' => 'Template', - 'options' => [null => 'Please choose'] + $this->listTemplates() + 'label' => $this->translate('Template'), + 'options' => [null => $this->translate('Please choose')] + Database::listTemplates(), + 'description' => $this->translate( + 'Specifies the template to use when exporting this report to pdf. (Default Icinga template)' + ) ]); $this->addElement('select', 'reportlet', [ - 'required' => true, - 'label' => 'Report', - 'options' => [null => 'Please choose'] + $this->listReports(), - 'class' => 'autosubmit' + 'required' => true, + 'class' => 'autosubmit', + 'label' => $this->translate('Report'), + 'options' => [null => $this->translate('Please choose')] + $this->listReports(), + 'description' => $this->translate('Specifies the type of the reportlet to be generated') ]); $values = $this->getValues(); @@ -63,7 +153,7 @@ class ReportForm extends CompatForm // $config->populate($this->getValues()); /** @var \Icinga\Module\Reporting\Hook\ReportHook $reportlet */ - $reportlet = new $values['reportlet']; + $reportlet = new $values['reportlet'](); $reportlet->initConfigForm($config); @@ -73,40 +163,43 @@ class ReportForm extends CompatForm } $this->addElement('submit', 'submit', [ - 'label' => $this->id === null ? 'Create Report' : 'Update Report' + 'label' => $this->getSubmitButtonLabel() ]); if ($this->id !== null) { /** @var FormSubmitElement $removeButton */ $removeButton = $this->createElement('submit', 'remove', [ - 'label' => 'Remove Report', + 'label' => $this->translate('Remove Report'), 'class' => 'btn-remove', 'formnovalidate' => true ]); $this->registerElement($removeButton); - $this->getElement('submit')->getWrapper()->prepend($removeButton); - - if ($removeButton->hasBeenPressed()) { - $this->getDb()->delete('report', ['id = ?' => $this->id]); - // Stupid cheat because ipl/html is not capable of multiple submit buttons - $this->getSubmitButton()->setValue($this->getSubmitButton()->getButtonLabel()); - $this->callOnSuccess = false; - $this->valid = true; + /** @var HtmlDocument $wrapper */ + $wrapper = $this->getElement('submit')->getWrapper(); + $wrapper->prepend($removeButton); + } elseif ($this->renderCreateAndShowButton) { + $createAndShow = $this->createElement('submit', 'create_show', [ + 'label' => $this->translate('Create and Show'), + ]); + $this->registerElement($createAndShow); - return; - } + /** @var HtmlDocument $wrapper */ + $wrapper = $this->getElement('submit')->getWrapper(); + $wrapper->prepend($createAndShow); } } public function onSuccess() { - if ($this->callOnSuccess === false) { + $db = Database::get(); + + if ($this->getPopulatedValue('remove')) { + $db->delete('report', ['id = ?' => $this->id]); + return; } - $db = $this->getDb(); - $values = $this->getValues(); $now = time() * 1000; @@ -155,14 +248,16 @@ class ReportForm extends CompatForm foreach ($values as $name => $value) { $db->insert('config', [ - 'reportlet_id' => $reportletId, - 'name' => $name, - 'value' => $value, - 'ctime' => $now, - 'mtime' => $now + 'reportlet_id' => $reportletId, + 'name' => $name, + 'value' => $value, + 'ctime' => $now, + 'mtime' => $now ]); } $db->commitTransaction(); + + $this->id = $reportId; } } |