SyncRuleForm: initial implementation

This commit is contained in:
Thomas Gelf 2018-06-11 23:13:03 +02:00
parent c587067a33
commit 5fe976e059
2 changed files with 117 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,72 @@
<?php
namespace Icinga\Module\Director\Web\Form;
use dipl\Html\Form;
use dipl\Html\FormDecorator\DdDtDecorator;
use dipl\Translation\TranslationHelper;
use dipl\Web\Url;
use Icinga\Module\Director\Objects\SyncRule;
class CloneSyncRuleForm extends Form
{
use TranslationHelper;
/** @var SyncRule */
protected $rule;
/** @var SyncRule|null */
protected $newRule;
public function __construct(SyncRule $rule)
{
$this->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')]);
}
}
}