diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
commit | f66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/Objects/ImportExportHelper.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip |
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/Objects/ImportExportHelper.php')
-rw-r--r-- | library/Director/Objects/ImportExportHelper.php | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/library/Director/Objects/ImportExportHelper.php b/library/Director/Objects/ImportExportHelper.php new file mode 100644 index 0000000..98d34c6 --- /dev/null +++ b/library/Director/Objects/ImportExportHelper.php @@ -0,0 +1,68 @@ +<?php + +namespace Icinga\Module\Director\Objects; + +use Icinga\Module\Director\Db; + +/** + * Helper class, allows to reduce duplicate code. Might be moved elsewhere + * afterwards + */ +class ImportExportHelper +{ + /** + * Does not support every type out of the box + * + * @param IcingaObject $object + * @return object + * @throws \Icinga\Exception\NotFoundError + */ + public static function simpleExport(IcingaObject $object) + { + $props = (array) $object->toPlainObject(); + $props['fields'] = static::fetchFields($object); + ksort($props); // TODO: ksort in toPlainObject? + + return (object) $props; + } + + public static function fetchFields(IcingaObject $object) + { + return static::loadFieldReferences( + $object->getConnection(), + $object->getShortTableName(), + $object->get('id') + ); + } + + /** + * @param Db $connection + * @param string $type Warning: this will not be validated. + * @param int $id + * @return array + */ + public static function loadFieldReferences(Db $connection, $type, $id) + { + $db = $connection->getDbAdapter(); + $res = $db->fetchAll( + $db->select()->from([ + 'f' => "icinga_${type}_field" + ], [ + 'f.datafield_id', + 'f.is_required', + 'f.var_filter', + ])->join(['df' => 'director_datafield'], 'df.id = f.datafield_id', []) + ->where("${type}_id = ?", $id) + ->order('varname ASC') + ); + + if (empty($res)) { + return []; + } + + foreach ($res as $field) { + $field->datafield_id = (int) $field->datafield_id; + } + return $res; + } +} |