Changeset 2566:9bf417837888 for inc/core/class.dc.workspace.php
- Timestamp:
- 11/17/13 20:25:53 (12 years ago)
- Branch:
- 2.6
- Children:
- 2567:6c11245cbf04, 2568:61c67a7d17fa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.workspace.php
r2229 r2566 22 22 protected $table; ///< <b>string</b> Preferences table name 23 23 protected $user_id; ///< <b>string</b> User ID 24 24 25 25 protected $global_prefs = array(); ///< <b>array</b> Global prefs array 26 26 protected $local_prefs = array(); ///< <b>array</b> Local prefs array 27 27 protected $prefs = array(); ///< <b>array</b> Associative prefs array 28 28 protected $ws; ///< <b>string</b> Current workspace 29 29 30 30 /** 31 31 Object constructor. Retrieves user prefs and puts them in $prefs 32 32 array. Local (user) prefs have a highest priority than global prefs. 33 33 34 34 @param name <b>string</b> ID for this workspace 35 35 */ … … 41 41 throw new Exception(sprintf(__('Invalid dcWorkspace: %s'),$name)); 42 42 } 43 43 44 44 $this->con =& $core->con; 45 45 $this->table = $core->prefix.'pref'; 46 46 $this->user_id =& $user_id; 47 47 48 48 try {$this->getPrefs($rs);} catch (Exception $e) { 49 49 if (version_compare($core->getVersion('core'),'2.3','>')) { … … 52 52 } 53 53 } 54 54 55 55 private function getPrefs($rs=null) 56 { 56 { 57 57 if ($rs == null) { 58 58 $strReq = 'SELECT user_id, pref_id, pref_value, '. … … 63 63 "AND pref_ws = '".$this->con->escape($this->ws)."' ". 64 64 'ORDER BY pref_id ASC '; 65 65 66 66 try { 67 67 $rs = $this->con->select($strReq); … … 78 78 $value = $rs->f('pref_value'); 79 79 $type = $rs->f('pref_type'); 80 80 81 81 if ($type == 'float' || $type == 'double') { 82 82 $type = 'float'; … … 84 84 $type = 'string'; 85 85 } 86 86 87 87 settype($value,$type); 88 88 89 89 $array = $rs->user_id ? 'local' : 'global'; 90 90 91 91 $this->{$array.'_prefs'}[$id] = array( 92 92 'ws' => $this->ws, … … 97 97 ); 98 98 } 99 99 100 100 $this->prefs = $this->global_prefs; 101 101 102 102 foreach ($this->local_prefs as $id => $v) { 103 103 $this->prefs[$id] = $v; 104 104 } 105 105 106 106 return true; 107 107 } 108 108 109 109 public function prefExists($id,$global=false) 110 110 { … … 112 112 return isset($this->{$array.'_prefs'}[$id]); 113 113 } 114 114 115 115 /** 116 116 Returns pref value if exists. 117 117 118 118 @param n <b>string</b> Pref name 119 119 @return <b>mixed</b> … … 124 124 return $this->prefs[$n]['value']; 125 125 } 126 126 127 127 return null; 128 128 } … … 130 130 /** 131 131 Returns global pref value if exists. 132 132 133 133 @param n <b>string</b> Pref name 134 134 @return <b>mixed</b> … … 139 139 return $this->global_prefs[$n]['value']; 140 140 } 141 141 142 142 return null; 143 143 } 144 144 145 145 /** 146 146 Returns local pref value if exists. 147 147 148 148 @param n <b>string</b> Pref name 149 149 @return <b>mixed</b> … … 154 154 return $this->local_prefs[$n]['value']; 155 155 } 156 156 157 157 return null; 158 } 158 } 159 159 /** 160 160 Magic __get method. … … 165 165 return $this->get($n); 166 166 } 167 167 168 168 /** 169 169 Sets a pref in $prefs property. This sets the pref for script 170 170 execution time only and if pref exists. 171 171 172 172 @param n <b>string</b> Pref name 173 173 @param v <b>mixed</b> Pref value … … 179 179 } 180 180 } 181 181 182 182 /** 183 183 Magic __set method. … … 188 188 $this->set($n,$v); 189 189 } 190 190 191 191 /** 192 192 Creates or updates a pref. 193 193 194 194 $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is 195 195 null and pref exists, it will keep current pref type. 196 196 197 197 $value_change allow you to not change pref. Useful if you need to change 198 198 a pref label or type and don't want to change its value. 199 199 200 200 @param id <b>string</b> Pref ID 201 201 @param value <b>mixed</b> Pref value … … 210 210 throw new Exception(sprintf(__('%s is not a valid pref id'),$id)); 211 211 } 212 212 213 213 # We don't want to change pref value 214 214 if (!$value_change) … … 220 220 } 221 221 } 222 222 223 223 # Pref type 224 224 if ($type == 'double') … … 240 240 $type = 'string'; 241 241 } 242 242 243 243 # We don't change label 244 244 if ($label == null) … … 250 250 } 251 251 } 252 252 253 253 settype($value,$type); 254 254 255 255 $cur = $this->con->openCursor($this->table); 256 256 $cur->pref_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value; 257 257 $cur->pref_type = $type; 258 258 $cur->pref_label = $label; 259 259 260 260 #If we are local, compare to global value 261 261 if (!$global && $this->prefExists($id,true)) … … 264 264 $same_pref = $g['ws'] == $this->ws && $g['value'] == $value 265 265 && $g['type'] == $type && $g['label'] == $label; 266 266 267 267 # Drop pref if same value as global 268 268 if ($same_pref && $this->prefExists($id,false)) { … … 272 272 } 273 273 } 274 274 275 275 if ($this->prefExists($id,$global) && $this->ws == $this->prefs[$id]['ws']) 276 276 { … … 280 280 $where = "WHERE user_id = '".$this->con->escape($this->user_id)."' "; 281 281 } 282 282 283 283 $cur->update($where."AND pref_id = '".$this->con->escape($id)."' AND pref_ws = '".$this->con->escape($this->ws)."' "); 284 284 } … … 288 288 $cur->user_id = $global ? null : $this->user_id; 289 289 $cur->pref_ws = $this->ws; 290 290 291 291 $cur->insert(); 292 292 } … … 305 305 throw new Exception(__('No workspace specified')); 306 306 } 307 307 308 308 if (!array_key_exists($oldId,$this->prefs) || array_key_exists($newId,$this->prefs)) { 309 309 return false; … … 322 322 return true; 323 323 } 324 325 /** 326 Removes an existing pref. Workspace 327 324 325 /** 326 Removes an existing pref. Workspace 327 328 328 @param id <b>string</b> Pref ID 329 329 @param force_global <b>boolean</b> Force global pref drop … … 334 334 throw new Exception(__('No workspace specified')); 335 335 } 336 336 337 337 $strReq = 'DELETE FROM '.$this->table.' '; 338 338 339 339 if (($force_global) || ($this->user_id === null)) { 340 340 $strReq .= 'WHERE user_id IS NULL '; … … 344 344 $global = false; 345 345 } 346 346 347 347 $strReq .= "AND pref_id = '".$this->con->escape($id)."' "; 348 348 $strReq .= "AND pref_ws = '".$this->con->escape($this->ws)."' "; 349 349 350 350 $this->con->execute($strReq); 351 351 352 352 if ($this->prefExists($id,$global)) { 353 353 $array = $global ? 'global' : 'local'; … … 362 362 363 363 /** 364 Removes all existing pref. in a Workspace 365 364 Removes all existing pref. in a Workspace 365 366 366 @param force_global <b>boolean</b> Force global pref drop 367 367 */ … … 371 371 throw new Exception(__('No workspace specified')); 372 372 } 373 373 374 374 $strReq = 'DELETE FROM '.$this->table.' '; 375 375 376 376 if (($force_global) || ($this->user_id === null)) { 377 377 $strReq .= 'WHERE user_id IS NULL '; … … 381 381 $global = false; 382 382 } 383 383 384 384 $strReq .= "AND pref_ws = '".$this->con->escape($this->ws)."' "; 385 385 386 386 $this->con->execute($strReq); 387 387 388 388 $array = $global ? 'global' : 'local'; 389 389 unset($this->{$array.'_prefs'}); 390 390 $this->{$array.'_prefs'} = array(); 391 391 392 392 $array = $global ? 'local' : 'global'; 393 393 $this->prefs = $this->{$array.'_prefs'}; 394 394 } 395 395 396 396 /** 397 397 Returns $prefs property content. 398 398 399 399 @return <b>array</b> 400 400 */ … … 403 403 return $this->prefs; 404 404 } 405 405 406 406 /** 407 407 Returns $local_prefs property content. 408 408 409 409 @return <b>array</b> 410 410 */ … … 416 416 /** 417 417 Returns $global_prefs property content. 418 418 419 419 @return <b>array</b> 420 420 */ … … 425 425 426 426 } 427 ?>
Note: See TracChangeset
for help on using the changeset viewer.