From 547f49f212c8c2e0cf75153835a047ab318eb49d Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Mon, 15 Aug 2022 10:10:11 +0200 Subject: [PATCH 01/19] Fetch all available Reportlets --- library/Reporting/Report.php | 42 ++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/library/Reporting/Report.php b/library/Reporting/Report.php index cecc4ec7..fe496c7c 100644 --- a/library/Reporting/Report.php +++ b/library/Reporting/Report.php @@ -72,37 +72,41 @@ public static function fromDb($id) ->columns('*') ->where(['report_id = ?' => $id]); - $row = $db->select($select)->fetch(); + $row = $db->select($select)->fetchAll(); if ($row === false) { throw new Exception('No reportlets configured.'); } - $reportlet = new Reportlet(); + $reportlets = []; + foreach ($row as $reportletRow) { + $reportlet = new Reportlet(); - $reportlet - ->setId($row->id) - ->setClass($row->class); + $reportlet + ->setId($reportletRow->id) + ->setClass($reportletRow->class); - $select = (new Sql\Select()) - ->from('config') - ->columns('*') - ->where(['reportlet_id = ?' => $row->id]); + $select = (new Sql\Select()) + ->from('config') + ->columns('*') + ->where(['reportlet_id = ?' => $reportletRow->id]); - $rows = $db->select($select)->fetchAll(); + $rows = $db->select($select)->fetchAll(); - $config = [ - 'name' => $report->getName(), - 'id' => $report->getId() - ]; + $config = [ + 'name' => $report->getName(), + 'id' => $report->getId() + ]; - foreach ($rows as $row) { - $config[$row->name] = $row->value; - } + foreach ($rows as $row) { + $config[$row->name] = $row->value; + } - $reportlet->setConfig($config); + $reportlet->setConfig($config); - $report->setReportlets([$reportlet]); + $reportlets[] = $reportlet; + } + $report->setReportlets($reportlets); $select = (new Sql\Select()) ->from('schedule') From c40c5d5184271e69a14977c4db5164dbac941c92 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Mon, 15 Aug 2022 10:25:53 +0200 Subject: [PATCH 02/19] Add all Reportlets to the ReportForm --- application/controllers/ReportController.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/application/controllers/ReportController.php b/application/controllers/ReportController.php index c400ded8..1fd4fde9 100644 --- a/application/controllers/ReportController.php +++ b/application/controllers/ReportController.php @@ -15,6 +15,7 @@ use Icinga\Module\Reporting\Web\Forms\SendForm; use Icinga\Module\Reporting\Web\Widget\CompatDropdown; use ipl\Html\Error; +use ipl\Html\Form; use ipl\Web\Url; use ipl\Web\Widget\ActionBar; use Icinga\Util\Environment; @@ -59,12 +60,12 @@ public function editAction() 'timeframe' => (string) $this->report->getTimeframe()->getId(), ]; - $reportlet = $this->report->getReportlets()[0]; + foreach ($this->report->getReportlets() as $key => $reportlet) { + $values['reportlet'][$key + 1]['__class'] = $reportlet->getClass(); - $values['reportlet'] = $reportlet->getClass(); - - foreach ($reportlet->getConfig() as $name => $value) { - $values[$name] = $value; + foreach ($reportlet->getConfig() as $name => $value) { + $values['reportlet'][$key + 1][$name] = $value; + } } $form = ReportForm::fromId($this->report->getId()) From 70fc4475e254c28c052d72d075206f257a7dc5e1 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Mon, 15 Aug 2022 10:29:12 +0200 Subject: [PATCH 03/19] Generate dynamic Reportlet Inputs --- library/Reporting/Web/Forms/ReportForm.php | 226 +++++++++++++++++---- 1 file changed, 190 insertions(+), 36 deletions(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index 7be7bad6..d4700091 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -4,13 +4,20 @@ namespace Icinga\Module\Reporting\Web\Forms; +use Generator; use Icinga\Authentication\Auth; use Icinga\Module\Reporting\Database; +use Icinga\Module\Reporting\Hook\ReportHook; use Icinga\Module\Reporting\ProvidedReports; +use Icinga\Module\Reporting\Report; use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator; +use ipl\Html\Attributes; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\Form; +use ipl\Html\FormElement\BaseFormElement; +use ipl\Html\Html; use ipl\Web\Compat\CompatForm; +use ipl\Web\Widget\Icon; class ReportForm extends CompatForm { @@ -39,6 +46,23 @@ public function hasBeenSubmitted(): bool return $this->hasBeenSent() && ($this->getPopulatedValue('submit') || $this->getPopulatedValue('remove')); } + public function __construct() + { + $this->on(static::ON_SENT, function () { + if ($this->getPressedSubmitElement() && $this->getPressedSubmitElement()->getName() === 'remove') { + $this->getDb()->delete('report', ['id = ?' => $this->id]); + } + }); + } + + public static function fromReport(Report $report): ReportForm + { + $self = new static(); + $self->setId($report->getId()); + + return $self; + } + protected function assemble() { $this->setDefaultElementDecorator(new CompatDecorator()); @@ -77,8 +101,8 @@ protected function assemble() 'options' => [null => $this->translate('Please choose')] + $this->listReports(), 'description' => $this->translate('Specifies the type of the reportlet to be generated') ]); - - $values = $this->getValues(); + $reportlets = $this->getPopulatedValue('reportlet') ?? [['__class' => '']]; + $hasEmptyReportlet = false; if (isset($values['reportlet'])) { $config = new Form(); @@ -91,22 +115,96 @@ protected function assemble() foreach ($config->getElements() as $element) { $this->addElement($element); - } - } - - $this->addElement('submit', 'submit', [ - 'label' => $this->id === null ? $this->translate('Create Report') : $this->translate('Update Report') - ]); - if ($this->id !== null) { - /** @var FormSubmitElement $removeButton */ - $removeButton = $this->createElement('submit', 'remove', [ - 'label' => $this->translate('Remove Report'), - 'class' => 'btn-remove', - 'formnovalidate' => true - ]); - $this->registerElement($removeButton); - $this->getElement('submit')->getWrapper()->prepend($removeButton); + foreach ($reportlets as $reportlet) { + if (empty($reportlet['__class'])) { + $hasEmptyReportlet = true; + } + } + + if (! $hasEmptyReportlet) { + $reportlets[] = ['__class' => '']; + } + + foreach ($reportlets as $key => $reportlet) { + if (isset($this->ignoredReportletId) && $key === $this->ignoredReportletId) { + continue; + } + if ($this->getPopulatedValue('remove_report_' . $key) !== null && sizeof($reportlets) > 1) { + continue; + } + + $wrapper = Html::tag('div'); + if ($key !== array_keys($reportlets)[count($reportlets) - 1]) { + $wrapper->addAttributes(Attributes::create(['class' => 'reportlet-container'])); + } + + /** @var FormSubmitElement $select */ + $select = $this->createElement('select', "reportlet[$key][__class]", [ + 'required' => sizeof($reportlets) <= 1, + 'label' => 'Reportlet', + 'options' => [null => 'Please choose'] + $this->listReports(), + 'class' => 'autosubmit' + ]); + $this + ->registerElement($select) + ->decorate($select); + $wrapper->addHtml($select); + + if ($key !== array_keys($reportlets)[count($reportlets) - 1]) { + $remove = $this->createElement('submitButton', 'remove_report_' . $key, [ + 'label' => new Icon('trash'), + 'class' => 'btn-remove-reportlet', + 'formnovalidate' => true, + 'title' => 'Remove Reportlet' + ]); + $this->registerElement($remove); + + $select->getWrapper()->ensureAssembled()->add($remove); + } + + $values = $this->getValues(); + if (isset($values["reportlet[$key][__class]"])) { + $config = new Form(); + + /** @var ReportHook $reportlet */ + $reportlet = new $values["reportlet[$key][__class]"]; + $reportlet->initConfigForm($config); + + foreach ($config->getElements() as $element) { + /** @var $element BaseFormElement */ + $element + ->setName("reportlet[$key][" . $element->getName() . "]") + ->setAttribute('class', 'tte'); + + $this + ->registerElement($element) + ->decorate($element); + + + $wrapper->addHtml($element); + } + } + + $this->addHtml($wrapper); + } + + $this->addElement('submit', 'submit', [ + 'label' => $this->id === null ? 'Create Report' : 'Update Report' + ]); + $this->setSubmitButton($this->getElement('submit')); + + if ($this->id !== null) { + /** @var FormSubmitElement $removeButton */ + $removeButton = $this->createElement('submit', 'remove', [ + 'label' => 'Remove Report', + 'class' => 'btn-remove', + 'formnovalidate' => true + ]); + $this->registerElement($removeButton); + $this->getElement('submit')->getWrapper()->prepend($removeButton); + } + } } } @@ -148,34 +246,90 @@ public function onSuccess() $reportId = $this->id; } - unset($values['name']); - unset($values['timeframe']); - if ($this->id !== null) { $db->delete('reportlet', ['report_id = ?' => $reportId]); } - $db->insert('reportlet', [ - 'report_id' => $reportId, - 'class' => $values['reportlet'], - 'ctime' => $now, - 'mtime' => $now - ]); + foreach (array_filter($this->getPopulatedValue('reportlet')) as $reportlet) { + array_walk($reportlet, function (&$value) { + if ($value === '') { + $value = null; + } + }); + + if (empty($reportlet['__class'])) { + continue; + } + + unset($values['reportlet']); + + foreach ($values as $name => $value) { + $db->insert('config', [ + 'reportlet_id' => $reportletId, + 'name' => $name, + 'value' => $value, + 'ctime' => $now, + 'mtime' => $now + ]); + + $reportletId = $db->lastInsertId(); + + foreach ($reportlet as $key => $value) { + if ($key === '__class') { + continue; + } + + $db->insert('config', [ + 'reportlet_id' => $reportletId, + 'name' => $key, + 'value' => $value, + 'ctime' => $now, + 'mtime' => $now + ]); + } + } + } - $reportletId = $db->lastInsertId(); + $db->commitTransaction(); + } - unset($values['reportlet']); + public function populate($values) + { + $flatten = []; foreach ($values as $name => $value) { - $db->insert('config', [ - 'reportlet_id' => $reportletId, - 'name' => $name, - 'value' => $value, - 'ctime' => $now, - 'mtime' => $now - ]); + if (is_array($value)) { + foreach ($this->flattenArray($name, $value) as $flattenKey => $flattenValue) { + $flatten[$flattenKey] = $flattenValue; + } + } + + $flatten[$name] = $value; } - $db->commitTransaction(); + parent::populate($flatten); + + return $this; + } + + /** + * Returns a generator containing the $values represented as strings + * + * @param $key + * @param array $values + * + * @return Generator + */ + + protected function flattenArray($key, array $values): Generator + { + foreach ($values as $_key => $_value) { + $effectiveKey = sprintf('%s[%s]', $key, $_key); + if (is_array($_value)) { + yield from $this->flattenArray($effectiveKey, $_value); + } else { + yield sprintf("%s", $effectiveKey) => $_value; + } + } } } From 00c7c87388b6b8bed0a97c429a968b91ebd21432 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Mon, 15 Aug 2022 10:30:16 +0200 Subject: [PATCH 04/19] Add styles to remove reportlet button and a border for reportlet inputs --- public/css/module.less | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/public/css/module.less b/public/css/module.less index 49cc4e5c..bf79259e 100644 --- a/public/css/module.less +++ b/public/css/module.less @@ -76,7 +76,7 @@ &:hover > .dropdown-menu { display: block; - box-shadow: 0 0 2em 0 rgba(0,0,0,.2); + box-shadow: 0 0 2em 0 rgba(0, 0, 0, .2); } .dropdown-item { @@ -110,7 +110,7 @@ .page-size-a4 { background-color: white; - box-shadow: 0 0 0.25cm rgba(0,0,0,0.5); + box-shadow: 0 0 0.25cm rgba(0, 0, 0, 0.5); display: block; margin: 0 auto 0.5cm; font-family: @font-family-print; @@ -213,3 +213,20 @@ form.icinga-form { } /* Form fallback styles end */ + +.reportlet-container { + border-bottom: 1px solid @gray-lighter; + + .btn-remove-reportlet { + background-color: @low-sat-blue !important; + color: @color-critical !important; + border: 0; + height: 27px; + width: 27px; + padding: 8px; + + &:hover { + background-color: @low-sat-blue-dark !important; + } + } +} From afda0b121fa375342e8488fd1fc67298d896a2bf Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Fri, 26 Aug 2022 11:38:58 +0200 Subject: [PATCH 05/19] Changed name of Accessed Values for Reportlets --- application/controllers/ReportController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/controllers/ReportController.php b/application/controllers/ReportController.php index 1fd4fde9..420c8a6a 100644 --- a/application/controllers/ReportController.php +++ b/application/controllers/ReportController.php @@ -61,10 +61,10 @@ public function editAction() ]; foreach ($this->report->getReportlets() as $key => $reportlet) { - $values['reportlet'][$key + 1]['__class'] = $reportlet->getClass(); + $values['reportlet'][$key]['reportlet'] = $reportlet->getClass(); foreach ($reportlet->getConfig() as $name => $value) { - $values['reportlet'][$key + 1][$name] = $value; + $values['reportlet'][$key][$name] = $value; } } From bea0a526ee18f7d43694ddd0462a3732b4728b5e Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Fri, 26 Aug 2022 11:39:38 +0200 Subject: [PATCH 06/19] Refactor ReportForm to use Collection --- library/Reporting/Web/Forms/ReportForm.php | 251 ++++++++------------- 1 file changed, 93 insertions(+), 158 deletions(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index d4700091..13e08cf0 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -4,19 +4,17 @@ namespace Icinga\Module\Reporting\Web\Forms; -use Generator; use Icinga\Authentication\Auth; use Icinga\Module\Reporting\Database; use Icinga\Module\Reporting\Hook\ReportHook; use Icinga\Module\Reporting\ProvidedReports; use Icinga\Module\Reporting\Report; use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator; -use ipl\Html\Attributes; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\Form; -use ipl\Html\FormElement\BaseFormElement; -use ipl\Html\Html; +use ipl\Html\FormElement\Collection; use ipl\Web\Compat\CompatForm; +use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; class ReportForm extends CompatForm @@ -94,117 +92,81 @@ protected function assemble() ) ]); - $this->addElement('select', 'reportlet', [ - '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') + $collection = new Collection('reportlet'); + $collection->setLabel('Reportlets'); + $collection->setAddTrigger('select', 'reportlet', [ + 'required' => false, + 'label' => 'Reportlet', + 'options' => [null => 'Please choose'] + $this->listReports(), + 'class' => 'autosubmit' ]); - $reportlets = $this->getPopulatedValue('reportlet') ?? [['__class' => '']]; - $hasEmptyReportlet = false; - - if (isset($values['reportlet'])) { - $config = new Form(); -// $config->populate($this->getValues()); - - /** @var \Icinga\Module\Reporting\Hook\ReportHook $reportlet */ - $reportlet = new $values['reportlet'](); - - $reportlet->initConfigForm($config); - - foreach ($config->getElements() as $element) { - $this->addElement($element); - - foreach ($reportlets as $reportlet) { - if (empty($reportlet['__class'])) { - $hasEmptyReportlet = true; - } - } + $collection->setRemoveTrigger('submitButton', 'remove_reportlet', [ + 'label' => new Icon('trash'), + 'class' => 'btn-remove-reportlet', + 'formnovalidate' => true, + 'title' => 'Remove Reportlet' + ]); + $collection->on(Collection::ON_LOAD, function ($group, $addElement, $removeElement) { + $group->setDefaultElementDecorator(new IcingaFormDecorator()); - if (! $hasEmptyReportlet) { - $reportlets[] = ['__class' => '']; - } + $this->decorate($addElement); - foreach ($reportlets as $key => $reportlet) { - if (isset($this->ignoredReportletId) && $key === $this->ignoredReportletId) { - continue; - } - if ($this->getPopulatedValue('remove_report_' . $key) !== null && sizeof($reportlets) > 1) { - continue; - } + $group + ->registerElement($addElement) + ->addHtml($addElement); - $wrapper = Html::tag('div'); - if ($key !== array_keys($reportlets)[count($reportlets) - 1]) { - $wrapper->addAttributes(Attributes::create(['class' => 'reportlet-container'])); - } + $addElement->getWrapper()->ensureAssembled()->add($removeElement); - /** @var FormSubmitElement $select */ - $select = $this->createElement('select', "reportlet[$key][__class]", [ - 'required' => sizeof($reportlets) <= 1, - 'label' => 'Reportlet', - 'options' => [null => 'Please choose'] + $this->listReports(), - 'class' => 'autosubmit' - ]); - $this - ->registerElement($select) - ->decorate($select); - $wrapper->addHtml($select); - - if ($key !== array_keys($reportlets)[count($reportlets) - 1]) { - $remove = $this->createElement('submitButton', 'remove_report_' . $key, [ - 'label' => new Icon('trash'), - 'class' => 'btn-remove-reportlet', - 'formnovalidate' => true, - 'title' => 'Remove Reportlet' - ]); - $this->registerElement($remove); - - $select->getWrapper()->ensureAssembled()->add($remove); - } + $innerCollection = new Collection('inner'); + $innerCollection->setLabel('Inner Collection'); + $innerCollection->setAddTrigger('select', 'reportlet', [ + 'required' => false, + 'label' => 'Reportlet', + 'options' => [null => 'Please choose'], + 'class' => 'autosubmit' + ]); + $innerCollection->on(Collection::ON_LOAD, function ($group, $addElement) { + $group->setDefaultElementDecorator(new IcingaFormDecorator()); + $group->addElement($addElement); + }); - $values = $this->getValues(); - if (isset($values["reportlet[$key][__class]"])) { - $config = new Form(); + $group->registerElement($innerCollection); + $group->addHtml($innerCollection); - /** @var ReportHook $reportlet */ - $reportlet = new $values["reportlet[$key][__class]"]; - $reportlet->initConfigForm($config); + if (isset($items['reportlet']) && ! empty($items['reportlet'])) { + $config = new Form(); - foreach ($config->getElements() as $element) { - /** @var $element BaseFormElement */ - $element - ->setName("reportlet[$key][" . $element->getName() . "]") - ->setAttribute('class', 'tte'); + /** @var ReportHook $reportlet */ + $reportlet = new $items['reportlet']; + $reportlet->initConfigForm($config); - $this - ->registerElement($element) - ->decorate($element); + foreach ($config->getElements() as $element) { + $this->decorate($element); + $group + ->registerElement($element) + ->addHtml($element); + } + } + }); - $wrapper->addHtml($element); - } - } + $this->registerElement($collection); + $this->add($collection); - $this->addHtml($wrapper); - } + $this->addElement('submit', 'submit', [ + 'label' => $this->id === null ? 'Create Report' : 'Update Report' + ]); + $this->setSubmitButton($this->getElement('submit')); - $this->addElement('submit', 'submit', [ - 'label' => $this->id === null ? 'Create Report' : 'Update Report' - ]); - $this->setSubmitButton($this->getElement('submit')); - - if ($this->id !== null) { - /** @var FormSubmitElement $removeButton */ - $removeButton = $this->createElement('submit', 'remove', [ - 'label' => 'Remove Report', - 'class' => 'btn-remove', - 'formnovalidate' => true - ]); - $this->registerElement($removeButton); - $this->getElement('submit')->getWrapper()->prepend($removeButton); - } - } + if ($this->id !== null) { + /** @var FormSubmitElement $removeButton */ + $removeButton = $this->createElement('submit', 'remove', [ + 'label' => 'Remove Report', + 'class' => 'btn-remove', + 'formnovalidate' => true + ]); + $this->registerElement($removeButton); + $this->getElement('submit')->getWrapper()->prepend($removeButton); } } @@ -250,86 +212,59 @@ public function onSuccess() $db->delete('reportlet', ['report_id = ?' => $reportId]); } - foreach (array_filter($this->getPopulatedValue('reportlet')) as $reportlet) { + foreach ($values['reportlet'] as $reportlet) { array_walk($reportlet, function (&$value) { if ($value === '') { $value = null; } }); - if (empty($reportlet['__class'])) { + if (empty($reportlet['reportlet'])) { continue; } - unset($values['reportlet']); - - foreach ($values as $name => $value) { - $db->insert('config', [ - 'reportlet_id' => $reportletId, - 'name' => $name, - 'value' => $value, - 'ctime' => $now, - 'mtime' => $now - ]); + $db->insert('reportlet', [ + 'report_id' => $reportId, + 'class' => $reportlet['reportlet'], + 'ctime' => $now, + 'mtime' => $now + ]); - $reportletId = $db->lastInsertId(); + $reportletId = $db->lastInsertId(); - foreach ($reportlet as $key => $value) { - if ($key === '__class') { - continue; - } + foreach ($reportlet as $key => $value) { + if ($key === 'reportlet') { + continue; + } + foreach ($values as $name => $value) { $db->insert('config', [ 'reportlet_id' => $reportletId, - 'name' => $key, + 'name' => $name, 'value' => $value, 'ctime' => $now, 'mtime' => $now ]); - } - } - } - $db->commitTransaction(); - } + $reportletId = $db->lastInsertId(); - public function populate($values) - { - $flatten = []; + foreach ($reportlet as $key => $value) { + if ($key === '__class') { + continue; + } - foreach ($values as $name => $value) { - if (is_array($value)) { - foreach ($this->flattenArray($name, $value) as $flattenKey => $flattenValue) { - $flatten[$flattenKey] = $flattenValue; + $db->insert('config', [ + 'reportlet_id' => $reportletId, + 'name' => $key, + 'value' => $value, + 'ctime' => $now, + 'mtime' => $now + ]); + } } } - - $flatten[$name] = $value; } - parent::populate($flatten); - - return $this; - } - - /** - * Returns a generator containing the $values represented as strings - * - * @param $key - * @param array $values - * - * @return Generator - */ - - protected function flattenArray($key, array $values): Generator - { - foreach ($values as $_key => $_value) { - $effectiveKey = sprintf('%s[%s]', $key, $_key); - if (is_array($_value)) { - yield from $this->flattenArray($effectiveKey, $_value); - } else { - yield sprintf("%s", $effectiveKey) => $_value; - } - } + $db->commitTransaction(); } } From fd111d89354b3af98c8fcdb4a304a174ce1b8aa9 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Fri, 26 Aug 2022 11:40:21 +0200 Subject: [PATCH 07/19] Added styles for Reportlet legend header and ignore last Element Border --- public/css/module.less | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/public/css/module.less b/public/css/module.less index bf79259e..6a508aed 100644 --- a/public/css/module.less +++ b/public/css/module.less @@ -214,19 +214,31 @@ form.icinga-form { /* Form fallback styles end */ -.reportlet-container { - border-bottom: 1px solid @gray-lighter; - - .btn-remove-reportlet { - background-color: @low-sat-blue !important; - color: @color-critical !important; - border: 0; - height: 27px; - width: 27px; - padding: 8px; - - &:hover { - background-color: @low-sat-blue-dark !important; +.reportlet { + legend { + width: 100%; + border-bottom: 1px solid @gray-lighter; + } + + .reportlet-container { + border-bottom: 1px solid @gray-lighter; + + .btn-remove-reportlet { + background-color: @low-sat-blue !important; + color: @color-critical !important; + border: 0; + height: 27px; + width: 27px; + padding: 8px; + + &:hover { + background-color: @low-sat-blue-dark !important; + } + } + + &:last-child { + border-bottom: 0; } } } + From daf04124c9740be8e1c78139880c90f2b01c63f0 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Wed, 14 Sep 2022 17:02:53 +0200 Subject: [PATCH 08/19] Remove innerCollection and fix API changes in Collection --- library/Reporting/Web/Forms/ReportForm.php | 53 +++++++++------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index 13e08cf0..ed6b7ccc 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -13,6 +13,7 @@ use ipl\Html\Contract\FormSubmitElement; use ipl\Html\Form; use ipl\Html\FormElement\Collection; +use ipl\Html\FormElement\Fieldset; use ipl\Web\Compat\CompatForm; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; @@ -94,19 +95,23 @@ protected function assemble() $collection = new Collection('reportlet'); $collection->setLabel('Reportlets'); - $collection->setAddTrigger('select', 'reportlet', [ - 'required' => false, - 'label' => 'Reportlet', - 'options' => [null => 'Please choose'] + $this->listReports(), - 'class' => 'autosubmit' - ]); - $collection->setRemoveTrigger('submitButton', 'remove_reportlet', [ - 'label' => new Icon('trash'), - 'class' => 'btn-remove-reportlet', - 'formnovalidate' => true, - 'title' => 'Remove Reportlet' - ]); - $collection->on(Collection::ON_LOAD, function ($group, $addElement, $removeElement) { + + + $collection + ->setAddElement('select', 'reportlet', [ + 'required' => false, + 'label' => 'Reportlet', + 'options' => [null => 'Please choose'] + $this->listReports(), + 'class' => 'autosubmit' + ]) + ->setRemoveElement('submitButton', 'remove_reportlet', [ + 'label' => new Icon('trash'), + 'class' => 'btn-remove-reportlet', + 'formnovalidate' => true, + 'title' => 'Remove Reportlet' + ]); + + $collection->onAssembleGroup(function (/** @var Fieldset $group */ $group, $addElement, $removeElement) { $group->setDefaultElementDecorator(new IcingaFormDecorator()); $this->decorate($addElement); @@ -115,29 +120,15 @@ protected function assemble() ->registerElement($addElement) ->addHtml($addElement); + $group->registerElement($removeElement); $addElement->getWrapper()->ensureAssembled()->add($removeElement); - $innerCollection = new Collection('inner'); - $innerCollection->setLabel('Inner Collection'); - $innerCollection->setAddTrigger('select', 'reportlet', [ - 'required' => false, - 'label' => 'Reportlet', - 'options' => [null => 'Please choose'], - 'class' => 'autosubmit' - ]); - $innerCollection->on(Collection::ON_LOAD, function ($group, $addElement) { - $group->setDefaultElementDecorator(new IcingaFormDecorator()); - $group->addElement($addElement); - }); - - $group->registerElement($innerCollection); - $group->addHtml($innerCollection); - - if (isset($items['reportlet']) && ! empty($items['reportlet'])) { + $reportletClass = $group->getPopulatedValue('reportlet'); + if (! empty($reportletClass)) { $config = new Form(); /** @var ReportHook $reportlet */ - $reportlet = new $items['reportlet']; + $reportlet = new $reportletClass(); $reportlet->initConfigForm($config); foreach ($config->getElements() as $element) { From dd691fa8b376e260163f0a2832c6d47622ac2922 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Wed, 14 Sep 2022 17:23:22 +0200 Subject: [PATCH 09/19] Change Reportlet classes because of chnages in Collection --- public/css/module.less | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/css/module.less b/public/css/module.less index 6a508aed..a5ac6d53 100644 --- a/public/css/module.less +++ b/public/css/module.less @@ -214,13 +214,13 @@ form.icinga-form { /* Form fallback styles end */ -.reportlet { +.collection { legend { width: 100%; border-bottom: 1px solid @gray-lighter; } - .reportlet-container { + .form-element-collection { border-bottom: 1px solid @gray-lighter; .btn-remove-reportlet { From 5450b928592c42d1573ebd9eab9aa7c8fc8eafaf Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Tue, 20 Sep 2022 14:32:16 +0200 Subject: [PATCH 10/19] Fix Reportlets not inserting properly --- library/Reporting/Web/Forms/ReportForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index ed6b7ccc..feaa10f3 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -203,7 +203,7 @@ public function onSuccess() $db->delete('reportlet', ['report_id = ?' => $reportId]); } - foreach ($values['reportlet'] as $reportlet) { + foreach ($this->getPopulatedValue('reportlet') as $reportlet) { array_walk($reportlet, function (&$value) { if ($value === '') { $value = null; From 474879891c4539896798c0ed0187f4cdb34e8566 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Thu, 22 Dec 2022 17:42:07 +0100 Subject: [PATCH 11/19] ReportForm: Pass elements into setAddElement and setRemoveElement --- library/Reporting/Web/Forms/ReportForm.php | 33 +++++++++++++--------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index feaa10f3..0635da08 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -96,22 +96,29 @@ protected function assemble() $collection = new Collection('reportlet'); $collection->setLabel('Reportlets'); + $addElement = $this->createElement('select', 'reportlet', [ + 'required' => false, + 'label' => 'Reportlet', + 'options' => [null => 'Please choose'] + $this->listReports(), + 'class' => 'autosubmit' + ]); + $this->registerElement($addElement); + $this->decorate($addElement); + + $removeElement = $this->createElement('submitButton', 'remove_reportlet', [ + 'label' => new Icon('trash'), + 'class' => 'btn-remove-reportlet', + 'formnovalidate' => true, + 'title' => 'Remove Reportlet' + ]); + $this->registerElement($removeElement); + $this->decorate($removeElement); $collection - ->setAddElement('select', 'reportlet', [ - 'required' => false, - 'label' => 'Reportlet', - 'options' => [null => 'Please choose'] + $this->listReports(), - 'class' => 'autosubmit' - ]) - ->setRemoveElement('submitButton', 'remove_reportlet', [ - 'label' => new Icon('trash'), - 'class' => 'btn-remove-reportlet', - 'formnovalidate' => true, - 'title' => 'Remove Reportlet' - ]); + ->setAddElement($addElement) + ->setRemoveElement($removeElement); - $collection->onAssembleGroup(function (/** @var Fieldset $group */ $group, $addElement, $removeElement) { + $collection->onAssembleGroup(function ($group, $addElement, $removeElement) { $group->setDefaultElementDecorator(new IcingaFormDecorator()); $this->decorate($addElement); From dc31aa921b4a09a4d03f9c0e0696ceb7cfa5eb95 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Fri, 3 Feb 2023 15:47:56 +0100 Subject: [PATCH 12/19] ReportForm: Fix decorating and simplify collection creation --- library/Reporting/Web/Forms/ReportForm.php | 38 ++++++++-------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index 0635da08..140470e2 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -93,30 +93,20 @@ protected function assemble() ) ]); - $collection = new Collection('reportlet'); - $collection->setLabel('Reportlets'); - - $addElement = $this->createElement('select', 'reportlet', [ - 'required' => false, - 'label' => 'Reportlet', - 'options' => [null => 'Please choose'] + $this->listReports(), - 'class' => 'autosubmit' - ]); - $this->registerElement($addElement); - $this->decorate($addElement); - - $removeElement = $this->createElement('submitButton', 'remove_reportlet', [ - 'label' => new Icon('trash'), - 'class' => 'btn-remove-reportlet', - 'formnovalidate' => true, - 'title' => 'Remove Reportlet' - ]); - $this->registerElement($removeElement); - $this->decorate($removeElement); - - $collection - ->setAddElement($addElement) - ->setRemoveElement($removeElement); + $collection = (new Collection('reportlet')) + ->setLabel('Reportlets') + ->setAddElement($this->createElement('select', 'reportlet', [ + 'required' => false, + 'label' => 'Reportlet', + 'options' => [null => 'Please choose'] + $this->listReports(), + 'class' => 'autosubmit' + ])) + ->setRemoveElement($this->createElement('submitButton', 'remove_reportlet', [ + 'label' => new Icon('trash'), + 'class' => 'btn-remove-reportlet', + 'formnovalidate' => true, + 'title' => 'Remove Reportlet' + ])); $collection->onAssembleGroup(function ($group, $addElement, $removeElement) { $group->setDefaultElementDecorator(new IcingaFormDecorator()); From cb2a71f291ad3f7a673d619c562ca1d5968a0944 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Fri, 3 Feb 2023 17:00:13 +0100 Subject: [PATCH 13/19] ReportForm: Chain group calls --- library/Reporting/Web/Forms/ReportForm.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index 140470e2..ae2969e0 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -14,6 +14,8 @@ use ipl\Html\Form; use ipl\Html\FormElement\Collection; use ipl\Html\FormElement\Fieldset; +use ipl\Html\FormElement\SelectElement; +use ipl\Html\FormElement\SubmitElement; use ipl\Web\Compat\CompatForm; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; @@ -115,9 +117,9 @@ protected function assemble() $group ->registerElement($addElement) - ->addHtml($addElement); + ->addHtml($addElement) + ->registerElement($removeElement); - $group->registerElement($removeElement); $addElement->getWrapper()->ensureAssembled()->add($removeElement); $reportletClass = $group->getPopulatedValue('reportlet'); From 74fdd46439c75d7c36ba33bb495b9bc0af844a19 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Mon, 6 Feb 2023 16:10:03 +0100 Subject: [PATCH 14/19] ReportForm: Remove `fromReport()` --- library/Reporting/Web/Forms/ReportForm.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index ae2969e0..7f6c3995 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -8,14 +8,10 @@ use Icinga\Module\Reporting\Database; use Icinga\Module\Reporting\Hook\ReportHook; use Icinga\Module\Reporting\ProvidedReports; -use Icinga\Module\Reporting\Report; use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\Form; use ipl\Html\FormElement\Collection; -use ipl\Html\FormElement\Fieldset; -use ipl\Html\FormElement\SelectElement; -use ipl\Html\FormElement\SubmitElement; use ipl\Web\Compat\CompatForm; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; @@ -56,14 +52,6 @@ public function __construct() }); } - public static function fromReport(Report $report): ReportForm - { - $self = new static(); - $self->setId($report->getId()); - - return $self; - } - protected function assemble() { $this->setDefaultElementDecorator(new CompatDecorator()); From 66996a330345fa2ccb53c714dbc205542f7dd84f Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Mon, 6 Feb 2023 16:37:00 +0100 Subject: [PATCH 15/19] ReportForm: Change addElement name --- library/Reporting/Web/Forms/ReportForm.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index 7f6c3995..00379a20 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -12,6 +12,7 @@ use ipl\Html\Contract\FormSubmitElement; use ipl\Html\Form; use ipl\Html\FormElement\Collection; +use ipl\Html\FormElement\FieldsetElement; use ipl\Web\Compat\CompatForm; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; @@ -85,7 +86,7 @@ protected function assemble() $collection = (new Collection('reportlet')) ->setLabel('Reportlets') - ->setAddElement($this->createElement('select', 'reportlet', [ + ->setAddElement($this->createElement('select', 'add_reportlet', [ 'required' => false, 'label' => 'Reportlet', 'options' => [null => 'Please choose'] + $this->listReports(), From 50557d1f99b4fffaf4b59f399040cea8ba3e5196 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Tue, 7 Feb 2023 16:19:00 +0100 Subject: [PATCH 16/19] ReportForm: Fix reportlet name --- library/Reporting/Web/Forms/ReportForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index 00379a20..1eb19aa7 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -111,7 +111,7 @@ protected function assemble() $addElement->getWrapper()->ensureAssembled()->add($removeElement); - $reportletClass = $group->getPopulatedValue('reportlet'); + $reportletClass = $group->getPopulatedValue('add_reportlet'); if (! empty($reportletClass)) { $config = new Form(); From 632b1835ec8535da32f74d8e42e9f3eb6d1f9b4f Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Thu, 9 Feb 2023 11:20:59 +0100 Subject: [PATCH 17/19] ReportForm: Fix inserting data --- library/Reporting/Web/Forms/ReportForm.php | 43 ++++++---------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index 1eb19aa7..980e729a 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -12,7 +12,6 @@ use ipl\Html\Contract\FormSubmitElement; use ipl\Html\Form; use ipl\Html\FormElement\Collection; -use ipl\Html\FormElement\FieldsetElement; use ipl\Web\Compat\CompatForm; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; @@ -86,7 +85,7 @@ protected function assemble() $collection = (new Collection('reportlet')) ->setLabel('Reportlets') - ->setAddElement($this->createElement('select', 'add_reportlet', [ + ->setAddElement($this->createElement('select', 'reportlet_class', [ 'required' => false, 'label' => 'Reportlet', 'options' => [null => 'Please choose'] + $this->listReports(), @@ -111,7 +110,7 @@ protected function assemble() $addElement->getWrapper()->ensureAssembled()->add($removeElement); - $reportletClass = $group->getPopulatedValue('add_reportlet'); + $reportletClass = $group->getPopulatedValue('reportlet_class'); if (! empty($reportletClass)) { $config = new Form(); @@ -198,13 +197,13 @@ public function onSuccess() } }); - if (empty($reportlet['reportlet'])) { + if (empty($reportlet['reportlet_class'])) { continue; } $db->insert('reportlet', [ 'report_id' => $reportId, - 'class' => $reportlet['reportlet'], + 'class' => $reportlet['reportlet_class'], 'ctime' => $now, 'mtime' => $now ]); @@ -212,35 +211,17 @@ public function onSuccess() $reportletId = $db->lastInsertId(); foreach ($reportlet as $key => $value) { - if ($key === 'reportlet') { + if ($key === 'reportlet_class') { continue; } - foreach ($values as $name => $value) { - $db->insert('config', [ - 'reportlet_id' => $reportletId, - 'name' => $name, - 'value' => $value, - 'ctime' => $now, - 'mtime' => $now - ]); - - $reportletId = $db->lastInsertId(); - - foreach ($reportlet as $key => $value) { - if ($key === '__class') { - continue; - } - - $db->insert('config', [ - 'reportlet_id' => $reportletId, - 'name' => $key, - 'value' => $value, - 'ctime' => $now, - 'mtime' => $now - ]); - } - } + $db->insert('config', [ + 'reportlet_id' => $reportletId, + 'name' => $key, + 'value' => $value, + 'ctime' => $now, + 'mtime' => $now + ]); } } From 65e2ffdce410751aaceb14afd5ac175b744332da Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Thu, 9 Feb 2023 11:21:19 +0100 Subject: [PATCH 18/19] ReportController: Fix collection addElement key --- application/controllers/ReportController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/controllers/ReportController.php b/application/controllers/ReportController.php index 420c8a6a..2d03f18d 100644 --- a/application/controllers/ReportController.php +++ b/application/controllers/ReportController.php @@ -61,7 +61,7 @@ public function editAction() ]; foreach ($this->report->getReportlets() as $key => $reportlet) { - $values['reportlet'][$key]['reportlet'] = $reportlet->getClass(); + $values['reportlet'][$key]['reportlet_class'] = $reportlet->getClass(); foreach ($reportlet->getConfig() as $name => $value) { $values['reportlet'][$key][$name] = $value; From 3bd3b85137ac7fb7e2e2ce54f1f0627ed591aff5 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Tue, 14 Feb 2023 11:45:08 +0100 Subject: [PATCH 19/19] ReportForm: Fix `setAddElement()` and `setRemoveElement` calls --- library/Reporting/Web/Forms/ReportForm.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/Reporting/Web/Forms/ReportForm.php b/library/Reporting/Web/Forms/ReportForm.php index 980e729a..98bbc656 100644 --- a/library/Reporting/Web/Forms/ReportForm.php +++ b/library/Reporting/Web/Forms/ReportForm.php @@ -85,18 +85,18 @@ protected function assemble() $collection = (new Collection('reportlet')) ->setLabel('Reportlets') - ->setAddElement($this->createElement('select', 'reportlet_class', [ + ->setAddElement('select', 'reportlet_class', [ 'required' => false, 'label' => 'Reportlet', 'options' => [null => 'Please choose'] + $this->listReports(), 'class' => 'autosubmit' - ])) - ->setRemoveElement($this->createElement('submitButton', 'remove_reportlet', [ + ]) + ->setRemoveElement('submitButton', 'remove_reportlet', [ 'label' => new Icon('trash'), 'class' => 'btn-remove-reportlet', 'formnovalidate' => true, 'title' => 'Remove Reportlet' - ])); + ]); $collection->onAssembleGroup(function ($group, $addElement, $removeElement) { $group->setDefaultElementDecorator(new IcingaFormDecorator());