2018-06-11 23:13:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Form;
|
|
|
|
|
2019-05-02 13:23:06 +02:00
|
|
|
use ipl\Html\Form;
|
|
|
|
use ipl\Html\FormDecorator\DdDtDecorator;
|
|
|
|
use gipfl\Translation\TranslationHelper;
|
|
|
|
use gipfl\IcingaWeb2\Url;
|
2018-06-11 23:13:03 +02:00
|
|
|
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;
|
2018-11-07 11:03:38 +01:00
|
|
|
unset($export->originalId);
|
|
|
|
|
2018-06-14 13:41:13 +02:00
|
|
|
if (SyncRule::existsWithName($newName, $this->getTargetDb())) {
|
2018-06-12 21:45:29 +02:00
|
|
|
$this->getElement('rule_name')->addMessage('Name already exists');
|
2018-06-11 23:13:03 +02:00
|
|
|
}
|
|
|
|
$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')]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|