IcingaCloneObjectForm: allow to clone fields

fixes #733
This commit is contained in:
Thomas Gelf 2017-10-08 21:49:31 +02:00
parent 3c082ae9e8
commit bc94f467ef
1 changed files with 36 additions and 17 deletions

View File

@ -54,6 +54,15 @@ class IcingaCloneObjectForm extends DirectorForm
], 'y'); ], 'y');
} }
if ($this->object->isTemplate() && $this->object->supportsFields()) {
$this->addBoolean('clone_fields', [
'label' => $this->translate('Clone Template Fields'),
'description' => $this->translate(
'Also clone fields provided by this Template'
)
], 'y');
}
$this->submitLabel = sprintf( $this->submitLabel = sprintf(
$this->translate('Clone "%s"'), $this->translate('Clone "%s"'),
$name $name
@ -63,52 +72,57 @@ class IcingaCloneObjectForm extends DirectorForm
public function onSuccess() public function onSuccess()
{ {
$object = $this->object; $object = $this->object;
$table = $object->getTableName();
$type = $object->getShortTableName();
$connection = $object->getConnection(); $connection = $object->getConnection();
$newname = $this->getValue('new_object_name'); $db = $connection->getDbAdapter();
$newName = $this->getValue('new_object_name');
$resolve = Acl::instance()->hasPermission('director/admin') $resolve = Acl::instance()->hasPermission('director/admin')
&& $this->getValue('clone_type') === 'flat'; && $this->getValue('clone_type') === 'flat';
$msg = sprintf( $msg = sprintf(
'The %s "%s" has been cloned from "%s"', 'The %s "%s" has been cloned from "%s"',
$object->getShortTableName(), $type,
$newname, $newName,
$object->getObjectName() $object->getObjectName()
); );
$new = $object::fromPlainObject( $new = $object::fromPlainObject(
$object->toPlainObject($resolve), $object->toPlainObject($resolve),
$connection $connection
)->set('object_name', $newname); )->set('object_name', $newName);
if ($new->isExternal()) { if ($new->isExternal()) {
$new->set('object_type', 'object'); $new->set('object_type', 'object');
} }
$services = [];
$sets = [];
if ($object instanceof IcingaHost) { if ($object instanceof IcingaHost) {
$new->set('api_key', null); $new->set('api_key', null);
if ($this->getValue('clone_services') === 'y') { if ($this->getValue('clone_services') === 'y') {
$services = $object->fetchServices(); $services = $object->fetchServices();
} else {
$services = [];
} }
if ($this->getValue('clone_service_sets') === 'y') { if ($this->getValue('clone_service_sets') === 'y') {
$sets = $object->fetchServiceSets(); $sets = $object->fetchServiceSets();
} else {
$sets = [];
} }
} elseif ($object instanceof IcingaServiceSet) { } elseif ($object instanceof IcingaServiceSet) {
if ($this->getValue('clone_services') === 'y') { if ($this->getValue('clone_services') === 'y') {
$services = $object->fetchServices(); $services = $object->fetchServices();
} else {
$services = [];
} }
$sets = []; }
if ($this->getValue('clone_fields') === 'y') {
$fields = $db->fetchAll(
$db->select()
->from($table . '_field')
->where("${type}_id = ?", $object->get('id'))
);
} else { } else {
$services = []; $fields = [];
$sets = [];
} }
if ($new->store()) { if ($new->store()) {
$newId = $new->get('id');
foreach ($services as $service) { foreach ($services as $service) {
$clone = IcingaService::fromPlainObject( $clone = IcingaService::fromPlainObject(
$service->toPlainObject(), $service->toPlainObject(),
@ -116,9 +130,9 @@ class IcingaCloneObjectForm extends DirectorForm
); );
if ($new instanceof IcingaHost) { if ($new instanceof IcingaHost) {
$clone->set('host_id', $new->get('id')); $clone->set('host_id', $newId);
} elseif ($new instanceof IcingaServiceSet) { } elseif ($new instanceof IcingaServiceSet) {
$clone->set('service_set_id', $new->get('id')); $clone->set('service_set_id', $newId);
} }
$clone->store(); $clone->store();
} }
@ -127,7 +141,12 @@ class IcingaCloneObjectForm extends DirectorForm
IcingaServiceSet::fromPlainObject( IcingaServiceSet::fromPlainObject(
$set->toPlainObject(), $set->toPlainObject(),
$connection $connection
)->set('host_id', $new->get('id'))->store(); )->set('host_id', $newId)->store();
}
foreach ($fields as $row) {
$row->{"${type}_id"} = $newId;
$db->insert($table . '_field', (array) $row);
} }
if ($new instanceof IcingaServiceSet) { if ($new instanceof IcingaServiceSet) {
@ -137,7 +156,7 @@ class IcingaCloneObjectForm extends DirectorForm
); );
} else { } else {
$this->setSuccessUrl( $this->setSuccessUrl(
'director/' . strtolower($object->getShortTableName()), 'director/' . strtolower($type),
$new->getUrlParams() $new->getUrlParams()
); );
} }