e340cb96626f1710001bd6e8d978fca232ad9ae8
[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\Imaging\Icon;
24 use TYPO3\CMS\Core\Imaging\IconFactory;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\SysAction\ActionTask;
27
28 /**
29 * Adds action links to the backend's toolbar
30 */
31 class ActionToolbarItem implements ToolbarItemInterface
32 {
33 /**
34 * @var array List of action entries
35 */
36 protected $actionEntries = [];
37
38 /**
39 * @var IconFactory
40 */
41 protected $iconFactory;
42
43 /**
44 * Constructor
45 */
46 public function __construct()
47 {
48 $this->getLanguageService()->includeLLFile('EXT:sys_action/Resources/Private/Language/locallang.xlf');
49 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
50 $this->initializeActionEntries();
51 }
52
53 /**
54 * Render toolbar icon
55 *
56 * @return string HTML
57 */
58 public function getItem()
59 {
60 $title = htmlspecialchars($this->getLanguageService()->getLL('action_toolbaritem'));
61 $icon = $this->iconFactory->getIcon('apps-toolbar-menu-actions', Icon::SIZE_SMALL)->render('inline');
62 return '
63 <span class="toolbar-item-icon" title="' . $title . '">' . $icon . '</span>
64 <span class="toolbar-item-title">' . $title . '</span>
65 ';
66 }
67
68 /**
69 * Render drop down
70 *
71 * @return string HTML
72 */
73 public function getDropDown()
74 {
75 $actionMenu = [];
76 $actionMenu[] = '<h3 class="dropdown-headline">' . htmlspecialchars($this->getLanguageService()->getLL('sys_action')) . '</h3>';
77 $actionMenu[] = '<hr>';
78 $actionMenu[] = '<div class="dropdown-table">';
79 foreach ($this->actionEntries as $linkConf) {
80 $actionMenu[] = '<div class="dropdown-table-row">';
81 $actionMenu[] = '<div class="dropdown-table-column dropdown-table-icon">';
82 $actionMenu[] = $linkConf[2];
83 $actionMenu[] = '</div>';
84 $actionMenu[] = '<div class="dropdown-table-column dropdown-table-title">';
85 $actionMenu[] = '<a class="t3js-topbar-link" href="' . htmlspecialchars($linkConf[1]) . '" target="list_frame">';
86 $actionMenu[] = htmlspecialchars($linkConf[0]);
87 $actionMenu[] = '</a>';
88 $actionMenu[] = '</div>';
89 $actionMenu[] = '</div>';
90 }
91 $actionMenu[] = '</div>';
92 return implode(LF, $actionMenu);
93 }
94
95 /**
96 * Gets the entries for the action menu
97 */
98 protected function initializeActionEntries()
99 {
100 $backendUser = $this->getBackendUser();
101
102 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_action');
103 $queryBuilder->getRestrictions()
104 ->removeAll()
105 ->add(GeneralUtility::makeInstance(HiddenRestriction::class))
106 ->add(GeneralUtility::makeInstance(RootLevelRestriction::class, [
107 'sys_action'
108 ]));
109
110 $queryBuilder
111 ->select('sys_action.*')
112 ->from('sys_action');
113
114 if (!empty($GLOBALS['TCA']['sys_action']['ctrl']['sortby'])) {
115 $queryBuilder->orderBy('sys_action.' . $GLOBALS['TCA']['sys_action']['ctrl']['sortby']);
116 }
117
118 $actions = [];
119 if (!$backendUser->isAdmin()) {
120 $groupList = $backendUser->groupList ?: '0';
121
122 $queryBuilder
123 ->join(
124 'sys_action',
125 'sys_action_asgr_mm',
126 'sys_action_asgr_mm',
127 $queryBuilder->expr()->eq(
128 'sys_action_asgr_mm.uid_local',
129 $queryBuilder->quoteIdentifier('sys_action.uid')
130 )
131 )
132 ->join(
133 'sys_action_asgr_mm',
134 'be_groups',
135 'be_groups',
136 $queryBuilder->expr()->eq(
137 'sys_action_asgr_mm.uid_foreign',
138 $queryBuilder->quoteIdentifier('be_groups.uid')
139 )
140 )
141 ->where(
142 $queryBuilder->expr()->in(
143 'be_groups.uid',
144 $queryBuilder->createNamedParameter(
145 GeneralUtility::intExplode(',', $groupList, true),
146 Connection::PARAM_INT_ARRAY
147 )
148 )
149 )
150 ->groupBy('sys_action.uid');
151 }
152
153 $result = $queryBuilder->execute();
154 while ($actionRow = $result->fetch()) {
155 $actions[] = [
156 $actionRow['title'],
157 sprintf(
158 '%s&SET[mode]=tasks&SET[function]=sys_action.%s&show=%u',
159 BackendUtility::getModuleUrl('user_task'),
160 ActionTask::class, // @todo: class name string is hand over as url parameter?!
161 $actionRow['uid']
162 ),
163 $this->iconFactory->getIconForRecord('sys_action', $actionRow, Icon::SIZE_SMALL)->render()
164 ];
165 }
166 $this->actionEntries = $actions;
167 }
168
169 /**
170 * This toolbar needs no additional attributes
171 *
172 * @return array
173 */
174 public function getAdditionalAttributes()
175 {
176 return [];
177 }
178
179 /**
180 * This item has a drop down
181 *
182 * @return bool
183 */
184 public function hasDropDown()
185 {
186 return true;
187 }
188
189 /**
190 * This toolbar is rendered if there are action entries, no further user restriction
191 *
192 * @return bool TRUE
193 */
194 public function checkAccess()
195 {
196 $result = false;
197 if (!empty($this->actionEntries)) {
198 $result = true;
199 }
200 return $result;
201 }
202
203 /**
204 * Position relative to others
205 *
206 * @return int
207 */
208 public function getIndex()
209 {
210 return 35;
211 }
212
213 /**
214 * Returns the current BE user.
215 *
216 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
217 */
218 protected function getBackendUser()
219 {
220 return $GLOBALS['BE_USER'];
221 }
222
223 /**
224 * Returns LanguageService
225 *
226 * @return \TYPO3\CMS\Lang\LanguageService
227 */
228 protected function getLanguageService()
229 {
230 return $GLOBALS['LANG'];
231 }
232 }