From fb26cc9375ce9ef8df748eb473eb6e58884421c6 Mon Sep 17 00:00:00 2001 From: Maryana Rozhankivska Date: Mon, 10 Mar 2014 16:28:47 +0200 Subject: a lot of enhancements related to tags: tags list is now sorted, shows number of articles, autocomplete added according to #477, #542 --- inc/poche/Database.class.php | 11 ++++++---- inc/poche/Poche.class.php | 52 +++++++++++++++++++++++++++++--------------- inc/poche/Tools.class.php | 10 +++++++++ 3 files changed, 52 insertions(+), 21 deletions(-) mode change 100644 => 100755 inc/poche/Tools.class.php (limited to 'inc') 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 { return $this->getHandle()->lastInsertId($column); } - public function retrieveAllTags($user_id) { - $sql = "SELECT DISTINCT tags.* FROM tags + public function retrieveAllTags($user_id, $term = null) { + $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id LEFT JOIN entries ON tags_entries.entry_id=entries.id - WHERE entries.content <> '' AND entries.user_id=?"; - $query = $this->executeQuery($sql, array($user_id)); + WHERE entries.content <> '' AND entries.user_id=? + ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ." + GROUP BY tags.id, tags.value + ORDER BY tags.value"; + $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) )); $tags = $query->fetchAll(); 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 Tools::logm('error : article not found'); Tools::redirect(); } + //get all already set tags to preven duplicates + $already_set_tags = array(); + $entry_tags = $this->store->retrieveTagsByEntry($entry_id); + foreach ($entry_tags as $tag) { + $already_set_tags[] = $tag['value']; + } foreach($tags as $key => $tag_value) { $value = trim($tag_value); - $tag = $this->store->retrieveTagByValue($value); - - if (is_null($tag)) { - # we create the tag - $tag = $this->store->createTag($value); - $sequence = ''; - if (STORAGE == 'postgres') { - $sequence = 'tags_id_seq'; - } - $tag_id = $this->store->getLastId($sequence); - } - else { - $tag_id = $tag['id']; + if ($value && !in_array($value, $already_set_tags)) { + $tag = $this->store->retrieveTagByValue($value); + + if (is_null($tag)) { + # we create the tag + $tag = $this->store->createTag($value); + $sequence = ''; + if (STORAGE == 'postgres') { + $sequence = 'tags_id_seq'; + } + $tag_id = $this->store->getLastId($sequence); + } + else { + $tag_id = $tag['id']; + } + + # we assign the tag to the article + $this->store->setTagToEntry($tag_id, $entry_id); } - - # we assign the tag to the article - $this->store->setTagToEntry($tag_id, $entry_id); } if(!$import) { Tools::redirect(); @@ -579,7 +587,17 @@ class Poche break; case 'tags': $token = $this->user->getConfigValue('token'); - $tags = $this->store->retrieveAllTags($this->user->getId()); + //if term is set - search tags for this term + $term = Tools::checkVar('term'); + $tags = $this->store->retrieveAllTags($this->user->getId(), $term); + if (Tools::isAjaxRequest()) { + $result = array(); + foreach ($tags as $tag) { + $result[] = $tag['value']; + } + echo json_encode($result); + exit; + } $tpl_vars = array( 'token' => $token, 'user_id' => $this->user->getId(), diff --git a/inc/poche/Tools.class.php b/inc/poche/Tools.class.php old mode 100644 new mode 100755 index eeb101b4..ad451fc6 --- a/inc/poche/Tools.class.php +++ b/inc/poche/Tools.class.php @@ -311,4 +311,14 @@ class Tools return json_decode($json, true); } + + /** + * Returns whether we handle an AJAX (XMLHttpRequest) request. + * @return boolean whether we handle an AJAX (XMLHttpRequest) request. + */ + public static function isAjaxRequest() + { + return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest'; + } + } -- cgit v1.2.3