Changeset 2505:62775923ddff
- Timestamp:
- 10/30/13 10:12:11 (12 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.namespace.php
r1179 r2505 22 22 protected $table; ///< <b>string</b> Settings table name 23 23 protected $blog_id; ///< <b>string</b> Blog ID 24 24 25 25 protected $global_settings = array(); ///< <b>array</b> Global settings array 26 26 protected $local_settings = array(); ///< <b>array</b> Local settings array 27 27 protected $settings = array(); ///< <b>array</b> Associative settings array 28 28 protected $ns; ///< <b>string</b> Current namespace 29 29 30 30 /** 31 31 Object constructor. Retrieves blog settings and puts them in $settings 32 32 array. Local (blog) settings have a highest priority than global settings. 33 33 34 34 @param name <b>string</b> ID for this namespace 35 35 */ … … 41 41 throw new Exception(sprintf(__('Invalid setting dcNamespace: %s'),$name)); 42 42 } 43 43 44 44 $this->con =& $core->con; 45 45 $this->table = $core->prefix.'setting'; 46 46 $this->blog_id =& $blog_id; 47 47 48 48 $this->getSettings($rs); 49 49 } 50 50 51 51 private function getSettings($rs=null) 52 { 52 { 53 53 if ($rs == null) { 54 54 $strReq = 'SELECT blog_id, setting_id, setting_value, '. … … 59 59 "AND setting_ns = '".$this->con->escape($this->ns)."' ". 60 60 'ORDER BY setting_id DESC '; 61 61 62 62 try { 63 63 $rs = $this->con->select($strReq); … … 74 74 $value = $rs->f('setting_value'); 75 75 $type = $rs->f('setting_type'); 76 76 77 77 if ($type == 'float' || $type == 'double') { 78 78 $type = 'float'; … … 80 80 $type = 'string'; 81 81 } 82 82 83 83 settype($value,$type); 84 84 85 85 $array = $rs->blog_id ? 'local' : 'global'; 86 86 87 87 $this->{$array.'_settings'}[$id] = array( 88 88 'ns' => $this->ns, … … 93 93 ); 94 94 } 95 95 96 96 $this->settings = $this->global_settings; 97 97 98 98 foreach ($this->local_settings as $id => $v) { 99 99 $this->settings[$id] = $v; 100 100 } 101 101 102 102 return true; 103 103 } 104 104 105 105 private function settingExists($id,$global=false) 106 106 { … … 108 108 return isset($this->{$array.'_settings'}[$id]); 109 109 } 110 110 111 111 /** 112 112 Returns setting value if exists. 113 113 114 114 @param n <b>string</b> Setting name 115 115 @return <b>mixed</b> … … 120 120 return $this->settings[$n]['value']; 121 121 } 122 122 123 123 return null; 124 124 } 125 125 126 /** 127 Returns global setting value if exists. 128 129 @param n <b>string</b> setting name 130 @return <b>mixed</b> 131 */ 132 public function getGlobal($n) 133 { 134 if (isset($this->global_settings[$n]['value'])) { 135 return $this->global_settings[$n]['value']; 136 } 137 138 return null; 139 } 140 141 /** 142 Returns local setting value if exists. 143 144 @param n <b>string</b> setting name 145 @return <b>mixed</b> 146 */ 147 public function getLocal($n) 148 { 149 if (isset($this->local_settings[$n]['value'])) { 150 return $this->local_settings[$n]['value']; 151 } 152 153 return null; 154 } 155 126 156 /** 127 157 Magic __get method. … … 132 162 return $this->get($n); 133 163 } 134 164 135 165 /** 136 166 Sets a setting in $settings property. This sets the setting for script 137 167 execution time only and if setting exists. 138 168 139 169 @param n <b>string</b> Setting name 140 170 @param v <b>mixed</b> Setting value … … 146 176 } 147 177 } 148 178 149 179 /** 150 180 Magic __set method. … … 155 185 $this->set($n,$v); 156 186 } 157 187 158 188 /** 159 189 Creates or updates a setting. 160 190 161 191 $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is 162 192 null and setting exists, it will keep current setting type. 163 193 164 194 $value_change allow you to not change setting. Useful if you need to change 165 195 a setting label or type and don't want to change its value. 166 196 167 197 @param id <b>string</b> Setting ID 168 198 @param value <b>mixed</b> Setting value … … 177 207 throw new Exception(sprintf(__('%s is not a valid setting id'),$id)); 178 208 } 179 209 180 210 # We don't want to change setting value 181 211 if (!$value_change) … … 187 217 } 188 218 } 189 219 190 220 # Setting type 191 221 if ($type == 'double') … … 207 237 $type = 'string'; 208 238 } 209 239 210 240 # We don't change label 211 241 if ($label == null) … … 217 247 } 218 248 } 219 249 220 250 settype($value,$type); 221 251 222 252 $cur = $this->con->openCursor($this->table); 223 253 $cur->setting_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value; 224 254 $cur->setting_type = $type; 225 255 $cur->setting_label = $label; 226 256 227 257 #If we are local, compare to global value 228 258 if (!$global && $this->settingExists($id,true)) … … 231 261 $same_setting = $g['ns'] == $this->ns && $g['value'] == $value 232 262 && $g['type'] == $type && $g['label'] == $label; 233 263 234 264 # Drop setting if same value as global 235 265 if ($same_setting && $this->settingExists($id,false)) { … … 239 269 } 240 270 } 241 271 242 272 if ($this->settingExists($id,$global) && $this->ns == $this->settings[$id]['ns']) 243 273 { … … 247 277 $where = "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; 248 278 } 249 279 250 280 $cur->update($where."AND setting_id = '".$this->con->escape($id)."' AND setting_ns = '".$this->con->escape($this->ns)."' "); 251 281 } … … 255 285 $cur->blog_id = $global ? null : $this->blog_id; 256 286 $cur->setting_ns = $this->ns; 257 287 258 288 $cur->insert(); 259 289 } … … 272 302 throw new Exception(__('No namespace specified')); 273 303 } 274 304 275 305 if (!array_key_exists($oldId,$this->settings) || array_key_exists($newId,$this->settings)) { 276 306 return false; … … 291 321 292 322 /** 293 Removes an existing setting in a Namespace 294 323 Removes an existing setting in a Namespace 324 295 325 @param id <b>string</b> Setting ID 296 326 */ … … 300 330 throw new Exception(__('No namespace specified')); 301 331 } 302 332 303 333 $strReq = 'DELETE FROM '.$this->table.' '; 304 334 305 335 if ($this->blog_id === null) { 306 336 $strReq .= 'WHERE blog_id IS NULL '; … … 308 338 $strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; 309 339 } 310 340 311 341 $strReq .= "AND setting_id = '".$this->con->escape($id)."' "; 312 342 $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; 313 343 314 344 $this->con->execute($strReq); 315 345 } 316 317 /** 318 Removes all existing settings in a Namespace 319 346 347 /** 348 Removes all existing settings in a Namespace 349 320 350 @param force_global <b>boolean</b> Force global pref drop 321 351 */ … … 325 355 throw new Exception(__('No namespace specified')); 326 356 } 327 357 328 358 $strReq = 'DELETE FROM '.$this->table.' '; 329 359 330 360 if (($force_global) || ($this->blog_id === null)) { 331 361 $strReq .= 'WHERE blog_id IS NULL '; … … 335 365 $global = false; 336 366 } 337 367 338 368 $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; 339 369 340 370 $this->con->execute($strReq); 341 371 342 372 $array = $global ? 'global' : 'local'; 343 373 unset($this->{$array.'_settings'}); 344 374 $this->{$array.'_settings'} = array(); 345 375 346 376 $array = $global ? 'local' : 'global'; 347 377 $this->settings = $this->{$array.'_settings'}; 348 378 } 349 379 350 380 /** 351 381 Returns $settings property content. 352 382 353 383 @return <b>array</b> 354 384 */ … … 357 387 return $this->settings; 358 388 } 359 389 360 390 /** 361 391 Returns $global_settings property content. 362 392 363 393 @return <b>array</b> 364 394 */
Note: See TracChangeset
for help on using the changeset viewer.