Merge pull request #689 from tcitworld/dev

Tag-related features
This commit is contained in:
Nicolas Lœuillet 2014-05-16 13:31:53 +02:00
commit 60c3a4d3e1
3 changed files with 70 additions and 32 deletions

View file

@ -512,6 +512,25 @@ class Database {
return $query;
}
public function cleanUnusedTag($tag_id) {
$sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
$query = $this->executeQuery($sql_action,array($tag_id));
$tagstokeep = $query->fetchAll();
$sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
$query = $this->executeQuery($sql_action,array($tag_id));
$alltags = $query->fetchAll();
foreach ($alltags as $tag) {
if ($tag && !in_array($tag,$tagstokeep)) {
$sql_action = "DELETE FROM tags WHERE id=?";
$params_action = array($tag[0]);
$this->executeQuery($sql_action, $params_action);
return true;
}
}
}
public function retrieveTagByValue($value) {
$tag = NULL;
$sql = "SELECT * FROM tags WHERE value=?";

View file

@ -511,8 +511,19 @@ class Poche
Tools::redirect();
break;
case 'add_tag' :
if (isset($_GET['search'])) {
//when we want to apply a tag to a search
$tags = array($_GET['search']);
$allentry_ids = $this->store->search($tags[0], $this->user->getId());
$entry_ids = array();
foreach ($allentry_ids as $eachentry) {
$entry_ids[] = $eachentry[0];
}
} else { //add a tag to a single article
$tags = explode(',', $_POST['value']);
$entry_id = $_POST['entry_id'];
$entry_ids = array($_POST['entry_id']);
}
foreach($entry_ids as $entry_id) {
$entry = $this->store->retrieveOneById($entry_id, $this->user->getId());
if (!$entry) {
$this->messages->add('e', _('Article not found!'));
@ -529,7 +540,6 @@ class Poche
$value = trim($tag_value);
if ($value && !in_array($value, $already_set_tags)) {
$tag = $this->store->retrieveTagByValue($value);
if (is_null($tag)) {
# we create the tag
$tag = $this->store->createTag($value);
@ -547,6 +557,9 @@ class Poche
$this->store->setTagToEntry($tag_id, $entry_id);
}
}
}
$this->messages->add('s', _('The tag has been applied successfully'));
Tools::logm('The tag has been applied successfully');
Tools::redirect();
break;
case 'remove_tag' :
@ -558,6 +571,11 @@ class Poche
Tools::redirect();
}
$this->store->removeTagForEntry($id, $tag_id);
Tools::logm('tag entry deleted');
if ($this->store->cleanUnusedTag($tag_id)) {
Tools::logm('tag deleted');
}
$this->messages->add('s', _('The tag has been successfully deleted'));
Tools::redirect();
break;
default:

View file

@ -25,7 +25,7 @@
{% block pager %}
{% if nb_results > 1 %}
<div class="results">
<div class="nb-results">{{ nb_results }} {% trans "results" %}{% if search_term is defined %}{% trans " found for « " %} {{ search_term }} »{% endif %}</div>
<div class="nb-results">{{ nb_results }} {% trans "results" %}{% if search_term is defined %} {% trans %}found for « {{ search_term }} »{% endtrans %}{% endif %}</div>
{{ page_links | raw }}
</div>
{% elseif nb_results == 1 %}
@ -57,6 +57,7 @@
{% endfor %}
</div>
{% if view == 'home' %}{% if nb_results > 1 %}<a title="{% trans "Mark all the entries as read" %}" href="./?action=archive_all">{{ "Mark all the entries as read" }}</a>{% endif %}{% endif %}
{% if search_term is defined %}<a title="{% trans %} Apply the tag {{ search_term }} to this search {% endtrans %}" href="./?action=add_tag&search={{ search_term }}">{% trans %} Apply the tag {{ search_term }} to this search {% endtrans %}</a>{% endif %}
{% endif %}
{{ block('pager') }}
{% endblock %}