mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-27 07:44:05 +02:00
CloneImportSource: one more cloning feature
This commit is contained in:
parent
7ffc64d9ed
commit
fc58e506ea
@ -6,6 +6,7 @@ use Icinga\Module\Director\Forms\ImportRowModifierForm;
|
||||
use Icinga\Module\Director\Forms\ImportSourceForm;
|
||||
use Icinga\Module\Director\Web\Controller\ActionController;
|
||||
use Icinga\Module\Director\Objects\ImportSource;
|
||||
use Icinga\Module\Director\Web\Form\CloneImportSourceForm;
|
||||
use Icinga\Module\Director\Web\Table\ImportrunTable;
|
||||
use Icinga\Module\Director\Web\Table\ImportsourceHookTable;
|
||||
use Icinga\Module\Director\Web\Table\PropertymodifierTable;
|
||||
@ -46,6 +47,17 @@ class ImportsourceController extends ActionController
|
||||
$this->translate('Import source: %s'),
|
||||
$source->get('source_name')
|
||||
)->setAutorefreshInterval(10);
|
||||
$this->actions()->add(
|
||||
Link::create(
|
||||
$this->translate('Download JSON'),
|
||||
$this->url()->with('format', 'json'),
|
||||
null,
|
||||
[
|
||||
'data-base-target' => '_blank',
|
||||
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$this->content()->add(new ImportSourceDetails($source));
|
||||
}
|
||||
@ -69,10 +81,19 @@ class ImportsourceController extends ActionController
|
||||
*/
|
||||
public function editAction()
|
||||
{
|
||||
$id = $this->params->getRequired('id');
|
||||
$form = ImportSourceForm::load()->setDb($this->db())
|
||||
->loadObject($this->params->getRequired('id'))
|
||||
->loadObject($id)
|
||||
->setListUrl('director/importsources')
|
||||
->handleRequest();
|
||||
$this->actions()->add(
|
||||
Link::create(
|
||||
$this->translate('Clone'),
|
||||
'director/importsource/clone',
|
||||
['id' => $id],
|
||||
['class' => 'icon-paste']
|
||||
)
|
||||
);
|
||||
$this->addTitle(
|
||||
$this->translate('Import source: %s'),
|
||||
$form->getObject()->get('source_name')
|
||||
@ -81,6 +102,41 @@ class ImportsourceController 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');
|
||||
$source = ImportSource::load($id, $this->db());
|
||||
$this->tabs()->add('show', [
|
||||
'url' => 'director/importsource',
|
||||
'urlParams' => ['id' => $id],
|
||||
'label' => $this->translate('Import Source'),
|
||||
])->add('clone', [
|
||||
'url' => 'director/importsource/clone',
|
||||
'urlParams' => ['id' => $id],
|
||||
'label' => $this->translate('Clone'),
|
||||
])->activate('clone');
|
||||
$this->addTitle('Clone: %s', $source->get('source_name'));
|
||||
$this->actions()->add(
|
||||
Link::create(
|
||||
$this->translate('Modify'),
|
||||
'director/importsource/edit',
|
||||
['id' => $source->get('id')],
|
||||
['class' => 'icon-paste']
|
||||
)
|
||||
);
|
||||
|
||||
$form = new CloneImportSourceForm($source);
|
||||
$this->content()->add($form);
|
||||
$form->handleRequest($this->getRequest());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Icinga\Exception\ConfigurationError
|
||||
* @throws \Icinga\Exception\MissingParameterException
|
||||
|
71
library/Director/Web/Form/CloneImportSourceForm.php
Normal file
71
library/Director/Web/Form/CloneImportSourceForm.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?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\ImportSource;
|
||||
|
||||
class CloneImportSourceForm extends Form
|
||||
{
|
||||
use TranslationHelper;
|
||||
|
||||
/** @var ImportSource */
|
||||
protected $source;
|
||||
|
||||
/** @var ImportSource|null */
|
||||
protected $newSource;
|
||||
|
||||
public function __construct(ImportSource $source)
|
||||
{
|
||||
$this->setDefaultElementDecorator(new DdDtDecorator());
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
protected function assemble()
|
||||
{
|
||||
$this->addElement('source_name', 'text', [
|
||||
'label' => $this->translate('New name'),
|
||||
'value' => $this->source->get('source_name'),
|
||||
]);
|
||||
$this->addElement('submit', 'submit', [
|
||||
'label' => $this->translate('Clone')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Icinga\Module\Director\Db
|
||||
*/
|
||||
protected function getTargetDb()
|
||||
{
|
||||
return $this->source->getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
|
||||
*/
|
||||
public function onSuccess()
|
||||
{
|
||||
$export = $this->source->export();
|
||||
$newName = $this->getValue('source_name');
|
||||
$export->source_name = $newName;
|
||||
|
||||
if (ImportSource::existsWithName($newName, $this->source->getConnection())) {
|
||||
$this->getElement('import_name')->addMessage('Name already exists');
|
||||
}
|
||||
$this->newSource = ImportSource::import($export, $this->getTargetDb());
|
||||
$this->newSource->store();
|
||||
$this->redirectOnSuccess();
|
||||
}
|
||||
|
||||
public function getSuccessUrl()
|
||||
{
|
||||
if ($this->newSource === null) {
|
||||
return parent::getSuccessUrl();
|
||||
} else {
|
||||
return Url::fromPath('director/importsource', ['id' => $this->newSource->get('id')]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user