‪TYPO3CMS  9.5
SearchTermRestriction.php
Go to the documentation of this file.
1 <?php
2 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 
24 
29 {
33  private ‪$searchDemand;
34 
39 
41  {
42  $this->searchDemand = ‪$searchDemand;
43  $this->queryBuilder = ‪$queryBuilder;
44  }
45 
46  public function ‪buildExpression(array $queriedTables, ‪ExpressionBuilder $expressionBuilder): ‪CompositeExpression
47  {
48  $constraints = [];
49  foreach ($queriedTables as $tableAlias => $tableName) {
50  if (!in_array($tableName, ['sys_file', 'sys_file_metadata'])) {
51  continue;
52  }
53  $constraints[] = $this->‪makeQuerySearchByTable($tableName, $tableAlias);
54  }
55 
56  return $expressionBuilder->‪orX(...$constraints);
57  }
58 
66  private function ‪makeQuerySearchByTable(string $tableName, string $tableAlias): CompositeExpression
67  {
68  $fieldsToSearchWithin = $this->‪extractSearchableFieldsFromTable($tableName);
69  $searchTerm = $this->searchDemand->getSearchTerm();
70  $constraints = [];
71 
72  $searchTermParts = str_getcsv($searchTerm, ' ');
73  foreach ($searchTermParts as $searchTermPart) {
74  $searchTermPart = trim($searchTermPart);
75  if ($searchTermPart === '') {
76  continue;
77  }
78  $constraintsForParts = [];
79  $like = '%' . $this->queryBuilder->escapeLikeWildcards($searchTermPart) . '%';
80  foreach ($fieldsToSearchWithin as $fieldName) {
81  if (!isset(‪$GLOBALS['TCA'][$tableName]['columns'][$fieldName])) {
82  continue;
83  }
84  $fieldConfig = ‪$GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'];
85  $fieldType = $fieldConfig['type'];
86  $evalRules = $fieldConfig['eval'] ?? '';
87 
88  // Check whether search should be case-sensitive or not
89  if (is_array($fieldConfig['search']) && in_array('case', $fieldConfig['search'], true)) {
90  // case sensitive
91  $searchConstraint = $this->queryBuilder->expr()->andX(
92  $this->queryBuilder->expr()->like(
93  $tableAlias . '.' . $fieldName,
94  $this->queryBuilder->createNamedParameter($like, \PDO::PARAM_STR)
95  )
96  );
97  } else {
98  $searchConstraint = $this->queryBuilder->expr()->andX(
99  // case insensitive
100  $this->queryBuilder->expr()->comparison(
101  'LOWER(' . $this->queryBuilder->quoteIdentifier($tableAlias . '.' . $fieldName) . ')',
102  'LIKE',
103  $this->queryBuilder->createNamedParameter(mb_strtolower($like), \PDO::PARAM_STR)
104  )
105  );
106  }
107 
108  // Assemble the search condition only if the field makes sense to be searched
109  if ($fieldType === 'text'
110  || $fieldType === 'flex'
111  || ($fieldType === 'input' && (!$evalRules || !preg_match('/\b(?:date|time|int)\b/', $evalRules)))
112  ) {
113  $constraintsForParts[] = $searchConstraint;
114  }
115  }
116  $constraints[] = $this->queryBuilder->expr()->orX(...$constraintsForParts);
117  }
118 
119  return $this->queryBuilder->expr()->andX(...$constraints);
120  }
121 
128  private function ‪extractSearchableFieldsFromTable(string $tableName): array
129  {
130  if ($searchFields = $this->searchDemand->getSearchFields()) {
131  if (empty($searchFields[$tableName])) {
132  return [];
133  }
134  foreach ($searchFields[$tableName] as $searchField) {
135  if (!isset(‪$GLOBALS['TCA'][$tableName]['columns'][$searchField])) {
136  throw new \RuntimeException(sprintf('Cannot use search field "%s" because it is not defined in TCA.', $searchField), 1556367071);
137  }
138  }
139 
140  return $searchFields;
141  }
142  $fieldListArray = [];
143  // Get the list of fields to search in from the TCA, if any
144  if (isset(‪$GLOBALS['TCA'][$tableName]['ctrl']['searchFields'])) {
145  $fieldListArray = GeneralUtility::trimExplode(',', ‪$GLOBALS['TCA'][$tableName]['ctrl']['searchFields'], true);
146  }
147 
148  return $fieldListArray;
149  }
150 }
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:33
‪TYPO3\CMS\Core\Resource\Search\QueryRestrictions\SearchTermRestriction\buildExpression
‪buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder)
Definition: SearchTermRestriction.php:44
‪TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionInterface
Definition: QueryRestrictionInterface.php:25
‪TYPO3\CMS\Core\Resource\Search\QueryRestrictions\SearchTermRestriction\$queryBuilder
‪QueryBuilder $queryBuilder
Definition: SearchTermRestriction.php:36
‪TYPO3\CMS\Core\Resource\Search\QueryRestrictions\SearchTermRestriction\$searchDemand
‪FileSearchDemand $searchDemand
Definition: SearchTermRestriction.php:32
‪TYPO3\CMS\Core\Resource\Search\QueryRestrictions\SearchTermRestriction\extractSearchableFieldsFromTable
‪array extractSearchableFieldsFromTable(string $tableName)
Definition: SearchTermRestriction.php:126
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:47
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression
Definition: CompositeExpression.php:23
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand
Definition: FileSearchDemand.php:24
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\orX
‪CompositeExpression orX(... $expressions)
Definition: ExpressionBuilder.php:80
‪TYPO3\CMS\Core\Resource\Search\QueryRestrictions
Definition: ConsistencyRestriction.php:3
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Resource\Search\QueryRestrictions\SearchTermRestriction\makeQuerySearchByTable
‪CompositeExpression makeQuerySearchByTable(string $tableName, string $tableAlias)
Definition: SearchTermRestriction.php:64
‪TYPO3\CMS\Core\Resource\Search\QueryRestrictions\SearchTermRestriction\__construct
‪__construct(FileSearchDemand $searchDemand, QueryBuilder $queryBuilder)
Definition: SearchTermRestriction.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Search\QueryRestrictions\SearchTermRestriction
Definition: SearchTermRestriction.php:29