[BUGFIX] FormEngine warning
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Container / InlineControlContainer.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\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Resource\Folder;
19 use TYPO3\CMS\Core\Utility\MathUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22 use TYPO3\CMS\Backend\Form\DataPreprocessor;
23 use TYPO3\CMS\Lang\LanguageService;
24 use TYPO3\CMS\Core\Utility\ArrayUtility;
25 use TYPO3\CMS\Backend\Form\Utility\FormEngineUtility;
26 use TYPO3\CMS\Backend\Utility\IconUtility;
27 use TYPO3\CMS\Backend\Form\InlineStackProcessor;
28 use TYPO3\CMS\Backend\Form\InlineRelatedRecordResolver;
29 use TYPO3\CMS\Backend\Form\Exception\AccessDeniedException;
30 use TYPO3\CMS\Backend\Form\NodeFactory;
31
32 /**
33 * Inline element entry container.
34 *
35 * This container is the entry step to rendering an inline element. It is created by SingleFieldContainer.
36 *
37 * The code creates the main structure for the single inline elements, initializes
38 * the inlineData array, that is manipulated and also returned back in its manipulated state.
39 * The "control" stuff of inline elements is rendered here, for example the "create new" button.
40 *
41 * For each existing inline relation an InlineRecordContainer is called for further processing.
42 */
43 class InlineControlContainer extends AbstractContainer {
44
45 /**
46 * Inline data array used in JS, returned as JSON object to frontend
47 *
48 * @var array
49 */
50 protected $inlineData = array();
51
52 /**
53 * @var InlineStackProcessor
54 */
55 protected $inlineStackProcessor;
56
57 /**
58 * Entry method
59 *
60 * @return array As defined in initializeResultArray() of AbstractNode
61 */
62 public function render() {
63 $languageService = $this->getLanguageService();
64
65 $this->inlineData = $this->globalOptions['inlineData'];
66
67 /** @var InlineStackProcessor $inlineStackProcessor */
68 $inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class);
69 $this->inlineStackProcessor = $inlineStackProcessor;
70 $inlineStackProcessor->initializeByGivenStructure($this->globalOptions['inlineStructure']);
71
72 $table = $this->globalOptions['table'];
73 $row = $this->globalOptions['databaseRow'];
74 $field = $this->globalOptions['fieldName'];
75 $parameterArray = $this->globalOptions['parameterArray'];
76
77 $resultArray = $this->initializeResultArray();
78 $html = '';
79
80 // An inline field must have a foreign_table, if not, stop all further inline actions for this field
81 if (
82 !$parameterArray['fieldConf']['config']['foreign_table']
83 || !is_array($GLOBALS['TCA'][$parameterArray['fieldConf']['config']['foreign_table']])
84 ) {
85 return $resultArray;
86 }
87
88 $config = FormEngineUtility::mergeInlineConfiguration($parameterArray['fieldConf']['config']);
89 $foreign_table = $config['foreign_table'];
90
91 $language = 0;
92 if (BackendUtility::isTableLocalizable($table)) {
93 $language = (int)$row[$GLOBALS['TCA'][$table]['ctrl']['languageField']];
94 }
95 $minItems = MathUtility::forceIntegerInRange($config['minitems'], 0);
96 $maxItems = MathUtility::forceIntegerInRange($config['maxitems'], 0);
97 if (!$maxItems) {
98 $maxItems = 100000;
99 }
100
101 // Add the current inline job to the structure stack
102 $newStructureItem = array(
103 'table' => $table,
104 'uid' => $row['uid'],
105 'field' => $field,
106 'config' => $config,
107 'localizationMode' => BackendUtility::getInlineLocalizationMode($table, $config),
108 );
109 // Extract FlexForm parts (if any) from element name, e.g. array('vDEF', 'lDEF', 'FlexField', 'vDEF')
110 if (!empty($parameterArray['itemFormElName'])) {
111 $flexFormParts = FormEngineUtility::extractFlexFormParts($parameterArray['itemFormElName']);
112 if ($flexFormParts !== NULL) {
113 $newStructureItem['flexform'] = $flexFormParts;
114 }
115 }
116 $inlineStackProcessor->pushStableStructureItem($newStructureItem);
117
118 // e.g. data[<table>][<uid>][<field>]
119 $nameForm = $inlineStackProcessor->getCurrentStructureFormPrefix();
120 // e.g. data-<pid>-<table1>-<uid1>-<field1>-<table2>-<uid2>-<field2>
121 $nameObject = $inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->globalOptions['inlineFirstPid']);
122
123 // Get the records related to this inline record
124 $inlineRelatedRecordResolver = GeneralUtility::makeInstance(InlineRelatedRecordResolver::class);
125 $relatedRecords = $inlineRelatedRecordResolver->getRelatedRecords($table, $field, $row, $parameterArray, $config, $this->globalOptions['inlineFirstPid']);
126
127 // Set the first and last record to the config array
128 $relatedRecordsUids = array_keys($relatedRecords['records']);
129 $config['inline']['first'] = reset($relatedRecordsUids);
130 $config['inline']['last'] = end($relatedRecordsUids);
131
132 $top = $inlineStackProcessor->getStructureLevel(0);
133
134 $this->inlineData['config'][$nameObject] = array(
135 'table' => $foreign_table,
136 'md5' => md5($nameObject)
137 );
138 $this->inlineData['config'][$nameObject . '-' . $foreign_table] = array(
139 'min' => $minItems,
140 'max' => $maxItems,
141 'sortable' => $config['appearance']['useSortable'],
142 'top' => array(
143 'table' => $top['table'],
144 'uid' => $top['uid']
145 ),
146 'context' => array(
147 'config' => $config,
148 'hmac' => GeneralUtility::hmac(serialize($config)),
149 ),
150 );
151 $this->inlineData['nested'][$nameObject] = $this->globalOptions['tabAndInlineStack'];
152
153 // If relations are required to be unique, get the uids that have already been used on the foreign side of the relation
154 if ($config['foreign_unique']) {
155 // If uniqueness *and* selector are set, they should point to the same field - so, get the configuration of one:
156 $selConfig = FormEngineUtility::getInlinePossibleRecordsSelectorConfig($config, $config['foreign_unique']);
157 // Get the used unique ids:
158 $uniqueIds = $this->getUniqueIds($relatedRecords['records'], $config, $selConfig['type'] == 'groupdb');
159 $possibleRecords = $this->getPossibleRecords($table, $field, $row, $config, 'foreign_unique');
160 $uniqueMax = $config['appearance']['useCombination'] || $possibleRecords === FALSE ? -1 : count($possibleRecords);
161 $this->inlineData['unique'][$nameObject . '-' . $foreign_table] = array(
162 'max' => $uniqueMax,
163 'used' => $uniqueIds,
164 'type' => $selConfig['type'],
165 'table' => $config['foreign_table'],
166 'elTable' => $selConfig['table'],
167 // element/record table (one step down in hierarchy)
168 'field' => $config['foreign_unique'],
169 'selector' => $selConfig['selector'],
170 'possible' => $this->getPossibleRecordsFlat($possibleRecords)
171 );
172 }
173
174 $resultArray['inlineData'] = $this->inlineData;
175
176 // Render the localization links
177 $localizationLinks = '';
178 if ($language > 0 && $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] > 0 && MathUtility::canBeInterpretedAsInteger($row['uid'])) {
179 // Add the "Localize all records" link before all child records:
180 if (isset($config['appearance']['showAllLocalizationLink']) && $config['appearance']['showAllLocalizationLink']) {
181 $localizationLinks .= ' ' . $this->getLevelInteractionLink('localize', $nameObject . '-' . $foreign_table, $config);
182 }
183 // Add the "Synchronize with default language" link before all child records:
184 if (isset($config['appearance']['showSynchronizationLink']) && $config['appearance']['showSynchronizationLink']) {
185 $localizationLinks .= ' ' . $this->getLevelInteractionLink('synchronize', $nameObject . '-' . $foreign_table, $config);
186 }
187 }
188
189 // Define how to show the "Create new record" link - if there are more than maxitems, hide it
190 if ($relatedRecords['count'] >= $maxItems || $uniqueMax > 0 && $relatedRecords['count'] >= $uniqueMax) {
191 $config['inline']['inlineNewButtonStyle'] = 'display: none;';
192 $config['inline']['inlineNewRelationButtonStyle'] = 'display: none;';
193 }
194
195 // Render the level links (create new record):
196 $levelLinks = $this->getLevelInteractionLink('newRecord', $nameObject . '-' . $foreign_table, $config);
197
198 // Wrap all inline fields of a record with a <div> (like a container)
199 $html .= '<div class="form-group" id="' . $nameObject . '">';
200 // Add the level links before all child records:
201 if ($config['appearance']['levelLinksPosition'] === 'both' || $config['appearance']['levelLinksPosition'] === 'top') {
202 $html .= '<div class="form-group t3js-formengine-validation-marker">' . $levelLinks . $localizationLinks . '</div>';
203 }
204 // If it's required to select from possible child records (reusable children), add a selector box
205 if ($config['foreign_selector'] && $config['appearance']['showPossibleRecordsSelector'] !== FALSE) {
206 // If not already set by the foreign_unique, set the possibleRecords here and the uniqueIds to an empty array
207 if (!$config['foreign_unique']) {
208 $possibleRecords = $this->getPossibleRecords($table, $field, $row, $config);
209 $uniqueIds = array();
210 }
211 $selectorBox = $this->renderPossibleRecordsSelector($possibleRecords, $config, $uniqueIds);
212 $html .= $selectorBox . $localizationLinks;
213 }
214 $title = $languageService->sL($parameterArray['fieldConf']['label']);
215 $html .= '<div class="panel-group panel-hover" data-title="' . htmlspecialchars($title) . '" id="' . $nameObject . '_records">';
216
217 $relationList = array();
218 if (!empty($relatedRecords['records'])) {
219 foreach ($relatedRecords['records'] as $rec) {
220 $options = $this->globalOptions;
221 $options['inlineRelatedRecordToRender'] = $rec;
222 $options['inlineRelatedRecordConfig'] = $config;
223 $options['inlineData'] = $this->inlineData;
224 $options['inlineStructure'] = $inlineStackProcessor->getStructure();
225 $options['renderType'] = 'inlineRecordContainer';
226 /** @var NodeFactory $nodeFactory */
227 $nodeFactory = $this->globalOptions['nodeFactory'];
228 try {
229 // This container may raise an access denied exception, to not kill further processing,
230 // just a simple "empty" return is created here to ignore this field.
231 $childArray = $nodeFactory->create($options)->render();
232 } catch (AccessDeniedException $e) {
233 $childArray = $this->initializeResultArray();
234 }
235 $html .= $childArray['html'];
236 $childArray['html'] = '';
237 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childArray);
238 if (!isset($rec['__virtual']) || !$rec['__virtual']) {
239 $relationList[] = $rec['uid'];
240 }
241 }
242 }
243 $html .= '</div>';
244 // Add the level links after all child records:
245 if ($config['appearance']['levelLinksPosition'] === 'both' || $config['appearance']['levelLinksPosition'] === 'bottom') {
246 $html .= $levelLinks . $localizationLinks;
247 }
248 if (is_array($config['customControls'])) {
249 $html .= '<div id="' . $nameObject . '_customControls">';
250 foreach ($config['customControls'] as $customControlConfig) {
251 $parameters = array(
252 'table' => $table,
253 'field' => $field,
254 'row' => $row,
255 'nameObject' => $nameObject,
256 'nameForm' => $nameForm,
257 'config' => $config
258 );
259 $html .= GeneralUtility::callUserFunction($customControlConfig, $parameters, $this);
260 }
261 $html .= '</div>';
262 }
263 // Add Drag&Drop functions for sorting to FormEngine::$additionalJS_post
264 if (count($relationList) > 1 && $config['appearance']['useSortable']) {
265 $resultArray['additionalJavaScriptPost'][] = 'inline.createDragAndDropSorting("' . $nameObject . '_records' . '");';
266 }
267 // Publish the uids of the child records in the given order to the browser
268 $html .= '<input type="hidden" name="' . $nameForm . '" value="' . implode(',', $relationList) . '" ' . $this->getValidationDataAsDataAttribute(array('type' => 'inline', 'minitems' => $minItems, 'maxitems' => $maxItems)) . ' class="inlineRecord" />';
269 // Close the wrap for all inline fields (container)
270 $html .= '</div>';
271
272 $resultArray['html'] = $html;
273 return $resultArray;
274 }
275
276 /**
277 * Gets the uids of a select/selector that should be unique and have already been used.
278 *
279 * @param array $records All inline records on this level
280 * @param array $conf The TCA field configuration of the inline field to be rendered
281 * @param bool $splitValue For usage with group/db, values come like "tx_table_123|Title%20abc", but we need "tx_table" and "123
282 * @return array The uids, that have been used already and should be used unique
283 */
284 protected function getUniqueIds($records, $conf = array(), $splitValue = FALSE) {
285 $uniqueIds = array();
286 if (isset($conf['foreign_unique']) && $conf['foreign_unique'] && !empty($records)) {
287 foreach ($records as $rec) {
288 // Skip virtual records (e.g. shown in localization mode):
289 if (!isset($rec['__virtual']) || !$rec['__virtual']) {
290 $value = $rec[$conf['foreign_unique']];
291 // Split the value and extract the table and uid:
292 if ($splitValue) {
293 $valueParts = GeneralUtility::trimExplode('|', $value);
294 $itemParts = explode('_', $valueParts[0]);
295 $value = array(
296 'uid' => array_pop($itemParts),
297 'table' => implode('_', $itemParts)
298 );
299 }
300 $uniqueIds[$rec['uid']] = $value;
301 }
302 }
303 }
304 return $uniqueIds;
305 }
306
307 /**
308 * Get possible records.
309 * Copied from FormEngine and modified.
310 *
311 * @param string $table The table name of the record
312 * @param string $field The field name which this element is supposed to edit
313 * @param array $row The record data array where the value(s) for the field can be found
314 * @param array $conf An array with additional configuration options.
315 * @param string $checkForConfField For which field in the foreign_table the possible records should be fetched
316 * @return mixed Array of possible record items; FALSE if type is "group/db", then everything could be "possible
317 */
318 protected function getPossibleRecords($table, $field, $row, $conf, $checkForConfField = 'foreign_selector') {
319 $backendUser = $this->getBackendUserAuthentication();
320 $languageService = $this->getLanguageService();
321
322 // ctrl configuration from TCA:
323 $tcaTableCtrl = $GLOBALS['TCA'][$table]['ctrl'];
324 // Field configuration from TCA:
325 $foreign_check = $conf[$checkForConfField];
326 $foreignConfig = FormEngineUtility::getInlinePossibleRecordsSelectorConfig($conf, $foreign_check);
327 $PA = $foreignConfig['PA'];
328 $config = $PA['fieldConf']['config'];
329 if ($foreignConfig['type'] == 'select') {
330 // Getting the selector box items from the system
331 $selItems = FormEngineUtility::addSelectOptionsToItemArray(
332 FormEngineUtility::initItemArray($PA['fieldConf']),
333 $PA['fieldConf'],
334 FormEngineUtility::getTSconfigForTableRow($table, $row),
335 $field
336 );
337
338 // Possibly filter some items:
339 $selItems = ArrayUtility::keepItemsInArray(
340 $selItems,
341 $PA['fieldTSConfig']['keepItems'],
342 function ($value) {
343 return $value[1];
344 }
345 );
346
347 // Possibly add some items:
348 $selItems = FormEngineUtility::addItems($selItems, $PA['fieldTSConfig']['addItems.']);
349 if (isset($config['itemsProcFunc']) && $config['itemsProcFunc']) {
350 $dataPreprocessor = GeneralUtility::makeInstance(DataPreprocessor::class);
351 $selItems = $dataPreprocessor->procItems($selItems, $PA['fieldTSConfig']['itemsProcFunc.'], $config, $table, $row, $field);
352 }
353 // Possibly remove some items:
354 $removeItems = GeneralUtility::trimExplode(',', $PA['fieldTSConfig']['removeItems'], TRUE);
355 foreach ($selItems as $tk => $p) {
356 // Checking languages and authMode:
357 $languageDeny = $tcaTableCtrl['languageField'] && (string)$tcaTableCtrl['languageField'] === $field && !$backendUser->checkLanguageAccess($p[1]);
358 $authModeDeny = $config['type'] == 'select' && $config['authMode'] && !$backendUser->checkAuthMode($table, $field, $p[1], $config['authMode']);
359 if (in_array($p[1], $removeItems) || $languageDeny || $authModeDeny) {
360 unset($selItems[$tk]);
361 } else {
362 if (isset($PA['fieldTSConfig']['altLabels.'][$p[1]])) {
363 $selItems[$tk][0] = htmlspecialchars($languageService->sL($PA['fieldTSConfig']['altLabels.'][$p[1]]));
364 }
365 if (isset($PA['fieldTSConfig']['altIcons.'][$p[1]])) {
366 $selItems[$tk][2] = $PA['fieldTSConfig']['altIcons.'][$p[1]];
367 }
368 }
369 // Removing doktypes with no access:
370 if (($table === 'pages' || $table === 'pages_language_overlay') && $field === 'doktype') {
371 if (!($backendUser->isAdmin() || GeneralUtility::inList($backendUser->groupData['pagetypes_select'], $p[1]))) {
372 unset($selItems[$tk]);
373 }
374 }
375 }
376 } else {
377 $selItems = FALSE;
378 }
379 return $selItems;
380 }
381
382 /**
383 * Makes a flat array from the $possibleRecords array.
384 * The key of the flat array is the value of the record,
385 * the value of the flat array is the label of the record.
386 *
387 * @param array $possibleRecords The possibleRecords array (for select fields)
388 * @return mixed A flat array with key=uid, value=label; if $possibleRecords isn't an array, FALSE is returned.
389 */
390 protected function getPossibleRecordsFlat($possibleRecords) {
391 $flat = FALSE;
392 if (is_array($possibleRecords)) {
393 $flat = array();
394 foreach ($possibleRecords as $record) {
395 $flat[$record[1]] = $record[0];
396 }
397 }
398 return $flat;
399 }
400
401 /**
402 * Creates the HTML code of a general link to be used on a level of inline children.
403 * The possible keys for the parameter $type are 'newRecord', 'localize' and 'synchronize'.
404 *
405 * @param string $type The link type, values are 'newRecord', 'localize' and 'synchronize'.
406 * @param string $objectPrefix The "path" to the child record to create (e.g. 'data-parentPageId-partenTable-parentUid-parentField-childTable]')
407 * @param array $conf TCA configuration of the parent(!) field
408 * @return string The HTML code of the new link, wrapped in a div
409 */
410 protected function getLevelInteractionLink($type, $objectPrefix, $conf = array()) {
411 $languageService = $this->getLanguageService();
412 $nameObject = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->globalOptions['inlineFirstPid']);
413 $attributes = array();
414 switch ($type) {
415 case 'newRecord':
416 $title = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:cm.createnew', TRUE);
417 $icon = 'actions-document-new';
418 $className = 'typo3-newRecordLink';
419 $attributes['class'] = 'btn btn-default inlineNewButton ' . $this->inlineData['config'][$nameObject]['md5'];
420 $attributes['onclick'] = 'return inline.createNewRecord(' . GeneralUtility::quoteJSvalue($objectPrefix) . ')';
421 if (!empty($conf['inline']['inlineNewButtonStyle'])) {
422 $attributes['style'] = $conf['inline']['inlineNewButtonStyle'];
423 }
424 if (!empty($conf['appearance']['newRecordLinkAddTitle'])) {
425 $title = sprintf(
426 $languageService->sL('LLL:EXT:lang/locallang_core.xlf:cm.createnew.link', TRUE),
427 $languageService->sL($GLOBALS['TCA'][$conf['foreign_table']]['ctrl']['title'], TRUE)
428 );
429 } elseif (isset($conf['appearance']['newRecordLinkTitle']) && $conf['appearance']['newRecordLinkTitle'] !== '') {
430 $title = $languageService->sL($conf['appearance']['newRecordLinkTitle'], TRUE);
431 }
432 break;
433 case 'localize':
434 $title = $languageService->sL('LLL:EXT:lang/locallang_misc.xlf:localizeAllRecords', 1);
435 $icon = 'actions-document-localize';
436 $className = 'typo3-localizationLink';
437 $attributes['class'] = 'btn btn-default';
438 $attributes['onclick'] = 'return inline.synchronizeLocalizeRecords(' . GeneralUtility::quoteJSvalue($objectPrefix) . ', \'localize\')';
439 break;
440 case 'synchronize':
441 $title = $languageService->sL('LLL:EXT:lang/locallang_misc.xlf:synchronizeWithOriginalLanguage', TRUE);
442 $icon = 'actions-document-synchronize';
443 $className = 'typo3-synchronizationLink';
444 $attributes['class'] = 'btn btn-default inlineNewButton ' . $this->inlineData['config'][$nameObject]['md5'];
445 $attributes['onclick'] = 'return inline.synchronizeLocalizeRecords(' . GeneralUtility::quoteJSvalue($objectPrefix) . ', \'synchronize\')';
446 break;
447 default:
448 $title = '';
449 $icon = '';
450 $className = '';
451 }
452 // Create the link:
453 $icon = $icon ? IconUtility::getSpriteIcon($icon, array('title' => htmlspecialchars($title))) : '';
454 $link = $this->wrapWithAnchor($icon . $title, '#', $attributes);
455 return '<div' . ($className ? ' class="' . $className . '"' : '') . '>' . $link . '</div>';
456 }
457
458 /**
459 * Wraps a text with an anchor and returns the HTML representation.
460 *
461 * @param string $text The text to be wrapped by an anchor
462 * @param string $link The link to be used in the anchor
463 * @param array $attributes Array of attributes to be used in the anchor
464 * @return string The wrapped text as HTML representation
465 */
466 protected function wrapWithAnchor($text, $link, $attributes = array()) {
467 $link = trim($link);
468 $result = '<a href="' . ($link ?: '#') . '"';
469 foreach ($attributes as $key => $value) {
470 $result .= ' ' . $key . '="' . htmlspecialchars(trim($value)) . '"';
471 }
472 $result .= '>' . $text . '</a>';
473 return $result;
474 }
475
476 /**
477 * Get a selector as used for the select type, to select from all available
478 * records and to create a relation to the embedding record (e.g. like MM).
479 *
480 * @param array $selItems Array of all possible records
481 * @param array $conf TCA configuration of the parent(!) field
482 * @param array $uniqueIds The uids that have already been used and should be unique
483 * @return string A HTML <select> box with all possible records
484 */
485 protected function renderPossibleRecordsSelector($selItems, $conf, $uniqueIds = array()) {
486 $foreign_selector = $conf['foreign_selector'];
487 $selConfig = FormEngineUtility::getInlinePossibleRecordsSelectorConfig($conf, $foreign_selector);
488 $item = '';
489 if ($selConfig['type'] === 'select') {
490 $item = $this->renderPossibleRecordsSelectorTypeSelect($selItems, $conf, $selConfig['PA'], $uniqueIds);
491 } elseif ($selConfig['type'] === 'groupdb') {
492 $item = $this->renderPossibleRecordsSelectorTypeGroupDB($conf, $selConfig['PA']);
493 }
494 return $item;
495 }
496
497 /**
498 * Generate a link that opens an element browser in a new window.
499 * For group/db there is no way to use a "selector" like a <select>|</select>-box.
500 *
501 * @param array $conf TCA configuration of the parent(!) field
502 * @param array $PA An array with additional configuration options
503 * @return string A HTML link that opens an element browser in a new window
504 */
505 protected function renderPossibleRecordsSelectorTypeGroupDB($conf, &$PA) {
506 $backendUser = $this->getBackendUserAuthentication();
507
508 $config = $PA['fieldConf']['config'];
509 ArrayUtility::mergeRecursiveWithOverrule($config, $conf);
510 $foreign_table = $config['foreign_table'];
511 $allowed = $config['allowed'];
512 $objectPrefix = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->globalOptions['inlineFirstPid']) . '-' . $foreign_table;
513 $nameObject = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->globalOptions['inlineFirstPid']);
514 $mode = 'db';
515 $showUpload = FALSE;
516 if (!empty($config['appearance']['createNewRelationLinkTitle'])) {
517 $createNewRelationText = $this->getLanguageService()->sL($config['appearance']['createNewRelationLinkTitle'], TRUE);
518 } else {
519 $createNewRelationText = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.createNewRelation', TRUE);
520 }
521 if (is_array($config['appearance'])) {
522 if (isset($config['appearance']['elementBrowserType'])) {
523 $mode = $config['appearance']['elementBrowserType'];
524 }
525 if ($mode === 'file') {
526 $showUpload = TRUE;
527 }
528 if (isset($config['appearance']['fileUploadAllowed'])) {
529 $showUpload = (bool)$config['appearance']['fileUploadAllowed'];
530 }
531 if (isset($config['appearance']['elementBrowserAllowed'])) {
532 $allowed = $config['appearance']['elementBrowserAllowed'];
533 }
534 }
535 $browserParams = '|||' . $allowed . '|' . $objectPrefix . '|inline.checkUniqueElement||inline.importElement';
536 $onClick = 'setFormValueOpenBrowser(' . GeneralUtility::quoteJSvalue($mode) . ', ' . GeneralUtility::quoteJSvalue($browserParams) . '); return false;';
537
538 $buttonStyle = '';
539 if (isset($config['inline']['inlineNewRelationButtonStyle'])) {
540 $buttonStyle = ' style="' . $config['inline']['inlineNewRelationButtonStyle'] . '"';
541 }
542
543 $item = '
544 <a href="#" class="btn btn-default inlineNewRelationButton ' . $this->inlineData['config'][$nameObject]['md5'] . '"
545 ' . $buttonStyle . ' onclick="' . htmlspecialchars($onClick) . '">
546 ' . IconUtility::getSpriteIcon('actions-insert-record', array('title' => $createNewRelationText)) . '
547 ' . $createNewRelationText . '
548 </a>';
549
550 $isDirectFileUploadEnabled = (bool)$this->getBackendUserAuthentication()->uc['edit_docModuleUpload'];
551 if ($showUpload && $isDirectFileUploadEnabled) {
552 $folder = $backendUser->getDefaultUploadFolder();
553 if (
554 $folder instanceof Folder
555 && $folder->checkActionPermission('add')
556 ) {
557 $maxFileSize = GeneralUtility::getMaxUploadFileSize() * 1024;
558 $item .= ' <a href="#" class="btn btn-default t3js-drag-uploader inlineNewFileUploadButton ' . $this->inlineData['config'][$nameObject]['md5'] . '"
559 ' . $buttonStyle . '
560 data-dropzone-target="#' . htmlspecialchars($this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->globalOptions['inlineFirstPid'])) . '"
561 data-insert-dropzone-before="1"
562 data-file-irre-object="' . htmlspecialchars($objectPrefix) . '"
563 data-file-allowed="' . htmlspecialchars($allowed) . '"
564 data-target-folder="' . htmlspecialchars($folder->getCombinedIdentifier()) . '"
565 data-max-file-size="' . htmlspecialchars($maxFileSize) . '"
566 ><span class="t3-icon t3-icon-actions t3-icon-actions-edit t3-icon-edit-upload">&nbsp;</span>';
567 $item .= $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_upload.select-and-submit', TRUE);
568 $item .= '</a>';
569 }
570 }
571
572 $item = '<div class="form-control-wrap">' . $item . '</div>';
573 $allowedList = '';
574 $allowedArray = GeneralUtility::trimExplode(',', $allowed, TRUE);
575 $allowedLabel = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.allowedFileExtensions', TRUE);
576 foreach ($allowedArray as $allowedItem) {
577 $allowedList .= '<span class="label label-success">' . strtoupper($allowedItem) . '</span> ';
578 }
579 if (!empty($allowedList)) {
580 $item .= '<div class="help-block">' . $allowedLabel . '<br>' . $allowedList . '</div>';
581 }
582 $item = '<div class="form-group t3js-formengine-validation-marker">' . $item . '</div>';
583 return $item;
584 }
585
586 /**
587 * Get a selector as used for the select type, to select from all available
588 * records and to create a relation to the embedding record (e.g. like MM).
589 *
590 * @param array $selItems Array of all possible records
591 * @param array $conf TCA configuration of the parent(!) field
592 * @param array $PA An array with additional configuration options
593 * @param array $uniqueIds The uids that have already been used and should be unique
594 * @return string A HTML <select> box with all possible records
595 */
596 protected function renderPossibleRecordsSelectorTypeSelect($selItems, $conf, &$PA, $uniqueIds = array()) {
597 $foreign_table = $conf['foreign_table'];
598 $foreign_selector = $conf['foreign_selector'];
599 $PA = array();
600 $PA['fieldConf'] = $GLOBALS['TCA'][$foreign_table]['columns'][$foreign_selector];
601 $PA['fieldTSConfig'] = FormEngineUtility::getTSconfigForTableRow($foreign_table, array(), $foreign_selector);
602 $config = $PA['fieldConf']['config'];
603 $item = '';
604 // @todo $disabled is not present - should be read from config?
605 $disabled = FALSE;
606 if (!$disabled) {
607 $nameObject = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->globalOptions['inlineFirstPid']);;
608 // Create option tags:
609 $opt = array();
610 $styleAttrValue = '';
611 foreach ($selItems as $p) {
612 if ($config['iconsInOptionTags']) {
613 $styleAttrValue = FormEngineUtility::optionTagStyle($p[2]);
614 }
615 if (!in_array($p[1], $uniqueIds)) {
616 $opt[] = '<option value="' . htmlspecialchars($p[1]) . '"' . ($styleAttrValue ? ' style="' . htmlspecialchars($styleAttrValue) . '"' : '') . '>' . htmlspecialchars($p[0]) . '</option>';
617 }
618 }
619 // Put together the selector box:
620 $itemListStyle = isset($config['itemListStyle']) ? ' style="' . htmlspecialchars($config['itemListStyle']) . '"' : '';
621 $size = (int)$conf['size'];
622 $size = $conf['autoSizeMax'] ? MathUtility::forceIntegerInRange(count($selItems) + 1, MathUtility::forceIntegerInRange($size, 1), $conf['autoSizeMax']) : $size;
623 $onChange = 'return inline.importNewRecord(' . GeneralUtility::quoteJSvalue($nameObject . '-' . $conf['foreign_table']) . ')';
624 $item = '
625 <select id="' . $nameObject . '-' . $conf['foreign_table'] . '_selector" class="form-control"' . ($size ? ' size="' . $size . '"' : '') . ' onchange="' . htmlspecialchars($onChange) . '"' . $PA['onFocus'] . $itemListStyle . ($conf['foreign_unique'] ? ' isunique="isunique"' : '') . '>
626 ' . implode('', $opt) . '
627 </select>';
628
629 if ($size <= 1) {
630 // Add a "Create new relation" link for adding new relations
631 // This is necessary, if the size of the selector is "1" or if
632 // there is only one record item in the select-box, that is selected by default
633 // The selector-box creates a new relation on using a onChange event (see some line above)
634 if (!empty($conf['appearance']['createNewRelationLinkTitle'])) {
635 $createNewRelationText = $this->getLanguageService()->sL($conf['appearance']['createNewRelationLinkTitle'], TRUE);
636 } else {
637 $createNewRelationText = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.createNewRelation', TRUE);
638 }
639 $item .= '
640 <span class="input-group-btn">
641 <a href="#" class="btn btn-default" onclick="' . htmlspecialchars($onChange) . '">
642 ' . IconUtility::getSpriteIcon('actions-document-new', array('title' => $createNewRelationText)) . $createNewRelationText . '
643 </a>
644 </span>';
645 } else {
646 $item .= '
647 <span class="input-group-btn btn"></span>';
648 }
649
650 // Wrap the selector and add a spacer to the bottom
651
652 $item = '<div class="input-group form-group t3js-formengine-validation-marker ' . $this->inlineData['config'][$nameObject]['md5'] . '">' . $item . '</div>';
653 }
654 return $item;
655 }
656
657 /**
658 * @return BackendUserAuthentication
659 */
660 protected function getBackendUserAuthentication() {
661 return $GLOBALS['BE_USER'];
662 }
663
664 /**
665 * @return LanguageService
666 */
667 protected function getLanguageService() {
668 return $GLOBALS['LANG'];
669 }
670
671 }