From c432fa1674fbe2b190cf0d0bab2e90302a61e98f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 6 Dec 2013 15:07:51 +0100 Subject: [PATCH] [add] assign and remove a tag to an entry --- inc/poche/Database.class.php | 31 +++++++++++++++++++++++++++++++ inc/poche/Poche.class.php | 31 +++++++++++++++++++++++++++++++ themes/default/edit-tags.twig | 8 +++++--- themes/default/view.twig | 2 +- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php index a89bce41..d95b9b81 100644 --- a/inc/poche/Database.class.php +++ b/inc/poche/Database.class.php @@ -289,4 +289,35 @@ class Database { return $tags; } + + public function removeTagForEntry($entry_id, $tag_id) { + $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?"; + $params_action = array($tag_id, $entry_id); + $query = $this->executeQuery($sql_action, $params_action); + return $query; + } + + public function retrieveTagByValue($value) { + $tag = NULL; + $sql = "SELECT * FROM tags WHERE value=?"; + $params = array($value); + $query = $this->executeQuery($sql, $params); + $tag = $query->fetchAll(); + + return isset($tag[0]) ? $tag[0] : null; + } + + public function createTag($value) { + $sql_action = 'INSERT INTO tags ( value ) VALUES (?)'; + $params_action = array($value); + $query = $this->executeQuery($sql_action, $params_action); + return $query; + } + + public function setTagToEntry($tag_id, $entry_id) { + $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)'; + $params_action = array($tag_id, $entry_id); + $query = $this->executeQuery($sql_action, $params_action); + return $query; + } } diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index 68f56d62..d415dd03 100644 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php @@ -397,6 +397,36 @@ class Poche Tools::redirect(); } break; + case 'add_tag' : + $tags = explode(',', $_POST['value']); + $entry_id = $_POST['entry_id']; + 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']; + } + + # we assign the tag to the article + $this->store->setTagToEntry($tag_id, $entry_id); + } + Tools::redirect(); + break; + case 'remove_tag' : + $tag_id = $_GET['tag_id']; + $this->store->removeTagForEntry($id, $tag_id); + Tools::redirect(); + break; default: break; } @@ -434,6 +464,7 @@ class Poche # tags $tags = $this->store->retrieveTagsByEntry($id); $tpl_vars = array( + 'entry_id' => $id, 'tags' => $tags, ); break; diff --git a/themes/default/edit-tags.twig b/themes/default/edit-tags.twig index 0bd1aba7..7116bba9 100644 --- a/themes/default/edit-tags.twig +++ b/themes/default/edit-tags.twig @@ -8,11 +8,13 @@ no tags {% endif %} -
+ - {% trans "you can type several tags, separated by comma" %}
+

{% trans "you can type several tags, separated by comma" %}

+
+{% trans "back to the article" %} {% endblock %} \ No newline at end of file diff --git a/themes/default/view.twig b/themes/default/view.twig index 7e096a95..64672b61 100644 --- a/themes/default/view.twig +++ b/themes/default/view.twig @@ -21,7 +21,7 @@

{{ entry.title|raw }}

{{ content | raw }} -- 2.41.0