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;
+ }
}
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;
}
# tags
$tags = $this->store->retrieveTagsByEntry($id);
$tpl_vars = array(
+ 'entry_id' => $id,
'tags' => $tags,
);
break;
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&tag_id={{ tag.id }}&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
<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&id={{ entry.id|e }}" title="{% trans "edit tags" %}">✎</a>
+ tags: {% for tag in tags %}<a href="./?view=tag&id={{ tag.id }}">{{ tag.value }}</a> {% endfor %}<a href="./?view=edit-tags&id={{ entry.id|e }}" title="{% trans "edit tags" %}">✎</a>
</aside>
<article>
{{ content | raw }}