diff --git a/library/Director/DirectorObject/Automation/BasketSnapshot.php b/library/Director/DirectorObject/Automation/BasketSnapshot.php index 4e5b2714..3acc770c 100644 --- a/library/Director/DirectorObject/Automation/BasketSnapshot.php +++ b/library/Director/DirectorObject/Automation/BasketSnapshot.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Director\DirectorObject\Automation; use Icinga\Module\Director\Core\Json; use Icinga\Module\Director\Db; use Icinga\Module\Director\Data\Db\DbObject; +use Icinga\Module\Director\Objects\DirectorDatafield; use Icinga\Module\Director\Objects\IcingaCommand; use Icinga\Module\Director\Objects\IcingaObject; use RuntimeException; @@ -23,6 +24,7 @@ class BasketSnapshot extends DbObject ]; protected $restoreOrder = [ + 'Datafield', 'Command', 'HostGroup', 'IcingaTemplateChoiceHost', @@ -48,6 +50,7 @@ class BasketSnapshot extends DbObject public static function getClassForType($type) { $types = [ + 'Datafield' => '\\Icinga\\Module\\Director\\Objects\\DirectorDatafield', 'Command' => '\\Icinga\\Module\\Director\\Objects\\IcingaCommand', 'HostGroup' => '\\Icinga\\Module\\Director\\Objects\\IcingaHostGroup', 'IcingaTemplateChoiceHost' => '\\Icinga\\Module\\Director\\Objects\\IcingaTemplateChoiceHost', @@ -73,10 +76,39 @@ class BasketSnapshot extends DbObject 'basket_uuid' => $basket->get('uuid') ], $db); $snapshot->addObjectsChosenByBasket($basket); + $snapshot->resolveRequiredFields(); return $snapshot; } + /** + * @throws \Icinga\Exception\NotFoundError + */ + protected function resolveRequiredFields() + { + $requiredIds = []; + foreach ($this->objects as $typeName => $objects) { + foreach ($objects as $key => $object) { + if (isset($object->fields)) { + foreach ($object->fields as $field) { + $requiredIds[$field->datafield_id] = true; + } + } + } + } + + $connection = $this->getConnection(); + if (! isset($this->objects['Datafield'])) { + $this->objects['Datafield'] = []; + } + $fields = & $this->objects['Datafield']; + foreach ($requiredIds as $id) { + if (! isset($fields[$id])) { + $fields[$id] = DirectorDatafield::loadWithAutoIncId((int) $id, $connection)->export(); + } + } + } + protected function addObjectsChosenByBasket(Basket $basket) { foreach ($basket->getChosenObjects() as $typeName => $selection) { diff --git a/library/Director/Objects/DirectorDatafield.php b/library/Director/Objects/DirectorDatafield.php index 83bb6923..5bcfd2a2 100644 --- a/library/Director/Objects/DirectorDatafield.php +++ b/library/Director/Objects/DirectorDatafield.php @@ -2,10 +2,13 @@ namespace Icinga\Module\Director\Objects; +use Icinga\Module\Director\Core\Json; use Icinga\Module\Director\Data\Db\DbObjectWithSettings; use Icinga\Module\Director\Db; +use Icinga\Module\Director\Exception\DuplicateKeyException; use Icinga\Module\Director\Hook\DataTypeHook; use Icinga\Module\Director\Web\Form\DirectorObjectForm; +use InvalidArgumentException; use Zend_Form_Element as ZfElement; class DirectorDatafield extends DbObjectWithSettings @@ -50,6 +53,10 @@ class DirectorDatafield extends DbObjectWithSettings return $obj; } + /** + * @return object + * @throws \Icinga\Exception\NotFoundError + */ public function export() { $plain = (object) $this->getProperties(); @@ -68,6 +75,38 @@ class DirectorDatafield extends DbObjectWithSettings return $plain; } + /** + * @param $plain + * @param Db $db + * @param bool $replace + * @return DirectorDatafield + * @throws DuplicateKeyException + * @throws \Icinga\Exception\NotFoundError + */ + public static function import($plain, Db $db, $replace = false) + { + $properties = (array) $plain; + if (isset($properties['originalId'])) { + $id = $properties['originalId']; + unset($properties['originalId']); + } else { + $id = null; + } + + if ($id) { + if (static::exists($id, $db)) { + $existing = static::loadWithAutoIncId($id, $db); + $existingProperties = (array) $existing->export(); + unset($existingProperties['originalId']); + if (Json::encode($properties) === Json::encode($existingProperties)) { + return $existing; + } + } + } + + return static::create($properties, $db); + } + protected function setObject(IcingaObject $object) { $this->object = $object;