From 3c461247a1f0d21feb5943b10b34ccce39398f08 Mon Sep 17 00:00:00 2001 From: jkummer Date: Fri, 1 Mar 2019 16:44:13 +0100 Subject: [PATCH] [TASK] Add Migration for related news. refs #31 --- .../Command/TtNewsImportCommandController.php | 12 +- Classes/Jobs/TTNewsRelatedNewsImportJob.php | 104 ++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 Classes/Jobs/TTNewsRelatedNewsImportJob.php diff --git a/Classes/Command/TtNewsImportCommandController.php b/Classes/Command/TtNewsImportCommandController.php index 11a5717..fe174b5 100644 --- a/Classes/Command/TtNewsImportCommandController.php +++ b/Classes/Command/TtNewsImportCommandController.php @@ -3,7 +3,6 @@ use GeorgRinger\News\Jobs\ImportJobInterface; use GeorgRinger\News\Utility\ImportJob; -use TYPO3\CMS\Extbase\Mvc\Controller\CommandController; /** * Controller to import news from tt_news @@ -34,4 +33,15 @@ public function importTtNewsNewsCommand() $job = $this->objectManager->get(\BeechIt\NewsTtnewsimport\Jobs\TTNewsNewsImportJob::class); $job->run(0); } + + /** + * Import tt_news related news records + * + * @cli + */ + public function importTtNewsRelatedNewsCommand() + { + $job = $this->objectManager->get(\BeechIt\NewsTtnewsimport\Jobs\TTNewsRelatedNewsImportJob::class); + $job->run(0); + } } diff --git a/Classes/Jobs/TTNewsRelatedNewsImportJob.php b/Classes/Jobs/TTNewsRelatedNewsImportJob.php new file mode 100644 index 0000000..3507e99 --- /dev/null +++ b/Classes/Jobs/TTNewsRelatedNewsImportJob.php @@ -0,0 +1,104 @@ +getMMData(); + if (!empty($mmData)) { + foreach ($mmData as $data) { + if (!empty($data)) { + // Insert (result/returned value is not checked, no id vil be returned) + $this->insertRelation($data); + // Update (result/returned value is not checked) + $this->updateRelation($data); + } + } + $result = true; // No output/feedback + } + return $result; + } + + /** + * Get the mm relation data for related news + * + * @return array $mmData + */ + protected function getMMData() + { + $mmData = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( + 'ttnmm.uid_local AS ttn_uid_local, + ttnmm.uid_foreign AS ttn_uid_foreign, + ttnmm.sorting AS ttn_sorting, + txn_local.uid AS txn_uid_local, + txn_foreign.uid AS txn_uid_foreign + ', + 'tt_news_related_mm AS ttnmm + JOIN tx_news_domain_model_news AS txn_local ON txn_local.import_id = ttnmm.uid_local + JOIN tx_news_domain_model_news AS txn_foreign ON txn_foreign.import_id = ttnmm.uid_foreign + ', + 'ttnmm.tablenames = \'tt_news\' + AND txn_local.import_source = \'TT_NEWS_IMPORT\' + AND txn_foreign.import_source = \'TT_NEWS_IMPORT\' + ', + 'ttnmm.uid_local, ttnmm.uid_foreign' // Exclude tt_news_related_mm duplicates + ); + return $mmData; + } + + /** + * Insert MM data to tx_news_domain_model_news_related_mm + * + * @param array $mmData + * @return void + */ + protected function insertRelation($mmData) + { + if ($mmData['txn_uid_local'] && $mmData['txn_uid_foreign']) { + $GLOBALS['TYPO3_DB']->exec_INSERTquery( + 'tx_news_domain_model_news_related_mm', + array( + 'uid_local' => $mmData['txn_uid_local'], + 'uid_foreign' => $mmData['txn_uid_foreign'], + 'sorting_foreign' => $mmData['ttn_sorting'], + ) + ); + } + } + + /** + * Update tx_news_domain_model_news.related + * + * @param array $mmData + * @return void + */ + protected function updateRelation($mmData) + { + // Get current value from news entry for related news + $news = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( + 'related', + 'tx_news_domain_model_news', + 'uid = ' . $mmData['txn_uid_local'] + ); + // Update news entry - increase value for related news + if (!empty($news) && isset($news[0]['related'])) { + $GLOBALS['TYPO3_DB']->exec_UPDATEquery( + 'tx_news_domain_model_news', + 'uid = ' . $mmData['txn_uid_local'], + array( + 'related' => $news[0]['related'] +1 + ) + ); + } + } +}