aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/Database.class.php
diff options
context:
space:
mode:
authortcit <tcit@tcit.fr>2014-10-08 19:26:26 +0200
committertcit <tcit@tcit.fr>2014-10-08 19:26:26 +0200
commit8327f1c371ad1d930bf9c9a13e443f2aa29ecfe3 (patch)
treeea559def90e546716f3d6016fe8f06f333249ef6 /inc/poche/Database.class.php
parentd05f5eeb1dfd989e76f6040b220fe52738284841 (diff)
parent73c833780c37278a319fd3bfff172eede1a040bd (diff)
downloadwallabag-8327f1c371ad1d930bf9c9a13e443f2aa29ecfe3.tar.gz
wallabag-8327f1c371ad1d930bf9c9a13e443f2aa29ecfe3.tar.zst
wallabag-8327f1c371ad1d930bf9c9a13e443f2aa29ecfe3.zip
Merge branch 'dev' into data-for-mysql
Diffstat (limited to 'inc/poche/Database.class.php')
-rwxr-xr-xinc/poche/Database.class.php181
1 files changed, 106 insertions, 75 deletions
diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php
index 11cccb72..b5dd2120 100755
--- a/inc/poche/Database.class.php
+++ b/inc/poche/Database.class.php
@@ -5,23 +5,28 @@
5 * @category wallabag 5 * @category wallabag
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org> 6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
7 * @copyright 2013 7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file 8 * @license http://opensource.org/licenses/MIT see COPYING file
9 */ 9 */
10 10
11class Database { 11class Database {
12
12 var $handle; 13 var $handle;
13 private $order = array( 14 private $order = array (
14 'ia' => 'ORDER BY entries.id', 15 'ia' => 'ORDER BY entries.id',
15 'id' => 'ORDER BY entries.id DESC', 16 'id' => 'ORDER BY entries.id DESC',
16 'ta' => 'ORDER BY lower(entries.title)', 17 'ta' => 'ORDER BY lower(entries.title)',
17 'td' => 'ORDER BY lower(entries.title) DESC', 18 'td' => 'ORDER BY lower(entries.title) DESC',
18 'default' => 'ORDER BY entries.id' 19 'default' => 'ORDER BY entries.id'
19 ); 20 );
20 21
21 function __construct() 22 function __construct()
22 { 23 {
23 switch (STORAGE) { 24 switch (STORAGE) {
24 case 'sqlite': 25 case 'sqlite':
26 // Check if /db is writeable
27 if ( !is_writable(STORAGE_SQLITE) || !is_writable(dirname(STORAGE_SQLITE))) {
28 die('An error occured: "db" directory must be writeable for your web server user!');
29 }
25 $db_path = 'sqlite:' . STORAGE_SQLITE; 30 $db_path = 'sqlite:' . STORAGE_SQLITE;
26 $this->handle = new PDO($db_path); 31 $this->handle = new PDO($db_path);
27 break; 32 break;
@@ -38,28 +43,17 @@ class Database {
38 } 43 }
39 44
40 $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 45 $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
46 $this->_checkTags();
41 Tools::logm('storage type ' . STORAGE); 47 Tools::logm('storage type ' . STORAGE);
42 } 48 }
43 49
44 private function getHandle() { 50 private function getHandle()
51 {
45 return $this->handle; 52 return $this->handle;
46 } 53 }
47 54
48 public function isInstalled() { 55 private function _checkTags()
49 $sql = "SELECT username FROM users"; 56 {
50 $query = $this->executeQuery($sql, array());
51 if ($query == false) {
52 die(STORAGE . ' database looks empty. You have to create it (you can find database structure in install folder).');
53 }
54 $hasAdmin = count($query->fetchAll());
55
56 if ($hasAdmin == 0)
57 return false;
58
59 return true;
60 }
61
62 public function checkTags() {
63 57
64 if (STORAGE == 'sqlite') { 58 if (STORAGE == 'sqlite') {
65 $sql = ' 59 $sql = '
@@ -123,9 +117,10 @@ class Database {
123 $query = $this->executeQuery($sql, array()); 117 $query = $this->executeQuery($sql, array());
124 } 118 }
125 119
126 public function install($login, $password) { 120 public function install($login, $password, $email = '')
121 {
127 $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)'; 122 $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)';
128 $params = array($login, $password, $login, ' '); 123 $params = array($login, $password, $login, $email);
129 $query = $this->executeQuery($sql, $params); 124 $query = $this->executeQuery($sql, $params);
130 125
131 $sequence = ''; 126 $sequence = '';
@@ -150,7 +145,8 @@ class Database {
150 return TRUE; 145 return TRUE;
151 } 146 }
152 147
153 public function getConfigUser($id) { 148 public function getConfigUser($id)
149 {
154 $sql = "SELECT * FROM users_config WHERE user_id = ?"; 150 $sql = "SELECT * FROM users_config WHERE user_id = ?";
155 $query = $this->executeQuery($sql, array($id)); 151 $query = $this->executeQuery($sql, array($id));
156 $result = $query->fetchAll(); 152 $result = $query->fetchAll();
@@ -163,7 +159,8 @@ class Database {
163 return $user_config; 159 return $user_config;
164 } 160 }
165 161
166 public function userExists($username) { 162 public function userExists($username)
163 {
167 $sql = "SELECT * FROM users WHERE username=?"; 164 $sql = "SELECT * FROM users WHERE username=?";
168 $query = $this->executeQuery($sql, array($username)); 165 $query = $this->executeQuery($sql, array($username));
169 $login = $query->fetchAll(); 166 $login = $query->fetchAll();
@@ -174,13 +171,14 @@ class Database {
174 } 171 }
175 } 172 }
176 173
177 public function login($username, $password, $isauthenticated=false) { 174 public function login($username, $password, $isauthenticated = FALSE)
175 {
178 if ($isauthenticated) { 176 if ($isauthenticated) {
179 $sql = "SELECT * FROM users WHERE username=?"; 177 $sql = "SELECT * FROM users WHERE username=?";
180 $query = $this->executeQuery($sql, array($username)); 178 $query = $this->executeQuery($sql, array($username));
181 } else { 179 } else {
182 $sql = "SELECT * FROM users WHERE username=? AND password=?"; 180 $sql = "SELECT * FROM users WHERE username=? AND password=?";
183 $query = $this->executeQuery($sql, array($username, $password)); 181 $query = $this->executeQuery($sql, array($username, $password));
184 } 182 }
185 $login = $query->fetchAll(); 183 $login = $query->fetchAll();
186 184
@@ -204,7 +202,8 @@ class Database {
204 $query = $this->executeQuery($sql_update, $params_update); 202 $query = $this->executeQuery($sql_update, $params_update);
205 } 203 }
206 204
207 public function updateUserConfig($userId, $key, $value) { 205 public function updateUserConfig($userId, $key, $value)
206 {
208 $config = $this->getConfigUser($userId); 207 $config = $this->getConfigUser($userId);
209 208
210 if (! isset($config[$key])) { 209 if (! isset($config[$key])) {
@@ -218,7 +217,8 @@ class Database {
218 $query = $this->executeQuery($sql, $params); 217 $query = $this->executeQuery($sql, $params);
219 } 218 }
220 219
221 private function executeQuery($sql, $params) { 220 private function executeQuery($sql, $params)
221 {
222 try 222 try
223 { 223 {
224 $query = $this->getHandle()->prepare($sql); 224 $query = $this->getHandle()->prepare($sql);
@@ -232,28 +232,32 @@ class Database {
232 } 232 }
233 } 233 }
234 234
235 public function listUsers($username=null) { 235 public function listUsers($username = NULL)
236 {
236 $sql = 'SELECT count(*) FROM users'.( $username ? ' WHERE username=?' : ''); 237 $sql = 'SELECT count(*) FROM users'.( $username ? ' WHERE username=?' : '');
237 $query = $this->executeQuery($sql, ( $username ? array($username) : array())); 238 $query = $this->executeQuery($sql, ( $username ? array($username) : array()));
238 list($count) = $query->fetch(); 239 list($count) = $query->fetch();
239 return $count; 240 return $count;
240 } 241 }
241 242
242 public function getUserPassword($userID) { 243 public function getUserPassword($userID)
244 {
243 $sql = "SELECT * FROM users WHERE id=?"; 245 $sql = "SELECT * FROM users WHERE id=?";
244 $query = $this->executeQuery($sql, array($userID)); 246 $query = $this->executeQuery($sql, array($userID));
245 $password = $query->fetchAll(); 247 $password = $query->fetchAll();
246 return isset($password[0]['password']) ? $password[0]['password'] : null; 248 return isset($password[0]['password']) ? $password[0]['password'] : null;
247 } 249 }
248 250
249 public function deleteUserConfig($userID) { 251 public function deleteUserConfig($userID)
252 {
250 $sql_action = 'DELETE from users_config WHERE user_id=?'; 253 $sql_action = 'DELETE from users_config WHERE user_id=?';
251 $params_action = array($userID); 254 $params_action = array($userID);
252 $query = $this->executeQuery($sql_action, $params_action); 255 $query = $this->executeQuery($sql_action, $params_action);
253 return $query; 256 return $query;
254 } 257 }
255 258
256 public function deleteTagsEntriesAndEntries($userID) { 259 public function deleteTagsEntriesAndEntries($userID)
260 {
257 $entries = $this->retrieveAll($userID); 261 $entries = $this->retrieveAll($userID);
258 foreach($entries as $entryid) { 262 foreach($entries as $entryid) {
259 $tags = $this->retrieveTagsByEntry($entryid); 263 $tags = $this->retrieveTagsByEntry($entryid);
@@ -264,20 +268,23 @@ class Database {
264 } 268 }
265 } 269 }
266 270
267 public function deleteUser($userID) { 271 public function deleteUser($userID)
272 {
268 $sql_action = 'DELETE from users WHERE id=?'; 273 $sql_action = 'DELETE from users WHERE id=?';
269 $params_action = array($userID); 274 $params_action = array($userID);
270 $query = $this->executeQuery($sql_action, $params_action); 275 $query = $this->executeQuery($sql_action, $params_action);
271 } 276 }
272 277
273 public function updateContentAndTitle($id, $title, $body, $user_id) { 278 public function updateContentAndTitle($id, $title, $body, $user_id)
279 {
274 $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?'; 280 $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?';
275 $params_action = array($body, $title, $id, $user_id); 281 $params_action = array($body, $title, $id, $user_id);
276 $query = $this->executeQuery($sql_action, $params_action); 282 $query = $this->executeQuery($sql_action, $params_action);
277 return $query; 283 return $query;
278 } 284 }
279 285
280 public function retrieveUnfetchedEntries($user_id, $limit) { 286 public function retrieveUnfetchedEntries($user_id, $limit)
287 {
281 288
282 $sql_limit = "LIMIT 0,".$limit; 289 $sql_limit = "LIMIT 0,".$limit;
283 if (STORAGE == 'postgres') { 290 if (STORAGE == 'postgres') {
@@ -291,7 +298,8 @@ class Database {
291 return $entries; 298 return $entries;
292 } 299 }
293 300
294 public function retrieveUnfetchedEntriesCount($user_id) { 301 public function retrieveUnfetchedEntriesCount($user_id)
302 {
295 $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=?"; 303 $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=?";
296 $query = $this->executeQuery($sql, array($user_id)); 304 $query = $this->executeQuery($sql, array($user_id));
297 list($count) = $query->fetch(); 305 list($count) = $query->fetch();
@@ -299,7 +307,8 @@ class Database {
299 return $count; 307 return $count;
300 } 308 }
301 309
302 public function retrieveAll($user_id) { 310 public function retrieveAll($user_id)
311 {
303 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id"; 312 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
304 $query = $this->executeQuery($sql, array($user_id)); 313 $query = $this->executeQuery($sql, array($user_id));
305 $entries = $query->fetchAll(); 314 $entries = $query->fetchAll();
@@ -307,7 +316,8 @@ class Database {
307 return $entries; 316 return $entries;
308 } 317 }
309 318
310 public function retrieveOneById($id, $user_id) { 319 public function retrieveOneById($id, $user_id)
320 {
311 $entry = NULL; 321 $entry = NULL;
312 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?"; 322 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
313 $params = array(intval($id), $user_id); 323 $params = array(intval($id), $user_id);
@@ -317,7 +327,8 @@ class Database {
317 return isset($entry[0]) ? $entry[0] : null; 327 return isset($entry[0]) ? $entry[0] : null;
318 } 328 }
319 329
320 public function retrieveOneByURL($url, $user_id) { 330 public function retrieveOneByURL($url, $user_id)
331 {
321 $entry = NULL; 332 $entry = NULL;
322 $sql = "SELECT * FROM entries WHERE url=? AND user_id=?"; 333 $sql = "SELECT * FROM entries WHERE url=? AND user_id=?";
323 $params = array($url, $user_id); 334 $params = array($url, $user_id);
@@ -327,13 +338,15 @@ class Database {
327 return isset($entry[0]) ? $entry[0] : null; 338 return isset($entry[0]) ? $entry[0] : null;
328 } 339 }
329 340
330 public function reassignTags($old_entry_id, $new_entry_id) { 341 public function reassignTags($old_entry_id, $new_entry_id)
342 {
331 $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?"; 343 $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?";
332 $params = array($new_entry_id, $old_entry_id); 344 $params = array($new_entry_id, $old_entry_id);
333 $query = $this->executeQuery($sql, $params); 345 $query = $this->executeQuery($sql, $params);
334 } 346 }
335 347
336 public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0) { 348 public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0)
349 {
337 switch ($view) { 350 switch ($view) {
338 case 'archive': 351 case 'archive':
339 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? "; 352 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? ";
@@ -361,9 +374,10 @@ class Database {
361 $entries = $query->fetchAll(); 374 $entries = $query->fetchAll();
362 375
363 return $entries; 376 return $entries;
364 } 377 }
365 378
366 public function getEntriesByViewCount($view, $user_id, $tag_id = 0) { 379 public function getEntriesByViewCount($view, $user_id, $tag_id = 0)
380 {
367 switch ($view) { 381 switch ($view) {
368 case 'archive': 382 case 'archive':
369 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; 383 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
@@ -391,7 +405,8 @@ class Database {
391 return $count; 405 return $count;
392 } 406 }
393 407
394 public function updateContent($id, $content, $user_id) { 408 public function updateContent($id, $content, $user_id)
409 {
395 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?'; 410 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
396 $params_action = array($content, $id, $user_id); 411 $params_action = array($content, $id, $user_id);
397 $query = $this->executeQuery($sql_action, $params_action); 412 $query = $this->executeQuery($sql_action, $params_action);
@@ -406,7 +421,8 @@ class Database {
406 * @param integer $user_id 421 * @param integer $user_id
407 * @return integer $id of inserted record 422 * @return integer $id of inserted record
408 */ 423 */
409 public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0) { 424 public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0)
425 {
410 $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)'; 426 $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)';
411 $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead); 427 $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead);
412 428
@@ -419,36 +435,42 @@ class Database {
419 return $id; 435 return $id;
420 } 436 }
421 437
422 public function deleteById($id, $user_id) { 438 public function deleteById($id, $user_id)
439 {
423 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?"; 440 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
424 $params_action = array($id, $user_id); 441 $params_action = array($id, $user_id);
425 $query = $this->executeQuery($sql_action, $params_action); 442 $query = $this->executeQuery($sql_action, $params_action);
426 return $query; 443 return $query;
427 } 444 }
428 445
429 public function favoriteById($id, $user_id) { 446 public function favoriteById($id, $user_id)
447 {
430 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?"; 448 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
431 $params_action = array($id, $user_id); 449 $params_action = array($id, $user_id);
432 $query = $this->executeQuery($sql_action, $params_action); 450 $query = $this->executeQuery($sql_action, $params_action);
433 } 451 }
434 452
435 public function archiveById($id, $user_id) { 453 public function archiveById($id, $user_id)
454 {
436 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?"; 455 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
437 $params_action = array($id, $user_id); 456 $params_action = array($id, $user_id);
438 $query = $this->executeQuery($sql_action, $params_action); 457 $query = $this->executeQuery($sql_action, $params_action);
439 } 458 }
440 459
441 public function archiveAll($user_id) { 460 public function archiveAll($user_id)
461 {
442 $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?"; 462 $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?";
443 $params_action = array($user_id, 1, 0); 463 $params_action = array($user_id, 1, 0);
444 $query = $this->executeQuery($sql_action, $params_action); 464 $query = $this->executeQuery($sql_action, $params_action);
445 } 465 }
446 466
447 public function getLastId($column = '') { 467 public function getLastId($column = '')
468 {
448 return $this->getHandle()->lastInsertId($column); 469 return $this->getHandle()->lastInsertId($column);
449 } 470 }
450 471
451 public function search($term, $user_id, $limit = '') { 472 public function search($term, $user_id, $limit = '')
473 {
452 $search = '%'.$term.'%'; 474 $search = '%'.$term.'%';
453 $sql_action = "SELECT * FROM entries WHERE user_id=? AND (content LIKE ? OR title LIKE ? OR url LIKE ?) "; //searches in content, title and URL 475 $sql_action = "SELECT * FROM entries WHERE user_id=? AND (content LIKE ? OR title LIKE ? OR url LIKE ?) "; //searches in content, title and URL
454 $sql_action .= $this->getEntriesOrder().' ' . $limit; 476 $sql_action .= $this->getEntriesOrder().' ' . $limit;
@@ -457,7 +479,8 @@ class Database {
457 return $query->fetchAll(); 479 return $query->fetchAll();
458 } 480 }
459 481
460 public function retrieveAllTags($user_id, $term = null) { 482 public function retrieveAllTags($user_id, $term = NULL)
483 {
461 $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags 484 $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags
462 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id 485 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
463 LEFT JOIN entries ON tags_entries.entry_id=entries.id 486 LEFT JOIN entries ON tags_entries.entry_id=entries.id
@@ -471,7 +494,8 @@ class Database {
471 return $tags; 494 return $tags;
472 } 495 }
473 496
474 public function retrieveTag($id, $user_id) { 497 public function retrieveTag($id, $user_id)
498 {
475 $tag = NULL; 499 $tag = NULL;
476 $sql = "SELECT DISTINCT tags.* FROM tags 500 $sql = "SELECT DISTINCT tags.* FROM tags
477 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id 501 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
@@ -481,10 +505,11 @@ class Database {
481 $query = $this->executeQuery($sql, $params); 505 $query = $this->executeQuery($sql, $params);
482 $tag = $query->fetchAll(); 506 $tag = $query->fetchAll();
483 507
484 return isset($tag[0]) ? $tag[0] : null; 508 return isset($tag[0]) ? $tag[0] : NULL;
485 } 509 }
486 510
487 public function retrieveEntriesByTag($tag_id, $user_id) { 511 public function retrieveEntriesByTag($tag_id, $user_id)
512 {
488 $sql = 513 $sql =
489 "SELECT entries.* FROM entries 514 "SELECT entries.* FROM entries
490 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id 515 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
@@ -495,7 +520,8 @@ class Database {
495 return $entries; 520 return $entries;
496 } 521 }
497 522
498 public function retrieveTagsByEntry($entry_id) { 523 public function retrieveTagsByEntry($entry_id)
524 {
499 $sql = 525 $sql =
500 "SELECT tags.* FROM tags 526 "SELECT tags.* FROM tags
501 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id 527 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
@@ -506,14 +532,16 @@ class Database {
506 return $tags; 532 return $tags;
507 } 533 }
508 534
509 public function removeTagForEntry($entry_id, $tag_id) { 535 public function removeTagForEntry($entry_id, $tag_id)
536 {
510 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?"; 537 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
511 $params_action = array($tag_id, $entry_id); 538 $params_action = array($tag_id, $entry_id);
512 $query = $this->executeQuery($sql_action, $params_action); 539 $query = $this->executeQuery($sql_action, $params_action);
513 return $query; 540 return $query;
514 } 541 }
515 542
516 public function cleanUnusedTag($tag_id) { 543 public function cleanUnusedTag($tag_id)
544 {
517 $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?"; 545 $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
518 $query = $this->executeQuery($sql_action,array($tag_id)); 546 $query = $this->executeQuery($sql_action,array($tag_id));
519 $tagstokeep = $query->fetchAll(); 547 $tagstokeep = $query->fetchAll();
@@ -532,7 +560,8 @@ class Database {
532 560
533 } 561 }
534 562
535 public function retrieveTagByValue($value) { 563 public function retrieveTagByValue($value)
564 {
536 $tag = NULL; 565 $tag = NULL;
537 $sql = "SELECT * FROM tags WHERE value=?"; 566 $sql = "SELECT * FROM tags WHERE value=?";
538 $params = array($value); 567 $params = array($value);
@@ -542,27 +571,29 @@ class Database {
542 return isset($tag[0]) ? $tag[0] : null; 571 return isset($tag[0]) ? $tag[0] : null;
543 } 572 }
544 573
545 public function createTag($value) { 574 public function createTag($value)
575 {
546 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)'; 576 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
547 $params_action = array($value); 577 $params_action = array($value);
548 $query = $this->executeQuery($sql_action, $params_action); 578 $query = $this->executeQuery($sql_action, $params_action);
549 return $query; 579 return $query;
550 } 580 }
551 581
552 public function setTagToEntry($tag_id, $entry_id) { 582 public function setTagToEntry($tag_id, $entry_id)
583 {
553 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)'; 584 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
554 $params_action = array($tag_id, $entry_id); 585 $params_action = array($tag_id, $entry_id);
555 $query = $this->executeQuery($sql_action, $params_action); 586 $query = $this->executeQuery($sql_action, $params_action);
556 return $query; 587 return $query;
557 } 588 }
558 589
559 private function getEntriesOrder() { 590 private function getEntriesOrder()
560 if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order)) { 591 {
561 return $this->order[$_SESSION['sort']]; 592 if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order)) {
562 } 593 return $this->order[$_SESSION['sort']];
563 else {
564 return $this->order['default'];
565 }
566 } 594 }
567 595 else {
596 return $this->order['default'];
597 }
598 }
568} 599}