From 90ab140f8c3cd3c93fd4c82dd5ba3c2d1b8d861b Mon Sep 17 00:00:00 2001
From: Torsten Oppermann <torsten@sgalinski.de>
Date: Thu, 13 Sep 2018 16:27:00 +0200
Subject: [PATCH] [TASK] Implementing signal slot for sitemap generation

---
 Classes/SignalSlot/SitemapSignalSlot.php | 61 ++++++++++++++++++++++--
 ext_localconf.php                        |  8 ++--
 2 files changed, 62 insertions(+), 7 deletions(-)

diff --git a/Classes/SignalSlot/SitemapSignalSlot.php b/Classes/SignalSlot/SitemapSignalSlot.php
index 43f33576..5e3fffb1 100644
--- a/Classes/SignalSlot/SitemapSignalSlot.php
+++ b/Classes/SignalSlot/SitemapSignalSlot.php
@@ -26,21 +26,76 @@ namespace SGalinski\SgJobs\SignalSlot;
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
 
+use TYPO3\CMS\Core\Database\DatabaseConnection;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+
 /**
  * Signal functions regarding sg_seo sitemap generation
  */
 class SitemapSignalSlot {
 
+	const PLUGIN_NAME = 'sgjobs_jobapplication';
+
+	/**
+	 * @var UriBuilder
+	 */
+	protected $uriBuilder;
+
 	/**
 	 * before the sitemap is generated
 	 *
 	 * @param array $pageList
 	 */
 	public function beforeSitemapGeneration(array &$pageList) {
+		/** @var $databaseConnection DatabaseConnection */
+		$databaseConnection = $GLOBALS['TYPO3_DB'];
+
+		// find sites where job detail plugin is added
+		$rows = $databaseConnection->exec_SELECTgetRows(
+			'pid, pages',
+			'tt_content',
+			'CType = "list" and list_type = "' . self::PLUGIN_NAME . '" and hidden = 0 and deleted = 0'
+		);
+
+		foreach ($rows as $row) {
+			$jobs = $this->getJobsByPid($row['pages']);
 
+			$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
+			$request = $objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Request::class);
+			$request->setRequestUri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
+			$request->setBaseUri(GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
+			$this->uriBuilder = $objectManager->get(UriBuilder::class);
+
+			foreach ($jobs as $job) {
+				$url = $this->uriBuilder->reset()->setTargetPageUid($row['pid'])->setArguments(
+					['tx_sgjobs_jobapplication' => ['jobId' => $job['uid']]]
+				)->buildFrontendUri();
+
+				$pageList[] = [
+					'url' => $url,
+					'title' => ''
+				];
+			}
+		}
+	}
+
+	/**
+	 * get all jobs stored on specific page ids
+	 *
+	 * @param string $pid
+	 * @return array|null
+	 */
+	private function getJobsByPid($pid) {
+		/** @var $databaseConnection DatabaseConnection */
+		$databaseConnection = $GLOBALS['TYPO3_DB'];
+		$rows = $databaseConnection->exec_SELECTgetRows(
+			'*',
+			'tx_sgjobs_domain_model_job',
+			'pid IN (' . $pid . ')'
+		);
 
-		// @todo
-		# find sites where job detail plugin is added
-		# add entry for each job as parameter pid=x jobuid = y to pagelist
+		return $rows;
 	}
 }
diff --git a/ext_localconf.php b/ext_localconf.php
index 4a6ec216..a801f268 100644
--- a/ext_localconf.php
+++ b/ext_localconf.php
@@ -66,8 +66,8 @@ $signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
 	\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class
 );
 $signalSlotDispatcher->connect(
-	\TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
-	'beforeSitemapGeneration',
-	SGalinski\SgSeo\Generator\PagesSitemapGenerator::class,
-	'accessPageList'
+	\SGalinski\SgSeo\Generator\PagesSitemapGenerator::class,
+	'accessPageList',
+	\SGalinski\SgJobs\SignalSlot\SitemapSignalSlot::class,
+	'beforeSitemapGeneration'
 );
-- 
GitLab