summaryrefslogtreecommitdiffstats
path: root/library/Director/Data/FieldReferenceLoader.php
blob: 99a9925b6f8bda9f5bb3452eb446e245c1b7da18 (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
<?php

namespace Icinga\Module\Director\Data;

use gipfl\ZfDb\Adapter\Adapter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject;

class FieldReferenceLoader
{
    /** @var Adapter|\Zend_Db_Adapter_Abstract */
    protected $db;

    public function __construct(Db $connection)
    {
        $this->db = $connection->getDbAdapter();
    }

    /**
     * @param int $id
     * @return array
     */
    public function loadFor(IcingaObject $object)
    {
        $db = $this->db;
        $id = $object->get('id');
        if ($id === null) {
            return [];
        }
        $type = $object->getShortTableName();
        $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 = ?", (int) $id)
                ->order('varname ASC')
        );

        if (empty($res)) {
            return [];
        }

        foreach ($res as $field) {
            $field->datafield_id = (int) $field->datafield_id;
        }

        return $res;
    }
}