diff options
Diffstat (limited to 'inc')
-rwxr-xr-x | inc/poche/Database.class.php | 19 | ||||
-rwxr-xr-x | inc/poche/Poche.class.php | 82 |
2 files changed, 69 insertions, 32 deletions
diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php index 141d7987..9e901974 100755 --- a/inc/poche/Database.class.php +++ b/inc/poche/Database.class.php | |||
@@ -511,6 +511,25 @@ 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 cleanUnusedTag($tag_id) { | ||
516 | $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?"; | ||
517 | $query = $this->executeQuery($sql_action,array($tag_id)); | ||
518 | $tagstokeep = $query->fetchAll(); | ||
519 | $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?"; | ||
520 | $query = $this->executeQuery($sql_action,array($tag_id)); | ||
521 | $alltags = $query->fetchAll(); | ||
522 | |||
523 | foreach ($alltags as $tag) { | ||
524 | if ($tag && !in_array($tag,$tagstokeep)) { | ||
525 | $sql_action = "DELETE FROM tags WHERE id=?"; | ||
526 | $params_action = array($tag[0]); | ||
527 | $this->executeQuery($sql_action, $params_action); | ||
528 | return true; | ||
529 | } | ||
530 | } | ||
531 | |||
532 | } | ||
514 | 533 | ||
515 | public function retrieveTagByValue($value) { | 534 | public function retrieveTagByValue($value) { |
516 | $tag = NULL; | 535 | $tag = NULL; |
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index 5aa6ea07..515d4cac 100755 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php | |||
@@ -511,42 +511,55 @@ 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); | ||
551 | } | ||
552 | else { | ||
553 | $tag_id = $tag['id']; | ||
539 | } | 554 | } |
540 | $tag_id = $this->store->getLastId($sequence); | 555 | |
541 | } | 556 | # we assign the tag to the article |
542 | else { | 557 | $this->store->setTagToEntry($tag_id, $entry_id); |
543 | $tag_id = $tag['id']; | 558 | } |
544 | } | ||
545 | |||
546 | # we assign the tag to the article | ||
547 | $this->store->setTagToEntry($tag_id, $entry_id); | ||
548 | } | 559 | } |
549 | } | 560 | } |
561 | $this->messages->add('s', _('The tag has been applied successfully')); | ||
562 | Tools::logm('The tag has been applied successfully'); | ||
550 | Tools::redirect(); | 563 | Tools::redirect(); |
551 | break; | 564 | break; |
552 | case 'remove_tag' : | 565 | case 'remove_tag' : |
@@ -558,6 +571,11 @@ class Poche | |||
558 | Tools::redirect(); | 571 | Tools::redirect(); |
559 | } | 572 | } |
560 | $this->store->removeTagForEntry($id, $tag_id); | 573 | $this->store->removeTagForEntry($id, $tag_id); |
574 | Tools::logm('tag entry deleted'); | ||
575 | if ($this->store->cleanUnusedTag($tag_id)) { | ||
576 | Tools::logm('tag deleted'); | ||
577 | } | ||
578 | $this->messages->add('s', _('The tag has been successfully deleted')); | ||
561 | Tools::redirect(); | 579 | Tools::redirect(); |
562 | break; | 580 | break; |
563 | default: | 581 | default: |