]>
Commit | Line | Data |
---|---|---|
14890de3 | 1 | <?php |
2 | /** | |
c95b78a8 | 3 | * wallabag, self hostable application allowing you to not miss any content anymore |
14890de3 | 4 | * |
c95b78a8 NL |
5 | * @category wallabag |
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | |
14890de3 | 7 | * @copyright 2013 |
3602405e | 8 | * @license http://opensource.org/licenses/MIT see COPYING file |
14890de3 | 9 | */ |
10 | ||
bc1ee852 | 11 | class Database { |
b3cda72e | 12 | |
14890de3 | 13 | var $handle; |
4e067cea NL |
14 | private $order = array ( |
15 | 'ia' => 'ORDER BY entries.id', | |
16 | 'id' => 'ORDER BY entries.id DESC', | |
17 | 'ta' => 'ORDER BY lower(entries.title)', | |
18 | 'td' => 'ORDER BY lower(entries.title) DESC', | |
19 | 'default' => 'ORDER BY entries.id' | |
032e0ca1 MR |
20 | ); |
21 | ||
182faf26 | 22 | function __construct() |
68857cea NL |
23 | { |
24 | switch (STORAGE) { | |
25 | case 'sqlite': | |
211068ce | 26 | // Check if /db is writeable |
b668db24 | 27 | if ( !is_writable(STORAGE_SQLITE) || !is_writable(dirname(STORAGE_SQLITE))) { |
211068ce MR |
28 | die('An error occured: "db" directory must be writeable for your web server user!'); |
29 | } | |
68857cea NL |
30 | $db_path = 'sqlite:' . STORAGE_SQLITE; |
31 | $this->handle = new PDO($db_path); | |
32 | break; | |
33 | case 'mysql': | |
b668db24 RR |
34 | $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB . ';charset=utf8mb4'; |
35 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD, array( | |
36 | PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4', | |
37 | )); | |
68857cea NL |
38 | break; |
39 | case 'postgres': | |
40 | $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; | |
182faf26 | 41 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); |
68857cea | 42 | break; |
8af31ae0 | 43 | default: |
44 | die(STORAGE . ' is not a recognised database system !'); | |
68857cea NL |
45 | } |
46 | ||
14890de3 | 47 | $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
3602405e | 48 | $this->_checkTags(); |
580d60b9 | 49 | Tools::logm('storage type ' . STORAGE); |
14890de3 | 50 | } |
51 | ||
b3cda72e NL |
52 | private function getHandle() |
53 | { | |
14890de3 | 54 | return $this->handle; |
55 | } | |
56 | ||
b3cda72e NL |
57 | private function _checkTags() |
58 | { | |
5cfafc61 NL |
59 | |
60 | if (STORAGE == 'sqlite') { | |
61 | $sql = ' | |
62 | CREATE TABLE IF NOT EXISTS tags ( | |
63 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, | |
64 | value TEXT | |
65 | )'; | |
66 | } | |
67 | elseif(STORAGE == 'mysql') { | |
68 | $sql = ' | |
69 | CREATE TABLE IF NOT EXISTS `tags` ( | |
70 | `id` int(11) NOT NULL AUTO_INCREMENT, | |
71 | `value` varchar(255) NOT NULL, | |
72 | PRIMARY KEY (`id`) | |
73 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
74 | '; | |
75 | } | |
76 | else { | |
77 | $sql = ' | |
5ce39784 | 78 | CREATE TABLE IF NOT EXISTS tags ( |
5cfafc61 NL |
79 | id bigserial primary key, |
80 | value varchar(255) NOT NULL | |
81 | ); | |
82 | '; | |
83 | } | |
aa8c9f2a | 84 | |
5cfafc61 NL |
85 | $query = $this->executeQuery($sql, array()); |
86 | ||
87 | if (STORAGE == 'sqlite') { | |
88 | $sql = ' | |
a562e390 | 89 | CREATE TABLE IF NOT EXISTS tags_entries ( |
5cfafc61 NL |
90 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, |
91 | entry_id INTEGER, | |
92 | tag_id INTEGER, | |
93 | FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE, | |
94 | FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE | |
95 | )'; | |
96 | } | |
97 | elseif(STORAGE == 'mysql') { | |
98 | $sql = ' | |
99 | CREATE TABLE IF NOT EXISTS `tags_entries` ( | |
100 | `id` int(11) NOT NULL AUTO_INCREMENT, | |
101 | `entry_id` int(11) NOT NULL, | |
102 | `tag_id` int(11) NOT NULL, | |
103 | FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE, | |
104 | FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE, | |
105 | PRIMARY KEY (`id`) | |
106 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
107 | '; | |
108 | } | |
109 | else { | |
110 | $sql = ' | |
5ce39784 | 111 | CREATE TABLE IF NOT EXISTS tags_entries ( |
5cfafc61 NL |
112 | id bigserial primary key, |
113 | entry_id integer NOT NULL, | |
114 | tag_id integer NOT NULL | |
115 | ) | |
116 | '; | |
117 | } | |
118 | ||
119 | $query = $this->executeQuery($sql, array()); | |
aa8c9f2a NL |
120 | } |
121 | ||
046b9316 | 122 | public function install($login, $password, $email = '') |
b3cda72e | 123 | { |
b916bcfc | 124 | $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)'; |
046b9316 | 125 | $params = array($login, $password, $login, $email); |
b916bcfc NL |
126 | $query = $this->executeQuery($sql, $params); |
127 | ||
128 | $sequence = ''; | |
129 | if (STORAGE == 'postgres') { | |
130 | $sequence = 'users_id_seq'; | |
131 | } | |
132 | ||
133 | $id_user = intval($this->getLastId($sequence)); | |
134 | ||
135 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; | |
00dbaf90 | 136 | $params = array($id_user, 'pager', PAGINATION); |
b916bcfc NL |
137 | $query = $this->executeQuery($sql, $params); |
138 | ||
139 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; | |
00dbaf90 NL |
140 | $params = array($id_user, 'language', LANG); |
141 | $query = $this->executeQuery($sql, $params); | |
182faf26 | 142 | |
00dbaf90 NL |
143 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; |
144 | $params = array($id_user, 'theme', DEFAULT_THEME); | |
7ce7ec4c | 145 | $query = $this->executeQuery($sql, $params); |
aa8c9f2a | 146 | |
7ce7ec4c NL |
147 | return TRUE; |
148 | } | |
aa8c9f2a | 149 | |
b3cda72e NL |
150 | public function getConfigUser($id) |
151 | { | |
7ce7ec4c NL |
152 | $sql = "SELECT * FROM users_config WHERE user_id = ?"; |
153 | $query = $this->executeQuery($sql, array($id)); | |
154 | $result = $query->fetchAll(); | |
155 | $user_config = array(); | |
182faf26 | 156 | |
7ce7ec4c NL |
157 | foreach ($result as $key => $value) { |
158 | $user_config[$value['name']] = $value['value']; | |
aa8c9f2a NL |
159 | } |
160 | ||
7ce7ec4c | 161 | return $user_config; |
aa8c9f2a NL |
162 | } |
163 | ||
b3cda72e NL |
164 | public function userExists($username) |
165 | { | |
027b4e15 DS |
166 | $sql = "SELECT * FROM users WHERE username=?"; |
167 | $query = $this->executeQuery($sql, array($username)); | |
168 | $login = $query->fetchAll(); | |
169 | if (isset($login[0])) { | |
170 | return true; | |
171 | } else { | |
172 | return false; | |
173 | } | |
174 | } | |
175 | ||
b3cda72e NL |
176 | public function login($username, $password, $isauthenticated = FALSE) |
177 | { | |
6af66b11 | 178 | if ($isauthenticated) { |
4e067cea NL |
179 | $sql = "SELECT * FROM users WHERE username=?"; |
180 | $query = $this->executeQuery($sql, array($username)); | |
6af66b11 | 181 | } else { |
4e067cea NL |
182 | $sql = "SELECT * FROM users WHERE username=? AND password=?"; |
183 | $query = $this->executeQuery($sql, array($username, $password)); | |
6af66b11 | 184 | } |
8d3275be | 185 | $login = $query->fetchAll(); |
aa8c9f2a | 186 | |
7ce7ec4c NL |
187 | $user = array(); |
188 | if (isset($login[0])) { | |
189 | $user['id'] = $login[0]['id']; | |
190 | $user['username'] = $login[0]['username']; | |
191 | $user['password'] = $login[0]['password']; | |
192 | $user['name'] = $login[0]['name']; | |
193 | $user['email'] = $login[0]['email']; | |
194 | $user['config'] = $this->getConfigUser($login[0]['id']); | |
195 | } | |
aa8c9f2a | 196 | |
7ce7ec4c | 197 | return $user; |
aa8c9f2a NL |
198 | } |
199 | ||
00dbaf90 | 200 | public function updatePassword($userId, $password) |
6499b26a | 201 | { |
8d3275be | 202 | $sql_update = "UPDATE users SET password=? WHERE id=?"; |
1e98ee1d NL |
203 | $params_update = array($password, $userId); |
204 | $query = $this->executeQuery($sql_update, $params_update); | |
00dbaf90 | 205 | } |
182faf26 | 206 | |
b3cda72e NL |
207 | public function updateUserConfig($userId, $key, $value) |
208 | { | |
58ace494 | 209 | $config = $this->getConfigUser($userId); |
182faf26 | 210 | |
d1d3498b | 211 | if (! isset($config[$key])) { |
da5fc42f | 212 | $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)"; |
58ace494 NL |
213 | } |
214 | else { | |
da5fc42f | 215 | $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?"; |
58ace494 NL |
216 | } |
217 | ||
218 | $params = array($value, $userId, $key); | |
219 | $query = $this->executeQuery($sql, $params); | |
6499b26a NL |
220 | } |
221 | ||
b3cda72e NL |
222 | private function executeQuery($sql, $params) |
223 | { | |
14890de3 | 224 | try |
225 | { | |
226 | $query = $this->getHandle()->prepare($sql); | |
227 | $query->execute($params); | |
228 | return $query; | |
229 | } | |
230 | catch (Exception $e) | |
231 | { | |
eb1af592 | 232 | Tools::logm('execute query error : '.$e->getMessage()); |
bc1ee852 | 233 | return FALSE; |
14890de3 | 234 | } |
235 | } | |
4d99bae8 | 236 | |
b3cda72e NL |
237 | public function listUsers($username = NULL) |
238 | { | |
4d99bae8 | 239 | $sql = 'SELECT count(*) FROM users'.( $username ? ' WHERE username=?' : ''); |
240 | $query = $this->executeQuery($sql, ( $username ? array($username) : array())); | |
241 | list($count) = $query->fetch(); | |
242 | return $count; | |
243 | } | |
244 | ||
b3cda72e NL |
245 | public function getUserPassword($userID) |
246 | { | |
4d99bae8 | 247 | $sql = "SELECT * FROM users WHERE id=?"; |
248 | $query = $this->executeQuery($sql, array($userID)); | |
249 | $password = $query->fetchAll(); | |
250 | return isset($password[0]['password']) ? $password[0]['password'] : null; | |
251 | } | |
252 | ||
b3cda72e NL |
253 | public function deleteUserConfig($userID) |
254 | { | |
4d99bae8 | 255 | $sql_action = 'DELETE from users_config WHERE user_id=?'; |
256 | $params_action = array($userID); | |
257 | $query = $this->executeQuery($sql_action, $params_action); | |
258 | return $query; | |
259 | } | |
260 | ||
b3cda72e NL |
261 | public function deleteTagsEntriesAndEntries($userID) |
262 | { | |
4d99bae8 | 263 | $entries = $this->retrieveAll($userID); |
264 | foreach($entries as $entryid) { | |
265 | $tags = $this->retrieveTagsByEntry($entryid); | |
266 | foreach($tags as $tag) { | |
267 | $this->removeTagForEntry($entryid,$tags); | |
268 | } | |
269 | $this->deleteById($entryid,$userID); | |
270 | } | |
271 | } | |
272 | ||
b3cda72e NL |
273 | public function deleteUser($userID) |
274 | { | |
4d99bae8 | 275 | $sql_action = 'DELETE from users WHERE id=?'; |
276 | $params_action = array($userID); | |
277 | $query = $this->executeQuery($sql_action, $params_action); | |
278 | } | |
14890de3 | 279 | |
b3cda72e NL |
280 | public function updateContentAndTitle($id, $title, $body, $user_id) |
281 | { | |
53e3158d NL |
282 | $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?'; |
283 | $params_action = array($body, $title, $id, $user_id); | |
284 | $query = $this->executeQuery($sql_action, $params_action); | |
53e3158d NL |
285 | return $query; |
286 | } | |
287 | ||
b3cda72e NL |
288 | public function retrieveUnfetchedEntries($user_id, $limit) |
289 | { | |
53e3158d NL |
290 | |
291 | $sql_limit = "LIMIT 0,".$limit; | |
292 | if (STORAGE == 'postgres') { | |
293 | $sql_limit = "LIMIT ".$limit." OFFSET 0"; | |
294 | } | |
295 | ||
0f859c6f | 296 | $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=? ORDER BY id " . $sql_limit; |
53e3158d NL |
297 | $query = $this->executeQuery($sql, array($user_id)); |
298 | $entries = $query->fetchAll(); | |
299 | ||
300 | return $entries; | |
301 | } | |
302 | ||
b3cda72e NL |
303 | public function retrieveUnfetchedEntriesCount($user_id) |
304 | { | |
0f859c6f | 305 | $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=?"; |
182faf26 MR |
306 | $query = $this->executeQuery($sql, array($user_id)); |
307 | list($count) = $query->fetch(); | |
308 | ||
309 | return $count; | |
310 | } | |
311 | ||
b3cda72e NL |
312 | public function retrieveAll($user_id) |
313 | { | |
182faf26 | 314 | $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id"; |
8d3275be | 315 | $query = $this->executeQuery($sql, array($user_id)); |
44e77bfa | 316 | $entries = $query->fetchAll(); |
317 | ||
318 | return $entries; | |
319 | } | |
320 | ||
b3cda72e NL |
321 | public function retrieveOneById($id, $user_id) |
322 | { | |
14890de3 | 323 | $entry = NULL; |
8d3275be NL |
324 | $sql = "SELECT * FROM entries WHERE id=? AND user_id=?"; |
325 | $params = array(intval($id), $user_id); | |
14890de3 | 326 | $query = $this->executeQuery($sql, $params); |
327 | $entry = $query->fetchAll(); | |
328 | ||
0c2f4537 | 329 | return isset($entry[0]) ? $entry[0] : null; |
14890de3 | 330 | } |
331 | ||
b3cda72e NL |
332 | public function retrieveOneByURL($url, $user_id) |
333 | { | |
488fc63b | 334 | $entry = NULL; |
182faf26 | 335 | $sql = "SELECT * FROM entries WHERE url=? AND user_id=?"; |
488fc63b MR |
336 | $params = array($url, $user_id); |
337 | $query = $this->executeQuery($sql, $params); | |
338 | $entry = $query->fetchAll(); | |
339 | ||
340 | return isset($entry[0]) ? $entry[0] : null; | |
341 | } | |
342 | ||
b3cda72e NL |
343 | public function reassignTags($old_entry_id, $new_entry_id) |
344 | { | |
488fc63b MR |
345 | $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?"; |
346 | $params = array($new_entry_id, $old_entry_id); | |
347 | $query = $this->executeQuery($sql, $params); | |
348 | } | |
349 | ||
b3cda72e NL |
350 | public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0) |
351 | { | |
032e0ca1 MR |
352 | switch ($view) { |
353 | case 'archive': | |
182faf26 | 354 | $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? "; |
032e0ca1 | 355 | $params = array($user_id, 1); |
14890de3 | 356 | break; |
032e0ca1 | 357 | case 'fav' : |
182faf26 | 358 | $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? "; |
032e0ca1 | 359 | $params = array($user_id, 1); |
14890de3 | 360 | break; |
032e0ca1 MR |
361 | case 'tag' : |
362 | $sql = "SELECT entries.* FROM entries | |
363 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id | |
182faf26 | 364 | WHERE entries.user_id=? AND tags_entries.tag_id = ? "; |
032e0ca1 | 365 | $params = array($user_id, $tag_id); |
14890de3 | 366 | break; |
367 | default: | |
182faf26 | 368 | $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? "; |
032e0ca1 | 369 | $params = array($user_id, 0); |
14890de3 | 370 | break; |
371 | } | |
372 | ||
032e0ca1 MR |
373 | $sql .= $this->getEntriesOrder().' ' . $limit; |
374 | ||
375 | $query = $this->executeQuery($sql, $params); | |
376 | $entries = $query->fetchAll(); | |
377 | ||
378 | return $entries; | |
b3cda72e | 379 | } |
032e0ca1 | 380 | |
b3cda72e NL |
381 | public function getEntriesByViewCount($view, $user_id, $tag_id = 0) |
382 | { | |
53e3158d | 383 | switch ($view) { |
14890de3 | 384 | case 'archive': |
182faf26 | 385 | $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; |
bc1ee852 | 386 | $params = array($user_id, 1); |
14890de3 | 387 | break; |
388 | case 'fav' : | |
182faf26 | 389 | $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_fav=? "; |
bc1ee852 | 390 | $params = array($user_id, 1); |
14890de3 | 391 | break; |
53e3158d NL |
392 | case 'tag' : |
393 | $sql = "SELECT count(*) FROM entries | |
394 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id | |
182faf26 | 395 | WHERE entries.user_id=? AND tags_entries.tag_id = ? "; |
53e3158d NL |
396 | $params = array($user_id, $tag_id); |
397 | break; | |
14890de3 | 398 | default: |
182faf26 | 399 | $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; |
8d3275be | 400 | $params = array($user_id, 0); |
14890de3 | 401 | break; |
402 | } | |
403 | ||
8d3275be | 404 | $query = $this->executeQuery($sql, $params); |
032e0ca1 | 405 | list($count) = $query->fetch(); |
14890de3 | 406 | |
53e3158d | 407 | return $count; |
14890de3 | 408 | } |
409 | ||
b3cda72e NL |
410 | public function updateContent($id, $content, $user_id) |
411 | { | |
b58e261d NL |
412 | $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?'; |
413 | $params_action = array($content, $id, $user_id); | |
414 | $query = $this->executeQuery($sql_action, $params_action); | |
415 | return $query; | |
416 | } | |
417 | ||
182faf26 MR |
418 | /** |
419 | * | |
420 | * @param string $url | |
421 | * @param string $title | |
422 | * @param string $content | |
423 | * @param integer $user_id | |
424 | * @return integer $id of inserted record | |
425 | */ | |
b3cda72e NL |
426 | public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0) |
427 | { | |
6400371f NL |
428 | $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)'; |
429 | $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead); | |
5425b0dd | 430 | |
182faf26 MR |
431 | if ( !$this->executeQuery($sql_action, $params_action) ) { |
432 | $id = null; | |
433 | } | |
434 | else { | |
1bcbe8be | 435 | $id = intval($this->getLastId( (STORAGE == 'postgres') ? 'entries_id_seq' : '') ); |
182faf26 MR |
436 | } |
437 | return $id; | |
14890de3 | 438 | } |
439 | ||
b3cda72e NL |
440 | public function deleteById($id, $user_id) |
441 | { | |
8d3275be NL |
442 | $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?"; |
443 | $params_action = array($id, $user_id); | |
14890de3 | 444 | $query = $this->executeQuery($sql_action, $params_action); |
29c6fd46 | 445 | return $query; |
14890de3 | 446 | } |
447 | ||
b3cda72e NL |
448 | public function favoriteById($id, $user_id) |
449 | { | |
bc1ee852 | 450 | $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?"; |
8d3275be | 451 | $params_action = array($id, $user_id); |
14890de3 | 452 | $query = $this->executeQuery($sql_action, $params_action); |
453 | } | |
454 | ||
b3cda72e NL |
455 | public function archiveById($id, $user_id) |
456 | { | |
bc1ee852 | 457 | $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?"; |
8d3275be | 458 | $params_action = array($id, $user_id); |
14890de3 | 459 | $query = $this->executeQuery($sql_action, $params_action); |
460 | } | |
461 | ||
b3cda72e NL |
462 | public function archiveAll($user_id) |
463 | { | |
f14807de NL |
464 | $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?"; |
465 | $params_action = array($user_id, 1, 0); | |
466 | $query = $this->executeQuery($sql_action, $params_action); | |
467 | } | |
468 | ||
b3cda72e NL |
469 | public function getLastId($column = '') |
470 | { | |
b916bcfc | 471 | return $this->getHandle()->lastInsertId($column); |
14890de3 | 472 | } |
5ce39784 | 473 | |
b3cda72e NL |
474 | public function search($term, $user_id, $limit = '') |
475 | { | |
a4585f7e MR |
476 | $search = '%'.$term.'%'; |
477 | $sql_action = "SELECT * FROM entries WHERE user_id=? AND (content LIKE ? OR title LIKE ? OR url LIKE ?) "; //searches in content, title and URL | |
478 | $sql_action .= $this->getEntriesOrder().' ' . $limit; | |
479 | $params_action = array($user_id, $search, $search, $search); | |
480 | $query = $this->executeQuery($sql_action, $params_action); | |
481 | return $query->fetchAll(); | |
482 | } | |
5bea1a4d | 483 | |
b3cda72e NL |
484 | public function retrieveAllTags($user_id, $term = NULL) |
485 | { | |
fb26cc93 | 486 | $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags |
b89d5a2b NL |
487 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
488 | LEFT JOIN entries ON tags_entries.entry_id=entries.id | |
182faf26 | 489 | WHERE entries.user_id=? |
fb26cc93 MR |
490 | ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ." |
491 | GROUP BY tags.id, tags.value | |
492 | ORDER BY tags.value"; | |
493 | $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) )); | |
2e2ebe5e NL |
494 | $tags = $query->fetchAll(); |
495 | ||
496 | return $tags; | |
497 | } | |
498 | ||
b3cda72e NL |
499 | public function retrieveTag($id, $user_id) |
500 | { | |
4886ed6d | 501 | $tag = NULL; |
e83cf5a7 | 502 | $sql = "SELECT DISTINCT tags.* FROM tags |
b89d5a2b NL |
503 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
504 | LEFT JOIN entries ON tags_entries.entry_id=entries.id | |
182faf26 | 505 | WHERE tags.id=? AND entries.user_id=?"; |
b89d5a2b | 506 | $params = array(intval($id), $user_id); |
4886ed6d NL |
507 | $query = $this->executeQuery($sql, $params); |
508 | $tag = $query->fetchAll(); | |
509 | ||
b3cda72e | 510 | return isset($tag[0]) ? $tag[0] : NULL; |
4886ed6d NL |
511 | } |
512 | ||
b3cda72e NL |
513 | public function retrieveEntriesByTag($tag_id, $user_id) |
514 | { | |
182faf26 | 515 | $sql = |
9de34d4e | 516 | "SELECT entries.* FROM entries |
4886ed6d | 517 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id |
25052a76 | 518 | WHERE tags_entries.tag_id = ? AND entries.user_id=? ORDER by entries.id DESC"; |
b89d5a2b | 519 | $query = $this->executeQuery($sql, array($tag_id, $user_id)); |
4886ed6d NL |
520 | $entries = $query->fetchAll(); |
521 | ||
522 | return $entries; | |
523 | } | |
524 | ||
b3cda72e NL |
525 | public function retrieveTagsByEntry($entry_id) |
526 | { | |
182faf26 | 527 | $sql = |
9de34d4e | 528 | "SELECT tags.* FROM tags |
5bea1a4d NL |
529 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
530 | WHERE tags_entries.entry_id = ?"; | |
2e2ebe5e NL |
531 | $query = $this->executeQuery($sql, array($entry_id)); |
532 | $tags = $query->fetchAll(); | |
5bea1a4d NL |
533 | |
534 | return $tags; | |
535 | } | |
c432fa16 | 536 | |
b3cda72e NL |
537 | public function removeTagForEntry($entry_id, $tag_id) |
538 | { | |
c432fa16 NL |
539 | $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?"; |
540 | $params_action = array($tag_id, $entry_id); | |
541 | $query = $this->executeQuery($sql_action, $params_action); | |
542 | return $query; | |
543 | } | |
9c743ab9 | 544 | |
b3cda72e NL |
545 | public function cleanUnusedTag($tag_id) |
546 | { | |
24696800 | 547 | $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?"; |
548 | $query = $this->executeQuery($sql_action,array($tag_id)); | |
9c743ab9 | 549 | $tagstokeep = $query->fetchAll(); |
24696800 | 550 | $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?"; |
551 | $query = $this->executeQuery($sql_action,array($tag_id)); | |
9c743ab9 | 552 | $alltags = $query->fetchAll(); |
24696800 | 553 | |
9c743ab9 | 554 | foreach ($alltags as $tag) { |
555 | if ($tag && !in_array($tag,$tagstokeep)) { | |
9c743ab9 | 556 | $sql_action = "DELETE FROM tags WHERE id=?"; |
557 | $params_action = array($tag[0]); | |
24696800 | 558 | $this->executeQuery($sql_action, $params_action); |
559 | return true; | |
9c743ab9 | 560 | } |
561 | } | |
24696800 | 562 | |
9c743ab9 | 563 | } |
c432fa16 | 564 | |
b3cda72e NL |
565 | public function retrieveTagByValue($value) |
566 | { | |
c432fa16 NL |
567 | $tag = NULL; |
568 | $sql = "SELECT * FROM tags WHERE value=?"; | |
569 | $params = array($value); | |
570 | $query = $this->executeQuery($sql, $params); | |
571 | $tag = $query->fetchAll(); | |
572 | ||
573 | return isset($tag[0]) ? $tag[0] : null; | |
574 | } | |
575 | ||
b3cda72e NL |
576 | public function createTag($value) |
577 | { | |
c432fa16 NL |
578 | $sql_action = 'INSERT INTO tags ( value ) VALUES (?)'; |
579 | $params_action = array($value); | |
580 | $query = $this->executeQuery($sql_action, $params_action); | |
581 | return $query; | |
582 | } | |
583 | ||
b3cda72e NL |
584 | public function setTagToEntry($tag_id, $entry_id) |
585 | { | |
c432fa16 NL |
586 | $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)'; |
587 | $params_action = array($tag_id, $entry_id); | |
588 | $query = $this->executeQuery($sql_action, $params_action); | |
589 | return $query; | |
590 | } | |
032e0ca1 | 591 | |
b3cda72e NL |
592 | private function getEntriesOrder() |
593 | { | |
594 | if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order)) { | |
595 | return $this->order[$_SESSION['sort']]; | |
032e0ca1 | 596 | } |
b3cda72e NL |
597 | else { |
598 | return $this->order['default']; | |
599 | } | |
600 | } | |
2cd263e0 | 601 | } |