diff --git a/extensions/tags/src/CategoriesHandler.php b/extensions/tags/src/CategoriesHandler.php deleted file mode 100755 index c21b49753..000000000 --- a/extensions/tags/src/CategoriesHandler.php +++ /dev/null @@ -1,66 +0,0 @@ -listen('Flarum\Forum\Events\RenderView', __CLASS__.'@renderForum'); - $events->listen('Flarum\Api\Events\SerializeRelationship', __CLASS__.'@serializeRelationship'); - $events->listen('Flarum\Core\Events\RegisterDiscussionGambits', __CLASS__.'@registerDiscussionGambits'); - $events->listen('Flarum\Core\Events\DiscussionWillBeSaved', __CLASS__.'@whenDiscussionWillBeSaved'); - } - - public function renderForum(RenderView $event) - { - $root = __DIR__.'/..'; - - $event->assets->addFile([ - $root.'/js/dist/extension.js', - $root.'/less/categories.less' - ]); - - $serializer = new CategorySerializer($event->action->actor); - $event->view->data = array_merge($event->view->data, $serializer->collection(Category::orderBy('position')->get())->toArray()); - } - - public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event) - { - if (isset($event->command->data['links']['category']['linkage'])) { - $linkage = $event->command->data['links']['category']['linkage']; - - $categoryId = (int) $linkage['id']; - $discussion = $event->discussion; - $user = $event->command->user; - - $oldCategoryId = (int) $discussion->category_id; - - if ($oldCategoryId === $categoryId) { - return; - } - - $discussion->category_id = $categoryId; - $discussion->raise(new DiscussionWasMoved($discussion, $user, $oldCategoryId)); - } - } - - public function registerDiscussionGambits(RegisterDiscussionGambits $event) - { - $event->gambits->add('Flarum\Categories\CategoryGambit'); - } - - public function serializeRelationship(SerializeRelationship $event) - { - if ($event->serializer instanceof DiscussionSerializer && $event->name === 'category') { - return $event->serializer->hasOne('Flarum\Categories\CategorySerializer', 'category'); - } - } -} diff --git a/extensions/tags/src/CategoriesServiceProvider.php b/extensions/tags/src/CategoriesServiceProvider.php index 6ce82ce4a..79804e807 100644 --- a/extensions/tags/src/CategoriesServiceProvider.php +++ b/extensions/tags/src/CategoriesServiceProvider.php @@ -1,12 +1,9 @@ subscribe('Flarum\Categories\CategoriesHandler'); - $events->subscribe('Flarum\Categories\DiscussionMovedNotifier'); + $events->subscribe('Flarum\Categories\Handlers\DiscussionMovedNotifier'); + $events->subscribe('Flarum\Categories\Handlers\CategoryPreloader'); + $events->subscribe('Flarum\Categories\Handlers\CategorySaver'); + + $this->forumAssets([ + __DIR__.'/../js/dist/extension.js', + __DIR__.'/../less/categories.less' + ]); + + $this->postType('Flarum\Categories\DiscussionMovedPost'); + + $this->discussionGambit('Flarum\Categories\CategoryGambit'); + + $this->notificationType('Flarum\Categories\DiscussionMovedNotification', ['alert' => true]); + + $this->relationship('Flarum\Core\Models\Discussion', 'belongsTo', 'category', 'Flarum\Categories\Category'); + + $this->serializeRelationship('Flarum\Api\Serializers\DiscussionSerializer', 'hasOne', 'category', 'Flarum\Categories\CategorySerializer'); - // Add the category relationship to the Discussion model, and include - // it in discussion-related API actions by default. - Discussion::addRelationship('category', function ($model) { - return $model->belongsTo('Flarum\Categories\Category', null, null, 'category'); - }); DiscussionsIndexAction::$include['category'] = true; DiscussionsShowAction::$include['category'] = true; - - // Add a new post and notification type to represent a discussion - // being moved from one category to another. - Post::addType('Flarum\Categories\DiscussionMovedPost'); - - $notifier->registerType('Flarum\Categories\DiscussionMovedNotification', ['alert' => true]); } + /** + * Register the service provider. + * + * @return void + */ public function register() { $this->app->bind( diff --git a/extensions/tags/src/Handlers/CategoryPreloader.php b/extensions/tags/src/Handlers/CategoryPreloader.php new file mode 100755 index 000000000..a6ac6e9f8 --- /dev/null +++ b/extensions/tags/src/Handlers/CategoryPreloader.php @@ -0,0 +1,19 @@ +listen('Flarum\Forum\Events\RenderView', __CLASS__.'@renderForum'); + } + + public function renderForum(RenderView $event) + { + $serializer = new CategorySerializer($event->action->actor); + $event->view->data = array_merge($event->view->data, $serializer->collection(Category::orderBy('position')->get())->toArray()); + } +} diff --git a/extensions/tags/src/Handlers/CategorySaver.php b/extensions/tags/src/Handlers/CategorySaver.php new file mode 100755 index 000000000..33870cdc1 --- /dev/null +++ b/extensions/tags/src/Handlers/CategorySaver.php @@ -0,0 +1,32 @@ +listen('Flarum\Core\Events\DiscussionWillBeSaved', __CLASS__.'@whenDiscussionWillBeSaved'); + } + + public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event) + { + if (isset($event->command->data['links']['category']['linkage'])) { + $linkage = $event->command->data['links']['category']['linkage']; + + $categoryId = (int) $linkage['id']; + $discussion = $event->discussion; + $user = $event->command->user; + + $oldCategoryId = (int) $discussion->category_id; + + if ($oldCategoryId === $categoryId) { + return; + } + + $discussion->category_id = $categoryId; + $discussion->raise(new DiscussionWasMoved($discussion, $user, $oldCategoryId)); + } + } +} diff --git a/extensions/tags/src/DiscussionMovedNotifier.php b/extensions/tags/src/Handlers/DiscussionMovedNotifier.php similarity index 91% rename from extensions/tags/src/DiscussionMovedNotifier.php rename to extensions/tags/src/Handlers/DiscussionMovedNotifier.php index 71adacb55..c50ba89fa 100755 --- a/extensions/tags/src/DiscussionMovedNotifier.php +++ b/extensions/tags/src/Handlers/DiscussionMovedNotifier.php @@ -1,5 +1,7 @@ -