2015-12-10 13:19:16 +01:00
|
|
|
<?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,
|
2016-01-19 16:43:26 +01:00
|
|
|
'value' => $this->object->object_name,
|
2015-12-10 13:19:16 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
$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';
|
2016-01-19 16:43:26 +01:00
|
|
|
|
2015-12-10 13:19:16 +01:00
|
|
|
$msg = sprintf(
|
2016-02-26 12:42:21 +01:00
|
|
|
'The %s "%s" has been cloned from "%s"',
|
2015-12-10 13:19:16 +01:00
|
|
|
$object->getShortTableName(),
|
|
|
|
$newname,
|
|
|
|
$object->object_name
|
|
|
|
);
|
|
|
|
|
|
|
|
$new = $object::fromPlainObject(
|
2016-02-26 12:42:21 +01:00
|
|
|
$object->toPlainObject($resolve),
|
|
|
|
$object->getConnection()
|
2015-12-10 13:19:16 +01:00
|
|
|
)->set('object_name', $newname);
|
|
|
|
|
2016-03-20 11:38:19 +01:00
|
|
|
$this->setSuccessUrl(
|
|
|
|
'director/' . strtolower($object->getShortTableName()),
|
|
|
|
$new->getUrlParams()
|
|
|
|
);
|
|
|
|
|
2015-12-10 13:19:16 +01:00
|
|
|
if ($new->store()) {
|
|
|
|
$this->redirectOnSuccess($msg);
|
|
|
|
} else {
|
|
|
|
$this->redirectOnFailure($msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setObject(IcingaObject $object)
|
|
|
|
{
|
|
|
|
$this->object = $object;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|