icingaweb2-module-director/application/forms/IcingaCloneObjectForm.php

72 lines
1.9 KiB
PHP
Raw Normal View History

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' => $this->translate('New name'),
2015-12-10 13:19:16 +01:00
'required' => true,
'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';
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);
$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;
}
}