TYPO3  7.6
PlainMailViewHelper.php
Go to the documentation of this file.
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 
22 {
32  public function render($labelContent = null, $content = null, $newLineAfterLabel = false, $indent = 0)
33  {
34  $templateVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
35  if (!$templateVariableContainer->exists(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'spaces')) {
36  $templateVariableContainer->add(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'spaces', 0);
37  }
38 
39  $spaces = $templateVariableContainer->get(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'spaces');
40  $output = '';
41  if ($labelContent) {
42  if ($labelContent instanceof \TYPO3\CMS\Form\Domain\Model\Element) {
43  $output = $this->getLabel($labelContent);
44  } else {
45  $output = $labelContent;
46  }
47  if ($newLineAfterLabel) {
48  if ($output !== '') {
49  $output = str_repeat(chr(32), $spaces) . $output . LF;
50  }
51  $this->setIndent($indent);
52  }
53  }
54 
55  if ($content) {
56  if (!$newLineAfterLabel) {
57  $this->setIndent($indent);
58  }
59  if (
60  $labelContent
61  && !$newLineAfterLabel
62  ) {
63  $output = $output . ': ' . $this->getValue($content);
64  } elseif (
65  $labelContent
66  && $newLineAfterLabel
67  ) {
68  $output =
69  $output .
70  str_repeat(chr(32), ($spaces + 4)) .
71  str_replace(LF, LF . str_repeat(chr(32), ($spaces + 4)), $this->getValue($content));
72  } else {
73  $output = $this->getValue($content);
74  }
75  }
76 
77  if (
78  $labelContent
79  || $content
80  ) {
81  if (
82  $output !== ''
83  && !$newLineAfterLabel
84  ) {
85  $output = str_repeat(chr(32), $spaces) . $output;
86  }
87  } else {
88  $this->setIndent($indent);
89  }
90 
91  return $output;
92  }
93 
99  protected function getLabel(\TYPO3\CMS\Form\Domain\Model\Element $model)
100  {
101  $label = '';
102  if ($model->getAdditionalArgument('legend')) {
103  $label = $model->getAdditionalArgument('legend');
104  } elseif ($model->getAdditionalArgument('label')) {
105  $label = $model->getAdditionalArgument('label');
106  }
107  return $label;
108  }
109 
115  protected function getValue($content)
116  {
117  $value = '';
118  if ($content instanceof \TYPO3\CMS\Form\Domain\Model\Element) {
119  $value = $content->getAdditionalArgument('value');
120  } else {
121  $value = $content;
122  }
123  return $value;
124  }
125 
132  public function setIndent($indent = 0)
133  {
134  $templateVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
135  $spaces = $templateVariableContainer->get(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'spaces');
136  $spaces += (int)$indent;
137  $templateVariableContainer->addOrUpdate(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'indent', $indent);
138  $templateVariableContainer->addOrUpdate(\TYPO3\CMS\Form\ViewHelpers\PlainMailViewHelper::class, 'spaces', $spaces);
139  }
140 }