From b6932da7127f2b16e52ba61b14a21fbfbd80637b Mon Sep 17 00:00:00 2001
From: Ian SEBBAGH <ianouf@gmail.com>
Date: Tue, 7 Jun 2016 14:15:42 +0200
Subject: [PATCH] [TASK] Doctrine: Migrate ExtendedTemplateService

Releases: master
Resolves: #76486
Change-Id: Idf39b190d92250c51fdb3f91faf9144ba26b1caa
Reviewed-on: https://review.typo3.org/48476
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../TypoScript/ExtendedTemplateService.php    | 98 +++++++++++--------
 1 file changed, 56 insertions(+), 42 deletions(-)

diff --git a/typo3/sysext/core/Classes/TypoScript/ExtendedTemplateService.php b/typo3/sysext/core/Classes/TypoScript/ExtendedTemplateService.php
index 5fbd70c5ad26..3931f44b923f 100644
--- a/typo3/sysext/core/Classes/TypoScript/ExtendedTemplateService.php
+++ b/typo3/sysext/core/Classes/TypoScript/ExtendedTemplateService.php
@@ -16,13 +16,15 @@ namespace TYPO3\CMS\Core\TypoScript;
 
 use TYPO3\CMS\Backend\Template\DocumentTemplate;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\QueryBuilder;
+use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Core\Exception;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
 use TYPO3\CMS\Core\Utility\PathUtility;
-use TYPO3\CMS\Dbal\Database\DatabaseConnection;
 use TYPO3\CMS\Frontend\Configuration\TypoScript\ConditionMatching\ConditionMatcher;
 use TYPO3\CMS\Lang\LanguageService;
 
@@ -854,58 +856,78 @@ class ExtendedTemplateService extends TemplateService
     }
 
     /**
-     * @param int $id
-     * @param int $template_uid
-     * @return array|NULL Returns the template record or NULL if none was found
+     * Get a single sys_template record attached to a single page.
+     * If multiple template records are on this page, the first (order by sorting)
+     * record will be returned, unless a specific template uid is specified via $templateUid
+     *
+     * @param int $pid The pid to select sys_template records from
+     * @param int $templateUid Optional template uid
+     * @return array|null Returns the template record or null if none was found
      */
-    public function ext_getFirstTemplate($id, $template_uid = 0)
+    public function ext_getFirstTemplate($pid, $templateUid = 0)
     {
+        if (empty($pid)) {
+            return null;
+        }
+
         // Query is taken from the runThroughTemplates($theRootLine) function in the parent class.
-        if ((int)$id) {
-            $addC = $template_uid ? ' AND uid=' . (int)$template_uid : '';
-            $where = 'pid=' . (int)$id . $addC . BackendUtility::deleteClause('sys_template');
-            $res = $this->getDatabaseConnection()->exec_SELECTquery('*', 'sys_template', $where, '', 'sorting', '1');
-            $row = $this->getDatabaseConnection()->sql_fetch_assoc($res);
-            BackendUtility::workspaceOL('sys_template', $row);
-            $this->getDatabaseConnection()->sql_free_result($res);
-            // Returns the template row if found.
-            return $row;
+        $queryBuilder = $this->getTemplateQueryBuilder($pid)
+            ->setMaxResults(1);
+        if ($templateUid) {
+            $queryBuilder->andWhere($queryBuilder->expr()->eq('uid', (int)$templateUid));
         }
-        return null;
+        $row = $queryBuilder->execute()->fetch();
+        BackendUtility::workspaceOL('sys_template', $row);
+
+        return $row;
     }
 
     /**
-     * @param int $id
+     * Get an array of all template records on a page.
+     *
+     * @param int $pid Pid to fetch sys_template records for
      * @return array[] Array of template records
      */
-    public function ext_getAllTemplates($id)
+    public function ext_getAllTemplates($pid): array
     {
-        if (!$id) {
-            return array();
+        if (empty($pid)) {
+            return [];
         }
-
-        // Query is taken from the runThroughTemplates($theRootLine) function in the parent class.
-        $res = $this->getDatabaseConnection()->exec_SELECTquery(
-            '*',
-            'sys_template',
-            'pid=' . (int)$id
-                . BackendUtility::deleteClause('sys_template'),
-            '',
-            'sorting'
-        );
-
-        $outRes = array();
-        while ($row = $this->getDatabaseConnection()->sql_fetch_assoc($res)) {
+        $result = $this->getTemplateQueryBuilder($pid)->execute();
+        $outRes = [];
+        while ($row = $result->fetch()) {
             BackendUtility::workspaceOL('sys_template', $row);
             if (is_array($row)) {
                 $outRes[] = $row;
             }
         }
-        $this->getDatabaseConnection()->sql_free_result($res);
-
         return $outRes;
     }
 
+    /**
+     * Internal helper method to prepare the query builder for
+     * getting sys_template records from a given pid
+     *
+     * @param int $pid The pid to select sys_template records from
+     * @return QueryBuilder Returns a QueryBuilder
+     */
+    protected function getTemplateQueryBuilder(int $pid): QueryBuilder
+    {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('sys_template');
+        $queryBuilder->getRestrictions()
+            ->removeAll()
+            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+        $queryBuilder->select('*')
+            ->from('sys_template')
+            ->where($queryBuilder->expr()->eq('pid', (int)$pid));
+        if (!empty($GLOBALS['TCA']['sys_template']['ctrl']['sortby'])) {
+            $queryBuilder->orderBy($GLOBALS['TCA']['sys_template']['ctrl']['sortby']);
+        }
+
+        return $queryBuilder;
+    }
+
     /**
      * This function compares the flattened constants (default and all).
      * Returns an array with the constants from the whole template which may be edited by the module.
@@ -1652,14 +1674,6 @@ class ExtendedTemplateService extends TemplateService
         return isset($GLOBALS['rootLine']) ? $GLOBALS['rootLine'] : array();
     }
 
-    /**
-     * @return DatabaseConnection
-     */
-    protected function getDatabaseConnection()
-    {
-        return $GLOBALS['TYPO3_DB'];
-    }
-
     /**
      * @return LanguageService
      */
-- 
GitLab