aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/RangeConfig.class.php
blob: fc90434cf68002d42e502b9f455d98e0412958ed (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
 * RangeConfig.class.php
 * provides access to object preferences
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      André Noack <noack@data-quest.de>
 * @author      Jan-Hendrik Willms <tleilax+studip@gmail.com>>
 * @copyright   2010 Stud.IP Core-Group
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
*/

class RangeConfig extends Config
{
    /**
     * range type
     */
    const RANGE_TYPE = 'range';

    /**
     * cache of created RangeConfig instances
     * @var array
     */
    protected static $instances = [];

    /**
     * range_id
     * @var string
     */
    private $range_id;

    /**
     * returns cached instance for given range_id
     * creates new objects if needed
     * @param string $range_id
     * @return RangeConfig
     */
    public static function get()
    {
        if (func_num_args() === 0 || func_get_arg(0) === null) {
            return new static(true);
        }

        $range_id = func_get_arg(0);

        if ($range_id instanceof Range) {
            $range_id = $range_id->getRangeId();
        }

        if (!isset(static::$instances[$range_id])) {
            static::$instances[$range_id] = new static($range_id);
        }
        return static::$instances[$range_id];
    }

    /**
     * set cached instance for given range_id
     * use for testing or to unset cached instance by passing
     * null as second param
     * @param string $range_id
     * @param RangeConfig $my_instance
     */
    public static function set()
    {
        list($range_id, $my_instance) = func_get_args();
        self::$instances[$range_id] = $my_instance;
    }

    /**
     * passing null as first param is for compatibility and
     * should be considered deprecated.
     * passing data array as second param only for testing
     * @param string $range_id
     * @param array $data
     */
    public function __construct($range_id = null, $data = null)
    {
        $this->range_id = $range_id;
        if ($range_id !== null || $data !== null) {
            $this->fetchData($data);
        }
    }

    /**
     * {@inheritdoc}
     */
    protected function fetchData($data = null)
    {
        if ($data !== null) {
            $this->data = $data;
        } else {
            $this->data = [];
            foreach (Config::get()->getFields(static::RANGE_TYPE) as $field) {
                $this->data[$field] = Config::get()->$field;
                $this->metadata[$field] = Config::get()->getMetadata($field);
            }
            try {
                $query = "SELECT `config_values`.`field`, `config_values`.`value`
                          FROM `config_values`
                          LEFT JOIN `config` USING (`field`)
                          WHERE `range_id` = :id
                            AND (
                                `field` IN (:fields)
                                OR `config`.`field` IS NULL
                            )";
                $statement = DBManager::get()->prepare($query);
                $statement->bindValue(':id', $this->range_id);
                $statement->bindValue(':fields', array_keys($this->data));
                $statement->execute();
            } catch (Exception $e) {
                //in case we have not migrated 226 yet:
                $query = 'SELECT field, value FROM user_config WHERE user_id = :id';
                $statement = DBManager::get()->prepare($query);
                $statement->bindValue(':id', $this->range_id);
                $statement->execute();
            }
            while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
                $this->data[$row['field']] = $this->convertFromDatabase(
                    $this->metadata[$row['field']]['type'] ?? 'string',
                    $row['value'],
                    $row['field']
                );
            }
        }
    }

    /**
     * returns the range id
     *
     * @return string
     */
    public function getRangeId()
    {
        return $this->range_id;
    }

    /**
     * @see lib/classes/Config::getValue()
     */
    public function getValue($field)
    {
        if (array_key_exists($field, $this->data)) {
            return $this->data[$field];
        }
        return null;
    }

    /**
     * @param string $field
     * @return bool
     */
    public function unsetValue($field)
    {
        return $this->delete($field);
    }

    /**
     * @see lib/classes/Config::store()
     */
    public function store($field, $value)
    {
        $entry = new ConfigValue([$field, $this->range_id]);
        $this->data[$field] = $value;

        // Check if entry is default and if so, delete it
        if (Config::get()->getValue($field) == $value) {
            $entry->delete();
            return 1;
        }

        // Otherwise convert it to an appropriate format and store it
        $metadata = Config::get()->getMetadata($field);
        $entry->value = $this->convertForDatabase($metadata['type'], $value, $field);

        $ret = $entry->store();
        if ($ret) {
            $this->fetchData();
        }

        return $ret;
    }

    /**
     * {@inheritdoc}
     */
    public function create($field, $data = [])
    {
        if (!isset($data['range'])) {
            $data['range'] = static::RANGE_TYPE;
        }

        parent::create($field, $data);
    }

    /**
     * {@inheritdoc}
     */
    public function delete($field)
    {
        $entry = ConfigValue::find([$field, $this->range_id]);
        if (!$entry) {
            return null;
        }

        if ($ret = $entry->delete()) {
            $this->data[$field] = Config::get()->$field;
        }
        return $ret;
    }

    /**
     * {@inheritdoc}
     */
    protected function getI18NIdentifier($field)
    {
        return md5("{$field}|{$this->range_id}");
    }
}