Completed
Push — master ( 818496...5be7b8 )
by
unknown
16:18
created

ShortcutButton   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 255
rs 10
c 0
b 0
f 0
wmc 22

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getPosition() 0 3 1
A getLanguageService() 0 3 1
A getGroup() 0 3 1
A getType() 0 3 1
A getGetVariables() 0 4 1
A __toString() 0 3 1
A setSetVariables() 0 4 1
A setGetVariables() 0 4 1
A getDisplayName() 0 3 1
A setModuleName() 0 4 1
A render() 0 23 4
A getBackendUser() 0 3 1
A setDisplayName() 0 4 1
A createShortcutMarkup() 0 32 2
A isValid() 0 3 1
A getModuleName() 0 3 1
A setArguments() 0 4 1
A getSetVariables() 0 4 1
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
16
namespace TYPO3\CMS\Backend\Template\Components\Buttons\Action;
17
18
use TYPO3\CMS\Backend\Backend\Shortcut\ShortcutRepository;
19
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
20
use TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface;
21
use TYPO3\CMS\Backend\Template\Components\Buttons\PositionInterface;
22
use TYPO3\CMS\Backend\Template\ModuleTemplate;
23
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
24
use TYPO3\CMS\Core\Imaging\Icon;
25
use TYPO3\CMS\Core\Imaging\IconFactory;
26
use TYPO3\CMS\Core\Localization\LanguageService;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Core\Utility\HttpUtility;
29
30
/**
31
 * ShortcutButton
32
 *
33
 * Renders a shortcut button in the DocHeader which will be rendered
34
 * to the right position using button group "91".
35
 *
36
 * EXAMPLE USAGE TO ADD A SHORTCUT BUTTON:
37
 *
38
 * $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
39
 * $myButton = $buttonBar->makeShortcutButton()
40
 *       ->setArguments([
41
 *          'route' => $request->getQueryParams()['route']
42
 *       ])
43
 *       ->setModuleName('my_info');
44
 * $buttonBar->addButton($myButton);
45
 */
