diff --git a/Classes/Command/MigrateNewsCommandController.php b/Classes/Command/MigrateNewsCommandController.php new file mode 100644 index 0000000000000000000000000000000000000000..e30e20b30591b380e58885ac82aebc4e3076e248 --- /dev/null +++ b/Classes/Command/MigrateNewsCommandController.php @@ -0,0 +1,120 @@ +<?php + +namespace SGalinski\SgNews\Command; + +/*************************************************************** + * Copyright notice + * + * (c) sgalinski Internet Services (https://www.sgalinski.de) + * + * All rights reserved + * + * This script is part of the TYPO3 project. The TYPO3 project is + * free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * The GNU General Public License can be found at + * http://www.gnu.org/copyleft/gpl.html. + * + * This script is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * This copyright notice MUST APPEAR in all copies of the script! + ***************************************************************/ + +use SGalinski\SgNews\Domain\Model\News; +use SGalinski\SgNews\Service\DataHandler; +use TYPO3\CMS\Backend\FrontendBackendUserAuthentication; +use TYPO3\CMS\Core\Database\DatabaseConnection; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Mvc\Controller\CommandController; +use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings; + +/** + * Command controller, that migrates data from tx_news to sg_news + */ +class MigrateNewsCommandController extends CommandController { + + /** + * @var bool + */ + protected $requestAdminPermissions = TRUE; + + /** + * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager + * @inject + */ + protected $persistenceManager; + + /** + * @var \SGalinski\SgNews\Domain\Repository\NewsRepository + * @inject + */ + protected $newsRepository; + + /** + * @param int $copyPageId + */ + public function runMigrateNewsCommand($copyPageId) { + /** @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, '', ''); + + $localDataHandler = GeneralUtility::makeInstance(DataHandler::class); + $beUser = $this->simulateBackendUser(); + $localCommandMap = [ + 'pages' => [ + $copyPageId => [ + 'copy' => 340 + ] + ] + ]; + + $resultArray = []; + while ($row = $result->fetch_assoc()) { + $resultArray[] = $row; + + $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]; + + /** @var $querySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */ + $querySettings = $this->objectManager->get(Typo3QuerySettings::class); + $querySettings->setIgnoreEnableFields(TRUE); + + $this->persistenceManager->persistAll(); + $this->newsRepository->setDefaultQuerySettings($querySettings); + + /** @var News $newsPage */ + $newsPage = $this->newsRepository->findByUid($newPageId); + + return; + } + } + + /** + * Simulate Backend User for DataHandler + */ + public function simulateBackendUser() { + /** @var \TYPO3\CMS\Backend\FrontendBackendUserAuthentication $BE_USER */ + $BE_USER = GeneralUtility::makeInstance(FrontendBackendUserAuthentication::class); + $BE_USER->setBeUserByName('admin'); + if ($BE_USER->user['uid']) { + $BE_USER->fetchGroupData(); + } + $BE_USER->uc_default['copyLevels'] = '9999'; + $BE_USER->uc = $BE_USER->uc_default; + $GLOBALS['PAGES_TYPES'][254]['allowedTables'] = '*'; + return $BE_USER; + } +} diff --git a/ext_localconf.php b/ext_localconf.php index 82df56e9e70c191461b3e0f98147bdab46cb1e09..df0651991c0af706669a5b68ed195ec65b1d13fb 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -98,4 +98,9 @@ if(!is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php']['drawHeaderHook'] = []; } $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php']['drawHeaderHook'][] = - \SGalinski\SgNews\Hooks\PageLayoutController::class . '->addNewsModuleLink'; \ No newline at end of file + \SGalinski\SgNews\Hooks\PageLayoutController::class . '->addNewsModuleLink'; + + +// register command controllers +$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = + 'SGalinski\SgNews\Command\MigrateNewsCommandController';