[FEATURE] Migrate suggest receiver to Doctrine 30/51230/3
authorStanislas Rolland <typo3@sjbr.ca>
Tue, 10 Jan 2017 02:41:30 +0000 (21:41 -0500)
committerStanislas Rolland <typo3@sjbr.ca>
Tue, 10 Jan 2017 02:46:42 +0000 (03:46 +0100)
Change-Id: Ibd7d26712935998948884a34dff071a9de619528
Resolves: #79229
Reviewed-on: https://review.typo3.org/51230
Reviewed-by: Stanislas Rolland <typo3@sjbr.ca>
Tested-by: Stanislas Rolland <typo3@sjbr.ca>
ChangeLog
Classes/Hook/Backend/Form/Wizard/SuggestReceiver.php

index 9c54d6b..40baa8c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,7 @@
        * Resolves #77245: Add languages "Swiss" and "Austrian"
        * Resolves #77246: Missing extbase declaration for field "static_lang_isocode" in sys_language
        * Follow-up to Resolves #79209: Migrate update script to Doctrine
+       * Resolves #79229: Migrate suggest receiver to Doctrine
 
 2017-01-08  Stanislas Rolland  <typo3(arobas)sjbr.ca>
 
index 0aa6b6d..fd9102d 100644 (file)
@@ -40,15 +40,71 @@ use SJBR\StaticInfoTables\Utility\LocalizationUtility;
  * @author Benjamin Mack <benni@typo3.org>
  * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
  */
-class SuggestReceiver extends \TYPO3\CMS\Backend\Form\Wizard\SuggestWizardDefaultReceiver {
+class SuggestReceiver extends \TYPO3\CMS\Backend\Form\Wizard\SuggestWizardDefaultReceiver
+{
+    /**
+     * Prepare the statement for selecting the records which will be returned to the selector. May also return some
+     * other records (e.g. from a mm-table) which will be used later on to select the real records
+     *
+     * @return void
+     */
+    protected function prepareSelectStatement()
+    {
+       if (class_exists(\TYPO3\CMS\Core\Database\ConnectionPool::class)) {
+               $expressionBuilder = $this->queryBuilder->expr();
+                       $searchWholePhrase = !isset($this->config['searchWholePhrase']) || $this->config['searchWholePhrase'];
+                       $searchString = $this->params['value'];
+                       $searchUid = (int)$searchString;
+                       if ($searchString !== '') {
+                               $likeCondition = ($searchWholePhrase ? '%' : '') . $searchString . '%';
+                               // Get the label field for the current language, if any is available
+                               $lang = LocalizationUtility::getCurrentLanguage();
+                               $lang = LocalizationUtility::getIsoLanguageKey($lang);
+                               $labelFields = LocalizationUtility::getLabelFields($this->table, $lang);
+                               $selectFieldsList = $labelFields[0] . ',' . $this->config['additionalSearchFields'];
+                               $selectFields = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $selectFieldsList, true);
+                               $selectFields = array_unique($selectFields);
+
+                               $selectParts = $expressionBuilder->orX();
+                               foreach ($selectFields as $field) {
+                                       $selectParts->add($expressionBuilder->like($field, $this->queryBuilder->createPositionalParameter($likeCondition)));
+                               }
+
+                               $searchClause = $expressionBuilder->orX($selectParts);
+                               if ($searchUid > 0 && $searchUid == $searchString) {
+                                       $searchClause->add($expressionBuilder->eq('uid', $searchUid));
+                               }
+
+                               $this->queryBuilder->andWhere($expressionBuilder->orX($searchClause));
+                       }
+                       if (!empty($this->allowedPages)) {
+                               $pidList = array_map('intval', $this->allowedPages);
+                               if (!empty($pidList)) {
+                                       $this->queryBuilder->andWhere(
+                                               $expressionBuilder->in('pid', $pidList)
+                                       );
+                               }
+                       }
+                       // add an additional search condition comment
+                       if (isset($this->config['searchCondition']) && $this->config['searchCondition'] !== '') {
+                               $this->queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($this->config['searchCondition']));
+                       }
+               } else {
+                       // TYPO3 CMS 7 LTS
+                       $this->prepareCompatibleSelectStatement();
+               }
+    }
 
        /**
+        * For TYPO3 CMS 7 LTS
+        *
         * Prepare the statement for selecting the records which will be returned to the selector. May also return some
         * other records (e.g. from a mm-table) which will be used later on to select the real records
         *
         * @return void
         */
-       protected function prepareSelectStatement() {
+       protected function prepareCompatibleSelectStatement()
+       {
                $searchWholePhrase = $this->config['searchWholePhrase'];
                $searchString = $this->params['value'];
                $searchUid = intval($searchString);
@@ -94,7 +150,36 @@ class SuggestReceiver extends \TYPO3\CMS\Backend\Form\Wizard\SuggestWizardDefaul
         *
         * @return void
         */
-       protected function prepareOrderByStatement() {
+       protected function prepareOrderByStatement()
+       {
+               if (class_exists(\TYPO3\CMS\Core\Database\ConnectionPool::class)) {
+                       // Get the label field for the current language, if any is available
+                       $lang = LocalizationUtility::getCurrentLanguage();
+                       $lang = LocalizationUtility::getIsoLanguageKey($lang);
+                       $labelFields = LocalizationUtility::getLabelFields($this->table, $lang);
+                       if (!empty($labelFields)) {
+                               foreach ($labelFields as $labelField) {
+                                       $this->queryBuilder->addOrderBy($labelField);
+                               }
+                       } else  if ($GLOBALS['TCA'][$this->table]['ctrl']['label']) {
+                               $this->queryBuilder->addOrderBy($GLOBALS['TCA'][$this->table]['ctrl']['label']);
+                       }
+               } else {
+                       // TYPO3 CMS 7 LTS
+                       $this->prepareCompatibleOrderByStatement();
+               }
+       }
+
+       /**
+        * For TYPO3 CMS 7 LTS
+        *
+        * Prepares the clause by which the result elements are sorted. See description of ORDER BY in
+        * SQL standard for reference.
+        *
+        * @return void
+        */
+       protected function prepareCompatibleOrderByStatement()
+       {
                if ($GLOBALS['TCA'][$this->table]['ctrl']['label']) {
                        $this->orderByStatement = $GLOBALS['TCA'][$this->table]['ctrl']['label'];
                }