| Total Complexity | 82 |
| Total Lines | 595 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PageInformationController 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 PageInformationController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class PageInformationController |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $fieldConfiguration = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int Value of the GET/POST var 'id' |
||
| 47 | */ |
||
| 48 | protected $id; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var InfoModuleController Contains a reference to the parent calling object |
||
| 52 | */ |
||
| 53 | protected $pObj; |
||
| 54 | |||
| 55 | protected IconFactory $iconFactory; |
||
| 56 | protected UriBuilder $uriBuilder; |
||
| 57 | protected ?BackendLayoutView $backendLayoutView = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $fieldArray; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Keys are fieldnames and values are td-css-classes to add in addElement(); |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $addElement_tdCssClass = []; |
||
| 70 | |||
| 71 | public function __construct(IconFactory $iconFactory, UriBuilder $uriBuilder) |
||
| 72 | { |
||
| 73 | $this->iconFactory = $iconFactory; |
||
| 74 | $this->uriBuilder = $uriBuilder; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Init, called from parent object |
||
| 79 | * |
||
| 80 | * @param InfoModuleController $pObj A reference to the parent (calling) object |
||
| 81 | * @param ServerRequestInterface $request |
||
| 82 | */ |
||
| 83 | public function init(InfoModuleController $pObj, ServerRequestInterface $request) |
||
| 84 | { |
||
| 85 | $this->pObj = $pObj; |
||
| 86 | $this->id = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0); |
||
| 87 | // Setting MOD_MENU items as we need them for logging: |
||
| 88 | $this->pObj->MOD_MENU = array_merge($this->pObj->MOD_MENU, $this->modMenu()); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Main, called from parent object |
||
| 93 | * |
||
| 94 | * @param ServerRequestInterface $request |
||
| 95 | * @return string Output HTML for the module. |
||
| 96 | */ |
||
| 97 | public function main(ServerRequestInterface $request) |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns the menu array |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | protected function modMenu() |
||
| 139 | { |
||
| 140 | $menu = [ |
||
| 141 | 'pages' => [], |
||
| 142 | 'depth' => [ |
||
| 143 | 0 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0'), |
||
| 144 | 1 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1'), |
||
| 145 | 2 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2'), |
||
| 146 | 3 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3'), |
||
| 147 | 4 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4'), |
||
| 148 | 999 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi') |
||
| 149 | ] |
||
| 150 | ]; |
||
| 151 | |||
| 152 | $this->fillFieldConfiguration($this->id); |
||
| 153 | foreach ($this->fieldConfiguration as $key => $item) { |
||
| 154 | $menu['pages'][$key] = $item['label']; |
||
| 155 | } |
||
| 156 | return $menu; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Function, which returns all tables to |
||
| 161 | * which the user has access. Also a set of standard tables (pages, sys_filemounts, etc...) |
||
| 162 | * are filtered out. So what is left is basically all tables which makes sense to list content from. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | protected function cleanTableNames(): string |
||
| 167 | { |
||
| 168 | // Get all table names: |
||
| 169 | $tableNames = array_flip(array_keys($GLOBALS['TCA'])); |
||
| 170 | // Unset common names: |
||
| 171 | unset($tableNames['pages']); |
||
| 172 | unset($tableNames['sys_filemounts']); |
||
| 173 | unset($tableNames['sys_action']); |
||
| 174 | unset($tableNames['sys_workflows']); |
||
| 175 | unset($tableNames['be_users']); |
||
| 176 | unset($tableNames['be_groups']); |
||
| 177 | $allowedTableNames = []; |
||
| 178 | // Traverse table names and set them in allowedTableNames array IF they can be read-accessed by the user. |
||
| 179 | if (is_array($tableNames)) { |
||
|
|
|||
| 180 | foreach ($tableNames as $k => $v) { |
||
| 181 | if (!($GLOBALS['TCA'][$k]['ctrl']['hideTable'] ?? false) && $this->getBackendUser()->check('tables_select', $k)) { |
||
| 182 | $allowedTableNames['table_' . $k] = $k; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | return implode(',', array_keys($allowedTableNames)); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Generate configuration for field selection |
||
| 191 | * |
||
| 192 | * @param int $pageId current page id |
||
| 193 | */ |
||
| 194 | protected function fillFieldConfiguration(int $pageId) |
||
| 195 | { |
||
| 196 | $modTSconfig = BackendUtility::getPagesTSconfig($pageId)['mod.']['web_info.']['fieldDefinitions.'] ?? []; |
||
| 197 | foreach ($modTSconfig as $key => $item) { |
||
| 198 | $fieldList = str_replace('###ALL_TABLES###', $this->cleanTableNames(), $item['fields']); |
||
| 199 | $fields = GeneralUtility::trimExplode(',', $fieldList, true); |
||
| 200 | $key = trim($key, '.'); |
||
| 201 | $this->fieldConfiguration[$key] = [ |
||
| 202 | 'label' => $item['label'] ? $this->getLanguageService()->sL($item['label']) : $key, |
||
| 203 | 'fields' => $fields |
||
| 204 | ]; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Renders records from the pages table from page id |
||
| 210 | * |
||
| 211 | * @param int $id Page id |
||
| 212 | * @param int $depth |
||
| 213 | * @param ServerRequestInterface $request |
||
| 214 | * @return string HTML for the listing |
||
| 215 | * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException |
||
| 216 | */ |
||
| 217 | protected function getTable_pages($id, int $depth, ServerRequestInterface $request) |
||
| 218 | { |
||
| 219 | $out = ''; |
||
| 220 | $lang = $this->getLanguageService(); |
||
| 221 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 222 | ->getQueryBuilderForTable('pages'); |
||
| 223 | $queryBuilder->getRestrictions() |
||
| 224 | ->removeAll() |
||
| 225 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 226 | $row = $queryBuilder |
||
| 227 | ->select('*') |
||
| 228 | ->from('pages') |
||
| 229 | ->where( |
||
| 230 | $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)), |
||
| 231 | $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW) |
||
| 232 | ) |
||
| 233 | ->execute() |
||
| 234 | ->fetch(); |
||
| 235 | BackendUtility::workspaceOL('pages', $row); |
||
| 236 | // If there was found a page: |
||
| 237 | if (is_array($row)) { |
||
| 238 | // Creating elements |
||
| 239 | $editUids = []; |
||
| 240 | // Getting children |
||
| 241 | $theRows = $this->getPageRecordsRecursive($row['uid'], $depth); |
||
| 242 | if ($this->getBackendUser()->doesUserHaveAccess($row, Permission::PAGE_EDIT) && $row['uid'] > 0) { |
||
| 243 | $editUids[] = $row['uid']; |
||
| 244 | } |
||
| 245 | $out .= $this->pages_drawItem($row, $this->fieldArray, $request); |
||
| 246 | // Traverse all pages selected: |
||
| 247 | foreach ($theRows as $sRow) { |
||
| 248 | if ($this->getBackendUser()->doesUserHaveAccess($sRow, Permission::PAGE_EDIT)) { |
||
| 249 | $editUids[] = $sRow['uid']; |
||
| 250 | } |
||
| 251 | $out .= $this->pages_drawItem($sRow, $this->fieldArray, $request); |
||
| 252 | } |
||
| 253 | // Header line is drawn |
||
| 254 | $headerCells = []; |
||
| 255 | $editIdList = implode(',', $editUids); |
||
| 256 | // Traverse fields (as set above) in order to create header values: |
||
| 257 | foreach ($this->fieldArray as $field) { |
||
| 258 | $editButton = ''; |
||
| 259 | if ($editIdList && isset($GLOBALS['TCA']['pages']['columns'][$field]) && $field !== 'uid') { |
||
| 260 | $iTitle = sprintf( |
||
| 261 | $lang->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:editThisColumn'), |
||
| 262 | rtrim(trim($lang->sL(BackendUtility::getItemLabel('pages', $field))), ':') |
||
| 263 | ); |
||
| 264 | $urlParameters = [ |
||
| 265 | 'edit' => [ |
||
| 266 | 'pages' => [ |
||
| 267 | $editIdList => 'edit' |
||
| 268 | ] |
||
| 269 | ], |
||
| 270 | 'columnsOnly' => $field, |
||
| 271 | 'returnUrl' => $request->getAttribute('normalizedParams')->getRequestUri() |
||
| 272 | ]; |
||
| 273 | $url = (string)$this->uriBuilder->buildUriFromRoute('record_edit', $urlParameters); |
||
| 274 | $editButton = '<a class="btn btn-default" href="' . htmlspecialchars($url) |
||
| 275 | . '" title="' . htmlspecialchars($iTitle) . '">' |
||
| 276 | . $this->iconFactory->getIcon('actions-document-open', Icon::SIZE_SMALL)->render() . '</a>'; |
||
| 277 | } |
||
| 278 | switch ($field) { |
||
| 279 | case 'title': |
||
| 280 | $headerCells[$field] = $editButton . ' <strong>' |
||
| 281 | . $lang->sL($GLOBALS['TCA']['pages']['columns'][$field]['label']) |
||
| 282 | . '</strong>'; |
||
| 283 | break; |
||
| 284 | case 'uid': |
||
| 285 | $headerCells[$field] = ''; |
||
| 286 | break; |
||
| 287 | case 'actual_backend_layout': |
||
| 288 | $headerCells[$field] = htmlspecialchars($lang->sL('LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:actual_backend_layout')); |
||
| 289 | break; |
||
| 290 | default: |
||
| 291 | if (strpos($field, 'table_') === 0) { |
||
| 292 | $f2 = substr($field, 6); |
||
| 293 | if ($GLOBALS['TCA'][$f2]) { |
||
| 294 | $headerCells[$field] = ' ' . |
||
| 295 | '<span title="' . |
||
| 296 | htmlspecialchars($lang->sL($GLOBALS['TCA'][$f2]['ctrl']['title'])) . |
||
| 297 | '">' . |
||
| 298 | $this->iconFactory->getIconForRecord($f2, [], Icon::SIZE_SMALL)->render() . |
||
| 299 | '</span>'; |
||
| 300 | } |
||
| 301 | } else { |
||
| 302 | $headerCells[$field] = $editButton . ' <strong>' |
||
| 303 | . htmlspecialchars($lang->sL($GLOBALS['TCA']['pages']['columns'][$field]['label'])) |
||
| 304 | . '</strong>'; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |
||
| 308 | $out = '<div class="table-responsive">' |
||
| 309 | . '<table class="table table-striped table-hover mb-0">' |
||
| 310 | . '<thead>' |
||
| 311 | . $this->addElement($headerCells) |
||
| 312 | . '</thead>' |
||
| 313 | . '<tbody>' |
||
| 314 | . $out |
||
| 315 | . '</tbody>' |
||
| 316 | . '</table>' |
||
| 317 | . '</div>'; |
||
| 318 | } |
||
| 319 | return $out; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Adds pages-rows to an array, selecting recursively in the page tree. |
||
| 324 | * |
||
| 325 | * @param int $pid Starting page id to select from |
||
| 326 | * @param string $iconPrefix Prefix for icon code. |
||
| 327 | * @param int $depth Depth (decreasing) |
||
| 328 | * @param array $rows Array which will accumulate page rows |
||
| 329 | * @return array $rows with added rows. |
||
| 330 | */ |
||
| 331 | protected function getPageRecordsRecursive(int $pid, int $depth, string $iconPrefix = '', array $rows = []): array |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Adds a list item for the pages-rendering |
||
| 385 | * |
||
| 386 | * @param array $row Record array |
||
| 387 | * @param array $fieldArr Field list |
||
| 388 | * @param ServerRequestInterface $request |
||
| 389 | * @return string HTML for the item |
||
| 390 | * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException |
||
| 391 | */ |
||
| 392 | protected function pages_drawItem($row, $fieldArr, ServerRequestInterface $request) |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Creates the icon image tag for the page and wraps it in a link which will trigger the click menu. |
||
| 485 | * |
||
| 486 | * @param array $row Record array |
||
| 487 | * @return string HTML for the icon |
||
| 488 | */ |
||
| 489 | protected function getIcon($row) |
||
| 490 | { |
||
| 491 | // Initialization |
||
| 492 | $toolTip = BackendUtility::getRecordToolTip($row, 'pages'); |
||
| 493 | $icon = '<span ' . $toolTip . '>' . $this->iconFactory->getIconForRecord('pages', $row, Icon::SIZE_SMALL)->render() . '</span>'; |
||
| 494 | // The icon with link |
||
| 495 | if ($this->getBackendUser()->recordEditAccessInternals('pages', $row)) { |
||
| 496 | $icon = BackendUtility::wrapClickMenuOnIcon($icon, 'pages', $row['uid']); |
||
| 497 | } |
||
| 498 | return $icon; |
||
| 499 | } |
||
| 500 | /** |
||
| 501 | * Returns the HTML code for rendering a field in the pages table. |
||
| 502 | * The row value is processed to a human readable form and the result is parsed through htmlspecialchars(). |
||
| 503 | * |
||
| 504 | * @param string $field The name of the field of which the value should be rendered. |
||
| 505 | * @param array $row The pages table row as an associative array. |
||
| 506 | * @return string The rendered table field value. |
||
| 507 | */ |
||
| 508 | protected function getPagesTableFieldValue($field, array $row) |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Counts and returns the number of records on the page with $pid |
||
| 515 | * |
||
| 516 | * @param string $table Table name |
||
| 517 | * @param int $pid Page id |
||
| 518 | * @return int Number of records. |
||
| 519 | */ |
||
| 520 | protected function numberOfRecords($table, $pid) |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Returns a table-row with the content from the fields in the input data array. |
||
| 544 | * OBS: $this->fieldArray MUST be set! (represents the list of fields to display) |
||
| 545 | * |
||
| 546 | * @param array $data Is the data array, record with the fields. Notice: These fields are (currently) NOT htmlspecialchar'ed before being wrapped in <td>-tags |
||
| 547 | * @return string HTML content for the table row |
||
| 548 | */ |
||
| 549 | protected function addElement($data) |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @return BackendUserAuthentication |
||
| 603 | */ |
||
| 604 | protected function getBackendUser(): BackendUserAuthentication |
||
| 605 | { |
||
| 606 | return $GLOBALS['BE_USER']; |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @return LanguageService|null |
||
| 611 | */ |
||
| 612 | protected function getLanguageService(): ?LanguageService |
||
| 613 | { |
||
| 614 | return $GLOBALS['LANG'] ?? null; |
||
| 615 | } |
||
| 616 | |||
| 617 | protected function getBackendLayouts(array $row, string $field): array |
||
| 633 | } |
||
| 634 | } |
||
| 635 |