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