| Total Complexity | 68 |
| Total Lines | 436 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like LiveSearch 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 LiveSearch, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class LiveSearch |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | const PAGE_JUMP_TABLE = 'pages'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | const RECURSIVE_PAGE_LEVEL = 99; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | const GROUP_TITLE_MAX_LENGTH = 15; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | const RECORD_TITLE_MAX_LENGTH = 28; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $queryString = ''; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | private $startCount = 0; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | private $limitCount = 5; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $userPermissions = ''; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var QueryParser |
||
| 82 | */ |
||
| 83 | protected $queryParser; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Initialize access settings |
||
| 87 | */ |
||
| 88 | public function __construct() |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Find records from database based on the given $searchQuery. |
||
| 96 | * |
||
| 97 | * @param string $searchQuery |
||
| 98 | * @return array Result list of database search. |
||
| 99 | */ |
||
| 100 | public function find($searchQuery) |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Find records from all registered TCA table & column values. |
||
| 125 | * |
||
| 126 | * @param array $pageIdList Comma separated list of page IDs |
||
| 127 | * @return array Records found in the database matching the searchQuery |
||
| 128 | */ |
||
| 129 | protected function findByGlobalTableList($pageIdList) |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Find records by given table name. |
||
| 160 | * |
||
| 161 | * @param string $tableName Database table name |
||
| 162 | * @param array $pageIdList Comma separated list of page IDs |
||
| 163 | * @param int $firstResult |
||
| 164 | * @param int $maxResults |
||
| 165 | * @return array Records found in the database matching the searchQuery |
||
| 166 | * @see getRecordArray() |
||
| 167 | * @see makeQuerySearchByTable() |
||
| 168 | * @see extractSearchableFieldsFromTable() |
||
| 169 | */ |
||
| 170 | protected function findByTable($tableName, $pageIdList, $firstResult, $maxResults) |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Process the Database operation to get the search result. |
||
| 213 | * |
||
| 214 | * @param QueryBuilder $queryBuilder Database table name |
||
| 215 | * @param string $tableName |
||
| 216 | * @return array |
||
| 217 | * @see getTitleFromCurrentRow() |
||
| 218 | * @see getEditLink() |
||
| 219 | */ |
||
| 220 | protected function getRecordArray($queryBuilder, $tableName) |
||
| 221 | { |
||
| 222 | $collect = []; |
||
| 223 | $result = $queryBuilder->execute(); |
||
| 224 | $iconFactory = GeneralUtility::makeInstance(IconFactory::class); |
||
| 225 | while ($row = $result->fetch()) { |
||
| 226 | BackendUtility::workspaceOL($tableName, $row); |
||
| 227 | if (!is_array($row)) { |
||
| 228 | continue; |
||
| 229 | } |
||
| 230 | $onlineUid = $row['t3ver_oid'] ?: $row['uid']; |
||
| 231 | $title = 'id=' . $row['uid'] . ', pid=' . $row['pid']; |
||
| 232 | $collect[$onlineUid] = [ |
||
| 233 | 'id' => $tableName . ':' . $row['uid'], |
||
| 234 | 'pageId' => $tableName === 'pages' ? $row['uid'] : $row['pid'], |
||
| 235 | 'typeLabel' => htmlspecialchars($this->getTitleOfCurrentRecordType($tableName)), |
||
| 236 | 'iconHTML' => '<span title="' . htmlspecialchars($title) . '">' . $iconFactory->getIconForRecord($tableName, $row, Icon::SIZE_SMALL)->render() . '</span>', |
||
| 237 | 'title' => htmlspecialchars(BackendUtility::getRecordTitle($tableName, $row)), |
||
| 238 | 'editLink' => htmlspecialchars($this->getEditLink($tableName, $row)) |
||
| 239 | ]; |
||
| 240 | } |
||
| 241 | return $collect; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Build a backend edit link based on given record. |
||
| 246 | * |
||
| 247 | * @param string $tableName Record table name |
||
| 248 | * @param array $row Current record row from database. |
||
| 249 | * @return string Link to open an edit window for record. |
||
| 250 | * @see \TYPO3\CMS\Backend\Utility\BackendUtility::readPageAccess() |
||
| 251 | */ |
||
| 252 | protected function getEditLink($tableName, $row) |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Retrieve the record name |
||
| 277 | * |
||
| 278 | * @param string $tableName Record table name |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | protected function getTitleOfCurrentRecordType($tableName) |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Build the MySql where clause by table. |
||
| 288 | * |
||
| 289 | * @param QueryBuilder $queryBuilder |
||
| 290 | * @param string $tableName Record table name |
||
| 291 | * @param array $fieldsToSearchWithin User right based visible fields where we can search within. |
||
| 292 | * @return CompositeExpression |
||
| 293 | */ |
||
| 294 | protected function makeQuerySearchByTable(QueryBuilder &$queryBuilder, $tableName, array $fieldsToSearchWithin) |
||
| 295 | { |
||
| 296 | $constraints = []; |
||
| 297 | |||
| 298 | // If the search string is a simple integer, assemble an equality comparison |
||
| 299 | if (MathUtility::canBeInterpretedAsInteger($this->queryString)) { |
||
| 300 | foreach ($fieldsToSearchWithin as $fieldName) { |
||
| 301 | if ($fieldName !== 'uid' |
||
| 302 | && $fieldName !== 'pid' |
||
| 303 | && !isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName]) |
||
| 304 | ) { |
||
| 305 | continue; |
||
| 306 | } |
||
| 307 | $fieldConfig = $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']; |
||
| 308 | $fieldType = $fieldConfig['type']; |
||
| 309 | $evalRules = $fieldConfig['eval'] ?: ''; |
||
| 310 | |||
| 311 | // Assemble the search condition only if the field is an integer, or is uid or pid |
||
| 312 | if ($fieldName === 'uid' |
||
| 313 | || $fieldName === 'pid' |
||
| 314 | || ($fieldType === 'input' && $evalRules && GeneralUtility::inList($evalRules, 'int')) |
||
| 315 | ) { |
||
| 316 | $constraints[] = $queryBuilder->expr()->eq( |
||
| 317 | $fieldName, |
||
| 318 | $queryBuilder->createNamedParameter($this->queryString, \PDO::PARAM_INT) |
||
| 319 | ); |
||
| 320 | } elseif ($fieldType === 'text' |
||
| 321 | || $fieldType === 'flex' |
||
| 322 | || ($fieldType === 'input' && (!$evalRules || !preg_match('/\b(?:date|time|int)\b/', $evalRules))) |
||
| 323 | ) { |
||
| 324 | // Otherwise and if the field makes sense to be searched, assemble a like condition |
||
| 325 | $constraints[] = $constraints[] = $queryBuilder->expr()->like( |
||
| 326 | $fieldName, |
||
| 327 | $queryBuilder->createNamedParameter( |
||
| 328 | '%' . $queryBuilder->escapeLikeWildcards((int)$this->queryString) . '%', |
||
| 329 | \PDO::PARAM_STR |
||
| 330 | ) |
||
| 331 | ); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | } else { |
||
| 335 | $like = '%' . $queryBuilder->escapeLikeWildcards($this->queryString) . '%'; |
||
| 336 | foreach ($fieldsToSearchWithin as $fieldName) { |
||
| 337 | if (!isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName])) { |
||
| 338 | continue; |
||
| 339 | } |
||
| 340 | $fieldConfig = &$GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']; |
||
| 341 | $fieldType = $fieldConfig['type']; |
||
| 342 | $evalRules = $fieldConfig['eval'] ?: ''; |
||
| 343 | |||
| 344 | // Check whether search should be case-sensitive or not |
||
| 345 | $searchConstraint = $queryBuilder->expr()->andX( |
||
| 346 | $queryBuilder->expr()->comparison( |
||
| 347 | 'LOWER(' . $queryBuilder->quoteIdentifier($fieldName) . ')', |
||
| 348 | 'LIKE', |
||
| 349 | $queryBuilder->createNamedParameter(mb_strtolower($like), \PDO::PARAM_STR) |
||
| 350 | ) |
||
| 351 | ); |
||
| 352 | |||
| 353 | if (is_array($fieldConfig['search'])) { |
||
| 354 | if (in_array('case', $fieldConfig['search'], true)) { |
||
| 355 | // Replace case insensitive default constraint |
||
| 356 | $searchConstraint = $queryBuilder->expr()->andX( |
||
| 357 | $queryBuilder->expr()->like( |
||
| 358 | $fieldName, |
||
| 359 | $queryBuilder->createNamedParameter($like, \PDO::PARAM_STR) |
||
| 360 | ) |
||
| 361 | ); |
||
| 362 | } |
||
| 363 | // Apply additional condition, if any |
||
| 364 | if ($fieldConfig['search']['andWhere']) { |
||
| 365 | $searchConstraint->add( |
||
| 366 | QueryHelper::stripLogicalOperatorPrefix($fieldConfig['search']['andWhere']) |
||
| 367 | ); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | // Assemble the search condition only if the field makes sense to be searched |
||
| 371 | if ($fieldType === 'text' |
||
| 372 | || $fieldType === 'flex' |
||
| 373 | || ($fieldType === 'input' && (!$evalRules || !preg_match('/\b(?:date|time|int)\b/', $evalRules))) |
||
| 374 | ) { |
||
| 375 | if ($searchConstraint->count() !== 0) { |
||
| 376 | $constraints[] = $searchConstraint; |
||
| 377 | } |
||
| 378 | } |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | // If no search field conditions have been build ensure no results are returned |
||
| 383 | if (empty($constraints)) { |
||
| 384 | return '0=1'; |
||
|
|
|||
| 385 | } |
||
| 386 | |||
| 387 | return $queryBuilder->expr()->orX(...$constraints); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Get all fields from given table where we can search for. |
||
| 392 | * |
||
| 393 | * @param string $tableName Name of the table for which to get the searchable fields |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | protected function extractSearchableFieldsFromTable($tableName) |
||
| 397 | { |
||
| 398 | // Get the list of fields to search in from the TCA, if any |
||
| 399 | if (isset($GLOBALS['TCA'][$tableName]['ctrl']['searchFields'])) { |
||
| 400 | $fieldListArray = GeneralUtility::trimExplode(',', $GLOBALS['TCA'][$tableName]['ctrl']['searchFields'], true); |
||
| 401 | } else { |
||
| 402 | $fieldListArray = []; |
||
| 403 | } |
||
| 404 | // Add special fields |
||
| 405 | if ($GLOBALS['BE_USER']->isAdmin()) { |
||
| 406 | $fieldListArray[] = 'uid'; |
||
| 407 | $fieldListArray[] = 'pid'; |
||
| 408 | } |
||
| 409 | return $fieldListArray; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Setter for limit value. |
||
| 414 | * |
||
| 415 | * @param int $limitCount |
||
| 416 | */ |
||
| 417 | public function setLimitCount($limitCount) |
||
| 418 | { |
||
| 419 | $limit = MathUtility::convertToPositiveInteger($limitCount); |
||
| 420 | if ($limit > 0) { |
||
| 421 | $this->limitCount = $limit; |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Setter for start count value. |
||
| 427 | * |
||
| 428 | * @param int $startCount |
||
| 429 | */ |
||
| 430 | public function setStartCount($startCount) |
||
| 431 | { |
||
| 432 | $this->startCount = MathUtility::convertToPositiveInteger($startCount); |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Setter for the search query string. |
||
| 437 | * |
||
| 438 | * @param string $queryString |
||
| 439 | */ |
||
| 440 | public function setQueryString($queryString) |
||
| 441 | { |
||
| 442 | $this->queryString = $queryString; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Creates an instance of \TYPO3\CMS\Backend\Tree\View\PageTreeView which will select a |
||
| 447 | * page tree to $depth and return the object. In that object we will find the ids of the tree. |
||
| 448 | * |
||
| 449 | * @param int $id Page id. |
||
| 450 | * @param int $depth Depth to go down. |
||
| 451 | * @return string Comma separated list of uids |
||
| 452 | */ |
||
| 453 | protected function getAvailablePageIds($id, $depth) |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return LanguageService|null |
||
| 470 | */ |
||
| 471 | protected function getLanguageService(): ?LanguageService |
||
| 474 | } |
||
| 475 | } |
||
| 476 |