From ad27ab206d8c9ef8cea635c34aadd6e0ac65e8db Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 8 Jun 2015 14:39:12 +0200 Subject: [PATCH] IcingaCommandArgumentForm: detect custom variables --- .../forms/IcingaCommandArgumentForm.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/application/forms/IcingaCommandArgumentForm.php b/application/forms/IcingaCommandArgumentForm.php index 6619207f..b83cbad3 100644 --- a/application/forms/IcingaCommandArgumentForm.php +++ b/application/forms/IcingaCommandArgumentForm.php @@ -27,6 +27,19 @@ class IcingaCommandArgumentForm extends DirectorObjectForm $this->addHidden('value_format', 'string'); // expression, json? + } + + + protected function beforeValidation($data = array()) + { + if (isset($data['argument_value']) && $value = $data['argument_value']) { + if (preg_match_all('/\$([a-z0-9_]+)\$/', $value, $m, PREG_PATTERN_ORDER)) { + foreach ($m[1] as $var) { + $this->addCustomVariable($var); + } + } + } + /* $this->optionalBoolean( 'required', @@ -36,4 +49,22 @@ class IcingaCommandArgumentForm extends DirectorObjectForm */ $this->addElement('submit', $this->translate('Store')); } + + protected function addCustomVariable($varname) + { + $a = new \Zend_Form_SubForm(); + $a->addElement('note', 'title', array( + 'label' => sprintf($this->translate('Custom Variable "%s"'), $varname), + )); + + $a->addElement('text', 'description', array( + 'label' => $this->translate('Description'), + 'required' => true, + )); + + $a->addElement('text', 'default_value', array( + 'label' => $this->translate('Default value'), + )); + $this->addSubForm($a, 'cv_' . $varname); + } }