| 1 | <?php |
|---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
|---|
| 3 | # |
|---|
| 4 | # This file is part of Dotclear 2. |
|---|
| 5 | # |
|---|
| 6 | # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear |
|---|
| 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 ----------------------------------------- |
|---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | @ingroup DC_CORE |
|---|
| 16 | @brief User prefs handler |
|---|
| 17 | |
|---|
| 18 | dcPrefs provides user preferences management. This class instance exists as |
|---|
| 19 | dcAuth $prefs property. You should create a new prefs instance when |
|---|
| 20 | updating another user prefs. |
|---|
| 21 | */ |
|---|
| 22 | class dcPrefs |
|---|
| 23 | { |
|---|
| 24 | protected $con; ///< <b>connection</b> Database connection object |
|---|
| 25 | protected $table; ///< <b>string</b> Prefs table name |
|---|
| 26 | protected $user_id; ///< <b>string</b> User ID |
|---|
| 27 | |
|---|
| 28 | protected $workspaces = array(); ///< <b>array</b> Associative workspaces array |
|---|
| 29 | |
|---|
| 30 | protected $ws; ///< <b>string</b> Current workspace |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | Object constructor. Retrieves user prefs and puts them in $workspaces |
|---|
| 34 | array. Local (user) prefs have a highest priority than global prefs. |
|---|
| 35 | |
|---|
| 36 | @param core <b>dcCore</b> dcCore object |
|---|
| 37 | @param user_id <b>string</b> User ID |
|---|
| 38 | */ |
|---|
| 39 | public function __construct($core,$user_id) |
|---|
| 40 | { |
|---|
| 41 | $this->con =& $core->con; |
|---|
| 42 | $this->table = $core->prefix.'pref'; |
|---|
| 43 | $this->user_id =& $user_id; |
|---|
| 44 | $this->loadPrefs(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | Retrieves all workspaces (and their prefs) from database, with one query. |
|---|
| 49 | */ |
|---|
| 50 | private function loadPrefs() |
|---|
| 51 | { |
|---|
| 52 | $strReq = 'SELECT user_id, pref_id, pref_value, '. |
|---|
| 53 | 'pref_type, pref_label, pref_ws '. |
|---|
| 54 | 'FROM '.$this->table.' '. |
|---|
| 55 | "WHERE user_id = '".$this->con->escape($this->user_id)."' ". |
|---|
| 56 | 'OR user_id IS NULL '. |
|---|
| 57 | 'ORDER BY pref_ws ASC, pref_id ASC'; |
|---|
| 58 | try { |
|---|
| 59 | $rs = $this->con->select($strReq); |
|---|
| 60 | } catch (Exception $e) { |
|---|
| 61 | trigger_error(__('Unable to retrieve workspaces:').' '.$this->con->error(), E_USER_ERROR); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /* Prevent empty tables (install phase, for instance) */ |
|---|
| 65 | if ($rs->isEmpty()) { |
|---|
| 66 | return; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | do { |
|---|
| 70 | $ws = trim($rs->f('pref_ws')); |
|---|
| 71 | if (!$rs->isStart()) { |
|---|
| 72 | // we have to go up 1 step, since workspaces construction performs a fetch() |
|---|
| 73 | // at very first time |
|---|
| 74 | $rs->movePrev(); |
|---|
| 75 | } |
|---|
| 76 | $this->workspaces[$ws] = new dcWorkspace($GLOBALS['core'], $this->user_id, $ws,$rs); |
|---|
| 77 | } while(!$rs->isStart()); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | Create a new workspace. If the workspace already exists, return it without modification. |
|---|
| 83 | |
|---|
| 84 | @param ws <b>string</b> Workspace name |
|---|
| 85 | @return <b>dcWorkspace</b> The workspace created |
|---|
| 86 | */ |
|---|
| 87 | public function addWorkspace($ws) |
|---|
| 88 | { |
|---|
| 89 | if (!array_key_exists($ws, $this->workspaces)) { |
|---|
| 90 | $this->workspaces[$ws] = new dcWorkspace($GLOBALS['core'], $this->user_id, $ws); |
|---|
| 91 | } |
|---|
| 92 | return $this->workspaces[$ws]; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | Returns full workspace with all prefs pertaining to it. |
|---|
| 97 | |
|---|
| 98 | @param ws <b>string</b> Workspace name |
|---|
| 99 | @return <b>dcWorkspace</b> |
|---|
| 100 | */ |
|---|
| 101 | public function get($ws) |
|---|
| 102 | { |
|---|
| 103 | if (array_key_exists($ws, $this->workspaces)) { |
|---|
| 104 | return $this->workspaces[$ws]; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | return null; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | Magic __get method. |
|---|
| 112 | @copydoc ::get |
|---|
| 113 | */ |
|---|
| 114 | public function __get($n) |
|---|
| 115 | { |
|---|
| 116 | return $this->get($n); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | Returns $workspaces property content. |
|---|
| 121 | |
|---|
| 122 | @return <b>array</b> |
|---|
| 123 | */ |
|---|
| 124 | public function dumpWorkspaces() |
|---|
| 125 | { |
|---|
| 126 | return $this->workspaces; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | } |
|---|
| 130 | ?> |
|---|