Dotclear

source: inc/core/class.dc.workspace.php @ 3225:f04d4b1a4dcf

Revision 3225:f04d4b1a4dcf, 10.3 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

Some info functions missing

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

Sites map