aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--inc/poche/Database.class.php31
-rw-r--r--inc/poche/Poche.class.php31
-rw-r--r--themes/default/edit-tags.twig8
-rw-r--r--themes/default/view.twig2
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 {
289 289
290 return $tags; 290 return $tags;
291 } 291 }
292
293 public function removeTagForEntry($entry_id, $tag_id) {
294 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
295 $params_action = array($tag_id, $entry_id);
296 $query = $this->executeQuery($sql_action, $params_action);
297 return $query;
298 }
299
300 public function retrieveTagByValue($value) {
301 $tag = NULL;
302 $sql = "SELECT * FROM tags WHERE value=?";
303 $params = array($value);
304 $query = $this->executeQuery($sql, $params);
305 $tag = $query->fetchAll();
306
307 return isset($tag[0]) ? $tag[0] : null;
308 }
309
310 public function createTag($value) {
311 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
312 $params_action = array($value);
313 $query = $this->executeQuery($sql_action, $params_action);
314 return $query;
315 }
316
317 public function setTagToEntry($tag_id, $entry_id) {
318 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
319 $params_action = array($tag_id, $entry_id);
320 $query = $this->executeQuery($sql_action, $params_action);
321 return $query;
322 }
292} 323}
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
397 Tools::redirect(); 397 Tools::redirect();
398 } 398 }
399 break; 399 break;
400 case 'add_tag' :
401 $tags = explode(',', $_POST['value']);
402 $entry_id = $_POST['entry_id'];
403 foreach($tags as $key => $tag_value) {
404 $value = trim($tag_value);
405 $tag = $this->store->retrieveTagByValue($value);
406
407 if (is_null($tag)) {
408 # we create the tag
409 $tag = $this->store->createTag($value);
410 $sequence = '';
411 if (STORAGE == 'postgres') {
412 $sequence = 'tags_id_seq';
413 }
414 $tag_id = $this->store->getLastId($sequence);
415 }
416 else {
417 $tag_id = $tag['id'];
418 }
419
420 # we assign the tag to the article
421 $this->store->setTagToEntry($tag_id, $entry_id);
422 }
423 Tools::redirect();
424 break;
425 case 'remove_tag' :
426 $tag_id = $_GET['tag_id'];
427 $this->store->removeTagForEntry($id, $tag_id);
428 Tools::redirect();
429 break;
400 default: 430 default:
401 break; 431 break;
402 } 432 }
@@ -434,6 +464,7 @@ class Poche
434 # tags 464 # tags
435 $tags = $this->store->retrieveTagsByEntry($id); 465 $tags = $this->store->retrieveTagsByEntry($id);
436 $tpl_vars = array( 466 $tpl_vars = array(
467 'entry_id' => $id,
437 'tags' => $tags, 468 'tags' => $tags,
438 ); 469 );
439 break; 470 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 @@
8no tags 8no tags
9{% endif %} 9{% endif %}
10<ul> 10<ul>
11{% for tag in tags %}<li>{{ tag.value }} <a href="#">✘</a></li>{% endfor %} 11{% for tag in tags %}<li>{{ tag.value }} <a href="./?action=remove_tag&amp;tag_id={{ tag.id }}&amp;id={{ entry_id }}">✘</a></li>{% endfor %}
12</ul> 12</ul>
13<form method="post" action="#"> 13<form method="post" action="./?action=add_tag">
14 <label for="value">New tags: </label><input type="text" id="value" name="value" required="required" /> 14 <label for="value">New tags: </label><input type="text" id="value" name="value" required="required" />
15 {% trans "you can type several tags, separated by comma" %}<br /> 15 <p>{% trans "you can type several tags, separated by comma" %}</p>
16 <input type="hidden" name="entry_id" value="{{ entry_id }}" />
16 <input type="submit" value="add tags" /> 17 <input type="submit" value="add tags" />
17</form> 18</form>
19<a href="./?view=view&id={{ entry_id }}">{% trans "back to the article" %}</a>
18{% endblock %} \ No newline at end of file 20{% 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 @@
21 <h1>{{ entry.title|raw }}</h1> 21 <h1>{{ entry.title|raw }}</h1>
22 </header> 22 </header>
23 <aside class="tags"> 23 <aside class="tags">
24 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> 24 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>
25 </aside> 25 </aside>
26 <article> 26 <article>
27 {{ content | raw }} 27 {{ content | raw }}