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