Dotclear

source: inc/core/class.dc.workspace.php @ 141:340bbc2cd9f4

Revision 141:340bbc2cd9f4, 8.8 KB checked in by xave <xave@…>, 14 years ago (diff)

Deported the error handling found in constructors. Now we can upgrade.

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

Sites map