From 5fe976e059d4ee844eeac47bc71e5d0eda3ce161 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 11 Jun 2018 23:13:03 +0200 Subject: [PATCH] SyncRuleForm: initial implementation --- .../controllers/SyncruleController.php | 45 ++++++++++++ .../Director/Web/Form/CloneSyncRuleForm.php | 72 +++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 library/Director/Web/Form/CloneSyncRuleForm.php diff --git a/application/controllers/SyncruleController.php b/application/controllers/SyncruleController.php index cea78ba2..f96b0ef5 100644 --- a/application/controllers/SyncruleController.php +++ b/application/controllers/SyncruleController.php @@ -6,9 +6,11 @@ use Icinga\Module\Director\Forms\SyncCheckForm; use Icinga\Module\Director\Forms\SyncPropertyForm; use Icinga\Module\Director\Forms\SyncRuleForm; use Icinga\Module\Director\Forms\SyncRunForm; +use Icinga\Module\Director\Objects\SyncProperty; use Icinga\Module\Director\Web\Controller\ActionController; use Icinga\Module\Director\Objects\SyncRule; use Icinga\Module\Director\Objects\SyncRun; +use Icinga\Module\Director\Web\Form\CloneSyncRuleForm; use Icinga\Module\Director\Web\Table\SyncpropertyTable; use Icinga\Module\Director\Web\Table\SyncRunTable; use Icinga\Module\Director\Web\Tabs\SyncRuleTabs; @@ -166,6 +168,14 @@ class SyncruleController extends ActionController $this->translate('Sync rule: %s'), $rule->rule_name )); + $this->actions()->add( + Link::create( + $this->translate('Clone'), + 'director/syncrule/clone', + ['id' => $id], + ['class' => 'icon-paste'] + ) + ); if (! $rule->hasSyncProperties()) { $this->addPropertyHint($rule); @@ -179,6 +189,41 @@ class SyncruleController extends ActionController $this->content()->add($form); } + /** + * @throws \Icinga\Exception\ConfigurationError + * @throws \Icinga\Exception\Http\HttpNotFoundException + * @throws \Icinga\Exception\MissingParameterException + * @throws \Icinga\Exception\NotFoundError + * @throws \Icinga\Exception\ProgrammingError + */ + public function cloneAction() + { + $id = $this->params->getRequired('id'); + $rule = SyncRule::load($id, $this->db()); + $this->tabs()->add('show', [ + 'url' => 'director/syncrule', + 'urlParams' => ['id' => $id], + 'label' => $this->translate('Sync rule'), + ])->add('clone', [ + 'url' => 'director/syncrule/clone', + 'urlParams' => ['id' => $id], + 'label' => $this->translate('Clone'), + ])->activate('clone'); + $this->addTitle('Clone: %s', $rule->get('rule_name')); + $this->actions()->add( + Link::create( + $this->translate('Modify'), + 'director/syncrule/edit', + ['id' => $rule->get('id')], + ['class' => 'icon-paste'] + ) + ); + + $form = new CloneSyncRuleForm($rule); + $this->content()->add($form); + $form->handleRequest($this->getRequest()); + } + /** * @throws \Icinga\Exception\Http\HttpNotFoundException * @throws \Icinga\Exception\IcingaException diff --git a/library/Director/Web/Form/CloneSyncRuleForm.php b/library/Director/Web/Form/CloneSyncRuleForm.php new file mode 100644 index 00000000..79297433 --- /dev/null +++ b/library/Director/Web/Form/CloneSyncRuleForm.php @@ -0,0 +1,72 @@ +setDefaultElementDecorator(new DdDtDecorator()); + $this->rule = $rule; + } + + protected function assemble() + { + $this->addElement('rule_name', 'text', [ + 'label' => $this->translate('New name'), + 'value' => $this->rule->get('rule_name'), + ]); + $this->addElement('submit', 'submit', [ + 'label' => $this->translate('Clone') + ]); + + } + + /** + * @return \Icinga\Module\Director\Db + */ + protected function getTargetDb() + { + return $this->rule->getConnection(); + } + + /** + * @throws \Icinga\Exception\NotFoundError + * @throws \Icinga\Module\Director\Exception\DuplicateKeyException + */ + public function onSuccess() + { + $export = $this->rule->export(); + $newName = $this->getValue('rule_name'); + $export->rule_name = $newName; + if (SyncRule::existsWithName($newName, $this->rule->getConnection())) { + $this->getElement('rule_name')->addMessage('Do no'); + } + $this->newRule = SyncRule::import($export, $this->getTargetDb()); + $this->newRule->store(); + $this->redirectOnSuccess(); + } + + public function getSuccessUrl() + { + if ($this->newRule === null) { + return parent::getSuccessUrl(); + } else { + return Url::fromPath('director/syncrule', ['id' => $this->newRule->get('id')]); + } + } +}