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