87bb6f6bd3234cfb5159d0e9dee5ee28ee010243
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Container / AbstractContainer.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Container;
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\Backend\Form\NodeFactory;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Backend\Form\AbstractNode;
20 use TYPO3\CMS\Backend\Form\ElementConditionMatcher;
21 use TYPO3\CMS\Backend\Utility\IconUtility;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
23 use TYPO3\CMS\Backend\Template\DocumentTemplate;
24
25 /**
26 * Abstract container has various methods used by the container classes
27 */
28 abstract class AbstractContainer extends AbstractNode {
29
30 /**
31 * Instance of the node factory to create sub elements and container.
32 *
33 * @var NodeFactory
34 */
35 protected $nodeFactory;
36
37 /**
38 * Container objects give $nodeFactory down to other containers.
39 *
40 * @param NodeFactory $nodeFactory
41 * @param array $data
42 */
43 public function __construct(NodeFactory $nodeFactory, array $data) {
44 parent::__construct($nodeFactory, $data);
45 $this->nodeFactory = $nodeFactory;
46 }
47
48 /**
49 * A single field of TCA 'types' 'showitem' can have four semicolon separated configuration options:
50 * fieldName: Name of the field to be found in TCA 'columns' section
51 * fieldLabel: An alternative field label
52 * paletteName: Name of a palette to be found in TCA 'palettes' section that is rendered after this field
53 * extra: Special configuration options of this field
54 *
55 * @param string $field Semicolon separated field configuration
56 * @throws \RuntimeException
57 * @return array
58 */
59 protected function explodeSingleFieldShowItemConfiguration($field) {
60 $fieldArray = GeneralUtility::trimExplode(';', $field);
61 if (empty($fieldArray[0])) {
62 throw new \RuntimeException('Field must not be empty', 1426448465);
63 }
64 return array(
65 'fieldName' => $fieldArray[0],
66 'fieldLabel' => $fieldArray[1] ?: NULL,
67 'paletteName' => $fieldArray[2] ?: NULL,
68 );
69 }
70
71 /**
72 * Evaluate condition of flex forms
73 *
74 * @param string $displayCondition The condition to evaluate
75 * @param array $flexFormData Given data the condition is based on
76 * @param string $flexFormLanguage Flex form language key
77 * @return bool TRUE if condition matched
78 */
79 protected function evaluateFlexFormDisplayCondition($displayCondition, $flexFormData, $flexFormLanguage) {
80 $elementConditionMatcher = GeneralUtility::makeInstance(ElementConditionMatcher::class);
81
82 $splitCondition = GeneralUtility::trimExplode(':', $displayCondition);
83 $skipCondition = FALSE;
84 $fakeRow = array();
85 switch ($splitCondition[0]) {
86 case 'FIELD':
87 list($sheetName, $fieldName) = GeneralUtility::trimExplode('.', $splitCondition[1], FALSE, 2);
88 $fieldValue = $flexFormData[$sheetName][$flexFormLanguage][$fieldName];
89 $splitCondition[1] = $fieldName;
90 $displayCondition = join(':', $splitCondition);
91 $fakeRow = array($fieldName => $fieldValue);
92 break;
93 case 'HIDE_FOR_NON_ADMINS':
94
95 case 'VERSION':
96
97 case 'HIDE_L10N_SIBLINGS':
98
99 case 'EXT':
100 break;
101 case 'REC':
102 $fakeRow = array('uid' => $this->data['databaseRow']['uid']);
103 break;
104 default:
105 $skipCondition = TRUE;
106 }
107 if ($skipCondition) {
108 return TRUE;
109 } else {
110 return $elementConditionMatcher->match($displayCondition, $fakeRow, 'vDEF');
111 }
112 }
113
114 /**
115 * Rendering preview output of a field value which is not shown as a form field but just outputted.
116 *
117 * @param string $value The value to output
118 * @param array $config Configuration for field.
119 * @param string $field Name of field.
120 * @return string HTML formatted output
121 */
122 protected function previewFieldValue($value, $config, $field = '') {
123 if ($config['config']['type'] === 'group' && ($config['config']['internal_type'] === 'file' || $config['config']['internal_type'] === 'file_reference')) {
124 // Ignore upload folder if internal_type is file_reference
125 if ($config['config']['internal_type'] === 'file_reference') {
126 $config['config']['uploadfolder'] = '';
127 }
128 $table = 'tt_content';
129 // Making the array of file items:
130 $itemArray = GeneralUtility::trimExplode(',', $value, TRUE);
131 // Showing thumbnails:
132 $thumbnail = '';
133 $imgs = array();
134 foreach ($itemArray as $imgRead) {
135 $imgParts = explode('|', $imgRead);
136 $imgPath = rawurldecode($imgParts[0]);
137 $rowCopy = array();
138 $rowCopy[$field] = $imgPath;
139 // Icon + click menu:
140 $absFilePath = GeneralUtility::getFileAbsFileName($config['config']['uploadfolder'] ? $config['config']['uploadfolder'] . '/' . $imgPath : $imgPath);
141 $fileInformation = pathinfo($imgPath);
142 $fileIcon = IconUtility::getSpriteIconForFile(
143 $imgPath,
144 array(
145 'title' => htmlspecialchars($fileInformation['basename'] . ($absFilePath && @is_file($absFilePath) ? ' (' . GeneralUtility::formatSize(filesize($absFilePath)) . 'bytes)' : ' - FILE NOT FOUND!'))
146 )
147 );
148 $imgs[] =
149 '<span class="text-nowrap">' .
150 BackendUtility::thumbCode(
151 $rowCopy,
152 $table,
153 $field,
154 '',
155 'thumbs.php',
156 $config['config']['uploadfolder'], 0, ' align="middle"'
157 ) .
158 ($absFilePath ? $this->getControllerDocumentTemplate()->wrapClickMenuOnIcon($fileIcon, $absFilePath, 0, 1, '', '+copy,info,edit,view') : $fileIcon) .
159 $imgPath .
160 '</span>';
161 }
162 return implode('<br />', $imgs);
163 } else {
164 return nl2br(htmlspecialchars($value));
165 }
166 }
167
168 /**
169 * @return DocumentTemplate
170 */
171 protected function getControllerDocumentTemplate() {
172 return $GLOBALS['SOBE']->doc;
173 }
174
175 }