diff --git a/Classes/Command/MigrateNewsCommandController.php b/Classes/Command/MigrateNewsCommandController.php index 6f5dc47d39ef8b847ac23f1a3ce94cd058f0db00..29fad9c6e8dfc3164c285a33cdd5506ac18a884a 100644 --- a/Classes/Command/MigrateNewsCommandController.php +++ b/Classes/Command/MigrateNewsCommandController.php @@ -28,10 +28,13 @@ namespace SGalinski\SgNews\Command; use SGalinski\SgNews\Domain\Model\News; use TYPO3\CMS\Backend\FrontendBackendUserAuthentication; +use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Database\DatabaseConnection; use TYPO3\CMS\Core\DataHandling\DataHandler; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\CommandController; +use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException; +use TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException; use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings; /** @@ -58,13 +61,16 @@ class MigrateNewsCommandController extends CommandController { /** * @param int $copyPageId + * @param string $copyPageId + * @throws IllegalObjectTypeException + * @throws UnknownObjectException */ - public function runMigrateNewsCommand($copyPageId) { + public function runMigrateNewsCommand($copyPageId, $categoryPids = '340') { /** @var DatabaseConnection $db */ $db = $GLOBALS['TYPO3_DB']; $where = 'hidden = 0 and deleted = 0'; /** @var \mysqli_result $result */ - $result = $db->exec_SELECTquery('*', 'tx_news_domain_model_news', $where, '', ''); + $result = $db->exec_SELECTquery('*', 'tx_news_domain_model_news', $where, '', '', '50'); $localDataHandler = GeneralUtility::makeInstance(DataHandler::class); $beUser = $this->simulateBackendUser(); @@ -80,32 +86,60 @@ class MigrateNewsCommandController extends CommandController { while ($row = $result->fetch_assoc()) { $resultArray[] = $row; - $localDataHandler->start([], $localCommandMap, $beUser); - $localDataHandler->bypassAccessCheckForRecords = TRUE; - $localDataHandler->checkModifyAccessList('pages'); - $localDataHandler->process_cmdmap(); + // if no l10n_parent exists, create a copy of the page + if ((int) $row['l10n_parent'] === 0) { + $localDataHandler->start([], $localCommandMap, $beUser); + $localDataHandler->bypassAccessCheckForRecords = TRUE; + $localDataHandler->checkModifyAccessList('pages'); + $localDataHandler->process_cmdmap(); - // get the id of the new object - $newPageId = $localDataHandler->copyMappingArray['pages'][$copyPageId]; + // get the id of the new object + $newPageId = $localDataHandler->copyMappingArray['pages'][$copyPageId]; - /** @var $querySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */ - $querySettings = $this->objectManager->get(Typo3QuerySettings::class); - $querySettings->setIgnoreEnableFields(TRUE); + /** @var News $newsPage */ + $newsPage = $this->newsRepository->findByUidIgnoreEnableFields($newPageId); - $this->persistenceManager->persistAll(); - $this->newsRepository->setDefaultQuerySettings($querySettings); + 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']); - /** @var News $newsPage */ - $newsPage = $this->newsRepository->findByUid($newPageId); + $this->newsRepository->update($newsPage); + $this->persistenceManager->persistAll(); + + // update content element from the new page + $where = 'uid = ' . $newPageId; + $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); + } return; } } + /** + * Updates content element of a news translation + * + * @param array $row + */ + private function updateTranslation(array $row) { + /** @var DatabaseConnection $db */ + $db = $GLOBALS['TYPO3_DB']; + + // update content element from the new page + $where = 'pid = ' . $row['l10n_parent']; + $db->exec_UPDATEquery('tt_content', $where, ['bodytext' => $row['bodytext']]); + } + /** * Simulate Backend User for DataHandler + * + * @return FrontendBackendUserAuthentication */ - public function simulateBackendUser() { + private function simulateBackendUser() { /** @var \TYPO3\CMS\Backend\FrontendBackendUserAuthentication $BE_USER */ $BE_USER = GeneralUtility::makeInstance(FrontendBackendUserAuthentication::class); $BE_USER->setBeUserByName('admin'); diff --git a/Classes/Domain/Repository/NewsRepository.php b/Classes/Domain/Repository/NewsRepository.php index 079b48b6c85f4e9d724178abdaa394628ac9584d..3e12339212cdab331991a295c204c751460a2683 100644 --- a/Classes/Domain/Repository/NewsRepository.php +++ b/Classes/Domain/Repository/NewsRepository.php @@ -215,9 +215,11 @@ class NewsRepository extends AbstractRepository { $onlyHighlighted = FALSE, array $categoryIds = NULL, $hideNeverHighlightedNews = FALSE, $sortBy = 'date', array $tagIds = NULL, $startTime = 0, $endTime = 0 ): int { - return $this->getCount($this->getQueryForLastUpdatedOrHighlightedNewsByCategories( - 0, $onlyHighlighted, $categoryIds, 0, $hideNeverHighlightedNews, $sortBy, $tagIds, $startTime, $endTime - )); + return $this->getCount( + $this->getQueryForLastUpdatedOrHighlightedNewsByCategories( + 0, $onlyHighlighted, $categoryIds, 0, $hideNeverHighlightedNews, $sortBy, $tagIds, $startTime, $endTime + ) + ); } /** @@ -452,4 +454,22 @@ class NewsRepository extends AbstractRepository { return $this->getCount($query); } + + /** + * Find news record by uid, but with ignoring enable fields + * + * @param int $uid + * @return object + */ + public function findByUidIgnoreEnableFields($uid) { + $query = $this->createQuery(); + /** @var $querySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */ + $querySettings = $query->getQuerySettings(); + $querySettings->setIgnoreEnableFields(TRUE); + $querySettings->setRespectStoragePage(FALSE); + $query->setQuerySettings($querySettings); + + $query->matching($query->equals('uid', $uid)); + return $query->execute()->getFirst(); + } }