diff options
Diffstat (limited to 'inc/poche')
-rwxr-xr-x | inc/poche/Database.class.php | 18 | ||||
-rwxr-xr-x | inc/poche/Poche.class.php | 87 |
2 files changed, 73 insertions, 32 deletions
diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php index 141d7987..02e8be8b 100755 --- a/inc/poche/Database.class.php +++ b/inc/poche/Database.class.php | |||
@@ -511,6 +511,24 @@ class Database { | |||
511 | $query = $this->executeQuery($sql_action, $params_action); | 511 | $query = $this->executeQuery($sql_action, $params_action); |
512 | return $query; | 512 | return $query; |
513 | } | 513 | } |
514 | |||
515 | public function cleanUnusedTags() { | ||
516 | $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id"; | ||
517 | $query = $this->executeQuery($sql_action,array()); | ||
518 | $tagstokeep = $query->fetchAll(); | ||
519 | $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id"; | ||
520 | $query = $this->executeQuery($sql_action,array()); | ||
521 | $alltags = $query->fetchAll(); | ||
522 | foreach ($alltags as $tag) { | ||
523 | if ($tag && !in_array($tag,$tagstokeep)) { | ||
524 | //delete tag | ||
525 | $sql_action = "DELETE FROM tags WHERE id=?"; | ||
526 | $params_action = array($tag[0]); | ||
527 | $query = $this->executeQuery($sql_action, $params_action); | ||
528 | return $query; | ||
529 | } | ||
530 | } | ||
531 | } | ||
514 | 532 | ||
515 | public function retrieveTagByValue($value) { | 533 | public function retrieveTagByValue($value) { |
516 | $tag = NULL; | 534 | $tag = NULL; |
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index 5aa6ea07..dc7b76d0 100755 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php | |||
@@ -511,42 +511,54 @@ class Poche | |||
511 | Tools::redirect(); | 511 | Tools::redirect(); |
512 | break; | 512 | break; |
513 | case 'add_tag' : | 513 | case 'add_tag' : |
514 | $tags = explode(',', $_POST['value']); | 514 | if (isset($_GET['search'])) { |
515 | $entry_id = $_POST['entry_id']; | 515 | //when we want to apply a tag to a search |
516 | $entry = $this->store->retrieveOneById($entry_id, $this->user->getId()); | 516 | $tags = array($_GET['search']); |
517 | if (!$entry) { | 517 | $allentry_ids = $this->store->search($tags[0], $this->user->getId()); |
518 | $this->messages->add('e', _('Article not found!')); | 518 | $entry_ids = array(); |
519 | Tools::logm('error : article not found'); | 519 | foreach ($allentry_ids as $eachentry) { |
520 | Tools::redirect(); | 520 | $entry_ids[] = $eachentry[0]; |
521 | } | 521 | } |
522 | //get all already set tags to preven duplicates | 522 | } else { //add a tag to a single article |
523 | $already_set_tags = array(); | 523 | $tags = explode(',', $_POST['value']); |
524 | $entry_tags = $this->store->retrieveTagsByEntry($entry_id); | 524 | $entry_ids = array($_POST['entry_id']); |
525 | foreach ($entry_tags as $tag) { | ||
526 | $already_set_tags[] = $tag['value']; | ||
527 | } | 525 | } |
528 | foreach($tags as $key => $tag_value) { | 526 | foreach($entry_ids as $entry_id) { |
529 | $value = trim($tag_value); | 527 | $entry = $this->store->retrieveOneById($entry_id, $this->user->getId()); |
530 | if ($value && !in_array($value, $already_set_tags)) { | 528 | if (!$entry) { |
531 | $tag = $this->store->retrieveTagByValue($value); | 529 | $this->messages->add('e', _('Article not found!')); |
532 | 530 | Tools::logm('error : article not found'); | |
533 | if (is_null($tag)) { | 531 | Tools::redirect(); |
534 | # we create the tag | 532 | } |
535 | $tag = $this->store->createTag($value); | 533 | //get all already set tags to preven duplicates |
536 | $sequence = ''; | 534 | $already_set_tags = array(); |
537 | if (STORAGE == 'postgres') { | 535 | $entry_tags = $this->store->retrieveTagsByEntry($entry_id); |
538 | $sequence = 'tags_id_seq'; | 536 | foreach ($entry_tags as $tag) { |
537 | $already_set_tags[] = $tag['value']; | ||
538 | } | ||
539 | foreach($tags as $key => $tag_value) { | ||
540 | $value = trim($tag_value); | ||
541 | if ($value && !in_array($value, $already_set_tags)) { | ||
542 | $tag = $this->store->retrieveTagByValue($value); | ||
543 | if (is_null($tag)) { | ||
544 | # we create the tag | ||
545 | $tag = $this->store->createTag($value); | ||
546 | $sequence = ''; | ||
547 | if (STORAGE == 'postgres') { | ||
548 | $sequence = 'tags_id_seq'; | ||
549 | } | ||
550 | $tag_id = $this->store->getLastId($sequence); | ||
539 | } | 551 | } |
540 | $tag_id = $this->store->getLastId($sequence); | 552 | else { |
541 | } | 553 | $tag_id = $tag['id']; |
542 | else { | 554 | } |
543 | $tag_id = $tag['id']; | 555 | |
544 | } | 556 | # we assign the tag to the article |
545 | 557 | $this->store->setTagToEntry($tag_id, $entry_id); | |
546 | # we assign the tag to the article | 558 | } |
547 | $this->store->setTagToEntry($tag_id, $entry_id); | ||
548 | } | 559 | } |
549 | } | 560 | } |
561 | $this->messages->add('s', _('The tag has been applied successfully')); | ||
550 | Tools::redirect(); | 562 | Tools::redirect(); |
551 | break; | 563 | break; |
552 | case 'remove_tag' : | 564 | case 'remove_tag' : |
@@ -558,6 +570,10 @@ class Poche | |||
558 | Tools::redirect(); | 570 | Tools::redirect(); |
559 | } | 571 | } |
560 | $this->store->removeTagForEntry($id, $tag_id); | 572 | $this->store->removeTagForEntry($id, $tag_id); |
573 | Tools::logm('tag entry deleted'); | ||
574 | $this->store->cleanUnusedTags(); | ||
575 | Tools::logm('old tags cleaned'); | ||
576 | $this->messages->add('s', _('The tag has been successfully deleted')); | ||
561 | Tools::redirect(); | 577 | Tools::redirect(); |
562 | break; | 578 | break; |
563 | default: | 579 | default: |
@@ -1119,6 +1135,13 @@ class Poche | |||
1119 | $this->messages->add('s', _('Cache deleted.')); | 1135 | $this->messages->add('s', _('Cache deleted.')); |
1120 | Tools::redirect(); | 1136 | Tools::redirect(); |
1121 | } | 1137 | } |
1138 | |||
1139 | public function cleanTags() { | ||
1140 | $this->store->cleanUnusedTags(); | ||
1141 | $this->messages->add('s', _('The unused tags have been cleaned.')); | ||
1142 | Tools::logm('clean tags'); | ||
1143 | Tools::redirect(); | ||
1144 | } | ||
1122 | 1145 | ||
1123 | /** | 1146 | /** |
1124 | * return new purifier object with actual config | 1147 | * return new purifier object with actual config |