[TASK] Move LanguageService to EXT:core
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / TcaInlineExpandCollapseState.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
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\FormDataProviderInterface;
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19
20 /**
21 * Fetch information of user specific inline record expanded / collapsed state
22 * from user->uc and put it into $result['inlineExpandCollapseStateArray']
23 */
24 class TcaInlineExpandCollapseState implements FormDataProviderInterface
25 {
26 /**
27 * Add inline expand / collapse state
28 *
29 * @param array $result
30 * @return array
31 */
32 public function addData(array $result)
33 {
34 if (empty($result['inlineExpandCollapseStateArray'])) {
35 if (!empty($result['inlineTopMostParentUid']) && !empty($result['inlineTopMostParentTableName'])) {
36 // Happens in inline ajax context, top parent uid and top parent table are set
37 $fullInlineState = unserialize($this->getBackendUser()->uc['inlineView']);
38 if (!is_array($fullInlineState)) {
39 $fullInlineState = [];
40 }
41 $inlineStateForTable = [];
42 if ($result['command'] !== 'new') {
43 $table = $result['inlineTopMostParentTableName'];
44 $uid = $result['inlineTopMostParentUid'];
45 if (!empty($fullInlineState[$table][$uid])) {
46 $inlineStateForTable = $fullInlineState[$table][$uid];
47 }
48 }
49 $result['inlineExpandCollapseStateArray'] = $inlineStateForTable;
50 } else {
51 // Default case for a single record
52 $fullInlineState = unserialize($this->getBackendUser()->uc['inlineView']);
53 if (!is_array($fullInlineState)) {
54 $fullInlineState = [];
55 }
56 $inlineStateForTable = [];
57 if ($result['command'] !== 'new') {
58 $table = $result['tableName'];
59 $uid = $result['databaseRow']['uid'];
60 if (!empty($fullInlineState[$table][$uid])) {
61 $inlineStateForTable = $fullInlineState[$table][$uid];
62 }
63 }
64 $result['inlineExpandCollapseStateArray'] = $inlineStateForTable;
65 }
66 }
67
68 if (!$result['isInlineChildExpanded']) {
69 // If the record is an inline child that is not expanded, it is not necessary to calculate all fields
70 $isExistingRecord = $result['command'] === 'edit';
71 $inlineConfig = $result['inlineParentConfig'];
72 $collapseAll = isset($inlineConfig['appearance']['collapseAll']) && $inlineConfig['appearance']['collapseAll'];
73 $expandAll = isset($inlineConfig['appearance']['collapseAll']) && !$inlineConfig['appearance']['collapseAll'];
74 $expandCollapseStateArray = $result['inlineExpandCollapseStateArray'];
75 $foreignTable = $result['inlineParentConfig']['foreign_table'];
76 $isExpandedByUcState = isset($expandCollapseStateArray[$foreignTable])
77 && is_array($expandCollapseStateArray[$foreignTable])
78 && in_array($result['databaseRow']['uid'], $expandCollapseStateArray[$foreignTable]) !== false;
79
80 if (!$isExistingRecord || ($isExpandedByUcState && !$collapseAll) || $expandAll || $result['isInlineAjaxOpeningContext']) {
81 $result['isInlineChildExpanded'] = true;
82 }
83 }
84
85 return $result;
86 }
87
88 /**
89 * @return BackendUserAuthentication
90 */
91 protected function getBackendUser()
92 {
93 return $GLOBALS['BE_USER'];
94 }
95 }