From 730704ade86268b42fddcca59866c680e25d7168 Mon Sep 17 00:00:00 2001
From: Torsten Oppermann <torsten@sgalinski.de>
Date: Fri, 28 Jul 2017 09:32:42 +0200
Subject: [PATCH] [TASK] Adding service function to read all categories for a
 siteroot id

---
 Classes/Controller/BackendController.php |  5 ++-
 Classes/Service/Backend/Utility.php      | 49 ++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/Classes/Controller/BackendController.php b/Classes/Controller/BackendController.php
index fa7812c..498055b 100644
--- a/Classes/Controller/BackendController.php
+++ b/Classes/Controller/BackendController.php
@@ -70,10 +70,13 @@ class BackendController extends ActionController {
 		$backendUser = $GLOBALS['BE_USER'];
 		$pageInfo = BackendUtility::readPageAccess($pageUid, $backendUser->getPagePermsClause(1));
 		$this->docHeaderComponent = GeneralUtility::makeInstance(DocHeaderComponent::class);
-
 		$this->docHeaderComponent->setMetaInformation($pageInfo);
 		Utility::makeButtons($this->docHeaderComponent, $this->request);
 
+		// retrieve next site root id
+		$siteRootId = Utility::getSiteRoot((int) GeneralUtility::_GP('id'));
+		Utility::getCategoriesForSiteRoot($siteRootId);
+
 		$this->view->assign('docHeader', $this->docHeaderComponent->docHeaderContent());
 		$this->view->assign('pageUid', $pageUid);
 	}
diff --git a/Classes/Service/Backend/Utility.php b/Classes/Service/Backend/Utility.php
index 789d240..7f4dbb9 100644
--- a/Classes/Service/Backend/Utility.php
+++ b/Classes/Service/Backend/Utility.php
@@ -28,6 +28,9 @@ namespace SGalinski\SgNews\Service\Backend;
 
 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
 use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent;
+use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\DatabaseConnection;
+use TYPO3\CMS\Core\Database\QueryGenerator;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -38,6 +41,7 @@ use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
  * Class Utility
  */
 class Utility {
+	const CATEGORY_DOKTYPE = 117;
 
 	/**
 	 * create buttons for the backend module header
@@ -75,4 +79,49 @@ class Utility {
 		$buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);
 		$docHeaderComponent->getButtonBar();
 	}
+
+	/**
+	 * Retrieves the next site root in the page hierarchy from the current page
+	 *
+	 * @param int $currentPid
+	 * @return int
+	 */
+	public static function getSiteRoot($currentPid) {
+		$rootLine = BackendUtility::BEgetRootLine((int) $currentPid);
+		$siteRoot = ['uid' => 0];
+
+		foreach ($rootLine as $page) {
+			if ($page['is_siteroot'] === '1') {
+				$siteRoot = $page;
+				break;
+			}
+		}
+
+		return $siteRoot['uid'];
+	}
+
+	/**
+	 * Get an array of all category titles below the given site root id
+	 *
+	 * @param $siteRootPid
+	 * @throws \InvalidArgumentException
+	 * @return array
+	 */
+	public static function getCategoriesForSiteRoot($siteRootPid) {
+		// get all pageids below the given siteroot
+		$queryGenerator = GeneralUtility::makeInstance(QueryGenerator::class);
+		$childPids = $queryGenerator->getTreeList($siteRootPid, PHP_INT_MAX, 0, 1);
+
+		// if doktype = 117 (category) then get the category name (page title)
+		/** @var DatabaseConnection $databaseConnection */
+		$databaseConnection = $GLOBALS['TYPO3_DB'];
+
+		$where = 'doktype = ' . self::CATEGORY_DOKTYPE . ' AND uid in (' . $childPids . ')';
+		$result = $databaseConnection->exec_SELECTquery('title', 'pages', $where)->fetch_all();
+		$categories = [];
+		foreach($result as $item) {
+			$categories[] = $item[0];
+		}
+		return $categories;
+	}
 }
-- 
GitLab