[ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of DotClear. 4 # Copyright (c) 2005 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # DotClear is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # DotClear is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with DotClear; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 /** 24 @ingroup DC_CORE 25 @brief Blog settings handler 26 27 dcSettings provides blog settings management. This class instance exists as 28 dcBlog $settings property. You should create a new settings instance when 29 updating another blog settings. 30 */ 31 class dcSettings 32 { 33 protected $con; ///< <b>connection</b> Database connection object 34 protected $table; ///< <b>string</b> Permission table name 35 protected $blog_id; ///< <b>string</b> Blog ID 36 37 protected $settings = array(); ///< <b>array</b> Associative settings array 38 protected $global_settings = array(); ///< <b>array</b> Global settings array 39 protected $local_settings = array(); ///< <b>array</b> Local settings array 40 41 protected $ns; ///< <b>string</b> Current namespace 42 43 /** 44 Object constructor. Retrieves blog settings and puts them in $settings 45 array. Local (blog) settings have a highest priority than global settings. 46 47 @param core <b>dcCore</b> dcCore object 48 @param blog_id <b>string</b> Blog ID 49 */ 50 public function __construct(&$core,$blog_id) 51 { 52 $this->con =& $core->con; 53 $this->table = $core->prefix.'setting'; 54 $this->blog_id =& $blog_id; 55 56 $this->getSettings(); 57 } 58 59 private function getSettings() 60 { 61 $strReq = 'SELECT blog_id, setting_id, setting_ns, setting_value, '. 62 'setting_type, setting_label '. 63 'FROM '.$this->table.' '. 64 "WHERE blog_id = '".$this->con->escape($this->blog_id)."' ". 65 'OR blog_id IS NULL '. 66 'ORDER BY setting_ns, setting_id DESC '; 67 68 try { 69 $rs = $this->con->select($strReq); 70 } catch (Exception $e) { 71 trigger_error(__('Unable to retrieve settings: ').$this->con->error(), E_USER_ERROR); 72 } 73 74 while ($rs->fetch()) 75 { 76 $id = trim($rs->f('setting_id')); 77 $value = $rs->f('setting_value'); 78 $type = $rs->f('setting_type'); 79 80 if ($type == 'float' || $type == 'double') { 81 $type = 'float'; 82 } elseif ($type != 'boolean' && $type != 'integer') { 83 $type = 'string'; 84 } 85 86 settype($value,$type); 87 88 $array = $rs->blog_id ? 'local' : 'global'; 89 90 $this->{$array.'_settings'}[$id] = array( 91 'ns' => $rs->f('setting_ns'), 92 'value' => $value, 93 'type' => $type, 94 'label' => (string) $rs->f('setting_label'), 95 'global' => $rs->blog_id == '' 96 ); 97 } 98 99 $this->settings = $this->global_settings; 100 101 foreach ($this->local_settings as $id => $v) { 102 $this->settings[$id] = $v; 103 } 104 105 return true; 106 } 107 108 private function settingExists($id,$global=false) 109 { 110 $array = $global ? 'global' : 'local'; 111 return isset($this->{$array.'_settings'}[$id]); 112 } 113 114 /** 115 Sets a working namespace. You should do this before adding any setting. 116 117 @param ns <b>string</b> Namespace name 118 */ 119 public function setNamespace($ns) 120 { 121 if (preg_match('/^[a-z]+$/',$ns)) { 122 $this->ns = $ns; 123 } 124 } 125 126 /** 127 Creates or updates a setting. 128 129 $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is 130 null and setting exists, it will keep current setting type. 131 132 $value_change allow you to not change setting. Useful if you need to change 133 a setting label or type and don't want to change its value. 134 135 Don't forget to set namespace before calling this method. 136 137 @param id <b>string</b> Setting ID 138 @param value <b>mixed</b> Setting value 139 @param type <b>string</b> Setting type 140 @param label <b>string</b> Setting label 141 @param value_change <b>boolean</b> Change setting value or not 142 @param global <b>boolean</b> Setting is global 143 */ 144 public function put($id,$value,$type=null,$label=null,$value_change=true,$global=false) 145 { 146 if (!$this->ns) { 147 throw new Exception(__('No namespace specified')); 148 } 149 150 if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/',$id)) { 151 throw new Exception(sprintf(__('%s is not a valid setting id'),$id)); 152 } 153 154 # We don't want to change setting value 155 if (!$value_change) 156 { 157 if (!$global && $this->settingExists($id,false)) { 158 $value = $this->local_settings[$id]['value']; 159 } elseif ($this->settingExists($id,true)) { 160 $value = $this->global_settings[$id]['value']; 161 } 162 } 163 164 # Setting type 165 if ($type == 'double') 166 { 167 $type = 'float'; 168 } 169 elseif ($type === null) 170 { 171 if (!$global && $this->settingExists($id,false)) { 172 $type = $this->local_settings[$id]['type']; 173 } elseif ($this->settingExists($id,true)) { 174 $type = $this->global_settings[$id]['type']; 175 } else { 176 $type = 'string'; 177 } 178 } 179 elseif ($type != 'boolean' && $type != 'integer' && $type != 'float') 180 { 181 $type = 'string'; 182 } 183 184 # We don't change label 185 if ($label == null) 186 { 187 if (!$global && $this->settingExists($id,false)) { 188 $label = $this->local_settings[$id]['label']; 189 } elseif ($this->settingExists($id,true)) { 190 $label = $this->global_settings[$id]['label']; 191 } 192 } 193 194 settype($value,$type); 195 196 $cur = $this->con->openCursor($this->table); 197 $cur->setting_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value; 198 $cur->setting_type = $type; 199 $cur->setting_label = $label; 200 201 #If we are local, compare to global value 202 if (!$global && $this->settingExists($id,true)) 203 { 204 $g = $this->global_settings[$id]; 205 $same_setting = $g['ns'] == $this->ns && $g['value'] == $value 206 && $g['type'] == $type && $g['label'] == $label; 207 208 # Drop setting if same value as global 209 if ($same_setting && $this->settingExists($id,false)) { 210 $this->drop($id); 211 } elseif ($same_setting) { 212 return; 213 } 214 } 215 216 if ($this->settingExists($id,$global) && $this->ns == $this->settings[$id]['ns']) 217 { 218 if ($global) { 219 $where = 'WHERE blog_id IS NULL '; 220 } else { 221 $where = "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; 222 } 223 224 $cur->update($where."AND setting_id = '".$this->con->escape($id)."' "); 225 } 226 else 227 { 228 $cur->setting_id = $id; 229 $cur->blog_id = $global ? null : $this->blog_id; 230 $cur->setting_ns = $this->ns; 231 232 $cur->insert(); 233 } 234 } 235 236 /** 237 Removes an existing setting. Namespace 238 239 @param id <b>string</b> Setting ID 240 */ 241 public function drop($id) 242 { 243 $strReq = 'DELETE FROM '.$this->table.' '; 244 245 if ($this->blog_id === null) { 246 $strReq .= 'WHERE blog_id IS NULL '; 247 } else { 248 $strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; 249 } 250 251 $strReq .= "AND setting_id = '".$this->con->escape($id)."' "; 252 253 $this->con->execute($strReq); 254 } 255 256 /** 257 Returns setting value if exists. 258 259 @param n <b>string</b> Setting name 260 @return <b>mixed</b> 261 */ 262 public function get($n) 263 { 264 if (isset($this->settings[$n]['value'])) { 265 return $this->settings[$n]['value']; 266 } 267 268 return null; 269 } 270 271 /** 272 Magic __get method. 273 @copydoc ::get 274 */ 275 public function __get($n) 276 { 277 return $this->get($n); 278 } 279 280 /** 281 Sets a setting in $settings property. This sets the setting for script 282 execution time only and if setting exists. 283 284 @param n <b>string</b> Setting name 285 @param v <b>mixed</b> Setting value 286 */ 287 public function set($n,$v) 288 { 289 if (isset($this->settings[$n])) { 290 $this->settings[$n]['value'] = $v; 291 } 292 } 293 294 /** 295 Magic __set method. 296 @copydoc ::set 297 */ 298 public function __set($n,$v) 299 { 300 $this->set($n,$v); 301 } 302 303 /** 304 Returns $settings property content. 305 306 @return <b>array</b> 307 */ 308 public function dumpSettings() 309 { 310 return $this->settings; 311 } 312 313 /** 314 Returns $global_settings property content. 315 316 @return <b>array</b> 317 */ 318 public function dumpGlobalSettings() 319 { 320 return $this->global_settings; 321 } 322 323 /** 324 @pre 325 toto 326 @endpre 327 */ 328 } 329 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |