[TASK] Add more fixers for php-cs-fixer
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Template / Components / Buttons / Action / ShortcutButton.php
1 <?php
2 namespace TYPO3\CMS\Backend\Template\Components\Buttons\Action;
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\Template\Components\ButtonBar;
18 use TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface;
19 use TYPO3\CMS\Backend\Template\Components\Buttons\PositionInterface;
20 use TYPO3\CMS\Backend\Template\ModuleTemplate;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
24
25 /**
26 * ShortcutButton
27 *
28 * Renders a shortcut button in the DocHeader which will be rendered
29 * to the right position using button group "91".
30 *
31 * EXAMPLE USAGE TO ADD A SHORTCUT BUTTON:
32 *
33 * $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
34 * $myButton = $buttonBar->makeShortcutButton()
35 * ->setModuleName('my_info');
36 * $buttonBar->addButton($myButton);
37 */
38 class ShortcutButton implements ButtonInterface, PositionInterface
39 {
40 /**
41 * @var string
42 */
43 protected $moduleName;
44
45 /**
46 * @var string
47 */
48 protected $displayName;
49
50 /**
51 * @var array
52 */
53 protected $setVariables = [];
54
55 /**
56 * @var array
57 */
58 protected $getVariables = [];
59
60 /**
61 * @var ControllerContext
62 */
63 protected $controllerContext;
64
65 /**
66 * Gets the name of the module.
67 *
68 * @return string
69 */
70 public function getModuleName()
71 {
72 return $this->moduleName;
73 }
74
75 /**
76 * Sets the name of the module.
77 *
78 * @param string $moduleName
79 * @return ShortcutButton
80 */
81 public function setModuleName($moduleName)
82 {
83 $this->moduleName = $moduleName;
84 return $this;
85 }
86
87 /**
88 * Gets the display name of the module.
89 *
90 * @return string
91 */
92 public function getDisplayName()
93 {
94 return $this->displayName;
95 }
96
97 /**
98 * Sets the display name of the module.
99 *
100 * @param string $displayName
101 * @return ShortcutButton
102 */
103 public function setDisplayName($displayName)
104 {
105 $this->displayName = $displayName;
106 return $this;
107 }
108
109 /**
110 * Gets the SET variables.
111 *
112 * @return array
113 */
114 public function getSetVariables()
115 {
116 return $this->setVariables;
117 }
118
119 /**
120 * Sets the SET variables.
121 *
122 * @param array $setVariables
123 * @return ShortcutButton
124 */
125 public function setSetVariables(array $setVariables)
126 {
127 $this->setVariables = $setVariables;
128 return $this;
129 }
130
131 /**
132 * Gets the GET variables.
133 *
134 * @return array
135 */
136 public function getGetVariables()
137 {
138 return $this->getVariables;
139 }
140
141 /**
142 * Sets the GET variables.
143 *
144 * @param array $getVariables
145 * @return ShortcutButton
146 */
147 public function setGetVariables(array $getVariables)
148 {
149 $this->getVariables = $getVariables;
150 return $this;
151 }
152
153 /**
154 * Gets the button position.
155 *
156 * @return string
157 */
158 public function getPosition()
159 {
160 return ButtonBar::BUTTON_POSITION_RIGHT;
161 }
162
163 /**
164 * Gets the button group.
165 *
166 * @return int
167 */
168 public function getGroup()
169 {
170 return 91;
171 }
172
173 /**
174 * Gets the type of the button
175 *
176 * @return string
177 */
178 public function getType()
179 {
180 return get_class($this);
181 }
182
183 /**
184 * Determines whether the button shall be rendered.
185 * Depends on the backend user permission to create
186 * shortcuts and the defined module name.
187 *
188 * @return bool
189 */
190 public function isValid()
191 {
192 $this->preProcess();
193
194 return
195 !empty($this->moduleName)
196 ;
197 }
198
199 /**
200 * Renders the button
201 *
202 * @return string
203 */
204 public function __toString()
205 {
206 return $this->render();
207 }
208
209 /**
210 * Renders the button
211 *
212 * @return string
213 */
214 public function render()
215 {
216 if ($this->getBackendUser()->mayMakeShortcut()) {
217 /** @var ModuleTemplate $moduleTemplate */
218 $moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
219 $shortcutMarkup = $moduleTemplate->makeShortcutIcon(
220 implode(',', $this->getVariables),
221 implode(',', $this->setVariables),
222 $this->moduleName,
223 '',
224 $this->displayName
225 );
226 } else {
227 $shortcutMarkup = '';
228 }
229
230 return $shortcutMarkup;
231 }
232
233 /**
234 * Pre-processes class member values.
235 */
236 protected function preProcess()
237 {
238 $emptyGetVariables = (count($this->getVariables) === 0);
239
240 // Set default GET parameters
241 if ($emptyGetVariables) {
242 $this->getVariables = ['id', 'M'];
243 }
244
245 // Automatically determine module name in Extbase context
246 if ($this->controllerContext !== null) {
247 $currentRequest = $this->controllerContext->getRequest();
248 $extensionName = $currentRequest->getControllerExtensionName();
249 $this->moduleName = $currentRequest->getPluginName();
250 // Extend default GET parameters
251 if ($emptyGetVariables) {
252 $modulePrefix = strtolower('tx_' . $extensionName . '_' . $this->moduleName);
253 $this->getVariables[] = $modulePrefix;
254 }
255 }
256 }
257
258 /**
259 * @return BackendUserAuthentication
260 */
261 protected function getBackendUser()
262 {
263 return $GLOBALS['BE_USER'];
264 }
265 }