Dotclear

source: inc/core/class.dc.workspace.php @ 3731:3770620079d4

Revision 3731:3770620079d4, 12.5 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Simplify licence block at the beginning of each file

Line 
1<?php
2/**
3 * @brief User workspace for preferences handler
4 *
5 * @package Dotclear
6 * @subpackage Core
7 *
8 * @copyright Olivier Meunier & Association Dotclear
9 * @copyright GPL-2.0-only
10 */
11
12if (!defined('DC_RC_PATH')) {return;}
13
14class dcWorkspace
15{
16    protected $con;     ///< <b>connection</b> Database connection object
17    protected $table;   ///< <b>string</b> Preferences table name
18    protected $user_id; ///< <b>string</b> User ID
19
20    protected $global_prefs = array(); ///< <b>array</b> Global prefs array
21    protected $local_prefs  = array(); ///< <b>array</b> Local prefs array
22    protected $prefs        = array(); ///< <b>array</b> Associative prefs array
23    protected $ws;                     ///< <b>string</b> Current workspace
24
25    /**
26    Object constructor. Retrieves user prefs and puts them in $prefs
27    array. Local (user) prefs have a highest priority than global prefs.
28
29    @param    name        <b>string</b>        ID for this workspace
30     */
31    public function __construct(&$core, $user_id, $name, $rs = null)
32    {
33        if (preg_match('/^[a-zA-Z][a-zA-Z0-9]+$/', $name)) {
34            $this->ws = $name;
35        } else {
36            throw new Exception(sprintf(__('Invalid dcWorkspace: %s'), $name));
37        }
38
39        $this->con     = &$core->con;
40        $this->table   = $core->prefix . 'pref';
41        $this->user_id = &$user_id;
42
43        try { $this->getPrefs($rs);} catch (Exception $e) {
44            if (version_compare($core->getVersion('core'), '2.3', '>')) {
45                trigger_error(__('Unable to retrieve prefs:') . ' ' . $this->con->error(), E_USER_ERROR);
46            }
47        }
48    }
49
50    private function getPrefs($rs = null)
51    {
52        if ($rs == null) {
53            $strReq = 'SELECT user_id, pref_id, pref_value, ' .
54            'pref_type, pref_label, pref_ws ' .
55            'FROM ' . $this->table . ' ' .
56            "WHERE (user_id = '" . $this->con->escape($this->user_id) . "' " .
57            'OR user_id IS NULL) ' .
58            "AND pref_ws = '" . $this->con->escape($this->ws) . "' " .
59                'ORDER BY pref_id ASC ';
60
61            try {
62                $rs = $this->con->select($strReq);
63            } catch (Exception $e) {
64                throw $e;
65            }
66        }
67        while ($rs->fetch()) {
68            if ($rs->f('pref_ws') != $this->ws) {
69                break;
70            }
71            $id    = trim($rs->f('pref_id'));
72            $value = $rs->f('pref_value');
73            $type  = $rs->f('pref_type');
74
75            if ($type == 'array') {
76                $value = @json_decode($value, true);
77            } else {
78                if ($type == 'float' || $type == 'double') {
79                    $type = 'float';
80                } elseif ($type != 'boolean' && $type != 'integer') {
81                    $type = 'string';
82                }
83            }
84
85            settype($value, $type);
86
87            $array = $rs->user_id ? 'local' : 'global';
88
89            $this->{$array . '_prefs'}[$id] = array(
90                'ws'     => $this->ws,
91                'value'  => $value,
92                'type'   => $type,
93                'label'  => (string) $rs->f('pref_label'),
94                'global' => $rs->user_id == ''
95            );
96        }
97
98        $this->prefs = $this->global_prefs;
99
100        foreach ($this->local_prefs as $id => $v) {
101            $this->prefs[$id] = $v;
102        }
103
104        return true;
105    }
106
107    public function prefExists($id, $global = false)
108    {
109        $array = $global ? 'global' : 'local';
110        return isset($this->{$array . '_prefs'}[$id]);
111    }
112
113    /**
114    Returns pref value if exists.
115
116    @param    n        <b>string</b>        Pref name
117    @return    <b>mixed</b>
118     */
119    public function get($n)
120    {
121        if (isset($this->prefs[$n]['value'])) {
122            return $this->prefs[$n]['value'];
123        }
124
125        return;
126    }
127
128    /**
129    Returns global pref value if exists.
130
131    @param    n        <b>string</b>        Pref name
132    @return    <b>mixed</b>
133     */
134    public function getGlobal($n)
135    {
136        if (isset($this->global_prefs[$n]['value'])) {
137            return $this->global_prefs[$n]['value'];
138        }
139
140        return;
141    }
142
143    /**
144    Returns local pref value if exists.
145
146    @param    n        <b>string</b>        Pref name
147    @return    <b>mixed</b>
148     */
149    public function getLocal($n)
150    {
151        if (isset($this->local_prefs[$n]['value'])) {
152            return $this->local_prefs[$n]['value'];
153        }
154
155        return;
156    }
157    /**
158    Magic __get method.
159    @copydoc ::get
160     */
161    public function __get($n)
162    {
163        return $this->get($n);
164    }
165
166    /**
167    Sets a pref in $prefs property. This sets the pref for script
168    execution time only and if pref exists.
169
170    @param    n        <b>string</b>        Pref name
171    @param    v        <b>mixed</b>        Pref value
172     */
173    public function set($n, $v)
174    {
175        if (isset($this->prefs[$n])) {
176            $this->prefs[$n]['value'] = $v;
177        }
178    }
179
180    /**
181    Magic __set method.
182    @copydoc ::set
183     */
184    public function __set($n, $v)
185    {
186        $this->set($n, $v);
187    }
188
189    /**
190    Creates or updates a pref.
191
192    $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is
193    null and pref exists, it will keep current pref type.
194
195    $value_change allow you to not change pref. Useful if you need to change
196    a pref label or type and don't want to change its value.
197
198    @param    id            <b>string</b>        Pref ID
199    @param    value        <b>mixed</b>        Pref value
200    @param    type            <b>string</b>        Pref type
201    @param    label        <b>string</b>        Pref label
202    @param    value_change    <b>boolean</b>        Change pref value or not
203    @param    global        <b>boolean</b>        Pref is global
204     */
205    public function put($id, $value, $type = null, $label = null, $value_change = true, $global = false)
206    {
207        if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/', $id)) {
208            throw new Exception(sprintf(__('%s is not a valid pref id'), $id));
209        }
210
211        # We don't want to change pref value
212        if (!$value_change) {
213            if (!$global && $this->prefExists($id, false)) {
214                $value = $this->local_prefs[$id]['value'];
215            } elseif ($this->prefExists($id, true)) {
216                $value = $this->global_prefs[$id]['value'];
217            }
218        }
219
220        # Pref type
221        if ($type == 'double') {
222            $type = 'float';
223        } elseif ($type === null) {
224            if (!$global && $this->prefExists($id, false)) {
225                $type = $this->local_prefs[$id]['type'];
226            } elseif ($this->prefExists($id, true)) {
227                $type = $this->global_prefs[$id]['type'];
228            } else {
229                if (is_array($value)) {
230                    $type = 'array';
231                } else {
232                    $type = 'string';
233                }
234            }
235        } elseif ($type != 'boolean' && $type != 'integer' && $type != 'float' && $type != 'array') {
236            $type = 'string';
237        }
238
239        # We don't change label
240        if ($label == null) {
241            if (!$global && $this->prefExists($id, false)) {
242                $label = $this->local_prefs[$id]['label'];
243            } elseif ($this->prefExists($id, true)) {
244                $label = $this->global_prefs[$id]['label'];
245            }
246        }
247
248        if ($type != 'array') {
249            settype($value, $type);
250        } else {
251            $value = json_encode($value);
252        }
253
254        $cur             = $this->con->openCursor($this->table);
255        $cur->pref_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value;
256        $cur->pref_type  = $type;
257        $cur->pref_label = $label;
258
259        #If we are local, compare to global value
260        if (!$global && $this->prefExists($id, true)) {
261            $g         = $this->global_prefs[$id];
262            $same_pref = $g['ws'] == $this->ws && $g['value'] == $value
263                && $g['type'] == $type && $g['label'] == $label;
264
265            # Drop pref if same value as global
266            if ($same_pref && $this->prefExists($id, false)) {
267                $this->drop($id);
268            } elseif ($same_pref) {
269                return;
270            }
271        }
272
273        if ($this->prefExists($id, $global) && $this->ws == $this->prefs[$id]['ws']) {
274            if ($global) {
275                $where = 'WHERE user_id IS NULL ';
276            } else {
277                $where = "WHERE user_id = '" . $this->con->escape($this->user_id) . "' ";
278            }
279
280            $cur->update($where . "AND pref_id = '" . $this->con->escape($id) . "' AND pref_ws = '" . $this->con->escape($this->ws) . "' ");
281        } else {
282            $cur->pref_id = $id;
283            $cur->user_id = $global ? null : $this->user_id;
284            $cur->pref_ws = $this->ws;
285
286            $cur->insert();
287        }
288    }
289
290    /**
291    Rename an existing pref in a Workspace
292
293    @param     $oldId     <b>string</b>     Current pref name
294    @param     $newId     <b>string</b>     New pref name
295    @return     <b>boolean</b>
296     */
297    public function rename($oldId, $newId)
298    {
299        if (!$this->ws) {
300            throw new Exception(__('No workspace specified'));
301        }
302
303        if (!array_key_exists($oldId, $this->prefs) || array_key_exists($newId, $this->prefs)) {
304            return false;
305        }
306
307        // Rename the pref in the prefs array
308        $this->prefs[$newId] = $this->prefs[$oldId];
309        unset($this->prefs[$oldId]);
310
311        // Rename the pref in the database
312        $strReq = 'UPDATE ' . $this->table .
313        " SET pref_id = '" . $this->con->escape($newId) . "' " .
314        " WHERE pref_ws = '" . $this->con->escape($this->ws) . "' " .
315        " AND pref_id = '" . $this->con->escape($oldId) . "' ";
316        $this->con->execute($strReq);
317        return true;
318    }
319
320    /**
321    Removes an existing pref. Workspace
322
323    @param    id        <b>string</b>        Pref ID
324    @param    force_global    <b>boolean</b>    Force global pref drop
325     */
326    public function drop($id, $force_global = false)
327    {
328        if (!$this->ws) {
329            throw new Exception(__('No workspace specified'));
330        }
331
332        $strReq = 'DELETE FROM ' . $this->table . ' ';
333
334        if (($force_global) || ($this->user_id === null)) {
335            $strReq .= 'WHERE user_id IS NULL ';
336            $global = true;
337        } else {
338            $strReq .= "WHERE user_id = '" . $this->con->escape($this->user_id) . "' ";
339            $global = false;
340        }
341
342        $strReq .= "AND pref_id = '" . $this->con->escape($id) . "' ";
343        $strReq .= "AND pref_ws = '" . $this->con->escape($this->ws) . "' ";
344
345        $this->con->execute($strReq);
346
347        if ($this->prefExists($id, $global)) {
348            $array = $global ? 'global' : 'local';
349            unset($this->{$array . '_prefs'}[$id]);
350        }
351
352        $this->prefs = $this->global_prefs;
353        foreach ($this->local_prefs as $id => $v) {
354            $this->prefs[$id] = $v;
355        }
356    }
357
358    /**
359    Removes all existing pref. in a Workspace
360
361    @param    force_global    <b>boolean</b>    Force global pref drop
362     */
363    public function dropAll($force_global = false)
364    {
365        if (!$this->ws) {
366            throw new Exception(__('No workspace specified'));
367        }
368
369        $strReq = 'DELETE FROM ' . $this->table . ' ';
370
371        if (($force_global) || ($this->user_id === null)) {
372            $strReq .= 'WHERE user_id IS NULL ';
373            $global = true;
374        } else {
375            $strReq .= "WHERE user_id = '" . $this->con->escape($this->user_id) . "' ";
376            $global = false;
377        }
378
379        $strReq .= "AND pref_ws = '" . $this->con->escape($this->ws) . "' ";
380
381        $this->con->execute($strReq);
382
383        $array = $global ? 'global' : 'local';
384        unset($this->{$array . '_prefs'});
385        $this->{$array . '_prefs'} = array();
386
387        $array       = $global ? 'local' : 'global';
388        $this->prefs = $this->{$array . '_prefs'};
389    }
390
391    /**
392    Returns $ws property content.
393
394    @return    <b>string</b>
395     */
396    public function dumpWorkspace()
397    {
398        return $this->ws;
399    }
400
401    /**
402    Returns $prefs property content.
403
404    @return    <b>array</b>
405     */
406    public function dumpPrefs()
407    {
408        return $this->prefs;
409    }
410
411    /**
412    Returns $local_prefs property content.
413
414    @return    <b>array</b>
415     */
416    public function dumpLocalPrefs()
417    {
418        return $this->local_prefs;
419    }
420
421    /**
422    Returns $global_prefs property content.
423
424    @return    <b>array</b>
425     */
426    public function dumpGlobalPrefs()
427    {
428        return $this->global_prefs;
429    }
430
431}
Note: See TracBrowser for help on using the repository browser.

Sites map