1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-08-01 17:38:38 +00:00

[add] assign and remove a tag to an entry

This commit is contained in:
Nicolas Lœuillet 2013-12-06 15:07:51 +01:00
parent f778e47283
commit c432fa1674
4 changed files with 68 additions and 4 deletions

View 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;
}
}

View 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;