Total Complexity | 124 |
Total Lines | 614 |
Duplicated Lines | 2.61 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like InlineRecordContainer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InlineRecordContainer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class InlineRecordContainer extends AbstractContainer |
||
45 | { |
||
46 | /** |
||
47 | * Inline data array used for JSON output |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $inlineData = []; |
||
52 | |||
53 | /** |
||
54 | * @var InlineStackProcessor |
||
55 | */ |
||
56 | protected $inlineStackProcessor; |
||
57 | |||
58 | /** |
||
59 | * Array containing instances of hook classes called once for IRRE objects |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $hookObjects = []; |
||
64 | |||
65 | /** |
||
66 | * @var IconFactory |
||
67 | */ |
||
68 | protected $iconFactory; |
||
69 | |||
70 | /** |
||
71 | * Default constructor |
||
72 | * |
||
73 | * @param NodeFactory $nodeFactory |
||
74 | * @param array $data |
||
75 | */ |
||
76 | public function __construct(NodeFactory $nodeFactory, array $data) |
||
77 | { |
||
78 | parent::__construct($nodeFactory, $data); |
||
79 | $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class); |
||
80 | $this->inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class); |
||
81 | $this->initHookObjects(); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Entry method |
||
86 | * |
||
87 | * @return array As defined in initializeResultArray() of AbstractNode |
||
88 | * @throws AccessDeniedContentEditException |
||
89 | */ |
||
90 | public function render() |
||
91 | { |
||
92 | $data = $this->data; |
||
93 | $this->inlineData = $data['inlineData']; |
||
94 | |||
95 | $inlineStackProcessor = $this->inlineStackProcessor; |
||
96 | $inlineStackProcessor->initializeByGivenStructure($data['inlineStructure']); |
||
97 | |||
98 | $record = $data['databaseRow']; |
||
99 | $inlineConfig = $data['inlineParentConfig']; |
||
100 | $foreignTable = $inlineConfig['foreign_table']; |
||
101 | |||
102 | $resultArray = $this->initializeResultArray(); |
||
103 | |||
104 | // Send a mapping information to the browser via JSON: |
||
105 | // e.g. data[<curTable>][<curId>][<curField>] => data-<pid>-<parentTable>-<parentId>-<parentField>-<curTable>-<curId>-<curField> |
||
106 | $formPrefix = $inlineStackProcessor->getCurrentStructureFormPrefix(); |
||
107 | $domObjectId = $inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($data['inlineFirstPid']); |
||
108 | $this->inlineData['map'][$formPrefix] = $domObjectId; |
||
109 | |||
110 | $resultArray['inlineData'] = $this->inlineData; |
||
111 | |||
112 | // If there is a selector field, normalize it: |
||
113 | if (!empty($inlineConfig['foreign_selector'])) { |
||
114 | $foreign_selector = $inlineConfig['foreign_selector']; |
||
115 | $valueToNormalize = $record[$foreign_selector]; |
||
116 | if (is_array($record[$foreign_selector])) { |
||
117 | // @todo: this can be kicked again if always prepared rows are handled here |
||
118 | $valueToNormalize = implode(',', $record[$foreign_selector]); |
||
119 | } |
||
120 | $record[$foreign_selector] = $this->normalizeUid($valueToNormalize); |
||
121 | } |
||
122 | |||
123 | // Get the current naming scheme for DOM name/id attributes: |
||
124 | $appendFormFieldNames = '[' . $foreignTable . '][' . $record['uid'] . ']'; |
||
125 | $objectId = $domObjectId . '-' . $foreignTable . '-' . $record['uid']; |
||
126 | $class = ''; |
||
127 | $html = ''; |
||
128 | $combinationHtml = ''; |
||
129 | $isNewRecord = $data['command'] === 'new'; |
||
130 | $hiddenField = ''; |
||
131 | if (isset($data['processedTca']['ctrl']['enablecolumns']['disabled'])) { |
||
132 | $hiddenField = $data['processedTca']['ctrl']['enablecolumns']['disabled']; |
||
133 | } |
||
134 | if (!$data['isInlineDefaultLanguageRecordInLocalizedParentContext']) { |
||
135 | if ($isNewRecord || $data['isInlineChildExpanded']) { |
||
136 | // Render full content ONLY IF this is an AJAX request, a new record, or the record is not collapsed |
||
137 | $combinationHtml = ''; |
||
138 | if (isset($data['combinationChild'])) { |
||
139 | $combinationChild = $this->renderCombinationChild($data, $appendFormFieldNames); |
||
140 | $combinationHtml = $combinationChild['html']; |
||
141 | $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $combinationChild, false); |
||
142 | } |
||
143 | $childArray = $this->renderChild($data); |
||
144 | $html = $childArray['html']; |
||
145 | $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childArray, false); |
||
146 | } else { |
||
147 | // This string is the marker for the JS-function to check if the full content has already been loaded |
||
148 | $html = '<!--notloaded-->'; |
||
149 | } |
||
150 | if ($isNewRecord) { |
||
151 | // Add pid of record as hidden field |
||
152 | $html .= '<input type="hidden" name="data' . htmlspecialchars($appendFormFieldNames) |
||
153 | . '[pid]" value="' . htmlspecialchars($record['pid']) . '"/>'; |
||
154 | // Tell DataHandler this record is expanded |
||
155 | $ucFieldName = 'uc[inlineView]' |
||
156 | . '[' . $data['inlineTopMostParentTableName'] . ']' |
||
157 | . '[' . $data['inlineTopMostParentUid'] . ']' |
||
158 | . $appendFormFieldNames; |
||
159 | $html .= '<input type="hidden" name="' . htmlspecialchars($ucFieldName) |
||
160 | . '" value="' . (int)$data['isInlineChildExpanded'] . '" />'; |
||
161 | } else { |
||
162 | // Set additional field for processing for saving |
||
163 | $html .= '<input type="hidden" name="cmd' . htmlspecialchars($appendFormFieldNames) |
||
164 | . '[delete]" value="1" disabled="disabled" />'; |
||
165 | if (!$data['isInlineChildExpanded'] && !empty($hiddenField)) { |
||
166 | $checked = !empty($record[$hiddenField]) ? ' checked="checked"' : ''; |
||
167 | $html .= '<input type="checkbox" data-formengine-input-name="data' |
||
168 | . htmlspecialchars($appendFormFieldNames) |
||
169 | . '[' . htmlspecialchars($hiddenField) . ']" value="1"' . $checked . ' />'; |
||
170 | $html .= '<input type="input" name="data' . htmlspecialchars($appendFormFieldNames) |
||
171 | . '[' . htmlspecialchars($hiddenField) . ']" value="' . htmlspecialchars($record[$hiddenField]) . '" />'; |
||
172 | } |
||
173 | } |
||
174 | // If this record should be shown collapsed |
||
175 | $class = $data['isInlineChildExpanded'] ? 'panel-visible' : 'panel-collapsed'; |
||
176 | } |
||
177 | if ($inlineConfig['renderFieldsOnly']) { |
||
178 | // Render "body" part only |
||
179 | $html = $html . $combinationHtml; |
||
180 | } else { |
||
181 | // Render header row and content (if expanded) |
||
182 | if ($data['isInlineDefaultLanguageRecordInLocalizedParentContext']) { |
||
183 | $class .= ' t3-form-field-container-inline-placeHolder'; |
||
184 | } |
||
185 | if (!empty($hiddenField) && isset($record[$hiddenField]) && (int)$record[$hiddenField]) { |
||
186 | $class .= ' t3-form-field-container-inline-hidden'; |
||
187 | } |
||
188 | $class .= ($isNewRecord ? ' inlineIsNewRecord' : ''); |
||
189 | $html = ' |
||
190 | <div class="panel panel-default panel-condensed ' . trim($class) . '" id="' . htmlspecialchars($objectId) . '_div"> |
||
191 | <div class="panel-heading" data-toggle="formengine-inline" id="' . htmlspecialchars($objectId) . '_header" data-expandSingle="' . ($inlineConfig['appearance']['expandSingle'] ? 1 : 0) . '"> |
||
192 | <div class="form-irre-header"> |
||
193 | <div class="form-irre-header-cell form-irre-header-icon"> |
||
194 | <span class="caret"></span> |
||
195 | </div> |
||
196 | ' . $this->renderForeignRecordHeader($data) . ' |
||
197 | </div> |
||
198 | </div> |
||
199 | <div class="panel-collapse" id="' . htmlspecialchars($objectId) . '_fields">' . $html . $combinationHtml . '</div> |
||
200 | </div>'; |
||
201 | } |
||
202 | |||
203 | $resultArray['html'] = $html; |
||
204 | return $resultArray; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Render inner child |
||
209 | * |
||
210 | * @param array $data |
||
211 | * @return array Result array |
||
212 | */ |
||
213 | protected function renderChild(array $data) |
||
214 | { |
||
215 | $domObjectId = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($data['inlineFirstPid']); |
||
216 | $data['tabAndInlineStack'][] = [ |
||
217 | 'inline', |
||
218 | $domObjectId . '-' . $data['tableName'] . '-' . $data['databaseRow']['uid'], |
||
219 | ]; |
||
220 | // @todo: ugly construct ... |
||
221 | $data['inlineData'] = $this->inlineData; |
||
222 | $data['renderType'] = 'fullRecordContainer'; |
||
223 | return $this->nodeFactory->create($data)->render(); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Render child child |
||
228 | * |
||
229 | * Render a table with FormEngine, that occurs on an intermediate table but should be editable directly, |
||
230 | * so two tables are combined (the intermediate table with attributes and the sub-embedded table). |
||
231 | * -> This is a direct embedding over two levels! |
||
232 | * |
||
233 | * @param array $data |
||
234 | * @param string $appendFormFieldNames The [<table>][<uid>] of the parent record (the intermediate table) |
||
235 | * @return array Result array |
||
236 | */ |
||
237 | protected function renderCombinationChild(array $data, $appendFormFieldNames) |
||
238 | { |
||
239 | $childData = $data['combinationChild']; |
||
240 | $parentConfig = $data['inlineParentConfig']; |
||
241 | |||
242 | $resultArray = $this->initializeResultArray(); |
||
243 | |||
244 | // Display Warning FlashMessage if it is not suppressed |
||
245 | if (!isset($parentConfig['appearance']['suppressCombinationWarning']) || empty($parentConfig['appearance']['suppressCombinationWarning'])) { |
||
246 | $combinationWarningMessage = 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:warning.inline_use_combination'; |
||
247 | if (!empty($parentConfig['appearance']['overwriteCombinationWarningMessage'])) { |
||
248 | $combinationWarningMessage = $parentConfig['appearance']['overwriteCombinationWarningMessage']; |
||
249 | } |
||
250 | $message = $this->getLanguageService()->sL($combinationWarningMessage); |
||
251 | $markup = []; |
||
252 | // @TODO: This is not a FlashMessage! The markup must be changed and special CSS |
||
253 | // @TODO: should be created, in order to prevent confusion. |
||
254 | $markup[] = '<div class="alert alert-warning">'; |
||
255 | $markup[] = ' <div class="media">'; |
||
256 | $markup[] = ' <div class="media-left">'; |
||
257 | $markup[] = ' <span class="fa-stack fa-lg">'; |
||
258 | $markup[] = ' <i class="fa fa-circle fa-stack-2x"></i>'; |
||
259 | $markup[] = ' <i class="fa fa-exclamation fa-stack-1x"></i>'; |
||
260 | $markup[] = ' </span>'; |
||
261 | $markup[] = ' </div>'; |
||
262 | $markup[] = ' <div class="media-body">'; |
||
263 | $markup[] = ' <div class="alert-message">' . htmlspecialchars($message) . '</div>'; |
||
264 | $markup[] = ' </div>'; |
||
265 | $markup[] = ' </div>'; |
||
266 | $markup[] = '</div>'; |
||
267 | $resultArray['html'] = implode(LF, $markup); |
||
268 | } |
||
269 | |||
270 | $childArray = $this->renderChild($childData); |
||
271 | $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childArray); |
||
272 | |||
273 | // If this is a new record, add a pid value to store this record and the pointer value for the intermediate table |
||
274 | View Code Duplication | if ($childData['command'] === 'new') { |
|
275 | $comboFormFieldName = 'data[' . $childData['tableName'] . '][' . $childData['databaseRow']['uid'] . '][pid]'; |
||
276 | $resultArray['html'] .= '<input type="hidden" name="' . htmlspecialchars($comboFormFieldName) . '" value="' . htmlspecialchars($childData['databaseRow']['pid']) . '" />'; |
||
277 | } |
||
278 | // If the foreign_selector field is also responsible for uniqueness, tell the browser the uid of the "other" side of the relation |
||
279 | View Code Duplication | if ($childData['command'] === 'new' || $parentConfig['foreign_unique'] === $parentConfig['foreign_selector']) { |
|
280 | $parentFormFieldName = 'data' . $appendFormFieldNames . '[' . $parentConfig['foreign_selector'] . ']'; |
||
281 | $resultArray['html'] .= '<input type="hidden" name="' . htmlspecialchars($parentFormFieldName) . '" value="' . htmlspecialchars($childData['databaseRow']['uid']) . '" />'; |
||
282 | } |
||
283 | |||
284 | return $resultArray; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Renders the HTML header for a foreign record, such as the title, toggle-function, drag'n'drop, etc. |
||
289 | * Later on the command-icons are inserted here. |
||
290 | * |
||
291 | * @param array $data Current data |
||
292 | * @return string The HTML code of the header |
||
293 | */ |
||
294 | protected function renderForeignRecordHeader(array $data) |
||
295 | { |
||
296 | $languageService = $this->getLanguageService(); |
||
297 | $inlineConfig = $data['inlineParentConfig']; |
||
298 | $foreignTable = $inlineConfig['foreign_table']; |
||
299 | $rec = $data['databaseRow']; |
||
300 | // Init: |
||
301 | $domObjectId = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($data['inlineFirstPid']); |
||
302 | $objectId = $domObjectId . '-' . $foreignTable . '-' . $rec['uid']; |
||
303 | |||
304 | $recordTitle = $data['recordTitle']; |
||
305 | if (!empty($recordTitle)) { |
||
306 | // The user function may return HTML, therefore we can't escape it |
||
307 | if (empty($data['processedTca']['ctrl']['formattedLabel_userFunc'])) { |
||
308 | $recordTitle = BackendUtility::getRecordTitlePrep($recordTitle); |
||
309 | } |
||
310 | } else { |
||
311 | $recordTitle = '<em>[' . htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.no_title')) . ']</em>'; |
||
312 | } |
||
313 | |||
314 | $altText = BackendUtility::getRecordIconAltText($rec, $foreignTable); |
||
315 | |||
316 | $iconImg = '<span title="' . $altText . '" id="' . htmlspecialchars($objectId) . '_icon' . '">' . $this->iconFactory->getIconForRecord($foreignTable, $rec, Icon::SIZE_SMALL)->render() . '</span>'; |
||
317 | $label = '<span id="' . $objectId . '_label">' . $recordTitle . '</span>'; |
||
318 | $ctrl = $this->renderForeignRecordHeaderControl($data); |
||
319 | $thumbnail = false; |
||
320 | |||
321 | // Renders a thumbnail for the header |
||
322 | if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['thumbnails'] && !empty($inlineConfig['appearance']['headerThumbnail']['field'])) { |
||
323 | $fieldValue = $rec[$inlineConfig['appearance']['headerThumbnail']['field']]; |
||
324 | $fileUid = $fieldValue[0]['uid']; |
||
325 | |||
326 | if (!empty($fileUid)) { |
||
327 | try { |
||
328 | $fileObject = ResourceFactory::getInstance()->getFileObject($fileUid); |
||
329 | } catch (\InvalidArgumentException $e) { |
||
330 | $fileObject = null; |
||
331 | } |
||
332 | if ($fileObject && $fileObject->isMissing()) { |
||
333 | $thumbnail .= '<span class="label label-danger">' |
||
334 | . htmlspecialchars(static::getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:warning.file_missing')) |
||
|
|||
335 | . '</span> ' . htmlspecialchars($fileObject->getName()) . '<br />'; |
||
336 | } elseif ($fileObject) { |
||
337 | $imageSetup = $inlineConfig['appearance']['headerThumbnail']; |
||
338 | unset($imageSetup['field']); |
||
339 | if (!empty($rec['crop'])) { |
||
340 | $imageSetup['crop'] = $rec['crop']; |
||
341 | } |
||
342 | $imageSetup = array_merge(['width' => '45', 'height' => '45c'], $imageSetup); |
||
343 | |||
344 | if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['thumbnails'] |
||
345 | && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileObject->getExtension())) { |
||
346 | $processedImage = $fileObject->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, $imageSetup); |
||
347 | // Only use a thumbnail if the processing process was successful by checking if image width is set |
||
348 | if ($processedImage->getProperty('width')) { |
||
349 | $imageUrl = $processedImage->getPublicUrl(true); |
||
350 | $thumbnail = '<img src="' . $imageUrl . '" ' . |
||
351 | 'width="' . $processedImage->getProperty('width') . '" ' . |
||
352 | 'height="' . $processedImage->getProperty('height') . '" ' . |
||
353 | 'alt="' . htmlspecialchars($altText) . '" ' . |
||
354 | 'title="' . htmlspecialchars($altText) . '">'; |
||
355 | } |
||
356 | } else { |
||
357 | $thumbnail = ''; |
||
358 | } |
||
359 | } |
||
360 | } |
||
361 | } |
||
362 | |||
363 | if (!empty($inlineConfig['appearance']['headerThumbnail']['field']) && $thumbnail) { |
||
364 | $mediaContainer = '<div class="form-irre-header-cell form-irre-header-thumbnail" id="' . $objectId . '_thumbnailcontainer">' . $thumbnail . '</div>'; |
||
365 | } else { |
||
366 | $mediaContainer = '<div class="form-irre-header-cell form-irre-header-icon" id="' . $objectId . '_iconcontainer">' . $iconImg . '</div>'; |
||
367 | } |
||
368 | $header = $mediaContainer . ' |
||
369 | <div class="form-irre-header-cell form-irre-header-body">' . $label . '</div> |
||
370 | <div class="form-irre-header-cell form-irre-header-control t3js-formengine-irre-control">' . $ctrl . '</div>'; |
||
371 | |||
372 | return $header; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Render the control-icons for a record header (create new, sorting, delete, disable/enable). |
||
377 | * Most of the parts are copy&paste from TYPO3\CMS\Recordlist\RecordList\DatabaseRecordList and |
||
378 | * modified for the JavaScript calls here |
||
379 | * |
||
380 | * @param array $data Current data |
||
381 | * @return string The HTML code with the control-icons |
||
382 | */ |
||
383 | protected function renderForeignRecordHeaderControl(array $data) |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * Normalize a relation "uid" published by transferData, like "1|Company%201" |
||
615 | * |
||
616 | * @param string $string A transferData reference string, containing the uid |
||
617 | * @return string The normalized uid |
||
618 | */ |
||
619 | protected function normalizeUid($string) |
||
620 | { |
||
621 | $parts = explode('|', $string); |
||
622 | return $parts[0]; |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Initialized the hook objects for this class. |
||
627 | * Each hook object has to implement the interface |
||
628 | * \TYPO3\CMS\Backend\Form\Element\InlineElementHookInterface |
||
629 | * |
||
630 | * @throws \UnexpectedValueException |
||
631 | */ |
||
632 | protected function initHookObjects() |
||
641 | } |
||
642 | } |
||
643 | |||
644 | /** |
||
645 | * @return BackendUserAuthentication |
||
646 | */ |
||
647 | protected function getBackendUserAuthentication() |
||
650 | } |
||
651 | |||
652 | /** |
||
653 | * @return LanguageService |
||
654 | */ |
||
655 | protected function getLanguageService() |
||
658 | } |
||
659 | } |
||
660 |