]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
[add] assign and remove a tag to an entry
authorNicolas Lœuillet <nicolas.loeuillet@gmail.com>
Fri, 6 Dec 2013 14:07:51 +0000 (15:07 +0100)
committerNicolas Lœuillet <nicolas.loeuillet@gmail.com>
Fri, 6 Dec 2013 14:07:51 +0000 (15:07 +0100)
inc/poche/Database.class.php
inc/poche/Poche.class.php
themes/default/edit-tags.twig
themes/default/view.twig

index a89bce41af00450f530dc3439047516f4a8fd96c..d95b9b8101c08903bd4dfa80875697aca957b9db 100644 (file)
@@ -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;
+    }
 }
index 68f56d623999c5f1c2ed7a6143f4ebc39e1d4b9f..d415dd03b6cd3820a7a1a62a041027657eeccd7d 100644 (file)
@@ -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;
index 0bd1aba7b96cc874915ac5a76cc8b106b8785527..7116bba9f55c01c305d6f8779ad85d876713f1ed 100644 (file)
@@ -8,11 +8,13 @@
 no tags
 {% endif %}
 <ul>
-{% for tag in tags %}<li>{{ tag.value }} <a href="#">✘</a></li>{% endfor %}
+{% for tag in tags %}<li>{{ tag.value }} <a href="./?action=remove_tag&amp;tag_id={{ tag.id }}&amp;id={{ entry_id }}">✘</a></li>{% endfor %}
 </ul>
-<form method="post" action="#">
+<form method="post" action="./?action=add_tag">
     <label for="value">New tags: </label><input type="text" id="value" name="value" required="required" />
-    {% trans "you can type several tags, separated by comma" %}<br />
+    <p>{% trans "you can type several tags, separated by comma" %}</p>
+    <input type="hidden" name="entry_id" value="{{ entry_id }}" />
     <input type="submit" value="add tags" />
 </form>
+<a href="./?view=view&id={{ entry_id }}">{% trans "back to the article" %}</a>
 {% endblock %}
\ No newline at end of file
index 7e096a95c0e8fb4e5d1a64a716e554a264e30f3d..64672b617752a0d60dcf74d13d51d9933d289c04 100644 (file)
@@ -21,7 +21,7 @@
                 <h1>{{ entry.title|raw }}</h1>
             </header>
             <aside class="tags">
-                tags: {% for tag in tags %}<a href="#">{{ tag.value }}</a> {% endfor %}<a href="./?view=edit-tags&amp;id={{ entry.id|e }}" title="{% trans "edit tags" %}">✎</a>
+                tags: {% for tag in tags %}<a href="./?view=tag&amp;id={{ tag.id }}">{{ tag.value }}</a> {% endfor %}<a href="./?view=edit-tags&amp;id={{ entry.id|e }}" title="{% trans "edit tags" %}">✎</a>
             </aside>
             <article>
                 {{ content | raw }}