Skip to content

Commit

Permalink
[BUGFIX] Allow multiple search words for file search
Browse files Browse the repository at this point in the history
This functionality was removed with the refactoring of
the search API in FAL and is therefore restored.

Resolves: #89168
Releases: master, 9.5
Change-Id: Ic39340c6ec6858355302757adefd1fb94dbc9e47
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61929
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
  • Loading branch information
liayn authored and ervaude committed Oct 10, 2019
1 parent fe83520 commit 16bed38
Showing 1 changed file with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,45 +69,54 @@ private function makeQuerySearchByTable(string $tableName, string $tableAlias):
$searchTerm = $this->searchDemand->getSearchTerm();
$constraints = [];

$like = '%' . $this->queryBuilder->escapeLikeWildcards($searchTerm) . '%';
foreach ($fieldsToSearchWithin as $fieldName) {
if (!isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName])) {
$searchTermParts = str_getcsv($searchTerm, ' ');
foreach ($searchTermParts as $searchTermPart) {
$searchTermPart = trim($searchTermPart);
if ($searchTermPart === '') {
continue;
}
$fieldConfig = $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'];
$fieldType = $fieldConfig['type'];
$evalRules = $fieldConfig['eval'] ?? '';
$constraintsForParts = [];
$like = '%' . $this->queryBuilder->escapeLikeWildcards($searchTermPart) . '%';
foreach ($fieldsToSearchWithin as $fieldName) {
if (!isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName])) {
continue;
}
$fieldConfig = $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'];
$fieldType = $fieldConfig['type'];
$evalRules = $fieldConfig['eval'] ?? '';

// Check whether search should be case-sensitive or not
if (is_array($fieldConfig['search']) && in_array('case', $fieldConfig['search'], true)) {
// case sensitive
$searchConstraint = $this->queryBuilder->expr()->andX(
$this->queryBuilder->expr()->like(
$tableAlias . '.' . $fieldName,
$this->queryBuilder->createNamedParameter($like, \PDO::PARAM_STR)
)
);
} else {
$searchConstraint = $this->queryBuilder->expr()->andX(
// Check whether search should be case-sensitive or not
if (is_array($fieldConfig['search']) && in_array('case', $fieldConfig['search'], true)) {
// case sensitive
$searchConstraint = $this->queryBuilder->expr()->andX(
$this->queryBuilder->expr()->like(
$tableAlias . '.' . $fieldName,
$this->queryBuilder->createNamedParameter($like, \PDO::PARAM_STR)
)
);
} else {
$searchConstraint = $this->queryBuilder->expr()->andX(
// case insensitive
$this->queryBuilder->expr()->comparison(
'LOWER(' . $this->queryBuilder->quoteIdentifier($tableAlias . '.' . $fieldName) . ')',
'LIKE',
$this->queryBuilder->createNamedParameter(mb_strtolower($like), \PDO::PARAM_STR)
)
);
}
$this->queryBuilder->expr()->comparison(
'LOWER(' . $this->queryBuilder->quoteIdentifier($tableAlias . '.' . $fieldName) . ')',
'LIKE',
$this->queryBuilder->createNamedParameter(mb_strtolower($like), \PDO::PARAM_STR)
)
);
}

// Assemble the search condition only if the field makes sense to be searched
if ($fieldType === 'text'
|| $fieldType === 'flex'
|| ($fieldType === 'input' && (!$evalRules || !preg_match('/date|time|int/', $evalRules)))
) {
$constraints[] = $searchConstraint;
// Assemble the search condition only if the field makes sense to be searched
if ($fieldType === 'text'
|| $fieldType === 'flex'
|| ($fieldType === 'input' && (!$evalRules || !preg_match('/date|time|int/', $evalRules)))
) {
$constraintsForParts[] = $searchConstraint;
}
}
$constraints[] = $this->queryBuilder->expr()->orX(...$constraintsForParts);
}

return $this->queryBuilder->expr()->orX(...$constraints);
return $this->queryBuilder->expr()->andX(...$constraints);
}

/**
Expand Down

0 comments on commit 16bed38

Please sign in to comment.