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