summaryrefslogtreecommitdiffstats
path: root/library/Director/Data/Db/DbObjectWithSettings.php
blob: 4f6b13921f86536b95a3ef655615a4a2719db926 (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
<?php

namespace Icinga\Module\Director\Data\Db;

use Icinga\Module\Director\Db;

abstract class DbObjectWithSettings extends DbObject
{
    /** @var Db $connection */
    protected $connection;

    protected $settingsTable = 'your_table_name';

    protected $settingsRemoteId = 'column_pointing_to_main_table_id';

    protected $settings = [];

    public function set($key, $value)
    {
        if ($this->hasProperty($key)) {
            return parent::set($key, $value);
        } elseif ($this->hasSetterForProperty($key)) { // Hint: hasProperty checks only for Getters
            return parent::set($key, $value);
        }

        if (! \array_key_exists($key, $this->settings) || $value !== $this->settings[$key]) {
            $this->hasBeenModified = true;
        }

        $this->settings[$key] = $value;
        return $this;
    }

    public function get($key)
    {
        if ($this->hasProperty($key)) {
            return parent::get($key);
        }

        if (array_key_exists($key, $this->settings)) {
            return $this->settings[$key];
        }

        return parent::get($key);
    }

    public function setSettings($settings)
    {
        $settings = (array) $settings;
        ksort($settings);
        if ($settings !== $this->settings) {
            $this->settings = $settings;
            $this->hasBeenModified = true;
        }

        return $this;
    }

    public function getSettings()
    {
        // Sort them, important only for new objects
        ksort($this->settings);
        return $this->settings;
    }

    public function getSetting($name, $default = null)
    {
        if (array_key_exists($name, $this->settings)) {
            return $this->settings[$name];
        }

        return $default;
    }

    public function getStoredSetting($name, $default = null)
    {
        $stored = $this->fetchSettingsFromDb();
        if (array_key_exists($name, $stored)) {
            return $stored[$name];
        }

        return $default;
    }

    public function __unset($key)
    {
        if ($this->hasProperty($key)) {
            parent::__unset($key);
        }

        if (array_key_exists($key, $this->settings)) {
            unset($this->settings[$key]);
            $this->hasBeenModified = true;
        }
    }

    protected function onStore()
    {
        $old = $this->fetchSettingsFromDb();
        $oldKeys = array_keys($old);
        $newKeys = array_keys($this->settings);
        $add = [];
        $mod = [];
        $del = [];
        $id = $this->get('id');

        foreach ($this->settings as $key => $val) {
            if (array_key_exists($key, $old)) {
                if ($old[$key] !== $this->settings[$key]) {
                    $mod[$key] = $this->settings[$key];
                }
            } else {
                $add[$key] = $this->settings[$key];
            }
        }

        foreach (array_diff($oldKeys, $newKeys) as $key) {
            $del[] = $key;
        }

        $where = sprintf($this->settingsRemoteId . ' = %d AND setting_name = ?', $id);
        $db = $this->getDb();
        foreach ($mod as $key => $val) {
            $db->update(
                $this->settingsTable,
                ['setting_value' => $val],
                $db->quoteInto($where, $key)
            );
        }

        foreach ($add as $key => $val) {
            $db->insert(
                $this->settingsTable,
                [
                    $this->settingsRemoteId => $id,
                    'setting_name'          => $key,
                    'setting_value'         => $val
                ]
            );
        }

        if (! empty($del)) {
            $where = sprintf($this->settingsRemoteId . ' = %d AND setting_name IN (?)', $id);
            $db->delete($this->settingsTable, $db->quoteInto($where, $del));
        }
    }

    protected function fetchSettingsFromDb()
    {
        $db = $this->getDb();
        $id = $this->get('id');
        if (! $id) {
            return [];
        }

        return $db->fetchPairs(
            $db->select()
               ->from($this->settingsTable, ['setting_name', 'setting_value'])
               ->where($this->settingsRemoteId . ' = ?', $id)
               ->order('setting_name')
        );
    }

    protected function onLoadFromDb()
    {
        $this->settings = $this->fetchSettingsFromDb();
    }
}