[BUGFIX] Allow multiple search words for file search 04/61704/2
authorMarkus Klein <markus.klein@typo3.org>
Fri, 13 Sep 2019 21:25:56 +0000 (23:25 +0200)
committerDaniel Goerz <daniel.goerz@posteo.de>
Thu, 10 Oct 2019 14:08:24 +0000 (16:08 +0200)
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/+/61704
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Henning Liebe <h.liebe@neusta.de>
Tested-by: Jörg Bösche <typo3@joergboesche.de>
Tested-by: Felix P. <f.pachowsky@neusta.de>
Tested-by: Julian Geils <j_geils@web.de>
Tested-by: Steffen Frese <steffenf14@gmail.com>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Henning Liebe <h.liebe@neusta.de>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Felix P. <f.pachowsky@neusta.de>
Reviewed-by: Julian Geils <j_geils@web.de>
Reviewed-by: Steffen Frese <steffenf14@gmail.com>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
typo3/sysext/core/Classes/Resource/Search/QueryRestrictions/SearchTermRestriction.php

index 7eb9ad0..6fd1d9b 100644 (file)
@@ -69,45 +69,54 @@ class SearchTermRestriction implements QueryRestrictionInterface
         $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);
     }
 
     /**