From 5815f156a84e8d97112785e2935215e31bba561b Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 15 Oct 2014 20:07:02 +0200 Subject: [PATCH] Added tag relationship management to backend --- TODO | 5 ----- data/config.ini | 2 ++ public_html/js/Auth.js | 2 ++ src/Controllers/TagController.php | 19 +++++++++++++++++-- src/Controllers/ViewProxies/TagViewProxy.php | 9 +++++++++ src/FormData/TagEditFormData.php | 12 ++++++++++++ src/Privilege.php | 2 ++ src/Services/TagService.php | 18 +++++++++++++++++- 8 files changed, 61 insertions(+), 8 deletions(-) diff --git a/TODO b/TODO index e37e1f7d..88a66191 100644 --- a/TODO +++ b/TODO @@ -33,11 +33,6 @@ everything related to tags: - privileges - template - ajax - - handle tag relations editing in backend - - tagservice::update - - tageditformdata - - tagcontroller - - privileges - handle relations in autocomplete refactors: diff --git a/data/config.ini b/data/config.ini index 489853c6..7cf9426b 100644 --- a/data/config.ini +++ b/data/config.ini @@ -60,6 +60,8 @@ changePostFlags = regularUser, powerUser, moderator, administrator listTags = regularUser, powerUser, moderator, administrator massTag = powerUser, moderator, administrator changeTagName = moderator, administrator +changeTagImplications = moderator, administrator +changeTagSuggestions = moderator, administrator banTags = moderator, administrator listComments = regularUser, powerUser, moderator, administrator diff --git a/public_html/js/Auth.js b/public_html/js/Auth.js index d095f094..9d6e46c4 100644 --- a/public_html/js/Auth.js +++ b/public_html/js/Auth.js @@ -45,6 +45,8 @@ App.Auth = function(_, jQuery, util, api, appState, promise) { listTags: 'listTags', massTag: 'massTag', changeTagName: 'changeTagName', + changeTagImplications: 'changeTagImplications', + changeTagSuggestions: 'changeTagSuggestions', banTags: 'banTags', viewHistory: 'viewHistory', diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index a4531477..624d7a9e 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -44,7 +44,7 @@ final class TagController extends AbstractController $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); $tag = $this->tagService->getByName($tagName); - return $this->tagViewProxy->fromEntity($tag); + return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); } public function getTags() @@ -84,7 +84,22 @@ final class TagController extends AbstractController if ($formData->banned !== null) $this->privilegeService->assertPrivilege(Privilege::BAN_TAGS); + if ($formData->implications !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_IMPLICATIONS); + + if ($formData->suggestions !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_SUGGESTIONS); + $tag = $this->tagService->updateTag($tag, $formData); - return $this->tagViewProxy->fromEntity($tag); + return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); + } + + private function getFullFetchConfig() + { + return + [ + TagViewProxy::FETCH_IMPLICATIONS => true, + TagViewProxy::FETCH_SUGGESTIONS => true, + ]; } } diff --git a/src/Controllers/ViewProxies/TagViewProxy.php b/src/Controllers/ViewProxies/TagViewProxy.php index c0d38619..8d531d9e 100644 --- a/src/Controllers/ViewProxies/TagViewProxy.php +++ b/src/Controllers/ViewProxies/TagViewProxy.php @@ -3,6 +3,9 @@ namespace Szurubooru\Controllers\ViewProxies; class TagViewProxy extends AbstractViewProxy { + const FETCH_IMPLICATIONS = 'fetchImplications'; + const FETCH_SUGGESTIONS = 'fetchSuggestions'; + public function fromEntity($tag, $config = []) { $result = new \StdClass; @@ -11,6 +14,12 @@ class TagViewProxy extends AbstractViewProxy $result->name = $tag->getName(); $result->usages = $tag->getUsages(); $result->banned = $tag->isBanned(); + + if (!empty($config[self::FETCH_IMPLICATIONS])) + $result->implications = $this->fromArray($tag->getImpliedTags()); + + if (!empty($config[self::FETCH_SUGGESTIONS])) + $result->suggestions = $this->fromArray($tag->getSuggestedTags()); } return $result; } diff --git a/src/FormData/TagEditFormData.php b/src/FormData/TagEditFormData.php index 51eb7ba2..f55f931c 100644 --- a/src/FormData/TagEditFormData.php +++ b/src/FormData/TagEditFormData.php @@ -7,14 +7,20 @@ class TagEditFormData implements IValidatable { public $name; public $banned; + public $implications; + public $suggestions; public function __construct($inputReader = null) { if ($inputReader !== null) { $this->name = $inputReader->name; + if ($inputReader->banned !== null) $this->banned = boolval($inputReader->banned); + + $this->implications = array_filter(array_unique(preg_split('/[\s+]/', $inputReader->implications))); + $this->suggestions = array_filter(array_unique(preg_split('/[\s+]/', $inputReader->suggestions))); } } @@ -22,6 +28,12 @@ class TagEditFormData implements IValidatable { if ($this->name !== null) $validator->validatePostTags([$this->name]); + + if (!empty($this->implications)) + $validator->validatePostTags($this->implications); + + if (!empty($this->suggestions)) + $validator->validatePostTags($this->suggestions); } } diff --git a/src/Privilege.php b/src/Privilege.php index 3369de56..77d96a6d 100644 --- a/src/Privilege.php +++ b/src/Privilege.php @@ -38,6 +38,8 @@ class Privilege const LIST_TAGS = 'listTags'; const MASS_TAG = 'massTag'; const CHANGE_TAG_NAME = 'changeTagName'; + const CHANGE_TAG_IMPLICATIONS = 'changeTagImplications'; + const CHANGE_TAG_SUGGESTIONS = 'changeTagSuggestions'; const BAN_TAGS = 'banTags'; const LIST_COMMENTS = 'listComments'; diff --git a/src/Services/TagService.php b/src/Services/TagService.php index 1f4e046b..c7967878 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -90,7 +90,7 @@ class TagService { $tagNameGetter = function($tag) { - return strtolower($tag->getName()); + return strtolower(is_string($tag) ? $tag : $tag->getName()); }; $tagsNotToCreate = []; @@ -105,6 +105,12 @@ class TagService if (isset($tagsNotToCreate[$tagNameGetter($tag)])) continue; + if (is_string($tag)) + { + $tagName = $tag; + $tag = new Tag(); + $tag->setName($tagName); + } $tag->setCreationTime($this->timeService->getCurrentTime()); $tagsToCreate[$tagNameGetter($tag)] = $tag; } @@ -169,4 +175,14 @@ class TagService { $tag->setName($newName); } + + private function updateImplications(Tag $tag, array $relatedNames) + { + $tag->setImpliedTags($this->createTags($relatedNames)); + } + + private function updateSuggestions(Tag $tag, array $relatedNames) + { + $tag->setSuggestedTags($this->createTags($relatedNames)); + } }