From fe3c5bcd264d02c92e9d65a1889211f48f49fe9e Mon Sep 17 00:00:00 2001
From: Torsten Oppermann <torsten@sgalinski.de>
Date: Mon, 23 Jul 2018 11:58:25 +0200
Subject: [PATCH] [TASK] Implementing cateegory map, reespecting corect title &
 ceategory translations

---
 .../Command/MigrateNewsCommandController.php  | 74 ++++++++++++-------
 1 file changed, 49 insertions(+), 25 deletions(-)

diff --git a/Classes/Command/MigrateNewsCommandController.php b/Classes/Command/MigrateNewsCommandController.php
index 1c6bd74..a2163b4 100644
--- a/Classes/Command/MigrateNewsCommandController.php
+++ b/Classes/Command/MigrateNewsCommandController.php
@@ -83,20 +83,28 @@ class MigrateNewsCommandController extends CommandController {
 	 */
 	protected $languageMap = [];
 
+	/**
+	 * @var array $languageMap
+	 */
+	protected $categoryMap = [];
+
 	/**
 	 * @param string $copyPageId the page id of the template that should be copied
 	 * @param int $categoryPid the page id of the category page
 	 * @param int $year only news from that year will be migrated
 	 * @param string $languageMapAsJson a json, mapping language ids (old => new). this is needed if the sys_language_uids have changed
+	 * @param string $categoryMapAsJson a json, mapping sys_category ids (old => new). t
 	 *
 	 * @throws IllegalObjectTypeException
 	 * @throws UnknownObjectException
 	 */
 	public function runMigrateNewsCommand(
 		$copyPageId, $categoryPid, $year = 2015,
-		$languageMapAsJson = '{"3":1,"1":0,"2":2,"0":3}'
+		$languageMapAsJson = '{"3":1,"1":0,"2":2,"0":3}',
+		$categoryMapAsJson = '{"2":10,"3":11,"4":12,"5":13,"6":14,"7":15,"8":16,"9":17}'
 	) {
 		$this->languageMap = json_decode($languageMapAsJson, TRUE);
+		$this->categoryMap = json_decode($categoryMapAsJson, TRUE);
 
 		/** @var DatabaseConnection $db */
 		$db = $GLOBALS['TYPO3_DB'];
@@ -121,7 +129,6 @@ class MigrateNewsCommandController extends CommandController {
 			if ((int) date('Y', ($row['datetime'])) !== $year) {
 				continue;
 			}
-
 			$resultArray[] = $row;
 
 			// if no l10n_parent exists, create a copy of the page
@@ -141,11 +148,13 @@ class MigrateNewsCommandController extends CommandController {
 				$newsPage = $this->newsRepository->findByUidIgnoreEnableFields($newPageId);
 
 				if ($newsPage !== NULL) {
-					// @todo create tt content element, map images, author, categories etc.
 					$title = date('Y-m-d', $row['datetime']) . ' - ' . $row['title'];
 					$newsPage->setTitle($title);
-					$newsPage->setAuthor($row['author']);
-					$newsPage->addTag($this->getMatchingTag($row));
+
+					$matchingTag = $this->getMatchingTag($row);
+					if ($matchingTag && $matchingTag instanceof Tag) {
+						$newsPage->addTag($matchingTag);
+					}
 
 					$this->newsRepository->update($newsPage);
 					$this->persistenceManager->persistAll();
@@ -162,32 +171,20 @@ class MigrateNewsCommandController extends CommandController {
 
 	/**
 	 * @param array $row
-	 * @return Tag $tag
+	 * @return Object $tag
 	 *
 	 * @throws IllegalObjectTypeException
 	 * @throws UnknownObjectException
 	 */
 	private function getMatchingTag(array $row) {
-		/** @var Tag $matchingTag */
-		$matchingTag = $this->tagRepository->findByUid((int) $row['categories']);
-
-		if ($matchingTag === NULL) {
-			$matchingTag = $this->objectManager->get(Tag::class);
-
-			/** @var DatabaseConnection $db */
-			$db = $GLOBALS['TYPO3_DB'];
-			$where = 'uid = ' . $row['categories'];
-
-			/** @var \mysqli_result $result */
-			$result = $db->exec_SELECTquery('title', 'sys_category', $where);
-			$matchingTag->setTitle($result->fetch_row()[0]);
-			$matchingTag->setPid(1);
-			$this->tagRepository->add($matchingTag);
+		// look up the correct category id, if they have changed
+		if (isset($this->categoryMap[(int) $row['categories']])) {
+			$categoryId = $this->categoryMap[(int) $row['categories']];
+		} else {
+			$categoryId = (int) $row['categories'];
 		}
 
-		$this->persistenceManager->persistAll();
-
-		return $matchingTag;
+		return $this->tagRepository->findByUid($categoryId);
 	}
 
 	/**
@@ -209,7 +206,7 @@ class MigrateNewsCommandController extends CommandController {
 		$result = $db->exec_SELECTquery('l18n_parent', 'tt_content', $where);
 		$originalContentElement = $result->fetch_row();
 
-		// if its the new defaul, there is no l18n_parent
+		// if its the new default, there is no l18n_parent
 		if ((int) $this->languageMap[(int) $row['sys_language_uid'] === 0]) {
 			$where = 'uid = ' . (int) $originalContentElement[0];
 		} else {
@@ -224,6 +221,33 @@ class MigrateNewsCommandController extends CommandController {
 		}
 
 		$db->exec_UPDATEquery('tt_content', $where, ['bodytext' => $row['bodytext']]);
+
+		// possibly the default language needs to be overwritten and the old default translation needs to be preserved
+		if (isset($this->languageMap[(int) $row['sys_language_uid']]) && $this->languageMap[(int) $row['sys_language_uid']] === 0) {
+
+			$where = 'uid = ' . (int) $parentId;
+			/** @var \mysqli_result $result */
+			$result = $db->exec_SELECTquery('title', 'pages', $where);
+
+			$where = 'pid = ' . (int) $parentId . ' AND sys_language_uid = ' . $this->languageMap[0];
+			$db->exec_UPDATEquery('pages_language_overlay', $where, ['title' => $result->fetch_row()[0]]);
+
+			$where = 'uid = ' . (int) $parentId;
+			$db->exec_UPDATEquery(
+				'pages', $where, ['title' => date('Y-m-d', $row['datetime']) . ' - ' . $row['title']]
+			);
+		} else {
+			// finally translate the page title if necessary
+			if (isset($this->languageMap[(int) $row['sys_language_uid']]) && $this->languageMap[(int) $row['sys_language_uid']] > 0) {
+				$where = 'pid = ' . (int) $parentId . ' AND sys_language_uid = ' . $this->languageMap[(int) $row['sys_language_uid']];
+			} else {
+				$where = 'pid = ' . (int) $parentId . ' AND sys_language_uid = ' . (int) $row['sys_language_uid'];
+			}
+		}
+
+		$db->exec_UPDATEquery(
+			'pages_language_overlay', $where, ['title' => date('Y-m-d', $row['datetime']) . ' - ' . $row['title']]
+		);
 	}
 
 	/**
-- 
GitLab