46500a755dc25ebcd1095e977cffb0e90100be18
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Template / Components / Buttons / InputButton.php
1 <?php
2 namespace TYPO3\CMS\Backend\Template\Components\Buttons;
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 /**
18 * InputButton
19 *
20 * This button type renders a HTML tag <button> and takes the HTML attributes
21 * name and value as additional attributes to those defined in AbstractButton.
22 *
23 * Since we no longer want to have any <input type="submit" /> in the TYPO3 core
24 * you should use this button type to send forms
25 *
26 * EXAMPLE USAGE TO ADD A BUTTON TO THE FIRST BUTTON GROUP IN THE LEFT BAR:
27 *
28 * $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
29 * $saveButton = $buttonBar->makeInputButton()
30 * ->setName('save')
31 * ->setValue('1')
32 * ->setIcon($this->iconFactory->getIcon('actions-document-save', Icon::SIZE_SMALL))
33 * ->setTitle('Save');
34 * $buttonBar->addButton($saveButton, ButtonBar::BUTTON_POSITION_LEFT, 1);
35 */
36 class InputButton extends AbstractButton implements ButtonInterface
37 {
38 /**
39 * Name Attribute of the button
40 *
41 * @var string
42 */
43 protected $name = '';
44
45 /**
46 * Value attribute of the button
47 *
48 * @var string
49 */
50 protected $value = '';
51
52 /**
53 * ID of the referenced <form> tag
54 *
55 * @var string
56 */
57 protected $form = '';
58
59 /**
60 * Get name
61 *
62 * @return string
63 */
64 public function getName()
65 {
66 return $this->name;
67 }
68
69 /**
70 * Set name
71 *
72 * @param string $name Name attribute
73 *
74 * @return InputButton
75 */
76 public function setName($name)
77 {
78 $this->name = $name;
79 return $this;
80 }
81
82 /**
83 * Get value
84 *
85 * @return string
86 */
87 public function getValue()
88 {
89 return $this->value;
90 }
91
92 /**
93 * Set value
94 *
95 * @param string $value Value attribute
96 *
97 * @return InputButton
98 */
99 public function setValue($value)
100 {
101 $this->value = $value;
102 return $this;
103 }
104
105 /**
106 * @return string
107 */
108 public function getForm()
109 {
110 return $this->form;
111 }
112
113 /**
114 * @param string $form
115 *
116 * @return InputButton
117 */
118 public function setForm($form)
119 {
120 $this->form = $form;
121 return $this;
122 }
123
124 /**
125 * Validates the current button
126 *
127 * @return bool
128 */
129 public function isValid()
130 {
131 if (
132 trim($this->getName()) !== ''
133 && trim($this->getValue()) !== ''
134 && trim($this->getTitle()) !== ''
135 && $this->getType() === InputButton::class
136 && $this->getIcon() !== null
137 ) {
138 return true;
139 }
140 return false;
141 }
142
143 /**
144 * Renders the markup of the button
145 *
146 * @return string
147 */
148 public function render()
149 {
150 $attributes = [
151 'name' => $this->getName(),
152 'class' => 'btn btn-default btn-sm ' . $this->getClasses(),
153 'value' => $this->getValue(),
154 'title' => $this->getTitle(),
155 'form' => trim($this->getForm())
156 ];
157 $labelText = '';
158 if ($this->showLabelText) {
159 $labelText = ' ' . $this->title;
160 }
161 foreach ($this->dataAttributes as $attributeName => $attributeValue) {
162 $attributes['data-' . $attributeName] = $attributeValue;
163 }
164 $attributesString = '';
165 foreach ($attributes as $key => $value) {
166 if ($value !== '') {
167 $attributesString .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
168 }
169 }
170 return '<button' . $attributesString . '>'
171 . $this->getIcon()->render() . htmlspecialchars($labelText)
172 . '</button>';
173 }
174
175 /**
176 * Magic method so Fluid can access a button via {button}
177 *
178 * @return string
179 */
180 public function __toString()
181 {
182 return $this->render();
183 }
184 }