IcingaCloneObjectForm: clone sets with services...

...and fix redirection

fixes #852
This commit is contained in:
Thomas Gelf 2017-09-11 16:02:39 +02:00
parent e76665741e
commit 804008673a
2 changed files with 54 additions and 8 deletions

View File

@ -34,13 +34,18 @@ class IcingaCloneObjectForm extends DirectorForm
)); ));
} }
if ($this->object instanceof IcingaHost) { if ($this->object instanceof IcingaHost
|| $this->object instanceof IcingaServiceSet
) {
$this->addBoolean('clone_services', [ $this->addBoolean('clone_services', [
'label' => $this->translate('Clone Services'), 'label' => $this->translate('Clone Services'),
'description' => $this->translate( 'description' => $this->translate(
'Also clone single Services defined for this Host' 'Also clone single Services defined for this Host'
) )
], 'y'); ], 'y');
}
if ($this->object instanceof IcingaHost) {
$this->addBoolean('clone_service_sets', [ $this->addBoolean('clone_service_sets', [
'label' => $this->translate('Clone Service Sets'), 'label' => $this->translate('Clone Service Sets'),
'description' => $this->translate( 'description' => $this->translate(
@ -91,6 +96,13 @@ class IcingaCloneObjectForm extends DirectorForm
} else { } else {
$sets = []; $sets = [];
} }
} elseif ($object instanceof IcingaServiceSet) {
if ($this->getValue('clone_services') === 'y') {
$services = $object->fetchServices();
} else {
$services = [];
}
$sets = [];
} else { } else {
$services = []; $services = [];
$sets = []; $sets = [];
@ -98,21 +110,37 @@ class IcingaCloneObjectForm extends DirectorForm
if ($new->store()) { if ($new->store()) {
foreach ($services as $service) { foreach ($services as $service) {
IcingaService::fromPlainObject( $clone = IcingaService::fromPlainObject(
$service->toPlainObject(), $service->toPlainObject(),
$connection $connection
)->set('host_id', $new->get('id'))->store(); );
if ($new instanceof IcingaHost) {
$clone->set('host_id', $new->get('id'));
} elseif ($new instanceof IcingaServiceSet) {
$clone->set('service_set_id', $new->get('id'));
} }
$clone->store();
}
foreach ($sets as $set) { foreach ($sets as $set) {
IcingaServiceSet::fromPlainObject( IcingaServiceSet::fromPlainObject(
$set->toPlainObject(), $set->toPlainObject(),
$connection $connection
)->set('host_id', $new->get('id'))->store(); )->set('host_id', $new->get('id'))->store();
} }
if ($new instanceof IcingaServiceSet) {
$this->setSuccessUrl(
'director/serviceset',
$new->getUrlParams()
);
} else {
$this->setSuccessUrl( $this->setSuccessUrl(
'director/' . strtolower($object->getShortTableName()), 'director/' . strtolower($object->getShortTableName()),
$new->getUrlParams() $new->getUrlParams()
); );
}
$this->redirectOnSuccess($msg); $this->redirectOnSuccess($msg);
} }

View File

@ -262,6 +262,24 @@ class IcingaServiceSet extends IcingaObject
return $where; return $where;
} }
/**
* @return IcingaService[]
*/
public function fetchServices()
{
$connection = $this->getConnection();
$db = $connection->getDbAdapter();
/** @var IcingaService[] $services */
$services = IcingaService::loadAll(
$connection,
$db->select()->from('icinga_service')
->where('service_set_id = ?', $this->get('id'))
);
return $services;
}
protected function beforeStore() protected function beforeStore()
{ {
parent::beforeStore(); parent::beforeStore();