| Conditions | 46 |
| Paths | > 20000 |
| Total Lines | 261 |
| Code Lines | 163 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 91 | public function render() |
||
| 92 | { |
||
| 93 | $languageService = $this->getLanguageService(); |
||
| 94 | |||
| 95 | $this->inlineData = $this->data['inlineData']; |
||
| 96 | |||
| 97 | /** @var InlineStackProcessor $inlineStackProcessor */ |
||
| 98 | $inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class); |
||
| 99 | $this->inlineStackProcessor = $inlineStackProcessor; |
||
| 100 | $inlineStackProcessor->initializeByGivenStructure($this->data['inlineStructure']); |
||
| 101 | |||
| 102 | $table = $this->data['tableName']; |
||
| 103 | $row = $this->data['databaseRow']; |
||
| 104 | $field = $this->data['fieldName']; |
||
| 105 | $parameterArray = $this->data['parameterArray']; |
||
| 106 | |||
| 107 | $resultArray = $this->initializeResultArray(); |
||
| 108 | |||
| 109 | $config = $parameterArray['fieldConf']['config']; |
||
| 110 | $foreign_table = $config['foreign_table']; |
||
| 111 | |||
| 112 | $language = 0; |
||
| 113 | $languageFieldName = $GLOBALS['TCA'][$table]['ctrl']['languageField']; |
||
| 114 | if (BackendUtility::isTableLocalizable($table)) { |
||
| 115 | $language = isset($row[$languageFieldName][0]) ? (int)$row[$languageFieldName][0] : (int)$row[$languageFieldName]; |
||
| 116 | } |
||
| 117 | |||
| 118 | // Add the current inline job to the structure stack |
||
| 119 | $newStructureItem = [ |
||
| 120 | 'table' => $table, |
||
| 121 | 'uid' => $row['uid'], |
||
| 122 | 'field' => $field, |
||
| 123 | 'config' => $config, |
||
| 124 | ]; |
||
| 125 | // Extract FlexForm parts (if any) from element name, e.g. array('vDEF', 'lDEF', 'FlexField', 'vDEF') |
||
| 126 | if (!empty($parameterArray['itemFormElName'])) { |
||
| 127 | $flexFormParts = $this->extractFlexFormParts($parameterArray['itemFormElName']); |
||
| 128 | if ($flexFormParts !== null) { |
||
| 129 | $newStructureItem['flexform'] = $flexFormParts; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | $inlineStackProcessor->pushStableStructureItem($newStructureItem); |
||
| 133 | |||
| 134 | // Transport the flexform DS identifier fields to the FormInlineAjaxController |
||
| 135 | if (!empty($newStructureItem['flexform']) |
||
| 136 | && isset($this->data['processedTca']['columns'][$field]['config']['dataStructureIdentifier']) |
||
| 137 | ) { |
||
| 138 | $config['dataStructureIdentifier'] = $this->data['processedTca']['columns'][$field]['config']['dataStructureIdentifier']; |
||
| 139 | } |
||
| 140 | |||
| 141 | // Hand over original returnUrl to FormInlineAjaxController. Needed if opening for instance a |
||
| 142 | // nested element in a new view to then go back to the original returnUrl and not the url of |
||
| 143 | // the inline ajax controller |
||
| 144 | $config['originalReturnUrl'] = $this->data['returnUrl']; |
||
| 145 | |||
| 146 | // e.g. data[<table>][<uid>][<field>] |
||
| 147 | $nameForm = $inlineStackProcessor->getCurrentStructureFormPrefix(); |
||
| 148 | // e.g. data-<pid>-<table1>-<uid1>-<field1>-<table2>-<uid2>-<field2> |
||
| 149 | $nameObject = $inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->data['inlineFirstPid']); |
||
| 150 | |||
| 151 | $config['inline']['first'] = false; |
||
| 152 | $firstChild = reset($this->data['parameterArray']['fieldConf']['children']); |
||
| 153 | if (isset($firstChild['databaseRow']['uid'])) { |
||
| 154 | $config['inline']['first'] = $firstChild['databaseRow']['uid']; |
||
| 155 | } |
||
| 156 | $config['inline']['last'] = false; |
||
| 157 | $lastChild = end($this->data['parameterArray']['fieldConf']['children']); |
||
| 158 | if (isset($lastChild['databaseRow']['uid'])) { |
||
| 159 | $config['inline']['last'] = $lastChild['databaseRow']['uid']; |
||
| 160 | } |
||
| 161 | |||
| 162 | $top = $inlineStackProcessor->getStructureLevel(0); |
||
| 163 | |||
| 164 | $this->inlineData['config'][$nameObject] = [ |
||
| 165 | 'table' => $foreign_table, |
||
| 166 | 'md5' => md5($nameObject) |
||
| 167 | ]; |
||
| 168 | $this->inlineData['config'][$nameObject . '-' . $foreign_table] = [ |
||
| 169 | 'min' => $config['minitems'], |
||
| 170 | 'max' => $config['maxitems'], |
||
| 171 | 'sortable' => $config['appearance']['useSortable'], |
||
| 172 | 'top' => [ |
||
| 173 | 'table' => $top['table'], |
||
| 174 | 'uid' => $top['uid'] |
||
| 175 | ], |
||
| 176 | 'context' => [ |
||
| 177 | 'config' => $config, |
||
| 178 | 'hmac' => GeneralUtility::hmac(json_encode($config), 'InlineContext'), |
||
| 179 | ], |
||
| 180 | ]; |
||
| 181 | $this->inlineData['nested'][$nameObject] = $this->data['tabAndInlineStack']; |
||
| 182 | |||
| 183 | $uniqueMax = 0; |
||
| 184 | $uniqueIds = []; |
||
| 185 | |||
| 186 | if ($config['foreign_unique']) { |
||
| 187 | // Add inlineData['unique'] with JS unique configuration |
||
| 188 | $type = $config['selectorOrUniqueConfiguration']['config']['type'] === 'select' ? 'select' : 'groupdb'; |
||
| 189 | foreach ($parameterArray['fieldConf']['children'] as $child) { |
||
| 190 | // Determine used unique ids, skip not localized records |
||
| 191 | if (!$child['isInlineDefaultLanguageRecordInLocalizedParentContext']) { |
||
| 192 | $value = $child['databaseRow'][$config['foreign_unique']]; |
||
| 193 | // We're assuming there is only one connected value here for both select and group |
||
| 194 | if ($type === 'select') { |
||
| 195 | // A resolved select field is an array - take first value |
||
| 196 | $value = $value['0']; |
||
| 197 | } else { |
||
| 198 | // A group field is still a list with pipe separated uid|tableName |
||
| 199 | $valueParts = GeneralUtility::trimExplode('|', $value); |
||
| 200 | $itemParts = explode('_', $valueParts[0]); |
||
| 201 | $value = [ |
||
| 202 | 'uid' => array_pop($itemParts), |
||
| 203 | 'table' => implode('_', $itemParts) |
||
| 204 | ]; |
||
| 205 | } |
||
| 206 | // @todo: This is weird, $value has different structure for group and select fields? |
||
| 207 | $uniqueIds[$child['databaseRow']['uid']] = $value; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | $possibleRecords = $config['selectorOrUniquePossibleRecords']; |
||
| 211 | $possibleRecordsUidToTitle = []; |
||
| 212 | foreach ($possibleRecords as $possibleRecord) { |
||
| 213 | $possibleRecordsUidToTitle[$possibleRecord[1]] = $possibleRecord[0]; |
||
| 214 | } |
||
| 215 | $uniqueMax = $config['appearance']['useCombination'] || empty($possibleRecords) ? -1 : count($possibleRecords); |
||
| 216 | $this->inlineData['unique'][$nameObject . '-' . $foreign_table] = [ |
||
| 217 | 'max' => $uniqueMax, |
||
| 218 | 'used' => $uniqueIds, |
||
| 219 | 'type' => $type, |
||
| 220 | 'table' => $foreign_table, |
||
| 221 | 'elTable' => $config['selectorOrUniqueConfiguration']['foreignTable'], |
||
| 222 | 'field' => $config['foreign_unique'], |
||
| 223 | 'selector' => $config['selectorOrUniqueConfiguration']['isSelector'] ? $type : false, |
||
| 224 | 'possible' => $possibleRecordsUidToTitle, |
||
| 225 | ]; |
||
| 226 | } |
||
| 227 | |||
| 228 | $resultArray['inlineData'] = $this->inlineData; |
||
| 229 | |||
| 230 | // @todo: It might be a good idea to have something like "isLocalizedRecord" or similar set by a data provider |
||
| 231 | $uidOfDefaultRecord = $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']]; |
||
| 232 | $isLocalizedParent = $language > 0 |
||
| 233 | && ($uidOfDefaultRecord[0] ?? $uidOfDefaultRecord) > 0 |
||
| 234 | && MathUtility::canBeInterpretedAsInteger($row['uid']); |
||
| 235 | $numberOfFullLocalizedChildren = 0; |
||
| 236 | $numberOfNotYetLocalizedChildren = 0; |
||
| 237 | foreach ($this->data['parameterArray']['fieldConf']['children'] as $child) { |
||
| 238 | if (!$child['isInlineDefaultLanguageRecordInLocalizedParentContext']) { |
||
| 239 | $numberOfFullLocalizedChildren ++; |
||
| 240 | } |
||
| 241 | if ($isLocalizedParent && $child['isInlineDefaultLanguageRecordInLocalizedParentContext']) { |
||
| 242 | $numberOfNotYetLocalizedChildren ++; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | // Render the localization links if needed |
||
| 247 | $localizationLinks = ''; |
||
| 248 | if ($numberOfNotYetLocalizedChildren) { |
||
| 249 | // Add the "Localize all records" link before all child records: |
||
| 250 | if (isset($config['appearance']['showAllLocalizationLink']) && $config['appearance']['showAllLocalizationLink']) { |
||
| 251 | $localizationLinks = ' ' . $this->getLevelInteractionLink('localize', $nameObject . '-' . $foreign_table, $config); |
||
| 252 | } |
||
| 253 | // Add the "Synchronize with default language" link before all child records: |
||
| 254 | if (isset($config['appearance']['showSynchronizationLink']) && $config['appearance']['showSynchronizationLink']) { |
||
| 255 | $localizationLinks .= ' ' . $this->getLevelInteractionLink('synchronize', $nameObject . '-' . $foreign_table, $config); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | // Define how to show the "Create new record" link - if there are more than maxitems, hide it |
||
| 260 | if ($numberOfFullLocalizedChildren >= $config['maxitems'] || $uniqueMax > 0 && $numberOfFullLocalizedChildren >= $uniqueMax) { |
||
| 261 | $config['inline']['inlineNewButtonStyle'] = 'display: none;'; |
||
| 262 | $config['inline']['inlineNewRelationButtonStyle'] = 'display: none;'; |
||
| 263 | $config['inline']['inlineOnlineMediaAddButtonStyle'] = 'display: none;'; |
||
| 264 | } |
||
| 265 | |||
| 266 | // Render the level links (create new record): |
||
| 267 | $levelLinks = ''; |
||
| 268 | if (!empty($config['appearance']['enabledControls']['new'])) { |
||
| 269 | $levelLinks = $this->getLevelInteractionLink('newRecord', $nameObject . '-' . $foreign_table, $config); |
||
| 270 | } |
||
| 271 | // Wrap all inline fields of a record with a <div> (like a container) |
||
| 272 | $html = '<div class="form-group" id="' . $nameObject . '">'; |
||
| 273 | // Add the level links before all child records: |
||
| 274 | if ($config['appearance']['levelLinksPosition'] === 'both' || $config['appearance']['levelLinksPosition'] === 'top') { |
||
| 275 | $html .= '<div class="form-group t3js-formengine-validation-marker">' . $levelLinks . $localizationLinks . '</div>'; |
||
| 276 | } |
||
| 277 | |||
| 278 | // If it's required to select from possible child records (reusable children), add a selector box |
||
| 279 | if ($config['foreign_selector'] && $config['appearance']['showPossibleRecordsSelector'] !== false) { |
||
| 280 | if ($config['selectorOrUniqueConfiguration']['config']['type'] === 'select') { |
||
| 281 | $selectorBox = $this->renderPossibleRecordsSelectorTypeSelect($config, $uniqueIds); |
||
| 282 | } else { |
||
| 283 | $selectorBox = $this->renderPossibleRecordsSelectorTypeGroupDB($config); |
||
| 284 | } |
||
| 285 | $html .= $selectorBox . $localizationLinks; |
||
| 286 | } |
||
| 287 | |||
| 288 | $title = $languageService->sL(trim($parameterArray['fieldConf']['label'])); |
||
| 289 | $html .= '<div class="panel-group panel-hover" data-title="' . htmlspecialchars($title) . '" id="' . $nameObject . '_records">'; |
||
| 290 | |||
| 291 | $sortableRecordUids = []; |
||
| 292 | foreach ($this->data['parameterArray']['fieldConf']['children'] as $options) { |
||
| 293 | $options['inlineParentUid'] = $row['uid']; |
||
| 294 | $options['inlineFirstPid'] = $this->data['inlineFirstPid']; |
||
| 295 | // @todo: this can be removed if this container no longer sets additional info to $config |
||
| 296 | $options['inlineParentConfig'] = $config; |
||
| 297 | $options['inlineData'] = $this->inlineData; |
||
| 298 | $options['inlineStructure'] = $inlineStackProcessor->getStructure(); |
||
| 299 | $options['inlineExpandCollapseStateArray'] = $this->data['inlineExpandCollapseStateArray']; |
||
| 300 | $options['renderType'] = 'inlineRecordContainer'; |
||
| 301 | $childResult = $this->nodeFactory->create($options)->render(); |
||
| 302 | $html .= $childResult['html']; |
||
| 303 | $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childResult, false); |
||
| 304 | if (!$options['isInlineDefaultLanguageRecordInLocalizedParentContext']) { |
||
| 305 | // Don't add record to list of "valid" uids if it is only the default |
||
| 306 | // language record of a not yet localized child |
||
| 307 | $sortableRecordUids[] = $options['databaseRow']['uid']; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | $html .= '</div>'; |
||
| 312 | |||
| 313 | $fieldWizardResult = $this->renderFieldWizard(); |
||
| 314 | $fieldWizardHtml = $fieldWizardResult['html']; |
||
| 315 | $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false); |
||
| 316 | $html .= $fieldWizardHtml; |
||
| 317 | |||
| 318 | // Add the level links after all child records: |
||
| 319 | if ($config['appearance']['levelLinksPosition'] === 'both' || $config['appearance']['levelLinksPosition'] === 'bottom') { |
||
| 320 | $html .= $levelLinks . $localizationLinks; |
||
| 321 | } |
||
| 322 | if (is_array($config['customControls'])) { |
||
| 323 | $html .= '<div id="' . $nameObject . '_customControls">'; |
||
| 324 | foreach ($config['customControls'] as $customControlConfig) { |
||
| 325 | $parameters = [ |
||
| 326 | 'table' => $table, |
||
| 327 | 'field' => $field, |
||
| 328 | 'row' => $row, |
||
| 329 | 'nameObject' => $nameObject, |
||
| 330 | 'nameForm' => $nameForm, |
||
| 331 | 'config' => $config |
||
| 332 | ]; |
||
| 333 | $html .= GeneralUtility::callUserFunction($customControlConfig, $parameters, $this); |
||
| 334 | } |
||
| 335 | $html .= '</div>'; |
||
| 336 | } |
||
| 337 | // Add Drag&Drop functions for sorting to FormEngine::$additionalJS_post |
||
| 338 | if (count($sortableRecordUids) > 1 && $config['appearance']['useSortable']) { |
||
| 339 | $resultArray['additionalJavaScriptPost'][] = 'inline.createDragAndDropSorting("' . $nameObject . '_records' . '");'; |
||
| 340 | } |
||
| 341 | $resultArray['requireJsModules'] = array_merge($resultArray['requireJsModules'], $this->requireJsModules); |
||
| 342 | |||
| 343 | // Publish the uids of the child records in the given order to the browser |
||
| 344 | $html .= '<input type="hidden" name="' . $nameForm . '" value="' . implode(',', $sortableRecordUids) . '" ' |
||
| 345 | . ' data-formengine-validation-rules="' . htmlspecialchars($this->getValidationDataAsJsonString(['type' => 'inline', 'minitems' => $config['minitems'], 'maxitems' => $config['maxitems']])) . '"' |
||
| 346 | . ' class="inlineRecord" />'; |
||
| 347 | // Close the wrap for all inline fields (container) |
||
| 348 | $html .= '</div>'; |
||
| 349 | |||
| 350 | $resultArray['html'] = $html; |
||
| 351 | return $resultArray; |
||
| 352 | } |
||
| 650 |