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