Passed
Push — master ( fb6744...da9c6d )
by
unknown
17:04
created

SiteLanguageContainer::render()   C

Complexity

Conditions 9
Paths 54

Size

Total Lines 146
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 99
c 1
b 0
f 0
dl 0
loc 146
rs 6.4662
cc 9
nc 54
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Backend\Form\Container;
19
20
use TYPO3\CMS\Backend\Form\InlineStackProcessor;
21
use TYPO3\CMS\Backend\Form\NodeFactory;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Fluid\View\StandaloneView;
24
25
/**
26
 * Site languages entry container
27
 *
28
 * @internal This container is only used in the site configuration module and is not public API
29
 */
30
class SiteLanguageContainer extends AbstractContainer
31
{
32
    private const FOREIGN_TABLE = 'site_language';
33
    private const FOREIGN_FIELD = 'languageId';
34
35
    protected array $inlineData;
36
    protected InlineStackProcessor $inlineStackProcessor;
37
38
    /**
39
     * Default field information enabled for this element.
40
     *
41
     * @var array
42
     */
43
    protected $defaultFieldInformation = [
44
        'tcaDescription' => [
45
            'renderType' => 'tcaDescription',
46
        ],
47
    ];
48
49
    /**
50
     * Container objects give $nodeFactory down to other containers.
51
     *
52
     * @param NodeFactory $nodeFactory
53
     * @param array $data
54
     */
55
    public function __construct(NodeFactory $nodeFactory, array $data)
56
    {
57
        parent::__construct($nodeFactory, $data);
58
        $this->inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class);
59
    }
60
61
    public function render(): array
62
    {
63
        $this->inlineData = $this->data['inlineData'];
64
65
        $this->inlineStackProcessor->initializeByGivenStructure($this->data['inlineStructure']);
66
67
        $row = $this->data['databaseRow'];
68
        $parameterArray = $this->data['parameterArray'];
69
        $config = $parameterArray['fieldConf']['config'];
70
        $resultArray = $this->initializeResultArray();
71
72
        // Add the current inline job to the structure stack
73
        $this->inlineStackProcessor->pushStableStructureItem([
74
            'table' => $this->data['tableName'],
75
            'uid' => $row['uid'],
76
            'field' => $this->data['fieldName'],
77
            'config' => $config,
78
        ]);
79
80
        // Hand over original returnUrl to SiteInlineAjaxController. Needed if opening for instance a
81
        // nested element in a new view to then go back to the original returnUrl and not the url of
82
        // the site inline ajax controller.
83
        $config['originalReturnUrl'] = $this->data['returnUrl'];
84
85
        // e.g. data[site][1][languages]
86
        $nameForm = $this->inlineStackProcessor->getCurrentStructureFormPrefix();
87
        // e.g. data-0-site-1-languages
88
        $nameObject = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->data['inlineFirstPid']);
89
        // e.g. array('table' => 'site', 'uid' => '1', 'field' => 'languages', 'config' => array())
90
        $top = $this->inlineStackProcessor->getStructureLevel(0);
91
92
        $this->inlineData['config'][$nameObject] = [
93
            'table' => self::FOREIGN_TABLE
94
        ];
95
96
        $configJson = (string)json_encode($config);
97
        $this->inlineData['config'][$nameObject . '-' . self::FOREIGN_TABLE] = [
98
            'min' => $config['minitems'],
99
            'max' => $config['maxitems'],
100
            'sortable' => false,
101
            'top' => [
102
                'table' => $top['table'],
103
                'uid' => $top['uid']
104
            ],
105
            'context' => [
106
                'config' => $configJson,
107
                'hmac' => GeneralUtility::hmac($configJson, 'InlineContext'),
108
            ],
109
        ];
110
        $this->inlineData['nested'][$nameObject] = $this->data['tabAndInlineStack'];
111
112
        $uniqueIds = [];
113
        foreach ($parameterArray['fieldConf']['children'] as $children) {
114
            $value = (int)($children['databaseRow'][self::FOREIGN_FIELD]['0'] ?? 0);
115
            if (isset($children['databaseRow']['uid'])) {
116
                $uniqueIds[$children['databaseRow']['uid']] = $value;
117
            }
118
        }
119
120
        $uniquePossibleRecords = $config['uniquePossibleRecords'] ?? [];
121
        $possibleRecordsUidToTitle = [];
