ObjectController: introduce clone form

This commit is contained in:
Thomas Gelf 2015-12-10 13:19:16 +01:00
parent 39532d7173
commit 20e0a6ca1f
2 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,63 @@
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Form\QuickForm;
class IcingaCloneObjectForm extends QuickForm
{
protected $object;
public function setup()
{
$this->addElement('text', 'new_object_name', array(
'label' => 'New name',
'required' => true,
));
$this->addElement('select', 'clone_type', array(
'label' => 'Clone type',
'required' => true,
'multiOptions' => array(
'equal' => $this->translate('Clone the object as is, preserving imports'),
'flat' => $this->translate('Flatten all inherited properties, strip imports'),
)
));
$this->submitLabel = sprintf(
$this->translate('Clone "%s"'),
$this->object->object_name
);
}
public function onSuccess()
{
$object = $this->object;
$newname = $this->getValue('new_object_name');
$resolve = $this->getValue('clone_type') === 'flat';
$msg = sprintf(
'The %s "%s" has been cloned from "%s"',
$object->getShortTableName(),
$newname,
$object->object_name
);
$new = $object::fromPlainObject(
$object->toPlainObject($resolve), $object->getConnection()
)->set('object_name', $newname);
if ($new->store()) {
$this->redirectOnSuccess($msg);
} else {
$this->redirectOnFailure($msg);
}
}
public function setObject(IcingaObject $object)
{
$this->object = $object;
return $this;
}
}

View File

@ -153,6 +153,23 @@ abstract class ObjectController extends ActionController
$this->render('object/form', null, true);
}
public function cloneAction()
{
$type = $this->getType();
$this->getTabs()->activate('modify');
$this->view->form = $form = $this->loadForm(
'icingaCloneObject'
)->setObject($this->object);
$this->view->title = sprintf(
$this->translate('Clone Icinga %s'),
ucfirst($type)
);
$this->view->form->handleRequest();
$this->render('object/form', null, true);
}
public function fieldsAction()
{
$object = $this->object;