File: item.inc.php

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (317 lines) | stat: -rw-r--r-- 8,706 bytes parent folder | download
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
 * A confItem defines a configuration item
 *
 * This file is part of Zoph.
 *
 * Zoph 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.
 *
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @package Zoph
 * @author Jeroen Roos
 */
namespace conf\item;

use PDO;

use db\select;
use db\param;
use db\clause;

use log;
use zophTable;
use template\block;

/**
 * Configuration item
 * @package Zoph
 * @author Jeroen Roos
 */
abstract class item extends zophTable {
    /** @var string The name of the database table */
    protected static $tableName="conf";
    /** @var array List of primary keys */
    protected static $primaryKeys=array("conf_id");
    /** @var array Fields that may not be empty */
    protected static $notNull=array();
    /** @var bool keep keys with insert. In most cases the keys are set by
                  the db with auto_increment */
    protected static $keepKeys = true;
    /** @var string URL for this class */
    protected static $url="config.php#";

    /** @var string Label to display */
    protected $label;
    /** @var string Longer description of item */
    protected $desc;
    /** @var string Default value */
    protected $default;
    /** @var string Input hint (format to use) */
    protected $hint;
    /** @var bool required, whether or not field may be empty*/
    protected $required=false;
    /** @var bool indicate that this configuration should no longer be used */
    protected $deprecated=false;
    /** @var bool internal, internal settings can not be changed from webinterface */
    protected $internal=false;
    /** @var array list of items that MUST be enabled for this item to be enabled */
    protected $requiresEnabled=array();
    /** @var array list of unmet requirements */
    protected $unmet=array();

    /**
     * Create conf\item object
     * @param string id, to fetch object from database.
     * @retrun conf\item new object
     */
    public function __construct(string $id = null) {
        if ($id === null || preg_match("/^[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/", $id)) {
            $this->set("conf_id", $id);
        } else {
            log::msg("Illegal configuration id", log::FATAL, log::VARS);
        }

    }

    /**
     * Update or insert configuration item
     * checks if item already exists in db
     * and updates it if it does or inserts
     * if it does not.
     */
    final public function update() : void {
        if ($this->checkValue($this->get("value"))) {
            $qry=new select(array("co" => "conf"));
            $qry->addFunction(array("count" => "COUNT(conf_id)"));
            $qry->where(new clause("conf_id=:confid"));
            $qry->addParam(new param(":confid", $this->fields["conf_id"], PDO::PARAM_STR));

            if ($qry->getCount() > 0) {
                parent::update();
            } else {
                parent::insert();
            }
        }
    }

    /**
     * Get name of item
     * @return string name
     */
    final public function getName() : string {
        return $this->fields["conf_id"];
    }


    /**
     * Get label for item
     * @return string label
     */
    final public function getLabel() : string {
        return (string) $this->label;
    }

    /**
     * Get description for item
     * @return string description
     */
    final public function getDesc() : string {
        return (string) $this->desc;
    }

    /**
     * Get value of item
     * if value is not set, get default
     * @return string value
     */
    public function getValue() : string|bool|int|null {
        if (!isset($this->fields["value"]) || $this->fields["value"]===null || !$this->requirementsMet()) {
            return $this->getDefault();
        } else {
            return $this->fields["value"];
        }
    }

    /**
     * Get value of item for display
     * @return string value
     */
    public function displayValue() : ?string {
        return $this->getValue();
    }

    /**
     * Set value of item
     * @param string value
     * @throws configurationException
     */
    public function setValue(?string $value) : void {
        if ($this->checkValue($value)) {
            $this->fields["value"]=$value;
        } else {
            throw new \configurationException("Configuration value for " .
                $this->getName() . " is illegal");
        }
    }

    /**
     * Get default value of item
     * @return string default value
     */
    final public function getDefault() : ?string {
        return $this->default;
    }

    /**
     * Get hint for item
     * @return string hint
     */
    final public function getHint() : string {
        return (string) $this->hint;
    }

    /**
     * Set name (id) of item
     * @param string name
     */
    final public function setName(string $name) : void{
        $this->fields["conf_id"]=$name;
    }

    /**
     * Set label for item
     * @param string label
     */
    final public function setLabel(string $label) : void {
        $this->label=$label;
    }

    /**
     * Set label for item
     * @param string label
     */
    final public function setDesc(string $desc) : void {
        $this->desc=$desc;
    }

    /**
     * Set hint for item
     * @param string hint
     */
    final public function setHint(string $hint) : void {
        $this->hint=$hint;
    }

    /**
     * Set whether or not a field is required
     * @param bool
     */
    final public function setRequired(bool $req=true) {
        $this->required=(bool) $req;
    }

    /**
     * Set whether or not a field is deprecated
     * @param bool
     */
    final public function setDeprecated(bool $dep=true) : void {
        $this->deprecated=(bool) $dep;
    }

    /**
     * Get whether or not a field is deprecated
     * @param bool
     */
    final public function isDeprecated() : bool {
        return (bool) $this->deprecated;
    }

    /**
     * Set whether or not a field is internal
     * an internal field is not exposed in the webinterface
     * and (at this moment) not stored in the database, although this is not enforced
     * as there may be a future use-case where this will change.
     * @param bool
     */
    final public function setInternal(bool $int=true) : void {
        $this->internal=(bool) $int;
    }

    /**
     * Set default value for item
     * @param string default
     */
    final public function setDefault(?string $default) : void {
        $this->default=$default;
    }

    /**
     * This item requires another item to be enabled
     * @param checkbox configuration item checkbox that must be enabled to use this parameter
     */
    final public function requiresEnabled(checkbox $item) : void {
        $this->requiresEnabled[]=$item;
    }

    /**
     * Are all requirements met?
     */
    final protected function requirementsMet() : bool {
        $met=true;
        foreach ($this->requiresEnabled as $req) {
            $req->lookup();
            if ((bool) $req->getValue() === false) {
                $met=false;
                $this->unmet[$req->getName()]="enabled";
            }
        }
        return $met;
    }

    /**
     * Return a template block to show the item is deprecated
     * @return block deprecation warning
     */
    final protected function displayDeprecationWarning() : ?block {
        if ($this->isDeprecated()) {
            return new block("confDeprecated");
        }
        return null;
    }

    /**
     * Return a template block to show the unmet requirements for this
     * confItem.
     * @return block overview of unmet items
     */
    final protected function displayUnmetRequirements() : ?block {
        if (!$this->requirementsMet()) {
            return new block("confUnmetRequirements", array(
                "unmet" => $this->unmet
            ));
        }
        return null;
    }

    /**
     * Display the item
     */
    abstract public function display() : ?block;

    /**
     * Check whether value is legal
     * @param string value
     */
    abstract public function checkValue(string $value) : bool;

}