[TASK] Use BE Routing / PSR-7 instead of BackendUtility::getModuleUrl
[Packages/TYPO3.CMS.git] / typo3 / sysext / sys_action / Classes / Backend / ToolbarItems / ActionToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\SysAction\Backend\ToolbarItems;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
18 use TYPO3\CMS\Core\Database\Connection;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
21 use TYPO3\CMS\Core\Database\Query\Restriction\RootLevelRestriction;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Fluid\View\StandaloneView;
24 use TYPO3\CMS\SysAction\ActionTask;
25
26 /**
27 * Adds action links to the backend's toolbar
28 */
29 class ActionToolbarItem implements ToolbarItemInterface
30 {
31 /**
32 * @var array
33 */
34 protected $availableActions = [];
35
36 /**
37 * Render toolbar icon via Fluid
38 *
39 * @return string HTML
40 */
41 public function getItem()
42 {
43 return $this->getFluidTemplateObject('ToolbarItem.html')->render();
44 }
45
46 /**
47 * Render drop down
48 *
49 * @return string HTML
50 */
51 public function getDropDown()
52 {
53 $view = $this->getFluidTemplateObject('DropDown.html');
54 $view->assign('actions', $this->availableActions);
55 return $view->render();
56 }
57
58 /**
59 * Stores the entries for the action menu in $this->availableActions
60 */
61 protected function setAvailableActions()
62 {
63 $actionEntries = [];
64 $backendUser = $this->getBackendUser();
65
66 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_action');
67 $queryBuilder->getRestrictions()
68 ->removeAll()
69 ->add(GeneralUtility::makeInstance(HiddenRestriction::class))
70 ->add(GeneralUtility::makeInstance(RootLevelRestriction::class, [
71 'sys_action'
72 ]));
73
74 $queryBuilder
75 ->select('sys_action.*')
76 ->from('sys_action');
77
78 if (!empty($GLOBALS['TCA']['sys_action']['ctrl']['sortby'])) {
79 $queryBuilder->orderBy('sys_action.' . $GLOBALS['TCA']['sys_action']['ctrl']['sortby']);
80 }
81
82 if (!$backendUser->isAdmin()) {
83 $groupList = $backendUser->groupList ?: '0';
84
85 $queryBuilder
86 ->join(
87 'sys_action',
88 'sys_action_asgr_mm',
89 'sys_action_asgr_mm',
90 $queryBuilder->expr()->eq(
91 'sys_action_asgr_mm.uid_local',
92 $queryBuilder->quoteIdentifier('sys_action.uid')
93 )
94 )
95 ->join(
96 'sys_action_asgr_mm',
97 'be_groups',
98 'be_groups',
99 $queryBuilder->expr()->eq(
100 'sys_action_asgr_mm.uid_foreign',
101 $queryBuilder->quoteIdentifier('be_groups.uid')
102 )
103 )
104 ->where(
105 $queryBuilder->expr()->in(
106 'be_groups.uid',
107 $queryBuilder->createNamedParameter(
108 GeneralUtility::intExplode(',', $groupList, true),
109 Connection::PARAM_INT_ARRAY
110 )
111 )
112 )
113 ->groupBy('sys_action.uid');
114 }
115
116 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
117 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
118 $result = $queryBuilder->execute();
119 while ($actionRow = $result->fetch()) {
120 $actionRow['link'] = sprintf(
121 '%s&SET[mode]=tasks&SET[function]=sys_action.%s&show=%u',
122 (string)$uriBuilder->buildUriFromRoute('user_task'),
123 ActionTask::class, // @todo: class name string is hand over as url parameter?!
124 $actionRow['uid']
125 );
126 $actionEntries[] = $actionRow;
127 }
128
129 $this->availableActions = $actionEntries;
130 }
131
132 /**
133 * This toolbar needs no additional attributes
134 *
135 * @return array
136 */
137 public function getAdditionalAttributes()
138 {
139 return [];
140 }
141
142 /**
143 * This item has a drop down
144 *
145 * @return bool
146 */
147 public function hasDropDown()
148 {
149 return true;
150 }
151
152 /**
153 * This toolbar is rendered if there are action entries, no further user restriction
154 *
155 * @return bool
156 */
157 public function checkAccess()
158 {
159 $this->setAvailableActions();
160 return !empty($this->availableActions);
161 }
162
163 /**
164 * Position relative to others
165 *
166 * @return int
167 */
168 public function getIndex()
169 {
170 return 35;
171 }
172
173 /**
174 * Returns the current BE user.
175 *
176 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
177 */
178 protected function getBackendUser()
179 {
180 return $GLOBALS['BE_USER'];
181 }
182
183 /**
184 * Returns a new standalone view, shorthand function
185 *
186 * @param string $filename Which templateFile should be used.
187 * @return StandaloneView
188 */
189 protected function getFluidTemplateObject(string $filename): StandaloneView
190 {
191 $view = GeneralUtility::makeInstance(StandaloneView::class);
192 $view->setLayoutRootPaths(['EXT:sys_action/Resources/Private/Layouts']);
193 $view->setPartialRootPaths([
194 'EXT:backend/Resources/Private/Partials/ToolbarItems',
195 'EXT:sys_action/Resources/Private/Partials'
196 ]);
197 $view->setTemplateRootPaths(['EXT:sys_action/Resources/Private/Templates/ToolbarItems']);
198 $view->setTemplate($filename);
199
200 $view->getRequest()->setControllerExtensionName('SysAction');
201 return $view;
202 }
203 }