Dotclear

source: inc/core/class.dc.workspace.php @ 147:421794608368

Revision 147:421794608368, 8.7 KB checked in by xave <xave@…>, 14 years ago (diff)

Gosh, that was dirty.

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2011 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          {
74               if ($rs->f('pref_ws') != $this->ws){
75                    break;
76               }
77               $id = trim($rs->f('pref_id'));
78               $value = $rs->f('pref_value');
79               $type = $rs->f('pref_type');
80               
81               if ($type == 'float' || $type == 'double') {
82                    $type = 'float';
83               } elseif ($type != 'boolean' && $type != 'integer') {
84                    $type = 'string';
85               }
86               
87               settype($value,$type);
88               
89               $array = $rs->user_id ? 'local' : 'global';
90               
91               $this->{$array.'_prefs'}[$id] = array(
92                    'ws' => $this->ws,
93                    'value' => $value,
94                    'type' => $type,
95                    'label' => (string) $rs->f('pref_label'),
96                    'global' => $rs->user_id == ''
97               );
98          }
99         
100          $this->prefs = $this->global_prefs;
101         
102          foreach ($this->local_prefs as $id => $v) {
103               $this->prefs[$id] = $v;
104          }
105         
106          return true;
107     }
108     
109     public function prefExists($id,$global=false)
110     {
111          $array = $global ? 'global' : 'local';
112          return isset($this->{$array.'_prefs'}[$id]);
113     }
114     
115     /**
116     Returns pref value if exists.
117     
118     @param    n         <b>string</b>       Pref name
119     @return   <b>mixed</b>
120     */
121     public function get($n)
122     {
123          if (isset($this->prefs[$n]['value'])) {
124               return $this->prefs[$n]['value'];
125          }
126         
127          return null;
128     }
129     
130     /**
131     Magic __get method.
132     @copydoc ::get
133     */
134     public function __get($n)
135     {
136          return $this->get($n);
137     }
138     
139     /**
140     Sets a pref in $prefs property. This sets the pref for script
141     execution time only and if pref exists.
142     
143     @param    n         <b>string</b>       Pref name
144     @param    v         <b>mixed</b>        Pref value
145     */
146     public function set($n,$v)
147     {
148          if (isset($this->prefs[$n])) {
149               $this->prefs[$n]['value'] = $v;
150          }
151     }
152     
153     /**
154     Magic __set method.
155     @copydoc ::set
156     */
157     public function __set($n,$v)
158     {
159          $this->set($n,$v);
160     }
161     
162     /**
163     Creates or updates a pref.
164     
165     $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is
166     null and pref exists, it will keep current pref type.
167     
168     $value_change allow you to not change pref. Useful if you need to change
169     a pref label or type and don't want to change its value.
170     
171     @param    id             <b>string</b>       Pref ID
172     @param    value          <b>mixed</b>        Pref value
173     @param    type           <b>string</b>       Pref type
174     @param    label          <b>string</b>       Pref label
175     @param    value_change   <b>boolean</b>      Change pref value or not
176     @param    global         <b>boolean</b>      Pref is global
177     */
178     public function put($id,$value,$type=null,$label=null,$value_change=true,$global=false)
179     {
180          if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/',$id)) {
181               throw new Exception(sprintf(__('%s is not a valid pref id'),$id));
182          }
183         
184          # We don't want to change pref value
185          if (!$value_change)
186          {
187               if (!$global && $this->prefExists($id,false)) {
188                    $value = $this->local_prefs[$id]['value'];
189               } elseif ($this->prefExists($id,true)) {
190                    $value = $this->global_prefs[$id]['value'];
191               }
192          }
193         
194          # Pref type
195          if ($type == 'double')
196          {
197               $type = 'float';
198          }
199          elseif ($type === null)
200          {
201               if (!$global && $this->prefExists($id,false)) {
202                    $type = $this->local_prefs[$id]['type'];
203               } elseif ($this->prefExists($id,true)) {
204                    $type = $this->global_prefs[$id]['type'];
205               } else {
206                    $type = 'string';
207               }
208          }
209          elseif ($type != 'boolean' && $type != 'integer' && $type != 'float')
210          {
211               $type = 'string';
212          }
213         
214          # We don't change label
215          if ($label == null)
216          {
217               if (!$global && $this->prefExists($id,false)) {
218                    $label = $this->local_prefs[$id]['label'];
219               } elseif ($this->prefExists($id,true)) {
220                    $label = $this->global_prefs[$id]['label'];
221               }
222          }
223         
224          settype($value,$type);
225         
226          $cur = $this->con->openCursor($this->table);
227          $cur->pref_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value;
228          $cur->pref_type = $type;
229          $cur->pref_label = $label;
230         
231          #If we are local, compare to global value
232          if (!$global && $this->prefExists($id,true))
233          {
234               $g = $this->global_prefs[$id];
235               $same_pref = $g['ws'] == $this->ws && $g['value'] == $value
236               && $g['type'] == $type && $g['label'] == $label;
237               
238               # Drop pref if same value as global
239               if ($same_pref && $this->prefExists($id,false)) {
240                    $this->drop($id);
241               } elseif ($same_pref) {
242                    return;
243               }
244          }
245         
246          if ($this->prefExists($id,$global) && $this->ws == $this->prefs[$id]['ws'])
247          {
248               if ($global) {
249                    $where = 'WHERE user_id IS NULL ';
250               } else {
251                    $where = "WHERE user_id = '".$this->con->escape($this->user_id)."' ";
252               }
253               
254               $cur->update($where."AND pref_id = '".$this->con->escape($id)."' AND pref_ws = '".$this->con->escape($this->ws)."' ");
255          }
256          else
257          {
258               $cur->pref_id = $id;
259               $cur->user_id = $global ? null : $this->user_id;
260               $cur->pref_ws = $this->ws;
261               
262               $cur->insert();
263          }
264     }
265     
266     /**
267     Removes an existing pref. Workspace
268     
269     @param    id        <b>string</b>       Pref ID
270     @param    force_global   <b>boolean</b> Force global pref drop
271     */
272     public function drop($id,$force_global=false)
273     {
274          if (!$this->ws) {
275               throw new Exception(__('No workspace specified'));
276          }
277         
278          $strReq = 'DELETE FROM '.$this->table.' ';
279         
280          if (($force_global) || ($this->user_id === null)) {
281               $strReq .= 'WHERE user_id IS NULL ';
282               $global = true;
283          } else {
284               $strReq .= "WHERE user_id = '".$this->con->escape($this->user_id)."' ";
285               $global = false;
286          }
287         
288          $strReq .= "AND pref_id = '".$this->con->escape($id)."' ";
289          $strReq .= "AND pref_ws = '".$this->con->escape($this->ws)."' ";
290         
291          $this->con->execute($strReq);
292         
293          if ($this->prefExists($id,$global)) {
294               $array = $global ? 'global' : 'local';
295               unset($this->{$array.'_prefs'}[$id]);
296          }
297
298          $this->prefs = $this->global_prefs;
299          foreach ($this->local_prefs as $id => $v) {
300               $this->prefs[$id] = $v;
301          }
302     }
303
304     /**
305     Removes all existing pref. in a Workspace
306     
307     @param    force_global   <b>boolean</b> Force global pref drop
308     */
309     public function dropAll($force_global=false)
310     {
311          if (!$this->ws) {
312               throw new Exception(__('No workspace specified'));
313          }
314         
315          $strReq = 'DELETE FROM '.$this->table.' ';
316         
317          if (($force_global) || ($this->user_id === null)) {
318               $strReq .= 'WHERE user_id IS NULL ';
319               $global = true;
320          } else {
321               $strReq .= "WHERE user_id = '".$this->con->escape($this->user_id)."' ";
322               $global = false;
323          }
324         
325          $strReq .= "AND pref_ws = '".$this->con->escape($this->ws)."' ";
326         
327          $this->con->execute($strReq);
328         
329          $array = $global ? 'global' : 'local';
330          unset($this->{$array.'_prefs'});
331          $this->{$array.'_prefs'} = array();
332         
333          $array = $global ? 'local' : 'global';
334          $this->prefs = $this->{$array.'_prefs'};
335     }
336     
337     /**
338     Returns $prefs property content.
339     
340     @return   <b>array</b>
341     */
342     public function dumpPrefs()
343     {
344          return $this->prefs;
345     }
346     
347     /**
348     Returns $local_prefs property content.
349     
350     @return   <b>array</b>
351     */
352     public function dumpLocalPrefs()
353     {
354          return $this->local_prefs;
355     }
356
357     /**
358     Returns $global_prefs property content.
359     
360     @return   <b>array</b>
361     */
362     public function dumpGlobalPrefs()
363     {
364          return $this->global_prefs;
365     }
366
367}
368?>
Note: See TracBrowser for help on using the repository browser.

Sites map