summaryrefslogtreecommitdiffstats
path: root/library/Director/Import/ImportSourceCoreApi.php
blob: 6d590ec36a5930582278ef4399b86a5887f36b12 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php

namespace Icinga\Module\Director\Import;

use Icinga\Application\Config;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Hook\ImportSourceHook;
use Icinga\Module\Director\Web\Form\QuickForm;

class ImportSourceCoreApi extends ImportSourceHook
{
    protected $connection;

    protected $db;

    protected $api;

    public function fetchData()
    {
        $func = 'get' . $this->getSetting('object_type') . 'Objects';
        $objects = $this->api()->$func();
        $result = array();
        foreach ($objects as $object) {
            $result[] = $object->toPlainObject();
        }

        return $result;
    }

    public function listColumns()
    {
        $res = $this->fetchData();
        if (empty($data)) {
            return array('object_name');
        }

        return array_keys((array) $res[0]);
    }

    public static function getDefaultKeyColumnName()
    {
        return 'object_name';
    }

    public static function addSettingsFormFields(QuickForm $form)
    {
        $form->addElement('select', 'object_type', array(
            'label'    => 'Object type',
            'required' => true,
            'multiOptions' => $form->optionalEnum(self::enumObjectTypes($form))
        ));
    }

    protected static function enumObjectTypes($form)
    {
        $types = array(
            'CheckCommand'        => $form->translate('Check Commands'),
            'NotificationCommand' => $form->translate('Notification Commands'),
            'Endpoint'            => $form->translate('Endpoints'),
            'Host'                => $form->translate('Hosts'),
            'HostGroup'           => $form->translate('Hostgroups'),
            'User'                => $form->translate('Users'),
            'UserGroup'           => $form->translate('Usergroups'),
            'Zone'                => $form->translate('Zones'),
        );

        asort($types);
        return $types;
    }

    protected function api()
    {
        if ($this->api === null) {
            $endpoint = $this->db()->getDeploymentEndpoint();
            $this->api = $endpoint->api()->setDb($this->db());
        }

        return $this->api;
    }

    protected function db()
    {
        if ($this->db === null) {
            $resourceName = Config::module('director')->get('db', 'resource');
            if ($resourceName) {
                $this->db = Db::fromResourceName($resourceName);
            }
        }

        return $this->db;
    }
}