46
class ShortcutButton implements ButtonInterface, PositionInterface
47
{
48
    /**
49
     * @var string
50
     */
51
    protected $moduleName = '';
52
53
    /**
54
     * @var string
55
     */
56
    protected $displayName = '';
57
58
    /**
59
     * @var array List of parameter/value pairs relevant for this shortcut
60
     */
61
    protected $arguments = [];
62
63
    /**
64
     * @var array
65
     * @deprecated since v11, will be removed in v12
66
     */
67
    protected $setVariables = [];
68
69
    /**
70
     * @var array
71
     * @deprecated since v11, will be removed in v12
72
     */
73
    protected $getVariables = [];
74
75
    /**
76
     * Gets the name of the module.
77
     *
78
     * @return string
79
     */
80
    public function getModuleName()
81
    {
82
        return $this->moduleName;
83
    }
84
85
    /**
86
     * Sets the name of the module.
87
     *
88
     * @param string $moduleName
89
     * @return ShortcutButton
90
     */
91
    public function setModuleName($moduleName)
92
    {
93
        $this->moduleName = $moduleName;
94
        return $this;
95
    }
96
97
    /**
98
     * Gets the display name of the module.
99
     *
100
     * @return string
101
     */
102
    public function getDisplayName()
103
    {
104
        return $this->displayName;
105
    }
106
107
    /**
108
     * Sets the display name of the module.
109
     *
110
     * @param string $displayName
111
     * @return ShortcutButton
112
     */
113
    public function setDisplayName($displayName)
114
    {
115
        $this->displayName = $displayName;
116
        return $this;
117
    }
118
119
    /**
120
     * @param array $arguments
121
     * @return $this
122
     */
123
    public function setArguments(array $arguments): self
124
    {
125
        $this->arguments = $arguments;
126
        return $this;
127
    }
128
129
    /**
130
     * Gets the SET variables.
131
     *
132
     * @return array
133
     * @deprecated since v11, will be removed in v12
134
     */
135
    public function getSetVariables()
136
    {
137
        trigger_error('Method getSetVariables() is deprecated and will be removed in v12. Please use ShortcutButton->setArguments() instead.', E_USER_DEPRECATED);
138
        return $this->setVariables;
139
    }
140
141
    /**
142
     * Sets the SET variables.
143
     *
144
     * @param array $setVariables
145
     * @return ShortcutButton
146
     * @deprecated since v11, will be removed in v12. Deprecation logged by ModuleTemplate->makeShortcutIcon()
147
     */
148
    public function setSetVariables(array $setVariables)
149
    {
150
        $this->setVariables = $setVariables;
151
        return $this;
152
    }
153
154
    /**
155
     * Gets the GET variables.
156
     *
157
     * @return array
158
     * @deprecated since v11, will be removed in v12
159
     */
160
    public function getGetVariables()
161
    {
162
        trigger_error('Method getGetVariables() is deprecated and will be removed in v12. Please use ShortcutButton->setArguments() instead.', E_USER_DEPRECATED);
163
        return $this->getVariables;
164
    }
165
166
    /**
167
     * Sets the GET variables.
168
     *
169
     * @param array $getVariables
170
     * @return ShortcutButton
171
     * @deprecated since v11, will be removed in v12. Deprecation logged by ModuleTemplate->makeShortcutIcon()
172
     */
173
    public function setGetVariables(array $getVariables)
174
    {
175
        $this->getVariables = $getVariables;
176
        return $this;
177
    }
178
179
    /**
180
     * Gets the button position.
181
     *
182
     * @return string
183
     */
184
    public function getPosition()
185
    {
186
        return ButtonBar::BUTTON_POSITION_RIGHT;
187
    }
188
189
    /**
190
     * Gets the button group.
191
     *
192
     * @return int
193
     */
194
    public function getGroup()
195
    {
196
        return 91;
197
    }
198
199
    /**
200
     * Gets the type of the button
201
     *
202
     * @return string
203
     */
204
    public function getType()
205
    {
206
        return static::class;
207
    }
208
209
    /**
210
     * Determines whether the button shall be rendered.
211
     *
212
     * @return bool
213
     */
214
    public function isValid()
215
    {
216
        return !empty($this->moduleName);
217
    }
218
219
    /**
220
     * Renders the button
221
     *
222
     * @return string
223
     */
224
    public function __toString()
225
    {
226
        return $this->render();
227
    }
228
229
    /**
230
     * Renders the button
231
     *
232
     * @return string
233
     */
234
    public function render()
235
    {
236
        if ($this->getBackendUser()->mayMakeShortcut()) {
237
            if (!empty($this->arguments)) {
238
                $shortcutMarkup = $this->createShortcutMarkup();
239
            } else {
240
                // @deprecated since v11, the else branch will be removed in v12. Deprecation thrown by makeShortcutIcon() below
241
                if (empty($this->getVariables)) {
242
                    $this->getVariables = ['id', 'route'];
243
                }
244
                $moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
245
                $shortcutMarkup = $moduleTemplate->makeShortcutIcon(
246
                    implode(',', $this->getVariables),
247
                    implode(',', $this->setVariables),
248
                    $this->moduleName,
249
                    '',
250
                    $this->displayName
251
                );
252
            }
253
        } else {
254
            $shortcutMarkup = '';
255
        }
256
        return $shortcutMarkup;
257
    }
258
259
    protected function createShortcutMarkup(): string
260
    {
261
        $moduleName = $this->moduleName;
262
        $storeUrl = HttpUtility::buildQueryString($this->arguments, '&');
263
264
        // Find out if this shortcut exists already. Note this is a hack based on the fact
265
        // that sys_be_shortcuts stores the entire request string and not just needed params as array.
266
        $pathInfo = parse_url(GeneralUtility::getIndpEnv('REQUEST_URI'));
267
        $shortcutUrl = $pathInfo['path'] . '?' . $storeUrl;
268
        $shortcutRepository = GeneralUtility::makeInstance(ShortcutRepository::class);
269
        $shortcutExist = $shortcutRepository->shortcutExists($shortcutUrl);
270
271
        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
272
        if ($shortcutExist) {
273
            $shortcutMarkup = '<a class="active btn btn-default btn-sm" title="">'
274
                . $iconFactory->getIcon('actions-system-shortcut-active', Icon::SIZE_SMALL)->render()
275
                . '</a>';
276
        } else {
277
            $languageService = $this->getLanguageService();
278
            $confirmationText = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.makeBookmark');
279
            $onClick = 'top.TYPO3.ShortcutMenu.createShortcut('
280
                . GeneralUtility::quoteJSvalue(rawurlencode($moduleName))
281
                . ', ' . GeneralUtility::quoteJSvalue(rawurlencode($shortcutUrl))
282
                . ', ' . GeneralUtility::quoteJSvalue($confirmationText)
283
                . ', \'\''
284
                . ', this'
285
                . ', ' . GeneralUtility::quoteJSvalue($this->displayName) . ');return false;';
286
            $shortcutMarkup = '<a href="#" class="btn btn-default btn-sm" onclick="' . htmlspecialchars($onClick) . '" title="'
287
                . htmlspecialchars($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.makeBookmark')) . '">'
288
                . $iconFactory->getIcon('actions-system-shortcut-new', Icon::SIZE_SMALL)->render() . '</a>';
289
        }
290
        return $shortcutMarkup;
291
    }
292
293
    protected function getBackendUser(): BackendUserAuthentication
294
    {
295
        return $GLOBALS['BE_USER'];
296
    }
297
298
    protected function getLanguageService(): LanguageService
299
    {
300
        return $GLOBALS['LANG'];
301
    }
302
}
303