[TASK] Doctrine: Migrate ExtendedTemplateService 76/48476/11
authorIan SEBBAGH <ianouf@gmail.com>
Tue, 7 Jun 2016 12:15:42 +0000 (14:15 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Wed, 8 Jun 2016 10:43:00 +0000 (12:43 +0200)
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>
typo3/sysext/core/Classes/TypoScript/ExtendedTemplateService.php

index 5fbd70c..3931f44 100644 (file)
@@ -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,59 +856,79 @@ 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.
      *
@@ -1653,14 +1675,6 @@ class ExtendedTemplateService extends TemplateService
     }
 
     /**
-     * @return DatabaseConnection
-     */
-    protected function getDatabaseConnection()
-    {
-        return $GLOBALS['TYPO3_DB'];
-    }
-
-    /**
      * @return LanguageService
      */
     protected function getLanguageService()