[TASK] Removes extra empty lines
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / PlainMailViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Form\ViewHelpers;
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 * A viewhelper for the plain mail view
19 */
20 class PlainMailViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
21 {
22 /**
23 * Render the plain mail view
24 *
25 * @param mixed $labelContent
26 * @param mixed $content
27 * @param boolean $newLineAfterLabel
28 * @param integer $indent
29 * @return string
30 */
31 public function render($labelContent = null, $content = null, $newLineAfterLabel = false, $indent = 0)
32 {
33 $templateVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
34 if (!$templateVariableContainer->exists(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'spaces')) {
35 $templateVariableContainer->add(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'spaces', 0);
36 }
37
38 $spaces = $templateVariableContainer->get(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'spaces');
39 $output = '';
40 if ($labelContent) {
41 if ($labelContent instanceof \TYPO3\CMS\Form\Domain\Model\Element) {
42 $output = $this->getLabel($labelContent);
43 } else {
44 $output = $labelContent;
45 }
46 if ($newLineAfterLabel) {
47 if ($output !== '') {
48 $output = str_repeat(chr(32), $spaces) . $output . LF;
49 }
50 $this->setIndent($indent);
51 }
52 }
53
54 if ($content) {
55 if (!$newLineAfterLabel) {
56 $this->setIndent($indent);
57 }
58 if (
59 $labelContent
60 && !$newLineAfterLabel
61 ) {
62 $output = $output . ': ' . $this->getValue($content);
63 } elseif (
64 $labelContent
65 && $newLineAfterLabel
66 ) {
67 $output =
68 $output .
69 str_repeat(chr(32), ($spaces + 4)) .
70 str_replace(LF, LF . str_repeat(chr(32), ($spaces + 4)), $this->getValue($content));
71 } else {
72 $output = $this->getValue($content);
73 }
74 }
75
76 if (
77 $labelContent
78 || $content
79 ) {
80 if (
81 $output !== ''
82 && !$newLineAfterLabel
83 ) {
84 $output = str_repeat(chr(32), $spaces) . $output;
85 }
86 } else {
87 $this->setIndent($indent);
88 }
89
90 return $output;
91 }
92
93 /**
94 * Get the label
95 * @param \TYPO3\CMS\Form\Domain\Model\Element $model
96 * @return string
97 */
98 protected function getLabel(\TYPO3\CMS\Form\Domain\Model\Element $model)
99 {
100 $label = '';
101 if ($model->getAdditionalArgument('legend')) {
102 $label = $model->getAdditionalArgument('legend');
103 } elseif ($model->getAdditionalArgument('label')) {
104 $label = $model->getAdditionalArgument('label');
105 }
106 return $label;
107 }
108
109 /**
110 * Get the label
111 * @param mixed $model
112 * @return string
113 */
114 protected function getValue($content)
115 {
116 $value = '';
117 if ($content instanceof \TYPO3\CMS\Form\Domain\Model\Element) {
118 $value = $content->getAdditionalArgument('value');
119 } else {
120 $value = $content;
121 }
122 return $value;
123 }
124
125 /**
126 * Set the current indent level
127 *
128 * @param integer $indent
129 * @return void
130 */
131 public function setIndent($indent = 0)
132 {
133 $templateVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
134 $spaces = $templateVariableContainer->get(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'spaces');
135 $spaces += (int)$indent;
136 $templateVariableContainer->addOrUpdate(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'indent', $indent);
137 $templateVariableContainer->addOrUpdate(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'spaces', $spaces);
138 }
139 }