Dotclear

source: inc/core/class.dc.workspace.php @ 13:c7a0f4cc3aab

Revision 13:c7a0f4cc3aab, 7.6 KB checked in by franckpaul, 14 years ago (diff)

Mise en place des options d'affichage des modules du dashboard
Fix de la gestion des préférences utilisateur (onglets User profile et User options)

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

Sites map