From 6aecfe69591c51a91f9f8e389aa2644a5b71a75b Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 23 Sep 2015 13:45:33 +0200 Subject: [PATCH] Introduce base form class ActionForm refs #5600 --- .../forms/Navigation/ActionForm.php | 75 +++++++++++++++++++ .../forms/Navigation/HostActionForm.php | 24 +----- .../forms/Navigation/ServiceActionForm.php | 24 +----- 3 files changed, 77 insertions(+), 46 deletions(-) create mode 100644 modules/monitoring/application/forms/Navigation/ActionForm.php diff --git a/modules/monitoring/application/forms/Navigation/ActionForm.php b/modules/monitoring/application/forms/Navigation/ActionForm.php new file mode 100644 index 000000000..e712b254d --- /dev/null +++ b/modules/monitoring/application/forms/Navigation/ActionForm.php @@ -0,0 +1,75 @@ +addElement( + 'text', + 'filter', + array( + 'allowEmpty' => true, + 'label' => $this->translate('Filter'), + 'description' => $this->translate( + 'Display this action only for objects matching this filter. Leave it blank' + . ' if you want this action being displayed regardless of the object' + ) + ) + ); + } + + /** + * {@inheritdoc} + */ + public function isValid($formData) + { + if (! parent::isValid($formData)) { + return false; + } + + if (($filterString = $this->getValue('filter')) !== null) { + $filter = Filter::matchAll(); + $filter->setAllowedFilterColumns(array( + 'host_name', + 'hostgroup_name', + 'instance_name', + 'service_description', + 'servicegroup_name', + function ($c) { + return preg_match('/^_(?:host|service)_/', $c); + } + )); + + try { + $filter->addFilter(Filter::fromQueryString($filterString)); + } catch (QueryException $_) { + $this->getElement('filter')->addError(sprintf( + $this->translate('Invalid filter provided. You can only use the following columns: %s'), + implode(', ', array( + 'instance_name', + 'host_name', + 'hostgroup_name', + 'service_description', + 'servicegroup_name', + '_(host|service)_' + )) + )); + return false; + } + } + + return true; + } +} diff --git a/modules/monitoring/application/forms/Navigation/HostActionForm.php b/modules/monitoring/application/forms/Navigation/HostActionForm.php index 726d740d0..2e8bf32cc 100644 --- a/modules/monitoring/application/forms/Navigation/HostActionForm.php +++ b/modules/monitoring/application/forms/Navigation/HostActionForm.php @@ -3,28 +3,6 @@ namespace Icinga\Module\Monitoring\Forms\Navigation; -use Icinga\Forms\Navigation\NavigationItemForm; - -class HostActionForm extends NavigationItemForm +class HostActionForm extends ActionForm { - /** - * {@inheritdoc} - */ - public function createElements(array $formData) - { - parent::createElements($formData); - - $this->addElement( - 'text', - 'filter', - array( - 'allowEmpty' => true, - 'label' => $this->translate('Filter'), - 'description' => $this->translate( - 'Display this action only for hosts matching this filter. Leave' - . ' blank if you want this action being displayed for all hosts' - ) - ) - ); - } } diff --git a/modules/monitoring/application/forms/Navigation/ServiceActionForm.php b/modules/monitoring/application/forms/Navigation/ServiceActionForm.php index 6be89b0a3..3c9b37d5a 100644 --- a/modules/monitoring/application/forms/Navigation/ServiceActionForm.php +++ b/modules/monitoring/application/forms/Navigation/ServiceActionForm.php @@ -3,28 +3,6 @@ namespace Icinga\Module\Monitoring\Forms\Navigation; -use Icinga\Forms\Navigation\NavigationItemForm; - -class ServiceActionForm extends NavigationItemForm +class ServiceActionForm extends ActionForm { - /** - * {@inheritdoc} - */ - public function createElements(array $formData) - { - parent::createElements($formData); - - $this->addElement( - 'text', - 'filter', - array( - 'allowEmpty' => true, - 'label' => $this->translate('Filter'), - 'description' => $this->translate( - 'Display this action only for services matching this filter. Leave' - . ' blank if you want this action being displayed for all services' - ) - ) - ); - } }