Issues (186)

Backend/ToolBarItems/NotificationsToolbarItem.php (3 issues)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Backend\ToolBarItems;
19
20
use CuyZ\Notiz\Backend\Module\ManagerModuleHandler;
21
use CuyZ\Notiz\Core\Definition\DefinitionService;
22
use CuyZ\Notiz\Service\Container;
23
use CuyZ\Notiz\Service\ExtensionConfigurationService;
24
use CuyZ\Notiz\Service\LocalizationService;
25
use CuyZ\Notiz\Service\ViewService;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
use Throwable;
29
use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
0 ignored issues
show
The type TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use TYPO3\CMS\Core\Imaging\IconFactory;
31
use TYPO3\CMS\Core\Page\PageRenderer;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Extbase\Object\ObjectManager;
0 ignored issues
show
The type TYPO3\CMS\Extbase\Object\ObjectManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
use TYPO3\CMS\Fluid\View\StandaloneView;
0 ignored issues
show
The type TYPO3\CMS\Fluid\View\StandaloneView was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
36
/**
37
 * Adds an item to the TYPO3 backend tool bar.
38
 */
39
class NotificationsToolbarItem implements ToolbarItemInterface
40
{
41
    /**
42
     * If true, all information will be added to the toolbar menu.
43
     *
44
     * The first run of the toolbar rendering (during the rendering of the TYPO3
45
     * backend) wont be full. This can improve performance if a lot of
46
     * notifications were to be listed.
47
     *
48
     * Periodic asynchronous requests will be dispatched when the TYPO3 backend
49
     * is rendered. These Ajax requests will contain all information, as they
50
     * are running as background tasks.
51
     *
52
     * @var bool
53
     */
54
    protected $fullMenu = false;
55
56
    /**
57
     * @var IconFactory
58
     */
59
    protected $iconFactory;
60
61
    /**
62
     * @var DefinitionService
63
     */
64
    protected $definitionService;
65
66
    /**
67
     * @var ExtensionConfigurationService
68
     */
69
    protected $extensionConfigurationService;
70
71
    /**
72
     * @var ViewService
73
     */
74
    protected $viewService;
75
76
    /**
77
     * @var ManagerModuleHandler
78
     */
79
    protected $managerModuleHandler;
80
81
    /**
82
     * Manual dependency injection.
83
     */
84
    public function __construct()
85
    {
86
        /** @var ObjectManager $objectManager */
87
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
88
89
        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
90
        $this->definitionService = $objectManager->get(DefinitionService::class);
91
        $this->extensionConfigurationService = $objectManager->get(ExtensionConfigurationService::class);
92
        $this->viewService = $objectManager->get(ViewService::class);
93
        $this->managerModuleHandler = $objectManager->get(ManagerModuleHandler::class);
94
95
        $this->initializeJavaScript();
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    public function checkAccess(): bool
102
    {
103
        return $this->managerModuleHandler->canBeAccessed();
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getItem(): string
110
    {
111
        return $this->getFluidTemplateObject('Backend/ToolBar/NotificationToolBarItem')->render();
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function hasDropDown(): bool
118
    {
119
        return true;
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getDropDown(): string
126
    {
127
        try {
128
            return $this->getDropDownFromDefinition();
129
        } catch (Throwable $exception) {
130
            return $this->getErrorDropDown($exception);
131
        }
132
    }
133
134
    /**
135
     * Action called periodically by Ajax (used to refresh the toolbar).
136
     *
137
     * @param ServerRequestInterface $request
138
     * @param ResponseInterface $response
139
     * @return ResponseInterface
140
     */
141
    public function renderMenuAction(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
142
    {
143
        $this->fullMenu = true;
144
145
        $response->getBody()->write($this->getDropDown());
146
147
        return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    protected function getDropDownFromDefinition(): string
154
    {
155
        $view = $this->getFluidTemplateObject('Backend/ToolBar/NotificationToolBarDropDown');
156
157
        if (!$this->definitionService->getValidationResult()->hasErrors()) {
158
            $definition = $this->definitionService->getDefinition();
159
160
            $view->assign('fullMenu', $this->fullMenu);
161
            $view->assign('definition', $definition);
162
163
            if ($this->fullMenu) {
164
                $notifications = [];
165
                $total = 0;
166
167
                foreach ($definition->getListableNotifications() as $notification) {
168
                    $number = $notification->getProcessor()->getTotalNumber();
169
                    $total += $number;
170
171
                    if ($number > 0) {
172
                        $notifications[] = $notification;
173
                    }
174
                }
175
176
                $view->assign('filledNotifications', $notifications);
177
                $view->assign('filledNotificationsTotal', $total);
178
            }
179
        }
180
181
        return $view->render();
182
    }
183
184
    /**
185
     * @param Throwable $exception
186
     * @return string
187
     */
188
    protected function getErrorDropDown(Throwable $exception): string
189
    {
190
        $view = $this->getFluidTemplateObject('Backend/ToolBar/NotificationToolBarDropDownError');
191
        $view->assign('exception', $exception);
192
        $view->assign('isAdmin', Container::getBackendUser()->isAdmin());
193
194
        return $view->render();
195
    }
196
197
    /**
198
     * @return array
199
     */
200
    public function getAdditionalAttributes(): array
201
    {
202
        return [];
203
    }
204
205
    /**
206
     * @return int
207
     */
208
    public function getIndex(): int
209
    {
210
        $index = $this->extensionConfigurationService->getConfigurationValue('toolbar.index');
211
212
        return (int)max(min($index, 100), 0);
213
    }
214
215
    /**
216
     * Requires the JavaScript script that will refresh the toolbar every now
217
     * and then.
218
     */
219
    protected function initializeJavaScript()
220
    {
221
        $pageRenderer = $this->getPageRenderer();
222
223
        $pageRenderer->loadRequireJsModule('TYPO3/CMS/Notiz/Toolbar');
224
        $pageRenderer->addInlineLanguageLabelArray([
225
            'notiz.toolbar.error.body' => LocalizationService::localize('Backend/Toolbar:exception'),
226
            'notiz.toolbar.error.refresh_label' => LocalizationService::localize('Backend/Toolbar:refresh'),
227
        ]);
228
    }
229
230
    /**
231
     * @param string $templateName
232
     * @return StandaloneView
233
     */
234
    protected function getFluidTemplateObject(string $templateName): StandaloneView
235
    {
236
        $view = $this->viewService->getStandaloneView($templateName);
237
238
        $view->assign('result', $this->definitionService->getValidationResult());
239
240
        return $view;
241
    }
242
243
    /**
244
     * @return PageRenderer
245
     */
246
    protected function getPageRenderer(): PageRenderer
247
    {
248
        /** @noinspection PhpIncompatibleReturnTypeInspection */
249
        return GeneralUtility::makeInstance(PageRenderer::class);
250
    }
251
}
252