forked from askvortsov1/flarum-categories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend.php
88 lines (76 loc) · 4.24 KB
/
extend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*
* This file is part of askvortsov/flarum-categories
*
* Copyright (c) 2021 Alexander Skvortsov.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Askvortsov\FlarumCategories;
use Askvortsov\FlarumCategories\Content\Categories;
use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Extend;
use Flarum\Post\Event\Hidden;
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Restored;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tags\Api\Controller\ListTagsController;
use Flarum\Tags\Api\Serializer\TagSerializer;
return [
(new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js')
->css(__DIR__.'/resources/less/forum.less')
->route('/categories', 'categories', Categories::class),
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js')
->css(__DIR__.'/resources/less/admin.less'),
(new Extend\Settings())
->serializeToForum('categories.keepTagsNav', 'askvortsov-categories.keep-tags-nav', 'boolval')
->serializeToForum('categories.fullPageDesktop', 'askvortsov-categories.full-page-desktop', 'boolval')
->serializeToForum('categories.compactMobile', 'askvortsov-categories.compact-mobile', 'boolval')
->serializeToForum('categories.parentRemoveIcon', 'askvortsov-categories.parent-remove-icon', 'boolval')
->serializeToForum('categories.parentRemoveDescription', 'askvortsov-categories.parent-remove-description', 'boolval')
->serializeToForum('categories.parentRemoveStats', 'askvortsov-categories.parent-remove-stats', 'boolval')
->serializeToForum('categories.parentRemoveLastDiscussion', 'askvortsov-categories.parent-remove-last-discussion', 'boolval')
->serializeToForum('categories.childBareIcon', 'askvortsov-categories.child-bare-icon', 'boolval', true)
->serializeToForum('categories.widgetHeader', 'askvortsov-categories.widget-header', 'boolval', false)
->serializeToForum('categories.widgetRight', 'askvortsov-categories.widget-right', 'boolval', false)
->serializeToForum('categories.widgetLeft', 'askvortsov-categories.widget-left', 'boolval', false)
->serializeToForum('categories.widgetFooter', 'askvortsov-categories.widget-footer', 'boolval', false)
->serializeToForum('categories.enablePrimaryTagColor', 'askvortsov-categories.enable-primary-tag-color', 'boolval', true)
->serializeToForum('categories.enablePrimaryChildTagColor', 'askvortsov-categories.enable-primary-child-tag-color', 'boolval', false),
(new Extend\ApiController(ListTagsController::class))
->addOptionalInclude('lastPostedDiscussion.lastPostedUser'),
(new Extend\ApiSerializer(TagSerializer::class))
->attributes(function ($serializer, $model, $attributes) {
$settings = resolve(SettingsRepositoryInterface::class);
if ($settings->get('askvortsov-categories.small-forum-optimized', false)) {
$result = $model->discussions()
->selectRaw('sum(comment_count) as postCount, count(id) as discussionCount')
->whereVisibleTo($serializer->getActor())
->get()[0];
$attributes['discussionCount'] = (int) $result['discussionCount'];
$attributes['postCount'] = (int) $result['postCount'];
} else {
// discussion count is loaded this way by default, no need to reiterate
$attributes['postCount'] = (int) $model->post_count;
}
return $attributes;
}),
(new Extend\ApiSerializer(BasicUserSerializer::class))
->attribute('joinTime', function ($serializer, $model) {
return $serializer->formatDate($model->joined_at);
}),
new Extend\Locales(__DIR__.'/resources/locale'),
(new Extend\Event())
->listen(Hidden::class, function (Hidden $event) {
Util::updateTagsPostCount($event->post, -1);
})
->listen(Posted::class, function (Posted $event) {
Util::updateTagsPostCount($event->post, 1);
})
->listen(Restored::class, function (Restored $event) {
Util::updateTagsPostCount($event->post, 1);
}),
];