diff options
Diffstat (limited to 'inc/poche/Database.class.php')
-rwxr-xr-x | inc/poche/Database.class.php | 82 |
1 files changed, 50 insertions, 32 deletions
diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php index 6f5c9ac0..9adb1644 100755 --- a/inc/poche/Database.class.php +++ b/inc/poche/Database.class.php | |||
@@ -18,7 +18,7 @@ class Database { | |||
18 | 'default' => 'ORDER BY entries.id' | 18 | 'default' => 'ORDER BY entries.id' |
19 | ); | 19 | ); |
20 | 20 | ||
21 | function __construct() | 21 | function __construct() |
22 | { | 22 | { |
23 | switch (STORAGE) { | 23 | switch (STORAGE) { |
24 | case 'sqlite': | 24 | case 'sqlite': |
@@ -27,11 +27,11 @@ class Database { | |||
27 | break; | 27 | break; |
28 | case 'mysql': | 28 | case 'mysql': |
29 | $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; | 29 | $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; |
30 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); | 30 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); |
31 | break; | 31 | break; |
32 | case 'postgres': | 32 | case 'postgres': |
33 | $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; | 33 | $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; |
34 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); | 34 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); |
35 | break; | 35 | break; |
36 | } | 36 | } |
37 | 37 | ||
@@ -51,7 +51,7 @@ class Database { | |||
51 | } | 51 | } |
52 | $hasAdmin = count($query->fetchAll()); | 52 | $hasAdmin = count($query->fetchAll()); |
53 | 53 | ||
54 | if ($hasAdmin == 0) | 54 | if ($hasAdmin == 0) |
55 | return false; | 55 | return false; |
56 | 56 | ||
57 | return true; | 57 | return true; |
@@ -140,7 +140,7 @@ class Database { | |||
140 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; | 140 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; |
141 | $params = array($id_user, 'language', LANG); | 141 | $params = array($id_user, 'language', LANG); |
142 | $query = $this->executeQuery($sql, $params); | 142 | $query = $this->executeQuery($sql, $params); |
143 | 143 | ||
144 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; | 144 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; |
145 | $params = array($id_user, 'theme', DEFAULT_THEME); | 145 | $params = array($id_user, 'theme', DEFAULT_THEME); |
146 | $query = $this->executeQuery($sql, $params); | 146 | $query = $this->executeQuery($sql, $params); |
@@ -153,7 +153,7 @@ class Database { | |||
153 | $query = $this->executeQuery($sql, array($id)); | 153 | $query = $this->executeQuery($sql, array($id)); |
154 | $result = $query->fetchAll(); | 154 | $result = $query->fetchAll(); |
155 | $user_config = array(); | 155 | $user_config = array(); |
156 | 156 | ||
157 | foreach ($result as $key => $value) { | 157 | foreach ($result as $key => $value) { |
158 | $user_config[$value['name']] = $value['value']; | 158 | $user_config[$value['name']] = $value['value']; |
159 | } | 159 | } |
@@ -201,10 +201,10 @@ class Database { | |||
201 | $params_update = array($password, $userId); | 201 | $params_update = array($password, $userId); |
202 | $query = $this->executeQuery($sql_update, $params_update); | 202 | $query = $this->executeQuery($sql_update, $params_update); |
203 | } | 203 | } |
204 | 204 | ||
205 | public function updateUserConfig($userId, $key, $value) { | 205 | public function updateUserConfig($userId, $key, $value) { |
206 | $config = $this->getConfigUser($userId); | 206 | $config = $this->getConfigUser($userId); |
207 | 207 | ||
208 | if (! isset($config[$key])) { | 208 | if (! isset($config[$key])) { |
209 | $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)"; | 209 | $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)"; |
210 | } | 210 | } |
@@ -252,8 +252,16 @@ class Database { | |||
252 | return $entries; | 252 | return $entries; |
253 | } | 253 | } |
254 | 254 | ||
255 | public function retrieveUnfetchedEntriesCount($user_id) { | ||
256 | $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND user_id=?"; | ||
257 | $query = $this->executeQuery($sql, array($user_id)); | ||
258 | list($count) = $query->fetch(); | ||
259 | |||
260 | return $count; | ||
261 | } | ||
262 | |||
255 | public function retrieveAll($user_id) { | 263 | public function retrieveAll($user_id) { |
256 | $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? ORDER BY id"; | 264 | $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id"; |
257 | $query = $this->executeQuery($sql, array($user_id)); | 265 | $query = $this->executeQuery($sql, array($user_id)); |
258 | $entries = $query->fetchAll(); | 266 | $entries = $query->fetchAll(); |
259 | 267 | ||
@@ -272,7 +280,7 @@ class Database { | |||
272 | 280 | ||
273 | public function retrieveOneByURL($url, $user_id) { | 281 | public function retrieveOneByURL($url, $user_id) { |
274 | $entry = NULL; | 282 | $entry = NULL; |
275 | $sql = "SELECT * FROM entries WHERE content <> '' AND url=? AND user_id=?"; | 283 | $sql = "SELECT * FROM entries WHERE url=? AND user_id=?"; |
276 | $params = array($url, $user_id); | 284 | $params = array($url, $user_id); |
277 | $query = $this->executeQuery($sql, $params); | 285 | $query = $this->executeQuery($sql, $params); |
278 | $entry = $query->fetchAll(); | 286 | $entry = $query->fetchAll(); |
@@ -289,22 +297,21 @@ class Database { | |||
289 | public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0) { | 297 | public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0) { |
290 | switch ($view) { | 298 | switch ($view) { |
291 | case 'archive': | 299 | case 'archive': |
292 | $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_read=? "; | 300 | $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? "; |
293 | $params = array($user_id, 1); | 301 | $params = array($user_id, 1); |
294 | break; | 302 | break; |
295 | case 'fav' : | 303 | case 'fav' : |
296 | $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_fav=? "; | 304 | $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? "; |
297 | $params = array($user_id, 1); | 305 | $params = array($user_id, 1); |
298 | break; | 306 | break; |
299 | case 'tag' : | 307 | case 'tag' : |
300 | $sql = "SELECT entries.* FROM entries | 308 | $sql = "SELECT entries.* FROM entries |
301 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id | 309 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id |
302 | WHERE entries.content <> '' AND | 310 | WHERE entries.user_id=? AND tags_entries.tag_id = ? "; |
303 | entries.user_id=? AND tags_entries.tag_id = ? "; | ||
304 | $params = array($user_id, $tag_id); | 311 | $params = array($user_id, $tag_id); |
305 | break; | 312 | break; |
306 | default: | 313 | default: |
307 | $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_read=? "; | 314 | $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? "; |
308 | $params = array($user_id, 0); | 315 | $params = array($user_id, 0); |
309 | break; | 316 | break; |
310 | } | 317 | } |
@@ -320,22 +327,21 @@ class Database { | |||
320 | public function getEntriesByViewCount($view, $user_id, $tag_id = 0) { | 327 | public function getEntriesByViewCount($view, $user_id, $tag_id = 0) { |
321 | switch ($view) { | 328 | switch ($view) { |
322 | case 'archive': | 329 | case 'archive': |
323 | $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_read=? "; | 330 | $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; |
324 | $params = array($user_id, 1); | 331 | $params = array($user_id, 1); |
325 | break; | 332 | break; |
326 | case 'fav' : | 333 | case 'fav' : |
327 | $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_fav=? "; | 334 | $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_fav=? "; |
328 | $params = array($user_id, 1); | 335 | $params = array($user_id, 1); |
329 | break; | 336 | break; |
330 | case 'tag' : | 337 | case 'tag' : |
331 | $sql = "SELECT count(*) FROM entries | 338 | $sql = "SELECT count(*) FROM entries |
332 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id | 339 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id |
333 | WHERE entries.content <> '' AND | 340 | WHERE entries.user_id=? AND tags_entries.tag_id = ? "; |
334 | entries.user_id=? AND tags_entries.tag_id = ? "; | ||
335 | $params = array($user_id, $tag_id); | 341 | $params = array($user_id, $tag_id); |
336 | break; | 342 | break; |
337 | default: | 343 | default: |
338 | $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_read=? "; | 344 | $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; |
339 | $params = array($user_id, 0); | 345 | $params = array($user_id, 0); |
340 | break; | 346 | break; |
341 | } | 347 | } |
@@ -353,11 +359,24 @@ class Database { | |||
353 | return $query; | 359 | return $query; |
354 | } | 360 | } |
355 | 361 | ||
356 | public function add($url, $title, $content, $user_id) { | 362 | /** |
357 | $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)'; | 363 | * |
358 | $params_action = array($url, $title, $content, $user_id); | 364 | * @param string $url |
359 | $query = $this->executeQuery($sql_action, $params_action); | 365 | * @param string $title |
360 | return $query; | 366 | * @param string $content |
367 | * @param integer $user_id | ||
368 | * @return integer $id of inserted record | ||
369 | */ | ||
370 | public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0) { | ||
371 | $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)'; | ||
372 | $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead); | ||
373 | if ( !$this->executeQuery($sql_action, $params_action) ) { | ||
374 | $id = null; | ||
375 | } | ||
376 | else { | ||
377 | $id = intval($this->getLastId( (STORAGE == 'postgres') ? 'users_id_seq' : '' )); | ||
378 | } | ||
379 | return $id; | ||
361 | } | 380 | } |
362 | 381 | ||
363 | public function deleteById($id, $user_id) { | 382 | public function deleteById($id, $user_id) { |
@@ -388,7 +407,7 @@ class Database { | |||
388 | public function getLastId($column = '') { | 407 | public function getLastId($column = '') { |
389 | return $this->getHandle()->lastInsertId($column); | 408 | return $this->getHandle()->lastInsertId($column); |
390 | } | 409 | } |
391 | 410 | ||
392 | public function search($term){ | 411 | public function search($term){ |
393 | $search = '%'.$term.'%'; | 412 | $search = '%'.$term.'%'; |
394 | $query = $this->getHandle()->prepare("SELECT * FROM entries WHERE content LIKE ? OR title LIKE ? OR url LIKE ?"); //searches in content, title and URL | 413 | $query = $this->getHandle()->prepare("SELECT * FROM entries WHERE content LIKE ? OR title LIKE ? OR url LIKE ?"); //searches in content, title and URL |
@@ -401,7 +420,7 @@ class Database { | |||
401 | $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags | 420 | $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags |
402 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id | 421 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
403 | LEFT JOIN entries ON tags_entries.entry_id=entries.id | 422 | LEFT JOIN entries ON tags_entries.entry_id=entries.id |
404 | WHERE entries.content <> '' AND entries.user_id=? | 423 | WHERE entries.user_id=? |
405 | ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ." | 424 | ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ." |
406 | GROUP BY tags.id, tags.value | 425 | GROUP BY tags.id, tags.value |
407 | ORDER BY tags.value"; | 426 | ORDER BY tags.value"; |
@@ -416,7 +435,7 @@ class Database { | |||
416 | $sql = "SELECT DISTINCT tags.* FROM tags | 435 | $sql = "SELECT DISTINCT tags.* FROM tags |
417 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id | 436 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
418 | LEFT JOIN entries ON tags_entries.entry_id=entries.id | 437 | LEFT JOIN entries ON tags_entries.entry_id=entries.id |
419 | WHERE entries.content <> '' AND tags.id=? AND entries.user_id=?"; | 438 | WHERE tags.id=? AND entries.user_id=?"; |
420 | $params = array(intval($id), $user_id); | 439 | $params = array(intval($id), $user_id); |
421 | $query = $this->executeQuery($sql, $params); | 440 | $query = $this->executeQuery($sql, $params); |
422 | $tag = $query->fetchAll(); | 441 | $tag = $query->fetchAll(); |
@@ -425,11 +444,10 @@ class Database { | |||
425 | } | 444 | } |
426 | 445 | ||
427 | public function retrieveEntriesByTag($tag_id, $user_id) { | 446 | public function retrieveEntriesByTag($tag_id, $user_id) { |
428 | $sql = | 447 | $sql = |
429 | "SELECT entries.* FROM entries | 448 | "SELECT entries.* FROM entries |
430 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id | 449 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id |
431 | WHERE entries.content <> '' AND | 450 | WHERE tags_entries.tag_id = ? AND entries.user_id=?"; |
432 | tags_entries.tag_id = ? AND entries.user_id=?"; | ||
433 | $query = $this->executeQuery($sql, array($tag_id, $user_id)); | 451 | $query = $this->executeQuery($sql, array($tag_id, $user_id)); |
434 | $entries = $query->fetchAll(); | 452 | $entries = $query->fetchAll(); |
435 | 453 | ||
@@ -437,7 +455,7 @@ class Database { | |||
437 | } | 455 | } |
438 | 456 | ||
439 | public function retrieveTagsByEntry($entry_id) { | 457 | public function retrieveTagsByEntry($entry_id) { |
440 | $sql = | 458 | $sql = |
441 | "SELECT tags.* FROM tags | 459 | "SELECT tags.* FROM tags |
442 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id | 460 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
443 | WHERE tags_entries.entry_id = ?"; | 461 | WHERE tags_entries.entry_id = ?"; |