aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc
diff options
context:
space:
mode:
Diffstat (limited to 'inc')
-rwxr-xr-xinc/poche/Database.class.php11
-rwxr-xr-xinc/poche/Poche.class.php52
-rwxr-xr-x[-rw-r--r--]inc/poche/Tools.class.php10
3 files changed, 52 insertions, 21 deletions
diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php
index edc775f5..5b51b507 100755
--- a/inc/poche/Database.class.php
+++ b/inc/poche/Database.class.php
@@ -389,12 +389,15 @@ class Database {
389 return $this->getHandle()->lastInsertId($column); 389 return $this->getHandle()->lastInsertId($column);
390 } 390 }
391 391
392 public function retrieveAllTags($user_id) { 392 public function retrieveAllTags($user_id, $term = null) {
393 $sql = "SELECT DISTINCT tags.* FROM tags 393 $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags
394 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id 394 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
395 LEFT JOIN entries ON tags_entries.entry_id=entries.id 395 LEFT JOIN entries ON tags_entries.entry_id=entries.id
396 WHERE entries.content <> '' AND entries.user_id=?"; 396 WHERE entries.content <> '' AND entries.user_id=?
397 $query = $this->executeQuery($sql, array($user_id)); 397 ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ."
398 GROUP BY tags.id, tags.value
399 ORDER BY tags.value";
400 $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) ));
398 $tags = $query->fetchAll(); 401 $tags = $query->fetchAll();
399 402
400 return $tags; 403 return $tags;
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php
index 0c8e798c..480f6d45 100755
--- a/inc/poche/Poche.class.php
+++ b/inc/poche/Poche.class.php
@@ -488,25 +488,33 @@ class Poche
488 Tools::logm('error : article not found'); 488 Tools::logm('error : article not found');
489 Tools::redirect(); 489 Tools::redirect();
490 } 490 }
491 //get all already set tags to preven duplicates
492 $already_set_tags = array();
493 $entry_tags = $this->store->retrieveTagsByEntry($entry_id);
494 foreach ($entry_tags as $tag) {
495 $already_set_tags[] = $tag['value'];
496 }
491 foreach($tags as $key => $tag_value) { 497 foreach($tags as $key => $tag_value) {
492 $value = trim($tag_value); 498 $value = trim($tag_value);
493 $tag = $this->store->retrieveTagByValue($value); 499 if ($value && !in_array($value, $already_set_tags)) {
494 500 $tag = $this->store->retrieveTagByValue($value);
495 if (is_null($tag)) { 501
496 # we create the tag 502 if (is_null($tag)) {
497 $tag = $this->store->createTag($value); 503 # we create the tag
498 $sequence = ''; 504 $tag = $this->store->createTag($value);
499 if (STORAGE == 'postgres') { 505 $sequence = '';
500 $sequence = 'tags_id_seq'; 506 if (STORAGE == 'postgres') {
501 } 507 $sequence = 'tags_id_seq';
502 $tag_id = $this->store->getLastId($sequence); 508 }
503 } 509 $tag_id = $this->store->getLastId($sequence);
504 else { 510 }
505 $tag_id = $tag['id']; 511 else {
512 $tag_id = $tag['id'];
513 }
514
515 # we assign the tag to the article
516 $this->store->setTagToEntry($tag_id, $entry_id);
506 } 517 }
507
508 # we assign the tag to the article
509 $this->store->setTagToEntry($tag_id, $entry_id);
510 } 518 }
511 if(!$import) { 519 if(!$import) {
512 Tools::redirect(); 520 Tools::redirect();
@@ -579,7 +587,17 @@ class Poche
579 break; 587 break;
580 case 'tags': 588 case 'tags':
581 $token = $this->user->getConfigValue('token'); 589 $token = $this->user->getConfigValue('token');
582 $tags = $this->store->retrieveAllTags($this->user->getId()); 590 //if term is set - search tags for this term
591 $term = Tools::checkVar('term');
592 $tags = $this->store->retrieveAllTags($this->user->getId(), $term);
593 if (Tools::isAjaxRequest()) {
594 $result = array();
595 foreach ($tags as $tag) {
596 $result[] = $tag['value'];
597 }
598 echo json_encode($result);
599 exit;
600 }
583 $tpl_vars = array( 601 $tpl_vars = array(
584 'token' => $token, 602 'token' => $token,
585 'user_id' => $this->user->getId(), 603 'user_id' => $this->user->getId(),
diff --git a/inc/poche/Tools.class.php b/inc/poche/Tools.class.php
index eeb101b4..ad451fc6 100644..100755
--- a/inc/poche/Tools.class.php
+++ b/inc/poche/Tools.class.php
@@ -311,4 +311,14 @@ class Tools
311 311
312 return json_decode($json, true); 312 return json_decode($json, true);
313 } 313 }
314
315 /**
316 * Returns whether we handle an AJAX (XMLHttpRequest) request.
317 * @return boolean whether we handle an AJAX (XMLHttpRequest) request.
318 */
319 public static function isAjaxRequest()
320 {
321 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
322 }
323
314} 324}