122
        foreach ($uniquePossibleRecords as $possibleRecord) {
123
            $possibleRecordsUidToTitle[$possibleRecord[1]] = $possibleRecord[0];
124
        }
125
        $this->inlineData['unique'][$nameObject . '-' . self::FOREIGN_TABLE] = [
126
            // Usually "max" would the the number of possible records. However, since
127
            // we also allow new languages to be created, we just use the maxitems value.
128
            'max' => $config['maxitems'],
129
            // "used" must be a string array
130
            'used' => array_map('strval', $uniqueIds),
131
            'table' => self::FOREIGN_TABLE,
132
            'elTable' => self::FOREIGN_TABLE,
133
            'field' => self::FOREIGN_FIELD,
134
            'possible' => $possibleRecordsUidToTitle,
135
        ];
136
137
        $resultArray['inlineData'] = $this->inlineData;
138
139
        $fieldInformationResult = $this->renderFieldInformation();
140
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
141
        $selectorOptions = $childRecordUids = $childHtml = [];
142
143
        foreach ($config['uniquePossibleRecords'] ?? [] as $record) {
144
            // Do not add the PHP_INT_MAX placeholder or already configured languages
145
            if ($record[1] !== PHP_INT_MAX && !in_array($record[1], $uniqueIds, true)) {
146
                $selectorOptions[] = ['value' => (string)$record[1], 'label' => (string)$record[0]];
147
            }
148
        }
149
150
        foreach ($this->data['parameterArray']['fieldConf']['children'] as $children) {
151
            $children['inlineParentUid'] = $row['uid'];
152
            $children['inlineFirstPid'] = $this->data['inlineFirstPid'];
153
            $children['inlineParentConfig'] = $config;
154
            $children['inlineData'] = $this->inlineData;
155
            $children['inlineStructure'] = $this->inlineStackProcessor->getStructure();
156
            $children['inlineExpandCollapseStateArray'] = $this->data['inlineExpandCollapseStateArray'];
157
            $children['renderType'] = 'inlineRecordContainer';
158
            $childResult = $this->nodeFactory->create($children)->render();
159
            $childHtml[] = $childResult['html'];
160
            $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childResult, false);
161
            if (isset($children['databaseRow']['uid'])) {
162
                $childRecordUids[] = $children['databaseRow']['uid'];
163
            }
164
        }
165
166
        $view = GeneralUtility::makeInstance(StandaloneView::class);
167
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
168
            'EXT:backend/Resources/Private/Templates/SiteLanguageContainer.html'
169
        ));
170
        $view->assignMultiple([
171
            'nameObject' => $nameObject,
172
            'nameForm' => $nameForm,
173
            'formGroupAttributes' => GeneralUtility::implodeAttributes([
174
                'class' => 'form-group',
175
                'id' => $nameObject,
176
                'data-uid' => (string)$row['uid'],
177
                'data-local-table' => (string)$top['table'],
178
                'data-local-field' => (string)$top['field'],
179
                'data-foreign-table' => self::FOREIGN_TABLE,
180
                'data-object-group' => $nameObject . '-' . self::FOREIGN_TABLE,
181
                'data-form-field' => $nameForm,
182
                'data-appearance' => (string)json_encode($config['appearance'] ?? ''),
183
            ], true),
184
            'fieldInformation' => $fieldInformationResult['html'],
185
            'selectorConfigutation' => [
186
                'identifier' => $nameObject . '-' . self::FOREIGN_TABLE . '_selector',
187
                'size' => $config['size'] ?? 4,
188
                'options' =>  $selectorOptions
189
            ],
190
            'inlineRecords' => [
191
                'identifier' => $nameObject . '_records',
192
                'title' => trim($parameterArray['fieldConf']['label'] ?? ''),
193
                'records' => implode(PHP_EOL, $childHtml)
194
            ],
195
            'childRecordUids' => implode(',', $childRecordUids),
196
            'validationRules' => $this->getValidationDataAsJsonString([
197
                'type' => 'inline',
198
                'minitems' => $config['minitems'] ?? null,
199
                'maxitems' => $config['maxitems'] ?? null,
200
            ]),
201
        ]);
202
203
        $resultArray['html'] = $view->render();
204
        $resultArray['requireJsModules'][] = 'TYPO3/CMS/Backend/FormEngine/Container/SiteLanguageContainer';
205
206
        return $resultArray;
207
    }
208
}
209