1 | <?php |
---|
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 | |
---|
16 | if (!defined('DC_RC_PATH')) {return;} |
---|
17 | |
---|
18 | class dcPrefs |
---|
19 | { |
---|
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 |
---|
23 | |
---|
24 | protected $workspaces = array(); ///< <b>array</b> Associative workspaces array |
---|
25 | |
---|
26 | protected $ws; ///< <b>string</b> Current workspace |
---|
27 | |
---|
28 | /** |
---|
29 | Object constructor. Retrieves user prefs and puts them in $workspaces |
---|
30 | array. Local (user) prefs have a highest priority than global prefs. |
---|
31 | |
---|
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 | } |
---|
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 | throw $e; |
---|
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 | Create a new workspace. If the workspace already exists, return it without modification. |
---|
82 | |
---|
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 | } |
---|
93 | |
---|
94 | /** |
---|
95 | Rename a workspace. |
---|
96 | |
---|
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 | } |
---|
106 | |
---|
107 | // Rename the workspace in the workspace array |
---|
108 | $this->workspaces[$newWs] = $this->workspaces[$oldWs]; |
---|
109 | unset($this->workspaces[$oldWs]); |
---|
110 | |
---|
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 | } |
---|
118 | |
---|
119 | /** |
---|
120 | Delete a whole workspace with all preferences pertaining to it. |
---|
121 | |
---|
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 | } |
---|
130 | |
---|
131 | // Remove the workspace from the workspace array |
---|
132 | unset($this->workspaces[$ws]); |
---|
133 | |
---|
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 | } |
---|
140 | |
---|
141 | /** |
---|
142 | Returns full workspace with all prefs pertaining to it. |
---|
143 | |
---|
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 | } |
---|
152 | |
---|
153 | return; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | Magic __get method. |
---|
158 | @copydoc ::get |
---|
159 | */ |
---|
160 | public function __get($n) |
---|
161 | { |
---|
162 | return $this->get($n); |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | Returns $workspaces property content. |
---|
167 | |
---|
168 | @return <b>array</b> |
---|
169 | */ |
---|
170 | public function dumpWorkspaces() |
---|
171 | { |
---|
172 | return $this->workspaces; |
---|
173 | } |
---|
174 | |
---|
175 | } |
---|