summaryrefslogtreecommitdiffstats
path: root/library/Director/DirectorObject/Automation/Basket.php
blob: 81ae107edb452e659c66ba1556635c5db5b56578 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php

namespace Icinga\Module\Director\DirectorObject\Automation;

use Icinga\Module\Director\Core\Json;
use Icinga\Module\Director\Data\Db\DbObject;

/**
 * Class Basket
 *
 * TODO
 * - create a UUID like in RFC4122
 */
class Basket extends DbObject implements ExportInterface
{
    const SELECTION_ALL = 'ALL';
    const SELECTION_NONE = 'IGNORE';
    const SELECTION_CUSTOM = '[]';

    protected $table = 'director_basket';

    protected $keyName = 'basket_name';

    protected $uuidColumn = 'uuid';

    protected $chosenObjects = [];

    protected $defaultProperties = [
        'uuid'        => null,
        'basket_name' => null,
        'objects'     => null,
        'owner_type'  => null,
        'owner_value' => null,
    ];

    protected $binaryProperties = [
        'uuid'
    ];

    public function getHexUuid()
    {
        return bin2hex($this->get('uuid'));
    }

    public function listObjectTypes()
    {
        return array_keys($this->objects);
    }

    public function getChosenObjects()
    {
        return $this->chosenObjects;
    }

    public function isEmpty()
    {
        return count($this->getChosenObjects()) === 0;
    }

    protected function onLoadFromDb()
    {
        $this->chosenObjects = (array) Json::decode($this->get('objects'));
        unset($this->chosenObjects['Datafield']); // Might be in old baskets
    }

    public function getUniqueIdentifier()
    {
        return $this->get('basket_name');
    }

    public function supportsCustomSelectionFor($type)
    {
        if (! array_key_exists($type, $this->chosenObjects)) {
            return false;
        }

        return is_array($this->chosenObjects[$type]);
    }

    public function setObjects($objects)
    {
        if (empty($objects)) {
            $this->chosenObjects = [];
        } else {
            $this->chosenObjects = [];
            foreach ((array) $objects as $type => $object) {
                $this->addObjects($type, $object);
            }
        }

        return $this;
    }

    /**
     * This is a weird method, as it is required to deal with raw form data
     *
     * @param $type
     * @param ExportInterface[]|bool $objects
     */
    public function addObjects($type, $objects = true)
    {
        BasketSnapshot::assertValidType($type);
        // '1' -> from Form!
        if ($objects === self::SELECTION_ALL) {
            $objects = true;
        } elseif ($objects === null || $objects === self::SELECTION_NONE) {
            return;
        } elseif ($objects === self::SELECTION_CUSTOM || is_array($objects)) {
            if (! isset($this->chosenObjects[$type]) || ! is_array($this->chosenObjects[$type])) {
                $this->chosenObjects[$type] = [];
            }
            if ($objects === self::SELECTION_CUSTOM) {
                $objects = [];
            }
        }

        if ($objects === true) {
            $this->chosenObjects[$type] = true;
        } elseif ($objects !== '0') { // TODO: what would generate '0'?
            foreach ($objects as $object) {
                $this->addObject($type, $object);
            }

            if (array_key_exists($type, $this->chosenObjects)) {
                ksort($this->chosenObjects[$type]);
            }
        }

        $this->reallySet('objects', Json::encode($this->chosenObjects));
    }

    public function hasObject($type, $object)
    {
        if (! $this->hasType($type)) {
            return false;
        }

        if ($this->chosenObjects[$type] === true) {
            return true;
        }

        if ($object instanceof ExportInterface) {
            $object = $object->getUniqueIdentifier();
        }

        if (is_array($this->chosenObjects[$type])) {
            return in_array($object, $this->chosenObjects[$type]);
        } else {
            return false;
        }
    }

    /**
     * @param $type
     * @param string $object
     */
    public function addObject($type, $object)
    {
        if (is_array($this->chosenObjects[$type])) {
            $this->chosenObjects[$type][] = $object;
        } else {
            throw new \InvalidArgumentException(sprintf(
                'The Basket "%s" has not been configured for single objects of type "%s"',
                $this->get('basket_name'),
                $type
            ));
        }
    }

    public function hasType($type)
    {
        return isset($this->chosenObjects[$type]);
    }

    protected function beforeStore()
    {
        if (! $this->hasBeenLoadedFromDb()) {
            // TODO: This is BS, use a real UUID
            $this->set('uuid', hex2bin(substr(sha1(microtime(true) . rand(1, 100000)), 0, 32)));
        }
    }
}