[FEATURE] Support multiple UID in PageRepository::getMenu() 42/36042/7
authorChristian Weiske <christian.weiske@netresearch.de>
Fri, 16 Jan 2015 12:56:18 +0000 (13:56 +0100)
committerMarkus Klein <klein.t3@reelworx.at>
Mon, 19 Jan 2015 01:20:47 +0000 (02:20 +0100)
Instead of accepting only a single page ID, PageRepository::getMenu()
now also supports an array of page IDs.
This can be used to fetch the child pages of several pages with a
single SQL query.

Change-Id: I8a8b434177bf9b28a6295a746113c9519bbac997
Resolves: #64257
Releases: master
Reviewed-on: http://review.typo3.org/36042
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
typo3/sysext/core/Documentation/Changelog/master/Feature-64257-MultipleUidInPageRepositoryGetMenu.rst [new file with mode: 0644]
typo3/sysext/frontend/Classes/Page/PageRepository.php
typo3/sysext/frontend/Tests/Functional/Fixtures/pages.xml [new file with mode: 0644]
typo3/sysext/frontend/Tests/Functional/Page/PageRepositoryTest.php [new file with mode: 0644]

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-64257-MultipleUidInPageRepositoryGetMenu.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-64257-MultipleUidInPageRepositoryGetMenu.rst
new file mode 100644 (file)
index 0000000..717e73f
--- /dev/null
@@ -0,0 +1,18 @@
+===================================================================
+Feature: #64257 - Support multiple UID in PageRepository::getMenu()
+===================================================================
+
+Description
+===========
+
+An array of ``uid`` values can be passed to
+``PageRepository::getMenu()``, providing the chance to build menues
+from several roots.
+
+Example: Fetch children of pages #2 and #3::
+
+.. code-block:: php
+
+  $pageRepository = new \TYPO3\CMS\Frontend\Page\PageRepository();
+  $pageRepository->init(false);
+  $rows = $pageRepository->getMenu(array(2, 3));
index c433e46..cc14f3c 100644 (file)
@@ -480,7 +480,7 @@ class PageRepository {
         * If the $uid being input does in itself require MPvars to define a correct
         * rootline these must be handled externally to this function.
         *
-        * @param int $uid The page id for which to fetch subpages (PID)
+        * @param int|int[] $uid The page id (or array of page ids) for which to fetch subpages (PID)
         * @param string $fields List of fields to select. Default is "*" = all
         * @param string $sortField The field to sort by. Default is "sorting
         * @param string $addWhere Optional additional where clauses. Like "AND title like '%blabla%'" for instance.
@@ -491,8 +491,15 @@ class PageRepository {
         */
        public function getMenu($uid, $fields = '*', $sortField = 'sorting', $addWhere = '', $checkShortcuts = TRUE) {
                $output = array();
-               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, 'pages', 'pid=' . (int)$uid . $this->where_hid_del . $this->where_groupAccess . ' ' . $addWhere, '', $sortField);
-               while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
+                       $fields,
+                       'pages',
+                       'pid IN (' . implode(',', $GLOBALS['TYPO3_DB']->cleanIntArray((array)$uid)) . ')' . $this->where_hid_del
+                               . $this->where_groupAccess . ' ' . $addWhere,
+                       '',
+                       $sortField
+               );
+               while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
                        $this->versionOL('pages', $row, TRUE);
                        if (is_array($row)) {
                                // Keep mount point:
diff --git a/typo3/sysext/frontend/Tests/Functional/Fixtures/pages.xml b/typo3/sysext/frontend/Tests/Functional/Fixtures/pages.xml
new file mode 100644 (file)
index 0000000..3184be4
--- /dev/null
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="utf-8"?>
+<dataset>
+    <pages>
+        <uid>1</uid>
+        <pid>0</pid>
+        <title>Root 1</title>
+        <deleted>0</deleted>
+        <perms_everybody>15</perms_everybody>
+    </pages>
+    <pages>
+        <uid>2</uid>
+        <pid>1</pid>
+        <title>Dummy 1-2</title>
+        <deleted>0</deleted>
+        <perms_everybody>15</perms_everybody>
+    </pages>
+    <pages>
+        <uid>3</uid>
+        <pid>1</pid>
+        <title>Dummy 1-3</title>
+        <deleted>0</deleted>
+        <perms_everybody>15</perms_everybody>
+    </pages>
+    <pages>
+        <uid>4</uid>
+        <pid>1</pid>
+        <title>Dummy 1-4</title>
+        <deleted>0</deleted>
+        <perms_everybody>15</perms_everybody>
+    </pages>
+    <pages>
+        <uid>5</uid>
+        <pid>2</pid>
+        <title>Dummy 1-2-5</title>
+        <deleted>0</deleted>
+        <perms_everybody>15</perms_everybody>
+    </pages>
+    <pages>
+        <uid>6</uid>
+        <pid>2</pid>
+        <title>Dummy 1-2-6</title>
+        <deleted>0</deleted>
+        <perms_everybody>15</perms_everybody>
+    </pages>
+    <pages>
+        <uid>7</uid>
+        <pid>2</pid>
+        <title>Dummy 1-2-7</title>
+        <deleted>0</deleted>
+        <perms_everybody>15</perms_everybody>
+    </pages>
+    <pages>
+        <uid>8</uid>
+        <pid>3</pid>
+        <title>Dummy 1-3-8</title>
+        <deleted>0</deleted>
+        <perms_everybody>15</perms_everybody>
+    </pages>
+    <pages>
+        <uid>9</uid>
+        <pid>3</pid>
+        <title>Dummy 1-3-9</title>
+        <deleted>0</deleted>
+        <perms_everybody>15</perms_everybody>
+    </pages>
+    <pages>
+        <uid>10</uid>
+        <pid>4</pid>
+        <title>Dummy 1-4-10</title>
+        <deleted>0</deleted>
+        <perms_everybody>15</perms_everybody>
+    </pages>
+</dataset>
+
diff --git a/typo3/sysext/frontend/Tests/Functional/Page/PageRepositoryTest.php b/typo3/sysext/frontend/Tests/Functional/Page/PageRepositoryTest.php
new file mode 100644 (file)
index 0000000..1102e17
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+namespace TYPO3\CMS\Frontend\Tests\Functional\Page;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Frontend\Page\PageRepository;
+
+/**
+ * Test case
+ */
+class PageRepositoryTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
+       /**
+        * @var string[]
+        */
+       protected $coreExtensionsToLoad = array();
+
+       /**
+        * @test
+        */
+       public function getMenuSingleUidRoot() {
+               $this->importDataSet(__DIR__ . '/../Fixtures/pages.xml');
+               $pageRepository = new PageRepository();
+               $pageRepository->init(false);
+
+               $rows = $pageRepository->getMenu(1, 'uid, title');
+               $this->assertArrayHasKey(2, $rows);
+               $this->assertArrayHasKey(3, $rows);
+               $this->assertArrayHasKey(4, $rows);
+               $this->assertCount(3, $rows);
+       }
+
+       /**
+        * @test
+        */
+       public function getMenuSingleUidSubpage() {
+               $this->importDataSet(__DIR__ . '/../Fixtures/pages.xml');
+               $pageRepository = new PageRepository();
+               $pageRepository->init(false);
+
+               $rows = $pageRepository->getMenu(2, 'uid, title');
+               $this->assertArrayHasKey(5, $rows);
+               $this->assertArrayHasKey(6, $rows);
+               $this->assertArrayHasKey(7, $rows);
+               $this->assertCount(3, $rows);
+       }
+
+       /**
+        * @test
+        */
+       public function getMenuMulipleUid() {
+               $this->importDataSet(__DIR__ . '/../Fixtures/pages.xml');
+               $pageRepository = new PageRepository();
+               $pageRepository->init(false);
+
+               $rows = $pageRepository->getMenu(array(2, 3), 'uid, title');
+               $this->assertArrayHasKey(5, $rows);
+               $this->assertArrayHasKey(6, $rows);
+               $this->assertArrayHasKey(7, $rows);
+               $this->assertArrayHasKey(8, $rows);
+               $this->assertArrayHasKey(9, $rows);
+               $this->assertCount(5, $rows);
+       }
+}