77ef3a0359beffcedf9ace94dc15e9b598c9e8dd
[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\Backend\Form\Utility\FormEngineUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Backend\Form\AbstractNode;
23 use TYPO3\CMS\Backend\Utility\BackendUtility;
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 {
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 {
62 $fieldArray = GeneralUtility::trimExplode(';', $field);
63 if (empty($fieldArray[0])) {
64 throw new \RuntimeException('Field must not be empty', 1426448465);
65 }
66 return array(
67 'fieldName' => $fieldArray[0],
68 'fieldLabel' => $fieldArray[1] ?: null,
69 'paletteName' => $fieldArray[2] ?: null,
70 );
71 }
72
73 /**
74 * Rendering preview output of a field value which is not shown as a form field but just outputted.
75 *
76 * @param string $value The value to output
77 * @param array $config Configuration for field.
78 * @param string $field Name of field.
79 * @return string HTML formatted output
80 */
81 protected function previewFieldValue($value, $config, $field = '')
82 {
83 if ($config['config']['type'] === 'group' && ($config['config']['internal_type'] === 'file' || $config['config']['internal_type'] === 'file_reference')) {
84 // Ignore upload folder if internal_type is file_reference
85 if ($config['config']['internal_type'] === 'file_reference') {
86 $config['config']['uploadfolder'] = '';
87 }
88 $table = 'tt_content';
89 // Making the array of file items:
90 $itemArray = GeneralUtility::trimExplode(',', $value, true);
91 // Showing thumbnails:
92 $thumbnail = '';
93 $imgs = array();
94 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
95 foreach ($itemArray as $imgRead) {
96 $imgParts = explode('|', $imgRead);
97 $imgPath = rawurldecode($imgParts[0]);
98 $rowCopy = array();
99 $rowCopy[$field] = $imgPath;
100 // Icon + click menu:
101 $absFilePath = GeneralUtility::getFileAbsFileName($config['config']['uploadfolder'] ? $config['config']['uploadfolder'] . '/' . $imgPath : $imgPath);
102 $fileInformation = pathinfo($imgPath);
103 $title = $fileInformation['basename'] . ($absFilePath && @is_file($absFilePath))
104 ? ' (' . GeneralUtility::formatSize(filesize($absFilePath)) . ')'
105 : ' - FILE NOT FOUND!';
106 $fileIcon = '<span title="' . htmlspecialchars($title) . '">' . $iconFactory->getIconForFileExtension($fileInformation['extension'], Icon::SIZE_SMALL)->render() . '</span>';
107 $imgs[] =
108 '<span class="text-nowrap">' .
109 BackendUtility::thumbCode(
110 $rowCopy,
111 $table,
112 $field,
113 '',
114 'thumbs.php',
115 $config['config']['uploadfolder'], 0, ' align="middle"'
116 ) .
117 ($absFilePath ? BackendUtility::wrapClickMenuOnIcon($fileIcon, $absFilePath, 0, 1, '', '+copy,info,edit,view') : $fileIcon) .
118 $imgPath .
119 '</span>';
120 }
121 return implode('<br />', $imgs);
122 } else {
123 return nl2br(htmlspecialchars($value));
124 }
125 }
126 }