[CLEANUP] Improve the @param/@return/@var PHPDoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / View / Confirmation / Element / AbstractElementView.php
1 <?php
2 namespace TYPO3\CMS\Form\View\Confirmation\Element;
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\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Form\Domain\Model\Element\AbstractElement;
19 use TYPO3\CMS\Form\Utility\FormUtility;
20
21 /**
22 * Abstract class for the form elements view
23 *
24 * @author Patrick Broens <patrick@patrickbroens.nl>
25 */
26 abstract class AbstractElementView {
27
28 /**
29 * The model for the current object
30 *
31 * @var \TYPO3\CMS\Form\Domain\Model\Element\AbstractElement
32 */
33 protected $model;
34
35 /**
36 * Wrap for elements
37 *
38 * @var string
39 */
40 protected $elementWrap = '
41 <li>
42 <element />
43 </li>
44 ';
45
46 /**
47 * True if element needs no element wrap
48 * like <li>element</li>
49 *
50 * @var bool
51 */
52 protected $noWrap = FALSE;
53
54 /**
55 * Constructor
56 *
57 * @param AbstractElement $model Current elements model
58 */
59 public function __construct($model) {
60 $this->model = $model;
61 }
62
63 /**
64 * Parse the XML of a view object,
65 * check the node type and name
66 * and add the proper XML part of child tags
67 * to the DOMDocument of the current tag
68 *
69 * @param \DOMDocument $dom
70 * @param \DOMNode $reference Current XML structure
71 * @param bool $emptyElement
72 * @return bool
73 */
74 protected function parseXML(\DOMDocument $dom, \DOMNode $reference, $emptyElement = FALSE) {
75 $node = $reference->firstChild;
76 while (!is_null($node)) {
77 $deleteNode = FALSE;
78 $nodeType = $node->nodeType;
79 $nodeName = $node->nodeName;
80 switch ($nodeType) {
81 case XML_TEXT_NODE:
82 break;
83 case XML_ELEMENT_NODE:
84 switch ($nodeName) {
85 case 'containerWrap':
86 $containerWrap = $this->render('containerWrap');
87 if ($containerWrap) {
88 $this->replaceNodeWithFragment($dom, $node, $containerWrap);
89 } else {
90 $emptyElement = TRUE;
91 }
92 $deleteNode = TRUE;
93 break;
94 case 'elements':
95 $replaceNode = $this->getChildElements($dom);
96 if ($replaceNode) {
97 $node->parentNode->replaceChild($replaceNode, $node);
98 } else {
99 $emptyElement = TRUE;
100 }
101 break;
102 case 'label':
103 if (!strrchr(get_class($this), 'AdditionalElement')) {
104 if ($this->model->additionalIsSet($nodeName)) {
105 $this->replaceNodeWithFragment($dom, $node, $this->getAdditional('label'));
106 }
107 $deleteNode = TRUE;
108 } else {
109 if (!$this->model->additionalIsSet($nodeName)) {
110 $deleteNode = TRUE;
111 }
112 }
113 break;
114 case 'legend':
115 if (!strrchr(get_class($this), 'AdditionalElement')) {
116 if ($this->model->additionalIsSet($nodeName)) {
117 $this->replaceNodeWithFragment($dom, $node, $this->getAdditional('legend'));
118 }
119 $deleteNode = TRUE;
120 }
121 break;
122 case 'inputvalue':
123 if (array_key_exists('checked', $this->model->getAllowedAttributes())) {
124 if (!$this->model->hasAttribute('checked')) {
125 $emptyElement = TRUE;
126 }
127 } elseif (array_key_exists('selected', $this->model->getAllowedAttributes()) && !$this->model->hasAttribute('selected')) {
128 $emptyElement = TRUE;
129 } else {
130 $inputValue = $this->getInputValue();
131 if ($inputValue != '') {
132 $replaceNode = $dom->createTextNode($this->getInputValue());
133 $node->parentNode->insertBefore($replaceNode, $node);
134 } else {
135 $emptyElement = TRUE;
136 }
137 }
138 $deleteNode = TRUE;
139 break;
140 case 'labelvalue':
141
142 case 'legendvalue':
143 $replaceNode = $dom->createTextNode($this->getAdditionalValue());
144 $node->parentNode->insertBefore($replaceNode, $node);
145 $deleteNode = TRUE;
146 break;
147 }
148 break;
149 }
150 // Parse the child nodes of this node if available
151 if ($node->hasChildNodes()) {
152 $emptyElement = $this->parseXML($dom, $node, $emptyElement);
153 }
154 // Get the current node for deletion if replaced. We need this because nextSibling can be empty
155 $oldNode = $node;
156 // Go to next sibling to parse
157 $node = $node->nextSibling;
158 // Delete the old node. This can only be done after going to the next sibling
159 if ($deleteNode) {
160 $oldNode->parentNode->removeChild($oldNode);
161 }
162 }
163 return $emptyElement;
164 }
165
166 /**
167 * Get the content for the current object as DOMDocument
168 *
169 * @param string $type Type of element for layout
170 * @param bool $returnFirstChild If TRUE, the first child will be returned instead of the DOMDocument
171 * @return \DOMNode XML part of the view object
172 */
173 public function render($type = 'element', $returnFirstChild = TRUE) {
174 $useLayout = $this->getLayout((string)$type);
175 $dom = new \DOMDocument('1.0', 'utf-8');
176 $dom->formatOutput = TRUE;
177 $dom->preserveWhiteSpace = FALSE;
178 $dom->loadXML($useLayout);
179 $emptyElement = $this->parseXML($dom, $dom);
180 if ($emptyElement) {
181 return NULL;
182 } elseif ($returnFirstChild) {
183 return $dom->firstChild;
184 } else {
185 return $dom;
186 }
187 }
188
189 /**
190 * Ask the layoutHandler to get the layout for this object
191 *
192 * @param string $type Layout type
193 * @return string HTML string of the layout to use for this element
194 */
195 public function getLayout($type) {
196 /** @var $layoutHandler \TYPO3\CMS\Form\Layout */
197 $layoutHandler = GeneralUtility::makeInstance(\TYPO3\CMS\Form\Layout::class);
198 switch ($type) {
199 case 'element':
200 $layoutDefault = $this->layout;
201 $layout = $layoutHandler->getLayoutByObject(FormUtility::getInstance()->getLastPartOfClassName($this, TRUE), $layoutDefault);
202 break;
203 case 'elementWrap':
204 $layoutDefault = $this->elementWrap;
205 $elementWrap = $layoutHandler->getLayoutByObject($type, $layoutDefault);
206 $layout = str_replace('<element />', $this->getLayout('element'), $elementWrap);
207 break;
208 case 'containerWrap':
209 $layoutDefault = $this->containerWrap;
210 $layout = $layoutHandler->getLayoutByObject($type, $layoutDefault);
211 break;
212 }
213 return $layout;
214 }
215
216 /**
217 * Replace the current node with a document fragment
218 *
219 * @param \DOMDocument $dom
220 * @param \DOMNode $node Current Node
221 * @param \DOMNode $value Value to import
222 * @return void
223 */
224 public function replaceNodeWithFragment(\DOMDocument $dom, \DOMNode $node, \DOMNode $value) {
225 $replaceNode = $dom->createDocumentFragment();
226 $domNode = $dom->importNode($value, TRUE);
227 $replaceNode->appendChild($domNode);
228 $node->parentNode->insertBefore($replaceNode, $node);
229 }
230
231 /**
232 * Set the attributes on the html tags according to the attributes that are
233 * assigned in the model for a certain element
234 *
235 * @param \DOMElement $domElement DOM element of the specific HTML tag
236 * @return void
237 */
238 public function setAttributes(\DOMElement $domElement) {
239 $attributes = $this->model->getAttributes();
240 foreach ($attributes as $key => $attribute) {
241 if (!empty($attribute)) {
242 $value = $attribute->getValue();
243 if ($value !== '') {
244 $domElement->setAttribute($key, $value);
245 }
246 }
247 }
248 }
249
250 /**
251 * Set a single attribute of a HTML tag specified by key
252 *
253 * @param \DOMElement $domElement DOM element of the specific HTML tag
254 * @param string $key Attribute key
255 * @return void
256 */
257 public function setAttribute(\DOMElement $domElement, $key) {
258 $attribute = $this->model->getAttributeValue((string)$key);
259 if (!empty($attribute)) {
260 $domElement->setAttribute($key, $attribute);
261 }
262 }
263
264 /**
265 * Sets the value of an attribute with the value of another attribute,
266 * for instance equalizing the name and id attributes for the form tag
267 *
268 * @param \DOMElement $domElement DOM element of the specific HTML tag
269 * @param string $key Key of the attribute which needs to be changed
270 * @param string $other Key of the attribute to take the value from
271 * @return void
272 */
273 public function setAttributeWithValueofOtherAttribute(\DOMElement $domElement, $key, $other) {
274 $attribute = $this->model->getAttributeValue((string)$other);
275 if (!empty($attribute)) {
276 $domElement->setAttribute($key, $attribute);
277 }
278 }
279
280 /**
281 * Load and instantiate an additional object
282 *
283 * @param string $class Type of additional
284 * @return AbstractElementView
285 */
286 protected function createAdditional($class) {
287 $class = strtolower((string)$class);
288 $className = 'TYPO3\\CMS\\Form\\View\\Confirmation\\Additional\\' . ucfirst($class) . 'AdditionalElementView';
289 return GeneralUtility::makeInstance($className, $this->model);
290 }
291
292 /**
293 * Create additional object by key and render the content
294 *
295 * @param string $key Type of additional
296 * @return \DOMNode
297 */
298 public function getAdditional($key) {
299 $additional = $this->createAdditional($key);
300 return $additional->render();
301 }
302
303 /**
304 * @return string
305 */
306 public function getInputValue() {
307 if (method_exists($this->model, 'getData')) {
308 $inputValue = nl2br($this->model->getData(), TRUE);
309 } else {
310 $inputValue = $this->model->getAttributeValue('value');
311 }
312 return $inputValue;
313 }
314
315 /**
316 * Return the id for the element wraps,
317 * like <li id="csc-form-"> ... </li>
318 *
319 * @return string
320 */
321 public function getElementWrapId() {
322 $elementId = (int)$this->model->getElementId();
323 $wrapId = 'csc-form-' . $elementId;
324 return $wrapId;
325 }
326
327 /**
328 * Returns the type for the element wraps,
329 * like <li class="csc-form-element csc-form-element-abstract">...</li>
330 *
331 * @return string
332 */
333 public function getElementWrapType() {
334 $elementType = strtolower(FormUtility::getInstance()->getLastPartOfClassName($this->model));
335 $wrapType = 'csc-form-element csc-form-element-' . $elementType;
336 return $wrapType;
337 }
338
339 /**
340 * Returns all element wraps.
341 *
342 * @return string
343 */
344 public function getElementWraps() {
345 $wraps = array(
346 $this->getElementWrapId(),
347 $this->getElementWrapType()
348 );
349 return implode(' ', $wraps);
350 }
351
352 /**
353 * Read the noWrap value of an element
354 * if TRUE the element does not need a element wrap
355 * like <li>element</li>
356 *
357 * @return bool
358 */
359 public function noWrap() {
360 return $this->noWrap;
361 }
362
363 }