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