[TASK] Integrate DocHeader action buttons
[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\DocumentTemplate;
18 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
19 use TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface;
20 use TYPO3\CMS\Backend\Template\Components\Buttons\PositionInterface;
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 array
50 */
51 protected $setVariables = array();
52
53 /**
54 * @var array
55 */
56 protected $getVariables = array();
57
58 /**
59 * @var ControllerContext
60 */
61 protected $controllerContext;
62
63 /**
64 * Gets the name of the module.
65 *
66 * @return string
67 */
68 public function getModuleName()
69 {
70 return $this->moduleName;
71 }
72
73 /**
74 * Sets the name of the module.
75 *
76 * @param string $moduleName
77 * @return ShortcutButton
78 */
79 public function setModuleName($moduleName)
80 {
81 $this->moduleName = $moduleName;
82 return $this;
83 }
84
85 /**
86 * Gets the SET variables.
87 *
88 * @return array
89 */
90 public function getSetVariables()
91 {
92 return $this->setVariables;
93 }
94
95 /**
96 * Sets the SET variables.
97 *
98 * @param array $setVariables
99 * @return ShortcutButton
100 */
101 public function setSetVariables(array $setVariables)
102 {
103 $this->setVariables = $setVariables;
104 return $this;
105 }
106
107 /**
108 * Gets the GET variables.
109 *
110 * @return array
111 */
112 public function getGetVariables()
113 {
114 return $this->getVariables;
115 }
116
117 /**
118 * Sets the GET variables.
119 *
120 * @param array $getVariables
121 * @return ShortcutButton
122 */
123 public function setGetVariables(array $getVariables)
124 {
125 $this->getVariables = $getVariables;
126 return $this;
127 }
128
129 /**
130 * Gets the button position.
131 *
132 * @return string
133 */
134 public function getPosition()
135 {
136 return ButtonBar::BUTTON_POSITION_RIGHT;
137 }
138
139 /**
140 * Gets the button group.
141 *
142 * @return int
143 */
144 public function getGroup()
145 {
146 return 91;
147 }
148
149 /**
150 * Gets the type of the button
151 *
152 * @return string
153 */
154 public function getType()
155 {
156 return get_class($this);
157 }
158
159 /**
160 * Determines whether the button shall be rendered.
161 * Depends on the backend user permission to create
162 * shortcuts and the defined module name.
163 *
164 * @return bool
165 */
166 public function isValid()
167 {
168 $this->preProcess();
169
170 return (
171 $this->getBackendUser()->mayMakeShortcut()
172 && !empty($this->moduleName)
173 );
174 }
175
176 /**
177 * Renders the button
178 *
179 * @return string
180 */
181 public function __toString()
182 {
183 return $this->render();
184 }
185
186 /**
187 * Renders the button
188 *
189 * @return string
190 */
191 public function render()
192 {
193 /** @var DocumentTemplate $documentTemplate */
194 $documentTemplate = GeneralUtility::makeInstance(DocumentTemplate::class);
195 $shortcutMarkup = $documentTemplate->makeShortcutIcon(
196 implode(',', $this->getVariables),
197 implode(',', $this->setVariables),
198 $this->moduleName
199 );
200
201 return '<span class="btn btn-sm btn-default">' . $shortcutMarkup . '</span>';
202 }
203
204 /**
205 * Pre-processes class member values.
206 */
207 protected function preProcess()
208 {
209 $emptyGetVariables = (count($this->getVariables) === 0);
210
211 // Set default GET parameters
212 if ($emptyGetVariables) {
213 $this->getVariables = array('id', 'M');
214 }
215
216 // Automatically determine module name in Extbase context
217 if ($this->controllerContext !== null) {
218 $currentRequest = $this->controllerContext->getRequest();
219 $extensionName = $currentRequest->getControllerExtensionName();
220 $this->moduleName = $currentRequest->getPluginName();
221 // Extend default GET parameters
222 if ($emptyGetVariables) {
223 $modulePrefix = strtolower('tx_' . $extensionName . '_' . $this->moduleName);
224 $this->getVariables[] = $modulePrefix;
225 }
226 }
227 }
228
229 /**
230 * @return BackendUserAuthentication
231 */
232 protected function getBackendUser()
233 {
234 return $GLOBALS['BE_USER'];
235 }
236 }