Dotclear

source: inc/core/class.dc.workspace.php @ 3730:5c45a5df9a59

Revision 3730:5c45a5df9a59, 12.7 KB checked in by franck <carnet.franck.paul@…>, 8 years ago (diff)

Code formatting (PSR-2)

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

Sites map