diff --git a/Classes/Command/MigrateNewsCommandController.php b/Classes/Command/MigrateNewsCommandController.php index 6162617fca47f0595f189a59436d59c829665d35..1c6bd744848b3d08d3f66f750b4c210833064a5e 100644 --- a/Classes/Command/MigrateNewsCommandController.php +++ b/Classes/Command/MigrateNewsCommandController.php @@ -27,6 +27,7 @@ namespace SGalinski\SgNews\Command; ***************************************************************/ use SGalinski\SgNews\Domain\Model\News; +use SGalinski\SgNews\Domain\Model\Tag; use TYPO3\CMS\Backend\FrontendBackendUserAuthentication; use TYPO3\CMS\Core\Database\DatabaseConnection; use TYPO3\CMS\Core\DataHandling\DataHandler; @@ -57,6 +58,12 @@ class MigrateNewsCommandController extends CommandController { */ protected $newsRepository; + /** + * @var \SGalinski\SgNews\Domain\Repository\TagRepository + * @inject + */ + protected $tagRepository; + /** * this array maps new pages to their original entry in the tx_news table * @@ -93,7 +100,8 @@ class MigrateNewsCommandController extends CommandController { /** @var DatabaseConnection $db */ $db = $GLOBALS['TYPO3_DB']; - $where = 'hidden = 0 and deleted = 0'; + $where = 'hidden = 0 and deleted = 0 and endtime = 0 and pid = 52'; + /** @var \mysqli_result $result */ $result = $db->exec_SELECTquery('*', 'tx_news_domain_model_news', $where); @@ -137,20 +145,49 @@ class MigrateNewsCommandController extends CommandController { $title = date('Y-m-d', $row['datetime']) . ' - ' . $row['title']; $newsPage->setTitle($title); $newsPage->setAuthor($row['author']); + $newsPage->addTag($this->getMatchingTag($row)); $this->newsRepository->update($newsPage); $this->persistenceManager->persistAll(); // update content element from the new page - $where = 'pid = ' . $newPageId . ' AND sys_language_uid = ' . (int) $row['sys_language_uid']; + $where = 'pid = ' . $newPageId . ' AND sys_language_uid = ' . $this->languageMap[(int) $row['sys_language_uid']]; $db->exec_UPDATEquery('tt_content', $where, ['bodytext' => $row['bodytext']]); } } else { // this row is a translation and should simply update the content accordingly $this->updateTranslation($row); } } + } + + /** + * @param array $row + * @return Tag $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); + } - $this->pagesToMigrate = $resultArray; + $this->persistenceManager->persistAll(); + + return $matchingTag; } /** @@ -172,14 +209,20 @@ class MigrateNewsCommandController extends CommandController { $result = $db->exec_SELECTquery('l18n_parent', 'tt_content', $where); $originalContentElement = $result->fetch_row(); - $where = 'l18n_parent = ' . (int) $originalContentElement[0]; + // if its the new defaul, there is no l18n_parent + if ((int) $this->languageMap[(int) $row['sys_language_uid'] === 0]) { + $where = 'uid = ' . (int) $originalContentElement[0]; + } else { + $where = 'l18n_parent = ' . (int) $originalContentElement[0]; + } // look up the correct language id, if they have changed - if ($this->languageMap[(int) $row['sys_language_uid']]) { + if (isset($this->languageMap[(int) $row['sys_language_uid']])) { $where .= ' AND sys_language_uid = ' . $this->languageMap[(int) $row['sys_language_uid']]; } else { $where .= ' AND sys_language_uid = ' . (int) $row['sys_language_uid']; } + $db->exec_UPDATEquery('tt_content', $where, ['bodytext' => $row['bodytext']]); }