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