blob: 98d34c6dce7a496eddd75c2b3e0b11b2c3aeabc8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
}
}
|