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