[3] | 1 | <?php |
---|
[3731] | 2 | /** |
---|
| 3 | * @brief User prefs handler |
---|
| 4 | * |
---|
| 5 | * dcPrefs provides user preferences management. This class instance exists as |
---|
| 6 | * dcAuth $prefs property. You should create a new prefs instance when |
---|
| 7 | * updating another user prefs. |
---|
| 8 | * |
---|
| 9 | * @package Dotclear |
---|
| 10 | * @subpackage Core |
---|
| 11 | * |
---|
| 12 | * @copyright Olivier Meunier & Association Dotclear |
---|
| 13 | * @copyright GPL-2.0-only |
---|
| 14 | */ |
---|
| 15 | |
---|
[3730] | 16 | if (!defined('DC_RC_PATH')) {return;} |
---|
[3] | 17 | |
---|
| 18 | class dcPrefs |
---|
| 19 | { |
---|
[3730] | 20 | protected $con; ///< <b>connection</b> Database connection object |
---|
| 21 | protected $table; ///< <b>string</b> Prefs table name |
---|
| 22 | protected $user_id; ///< <b>string</b> User ID |
---|
[2566] | 23 | |
---|
[3730] | 24 | protected $workspaces = array(); ///< <b>array</b> Associative workspaces array |
---|
[2566] | 25 | |
---|
[3730] | 26 | protected $ws; ///< <b>string</b> Current workspace |
---|
[2566] | 27 | |
---|
[3730] | 28 | /** |
---|
| 29 | Object constructor. Retrieves user prefs and puts them in $workspaces |
---|
| 30 | array. Local (user) prefs have a highest priority than global prefs. |
---|
[2566] | 31 | |
---|
[3730] | 32 | @param core <b>dcCore</b> dcCore object |
---|
| 33 | @param user_id <b>string</b> User ID |
---|
| 34 | */ |
---|
| 35 | public function __construct($core, $user_id) |
---|
| 36 | { |
---|
| 37 | $this->con = &$core->con; |
---|
| 38 | $this->table = $core->prefix . 'pref'; |
---|
| 39 | $this->user_id = &$user_id; |
---|
| 40 | try { $this->loadPrefs();} catch (Exception $e) { |
---|
| 41 | if (version_compare($core->getVersion('core'), '2.3', '>')) { |
---|
| 42 | trigger_error(__('Unable to retrieve workspaces:') . ' ' . $this->con->error(), E_USER_ERROR); |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | } |
---|
[2566] | 46 | |
---|
[3730] | 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 | throw $e; |
---|
| 62 | } |
---|
[2566] | 63 | |
---|
[3730] | 64 | /* Prevent empty tables (install phase, for instance) */ |
---|
| 65 | if ($rs->isEmpty()) { |
---|
| 66 | return; |
---|
| 67 | } |
---|
[2566] | 68 | |
---|
[3730] | 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 | } |
---|
[2566] | 79 | |
---|
[3730] | 80 | /** |
---|
| 81 | Create a new workspace. If the workspace already exists, return it without modification. |
---|
[2566] | 82 | |
---|
[3730] | 83 | @param ws <b>string</b> Workspace name |
---|
| 84 | @return <b>dcWorkspace</b> The workspace created |
---|
| 85 | */ |
---|
| 86 | public function addWorkspace($ws) |
---|
| 87 | { |
---|
| 88 | if (!array_key_exists($ws, $this->workspaces)) { |
---|
| 89 | $this->workspaces[$ws] = new dcWorkspace($GLOBALS['core'], $this->user_id, $ws); |
---|
| 90 | } |
---|
| 91 | return $this->workspaces[$ws]; |
---|
| 92 | } |
---|
[2566] | 93 | |
---|
[3730] | 94 | /** |
---|
| 95 | Rename a workspace. |
---|
[2566] | 96 | |
---|
[3730] | 97 | @param oldWs <b>string</b> Old workspace name |
---|
| 98 | @param newws <b>string</b> New workspace name |
---|
| 99 | @return <b>boolean</b> |
---|
| 100 | */ |
---|
| 101 | public function renWorkspace($oldNs, $newNs) |
---|
| 102 | { |
---|
| 103 | if (!array_key_exists($oldWs, $this->workspaces) || array_key_exists($newWs, $this->workspaces)) { |
---|
| 104 | return false; |
---|
| 105 | } |
---|
[934] | 106 | |
---|
[3730] | 107 | // Rename the workspace in the workspace array |
---|
| 108 | $this->workspaces[$newWs] = $this->workspaces[$oldWs]; |
---|
| 109 | unset($this->workspaces[$oldWs]); |
---|
[934] | 110 | |
---|
[3730] | 111 | // Rename the workspace in the database |
---|
| 112 | $strReq = 'UPDATE ' . $this->table . |
---|
| 113 | " SET pref_ws = '" . $this->con->escape($newWs) . "' " . |
---|
| 114 | " WHERE pref_ws = '" . $this->con->escape($oldWs) . "' "; |
---|
| 115 | $this->con->execute($strReq); |
---|
| 116 | return true; |
---|
| 117 | } |
---|
[934] | 118 | |
---|
[3730] | 119 | /** |
---|
| 120 | Delete a whole workspace with all preferences pertaining to it. |
---|
[934] | 121 | |
---|
[3730] | 122 | @param ws <b>string</b> Workspace name |
---|
| 123 | @return <b>boolean</b> |
---|
| 124 | */ |
---|
| 125 | public function delWorkspace($ws) |
---|
| 126 | { |
---|
| 127 | if (!array_key_exists($ws, $this->workspaces)) { |
---|
| 128 | return false; |
---|
| 129 | } |
---|
[934] | 130 | |
---|
[3730] | 131 | // Remove the workspace from the workspace array |
---|
| 132 | unset($this->workspaces[$ws]); |
---|
[934] | 133 | |
---|
[3730] | 134 | // Delete all preferences from the workspace in the database |
---|
| 135 | $strReq = 'DELETE FROM ' . $this->table . |
---|
| 136 | " WHERE pref_ws = '" . $this->con->escape($ws) . "' "; |
---|
| 137 | $this->con->execute($strReq); |
---|
| 138 | return true; |
---|
| 139 | } |
---|
[934] | 140 | |
---|
[3730] | 141 | /** |
---|
| 142 | Returns full workspace with all prefs pertaining to it. |
---|
[2566] | 143 | |
---|
[3730] | 144 | @param ws <b>string</b> Workspace name |
---|
| 145 | @return <b>dcWorkspace</b> |
---|
| 146 | */ |
---|
| 147 | public function get($ws) |
---|
| 148 | { |
---|
| 149 | if (array_key_exists($ws, $this->workspaces)) { |
---|
| 150 | return $this->workspaces[$ws]; |
---|
| 151 | } |
---|
[2566] | 152 | |
---|
[3730] | 153 | return; |
---|
| 154 | } |
---|
[2566] | 155 | |
---|
[3730] | 156 | /** |
---|
| 157 | Magic __get method. |
---|
| 158 | @copydoc ::get |
---|
| 159 | */ |
---|
| 160 | public function __get($n) |
---|
| 161 | { |
---|
| 162 | return $this->get($n); |
---|
| 163 | } |
---|
[2566] | 164 | |
---|
[3730] | 165 | /** |
---|
| 166 | Returns $workspaces property content. |
---|
[2566] | 167 | |
---|
[3730] | 168 | @return <b>array</b> |
---|
| 169 | */ |
---|
| 170 | public function dumpWorkspaces() |
---|
| 171 | { |
---|
| 172 | return $this->workspaces; |
---|
| 173 | } |
---|
[2566] | 174 | |
---|
[3] | 175 | } |
---|