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