From 005d857cfb4e695da6fa2997b4eeda02d20a48f4 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 4 Nov 2014 19:26:27 +0100 Subject: [PATCH] Added manual tag removal --- data/config.ini | 1 + public_html/js/Auth.js | 1 + public_html/js/Presenters/TagPresenter.js | 18 ++++++++++++++++-- public_html/templates/tag.tpl | 3 ++- src/Controllers/TagController.php | 8 ++++++++ src/Privilege.php | 1 + src/Services/TagService.php | 13 +++++++++++++ 7 files changed, 42 insertions(+), 3 deletions(-) diff --git a/data/config.ini b/data/config.ini index d17520ed..aaf74d6c 100644 --- a/data/config.ini +++ b/data/config.ini @@ -68,6 +68,7 @@ changeTagCategory = moderator, administrator changeTagImplications = moderator, administrator changeTagSuggestions = moderator, administrator banTags = moderator, administrator +deleteTags = moderator, administrator listComments = regularUser, powerUser, moderator, administrator addComments = regularUser, powerUser, moderator, administrator diff --git a/public_html/js/Auth.js b/public_html/js/Auth.js index e2d9ed05..dc717965 100644 --- a/public_html/js/Auth.js +++ b/public_html/js/Auth.js @@ -45,6 +45,7 @@ App.Auth = function(_, jQuery, util, api, appState, promise) { editAllComments: 'editAllComments', deleteOwnComments: 'deleteOwnComments', deleteAllComments: 'deleteAllComments', + deleteTags: 'deleteTags', listTags: 'listTags', massTag: 'massTag', diff --git a/public_html/js/Presenters/TagPresenter.js b/public_html/js/Presenters/TagPresenter.js index c91e9f5f..b7ea4470 100644 --- a/public_html/js/Presenters/TagPresenter.js +++ b/public_html/js/Presenters/TagPresenter.js @@ -83,12 +83,14 @@ App.Presenters.TagPresenter = function( tagCategories: JSON.parse(jQuery('head').attr('data-tag-categories')), })); $el.find('.post-list').hide(); - $el.find('form').submit(editFormSubmitted); + $el.find('form').submit(function(e) { e.preventDefault(); }); + $el.find('form button[name=update]').click(updateButtonClicked); + $el.find('form button[name=delete]').click(deleteButtonClicked); implicationsTagInput = App.Controls.TagInput($el.find('[name=implications]')); suggestionsTagInput = App.Controls.TagInput($el.find('[name=suggestions]')); } - function editFormSubmitted(e) { + function updateButtonClicked(e) { e.preventDefault(); var $form = $el.find('form'); var formData = {}; @@ -121,6 +123,18 @@ App.Presenters.TagPresenter = function( }); } + function deleteButtonClicked(e) { + if (!window.confirm('Are you sure you want to delete this tag?')) { + return; + } + promise.wait(api.delete('/tags/' + tag.name)) + .then(function(response) { + router.navigate('#/tags'); + }).fail(function(response) { + window.alert(response.json && response.json.error || 'An error occured.'); + }); + } + function renderPosts(posts) { var $target = $el.find('.post-list ul'); _.each(posts, function(post) { diff --git a/public_html/templates/tag.tpl b/public_html/templates/tag.tpl index 87e2e2a7..1085f9e7 100644 --- a/public_html/templates/tag.tpl +++ b/public_html/templates/tag.tpl @@ -70,7 +70,8 @@
- + +
<% } %> diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index 049d7fc0..b6c402d0 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -37,6 +37,7 @@ final class TagController extends AbstractController $router->get('/api/tags/:tagName', [$this, 'getTag']); $router->get('/api/tags/:tagName/siblings', [$this, 'getTagSiblings']); $router->put('/api/tags/:tagName', [$this, 'updateTag']); + $router->delete('/api/tags/:tagName', [$this, 'deleteTag']); } public function getTag($tagName) @@ -97,6 +98,13 @@ final class TagController extends AbstractController return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); } + public function deleteTag($tagName) + { + $tag = $this->tagService->getByName($tagName); + $this->privilegeService->assertPrivilege(Privilege::DELETE_TAGS); + return $this->tagService->deleteTag($tag); + } + private function getFullFetchConfig() { return diff --git a/src/Privilege.php b/src/Privilege.php index 9ca3cb9c..d206abd1 100644 --- a/src/Privilege.php +++ b/src/Privilege.php @@ -46,6 +46,7 @@ class Privilege const CHANGE_TAG_IMPLICATIONS = 'changeTagImplications'; const CHANGE_TAG_SUGGESTIONS = 'changeTagSuggestions'; const BAN_TAGS = 'banTags'; + const DELETE_TAGS = 'deleteTags'; const LIST_COMMENTS = 'listComments'; const ADD_COMMENTS = 'addComments'; diff --git a/src/Services/TagService.php b/src/Services/TagService.php index 047861da..0869fcc5 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -165,6 +165,19 @@ class TagService return $ret; } + public function deleteTag(Tag $tag) + { + if ($tag->getUsages() !== 0) + throw new \DomainException('Only tags with no usages can be deleted.'); + + $transactionFunc = function() use ($tag) + { + $this->tagDao->deleteById($tag->getId()); + }; + + $this->transactionManager->commit($transactionFunc); + } + private function updateTagName(Tag $tag, $newName) { $otherTag = $this->tagDao->findByName($newName);