Dotclear

source: inc/core/class.dc.workspace.php @ 1179:a43a29427ef3

Revision 1179:a43a29427ef3, 9.5 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Update copyright notice

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          {
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     Rename an existing pref in a Workspace
268
269     @param    $oldId    <b>string</b>  Current pref name
270     @param    $newId    <b>string</b>  New pref name
271     @return   <b>boolean</b>
272     */
273     public function rename($oldId,$newId)
274     {
275          if (!$this->ws) {
276               throw new Exception(__('No workspace specified'));
277          }
278         
279          if (!array_key_exists($oldId,$this->prefs) || array_key_exists($newId,$this->prefs)) {
280               return false;
281          }
282
283          // Rename the pref in the prefs array
284          $this->prefs[$newId] = $this->prefs[$oldId];
285          unset($this->prefs[$oldId]);
286
287          // Rename the pref in the database
288          $strReq = 'UPDATE '.$this->table.
289               " SET pref_id = '".$this->con->escape($newId)."' ".
290               " WHERE pref_ws = '".$this->con->escape($this->ws)."' ".
291               " AND pref_id = '".$this->con->escape($oldId)."' ";
292          $this->con->execute($strReq);
293          return true;
294     }
295     
296     /**
297     Removes an existing pref. Workspace
298     
299     @param    id        <b>string</b>       Pref ID
300     @param    force_global   <b>boolean</b> Force global pref drop
301     */
302     public function drop($id,$force_global=false)
303     {
304          if (!$this->ws) {
305               throw new Exception(__('No workspace specified'));
306          }
307         
308          $strReq = 'DELETE FROM '.$this->table.' ';
309         
310          if (($force_global) || ($this->user_id === null)) {
311               $strReq .= 'WHERE user_id IS NULL ';
312               $global = true;
313          } else {
314               $strReq .= "WHERE user_id = '".$this->con->escape($this->user_id)."' ";
315               $global = false;
316          }
317         
318          $strReq .= "AND pref_id = '".$this->con->escape($id)."' ";
319          $strReq .= "AND pref_ws = '".$this->con->escape($this->ws)."' ";
320         
321          $this->con->execute($strReq);
322         
323          if ($this->prefExists($id,$global)) {
324               $array = $global ? 'global' : 'local';
325               unset($this->{$array.'_prefs'}[$id]);
326          }
327
328          $this->prefs = $this->global_prefs;
329          foreach ($this->local_prefs as $id => $v) {
330               $this->prefs[$id] = $v;
331          }
332     }
333
334     /**
335     Removes all existing pref. in a Workspace
336     
337     @param    force_global   <b>boolean</b> Force global pref drop
338     */
339     public function dropAll($force_global=false)
340     {
341          if (!$this->ws) {
342               throw new Exception(__('No workspace specified'));
343          }
344         
345          $strReq = 'DELETE FROM '.$this->table.' ';
346         
347          if (($force_global) || ($this->user_id === null)) {
348               $strReq .= 'WHERE user_id IS NULL ';
349               $global = true;
350          } else {
351               $strReq .= "WHERE user_id = '".$this->con->escape($this->user_id)."' ";
352               $global = false;
353          }
354         
355          $strReq .= "AND pref_ws = '".$this->con->escape($this->ws)."' ";
356         
357          $this->con->execute($strReq);
358         
359          $array = $global ? 'global' : 'local';
360          unset($this->{$array.'_prefs'});
361          $this->{$array.'_prefs'} = array();
362         
363          $array = $global ? 'local' : 'global';
364          $this->prefs = $this->{$array.'_prefs'};
365     }
366     
367     /**
368     Returns $prefs property content.
369     
370     @return   <b>array</b>
371     */
372     public function dumpPrefs()
373     {
374          return $this->prefs;
375     }
376     
377     /**
378     Returns $local_prefs property content.
379     
380     @return   <b>array</b>
381     */
382     public function dumpLocalPrefs()
383     {
384          return $this->local_prefs;
385     }
386
387     /**
388     Returns $global_prefs property content.
389     
390     @return   <b>array</b>
391     */
392     public function dumpGlobalPrefs()
393     {
394          return $this->global_prefs;
395     }
396
397}
398?>
Note: See TracBrowser for help on using the repository browser.

Sites map