[TASK] Integrate DocHeader action buttons
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Template / Components / ButtonBar.php
1 <?php
2 namespace TYPO3\CMS\Backend\Template\Components;
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\Buttons\ButtonInterface;
18 use TYPO3\CMS\Backend\Template\Components\Buttons\PositionInterface;
19 use TYPO3\CMS\Backend\Template\Components\Buttons\FullyRenderedButton;
20 use TYPO3\CMS\Backend\Template\Components\Buttons\InputButton;
21 use TYPO3\CMS\Backend\Template\Components\Buttons\LinkButton;
22 use TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton;
23 use TYPO3\CMS\Backend\Template\Components\Buttons\Action\HelpButton;
24 use TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * Bar holding the buttons
29 */
30 class ButtonBar
31 {
32 /**
33 * Identifier for the left button bar
34 */
35 const BUTTON_POSITION_LEFT = 'left';
36
37 /**
38 * Identifier for the right button bar
39 */
40 const BUTTON_POSITION_RIGHT = 'right';
41
42 /**
43 * Internal array of all registered buttons
44 *
45 * @var array
46 */
47 protected $buttons = [];
48
49 /**
50 * Add button
51 *
52 * @param ButtonInterface $button The Button Object to add
53 * @param string $buttonPosition Position of the button (left/right)
54 * @param int $buttonGroup Buttongroup of the button
55 *
56 * @throws \InvalidArgumentException In case a button is not valid
57 *
58 * @return $this
59 */
60 public function addButton(
61 ButtonInterface $button,
62 $buttonPosition = self::BUTTON_POSITION_LEFT,
63 $buttonGroup = 1
64 ) {
65 if (!$button->isValid()) {
66 throw new \InvalidArgumentException('Button "' . $button->getType() . '" is not valid', 1441706370);
67 }
68 // Determine the default button position
69 if ($button instanceof PositionInterface) {
70 $buttonPosition = $button->getPosition();
71 $buttonGroup = $button->getGroup();
72 }
73 // We make the button immutable here
74 $this->buttons[$buttonPosition][$buttonGroup][] = clone $button;
75 return $this;
76 }
77
78 /**
79 * Creates a new button of the given type
80 *
81 * @param string $button ButtonClass to invoke. Must implement ButtonInterface
82 *
83 * @throws \InvalidArgumentException In case a ButtonClass does not implement
84 * ButtonInterface
85 *
86 * @return ButtonInterface
87 */
88 public function makeButton($button)
89 {
90 if (!in_array(ButtonInterface::class, class_implements($button), true)) {
91 throw new \InvalidArgumentException('A Button must implement ButtonInterface', 1441706378);
92 }
93 return GeneralUtility::makeInstance($button);
94 }
95
96 /**
97 * Creates a new InputButton
98 *
99 * @return InputButton
100 */
101 public function makeInputButton()
102 {
103 return GeneralUtility::makeInstance(InputButton::class);
104 }
105
106 /**
107 * Creates a new SplitButton
108 *
109 * @return SplitButton
110 */
111 public function makeSplitButton()
112 {
113 return GeneralUtility::makeInstance(SplitButton::class);
114 }
115
116 /**
117 * Creates a new LinkButton
118 *
119 * @return LinkButton
120 */
121 public function makeLinkButton()
122 {
123 return GeneralUtility::makeInstance(LinkButton::class);
124 }
125
126 /**
127 * Creates a new FullyRenderedButton
128 *
129 * @return FullyRenderedButton
130 */
131 public function makeFullyRenderedButton()
132 {
133 return GeneralUtility::makeInstance(FullyRenderedButton::class);
134 }
135
136 /**
137 * Creates a new ShortcutButton
138 *
139 * @return ShortcutButton
140 */
141 public function makeShortcutButton()
142 {
143 return GeneralUtility::makeInstance(ShortcutButton::class);
144 }
145
146 /**
147 * Creates a new HelpButton
148 *
149 * @return HelpButton
150 */
151 public function makeHelpButton()
152 {
153 return GeneralUtility::makeInstance(HelpButton::class);
154 }
155
156 /**
157 * Returns an associative array of all buttons in the form of
158 * ButtonPosition > ButtonGroup > Button
159 *
160 * @return array
161 */
162 public function getButtons()
163 {
164 // here we need to call the sorting methods and stuff.
165 foreach ($this->buttons as $position => $_) {
166 ksort($this->buttons[$position]);
167 }
168 // @todo do we want to provide a hook here?
169 return $this->buttons;
170 }
171 }