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

127 lines
4.0 KiB
PHP
Raw Normal View History

2015-12-10 13:19:16 +01:00
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Acl;
use Icinga\Module\Director\Objects\IcingaHost;
2015-12-10 13:19:16 +01:00
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet;
2017-07-25 10:16:15 +02:00
use Icinga\Module\Director\Web\Form\DirectorForm;
2015-12-10 13:19:16 +01:00
2017-07-25 10:16:15 +02:00
class IcingaCloneObjectForm extends DirectorForm
2015-12-10 13:19:16 +01:00
{
2016-11-01 18:28:36 +01:00
/** @var IcingaObject */
2015-12-10 13:19:16 +01:00
protected $object;
public function setup()
{
2017-07-25 10:16:15 +02:00
$name = $this->object->getObjectName();
2015-12-10 13:19:16 +01:00
$this->addElement('text', 'new_object_name', array(
'label' => $this->translate('New name'),
2015-12-10 13:19:16 +01:00
'required' => true,
2017-07-25 10:16:15 +02:00
'value' => $name,
2015-12-10 13:19:16 +01:00
));
if (Acl::instance()->hasPermission('director/admin')) {
$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'),
)
));
}
if ($this->object instanceof IcingaHost) {
$this->addBoolean('clone_services', [
'label' => $this->translate('Clone Services'),
'description' => $this->translate(
'Also clone single Services defined for this Host'
)
], 'y');
$this->addBoolean('clone_service_sets', [
'label' => $this->translate('Clone Service Sets'),
'description' => $this->translate(
'Also clone single Service Sets defined for this Host'
)
], 'y');
}
2015-12-10 13:19:16 +01:00
$this->submitLabel = sprintf(
$this->translate('Clone "%s"'),
2017-07-25 10:16:15 +02:00
$name
2015-12-10 13:19:16 +01:00
);
}
public function onSuccess()
{
$object = $this->object;
$connection = $object->getConnection();
2015-12-10 13:19:16 +01:00
$newname = $this->getValue('new_object_name');
$resolve = Acl::instance()->hasPermission('director/admin')
&& $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,
2016-11-01 18:28:36 +01:00
$object->getObjectName()
2015-12-10 13:19:16 +01:00
);
$new = $object::fromPlainObject(
2016-02-26 12:42:21 +01:00
$object->toPlainObject($resolve),
$connection
2015-12-10 13:19:16 +01:00
)->set('object_name', $newname);
if ($new->isExternal()) {
$new->set('object_type', 'object');
}
if ($object instanceof IcingaHost) {
$new->set('api_key', null);
if ($this->getValue('clone_services') === 'y') {
$services = $object->fetchServices();
} else {
$services = [];
}
if ($this->getValue('clone_service_sets') === 'y') {
$sets = $object->fetchServiceSets();
} else {
$sets = [];
}
} else {
$services = [];
$sets = [];
}
2015-12-10 13:19:16 +01:00
if ($new->store()) {
foreach ($services as $service) {
IcingaService::fromPlainObject(
$service->toPlainObject(),
$connection
)->set('host_id', $new->get('id'))->store();
}
foreach ($sets as $set) {
IcingaServiceSet::fromPlainObject(
$set->toPlainObject(),
$connection
)->set('host_id', $new->get('id'))->store();
}
$this->setSuccessUrl(
'director/' . strtolower($object->getShortTableName()),
$new->getUrlParams()
);
2015-12-10 13:19:16 +01:00
$this->redirectOnSuccess($msg);
}
}
public function setObject(IcingaObject $object)
{
$this->object = $object;
return $this;
}
}