[FEATURE] Make DatabaseRecordList configurable to be editable
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / ViewHelper / TagBuilder.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\ViewHelper;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * Tag builder. Can be easily accessed in AbstractTagBasedViewHelper
16 *
17 * @api
18 */
19 class TagBuilder {
20
21 /**
22 * Name of the Tag to be rendered
23 *
24 * @var string
25 */
26 protected $tagName = '';
27
28 /**
29 * Content of the tag to be rendered
30 *
31 * @var string
32 */
33 protected $content = '';
34
35 /**
36 * Attributes of the tag to be rendered
37 *
38 * @var array
39 */
40 protected $attributes = array();
41
42 /**
43 * Specifies whether this tag needs a closing tag.
44 * E.g. <textarea> cant be self-closing even if its empty
45 *
46 * @var bool
47 */
48 protected $forceClosingTag = FALSE;
49
50 /**
51 * Constructor
52 *
53 * @param string $tagName name of the tag to be rendered
54 * @param string $tagContent content of the tag to be rendered
55 * @api
56 */
57 public function __construct($tagName = '', $tagContent = '') {
58 $this->setTagName($tagName);
59 $this->setContent($tagContent);
60 }
61
62 /**
63 * Sets the tag name
64 *
65 * @param string $tagName name of the tag to be rendered
66 * @return void
67 * @api
68 */
69 public function setTagName($tagName) {
70 $this->tagName = $tagName;
71 }
72
73 /**
74 * Gets the tag name
75 *
76 * @return string tag name of the tag to be rendered
77 * @api
78 */
79 public function getTagName() {
80 return $this->tagName;
81 }
82
83 /**
84 * Sets the content of the tag
85 *
86 * @param string $tagContent content of the tag to be rendered
87 * @return void
88 * @api
89 */
90 public function setContent($tagContent) {
91 $this->content = $tagContent;
92 }
93
94 /**
95 * Gets the content of the tag
96 *
97 * @return string content of the tag to be rendered
98 * @api
99 */
100 public function getContent() {
101 return $this->content;
102 }
103
104 /**
105 * Returns TRUE if tag contains content, otherwise FALSE
106 *
107 * @return bool TRUE if tag contains text, otherwise FALSE
108 * @api
109 */
110 public function hasContent() {
111 if ($this->content === NULL) {
112 return FALSE;
113 }
114 return $this->content !== '';
115 }
116
117 /**
118 * Set this to TRUE to force a closing tag
119 * E.g. <textarea> cant be self-closing even if its empty
120 *
121 * @param bool $forceClosingTag
122 * @api
123 */
124 public function forceClosingTag($forceClosingTag) {
125 $this->forceClosingTag = $forceClosingTag;
126 }
127
128 /**
129 * Returns TRUE if the tag has an attribute with the given name
130 *
131 * @param string $attributeName name of the attribute
132 * @return bool TRUE if the tag has an attribute with the given name, otherwise FALSE
133 * @api
134 */
135 public function hasAttribute($attributeName) {
136 return array_key_exists($attributeName, $this->attributes);
137 }
138
139 /**
140 * Get an attribute from the $attributes-collection
141 *
142 * @param string $attributeName name of the attribute
143 * @return string The attribute value or NULL if the attribute is not registered
144 * @api
145 */
146 public function getAttribute($attributeName) {
147 if (!$this->hasAttribute($attributeName)) {
148 return NULL;
149 }
150 return $this->attributes[$attributeName];
151 }
152
153 /**
154 * Get all attribute from the $attributes-collection
155 *
156 * @return array Attributes indexed by attribute name
157 * @api
158 */
159 public function getAttributes() {
160 return $this->attributes;
161 }
162
163 /**
164 * Adds an attribute to the $attributes-collection
165 *
166 * @param string $attributeName name of the attribute to be added to the tag
167 * @param string $attributeValue attribute value
168 * @param bool $escapeSpecialCharacters apply htmlspecialchars to attribute value
169 * @return void
170 * @api
171 */
172 public function addAttribute($attributeName, $attributeValue, $escapeSpecialCharacters = TRUE) {
173 if ($escapeSpecialCharacters) {
174 $attributeValue = htmlspecialchars($attributeValue);
175 }
176 $this->attributes[$attributeName] = $attributeValue;
177 }
178
179 /**
180 * Adds attributes to the $attributes-collection
181 *
182 * @param array $attributes collection of attributes to add. key = attribute name, value = attribute value
183 * @param bool $escapeSpecialCharacters apply htmlspecialchars to attribute values#
184 * @return void
185 * @api
186 */
187 public function addAttributes(array $attributes, $escapeSpecialCharacters = TRUE) {
188 foreach ($attributes as $attributeName => $attributeValue) {
189 $this->addAttribute($attributeName, $attributeValue, $escapeSpecialCharacters);
190 }
191 }
192
193 /**
194 * Removes an attribute from the $attributes-collection
195 *
196 * @param string $attributeName name of the attribute to be removed from the tag
197 * @return void
198 * @api
199 */
200 public function removeAttribute($attributeName) {
201 unset($this->attributes[$attributeName]);
202 }
203
204 /**
205 * Resets the TagBuilder by setting all members to their default value
206 *
207 * @return void
208 * @api
209 */
210 public function reset() {
211 $this->tagName = '';
212 $this->content = '';
213 $this->attributes = array();
214 $this->forceClosingTag = FALSE;
215 }
216
217 /**
218 * Renders and returns the tag
219 *
220 * @return string
221 * @api
222 */
223 public function render() {
224 if (empty($this->tagName)) {
225 return '';
226 }
227 $output = '<' . $this->tagName;
228 foreach ($this->attributes as $attributeName => $attributeValue) {
229 $output .= ' ' . $attributeName . '="' . $attributeValue . '"';
230 }
231 if ($this->hasContent() || $this->forceClosingTag) {
232 $output .= '>' . $this->content . '</' . $this->tagName . '>';
233 } else {
234 $output .= ' />';
235 }
236 return $output;
237 }
238
239 }