063e139d5bf45fd2b57f770346e8b2f9cfdca415
[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 * Get name
54 *
55 * @return string
56 */
57 public function getName()
58 {
59 return $this->name;
60 }
61
62 /**
63 * Set name
64 *
65 * @param string $name Name attribute
66 *
67 * @return InputButton
68 */
69 public function setName($name)
70 {
71 $this->name = $name;
72 return $this;
73 }
74
75 /**
76 * Get value
77 *
78 * @return string
79 */
80 public function getValue()
81 {
82 return $this->value;
83 }
84
85 /**
86 * Set value
87 *
88 * @param string $value Value attribute
89 *
90 * @return InputButton
91 */
92 public function setValue($value)
93 {
94 $this->value = $value;
95 return $this;
96 }
97
98 /**
99 * Validates the current button
100 *
101 * @return bool
102 */
103 public function isValid()
104 {
105 if (
106 trim($this->getName()) !== ''
107 && trim($this->getValue()) !== ''
108 && trim($this->getTitle()) !== ''
109 && $this->getType() === InputButton::class
110 && $this->getIcon() !== null
111 ) {
112 return true;
113 }
114 return false;
115 }
116
117 /**
118 * Renders the markup of the button
119 *
120 * @return string
121 */
122 public function render()
123 {
124 $attributes = array(
125 'name' => $this->getName(),
126 'class' => 'btn btn-default btn-sm ' . $this->getClasses(),
127 'value' => $this->getValue(),
128 'title' => $this->getTitle()
129 );
130 $labelText = '';
131 if ($this->showLabelText) {
132 $labelText = ' ' . $this->title;
133 }
134 foreach ($this->dataAttributes as $attributeName => $attributeValue) {
135 $attributes['data-' . htmlspecialchars($attributeName)] = $attributeValue;
136 }
137 $attributesString = '';
138 foreach ($attributes as $key => $value) {
139 $attributesString .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
140 }
141 return '<button' . $attributesString . '>'
142 . $this->getIcon()->render() . htmlspecialchars($labelText)
143 . '</button>';
144 }
145
146 /**
147 * Magic method so Fluid can access a button via {button}
148 *
149 * @return string
150 */
151 public function __toString()
152 {
153 return $this->render();
154 }
155 }