From 120a5480f76a38f60197a8739f90b0b6e09a3aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Thu, 4 Feb 2021 21:01:26 +0100 Subject: [PATCH] Implement automatic xml import controlled by uploaded timestamp files --- .../Service/ContingentImportService.php | 16 ++++++- .../Classes/Service/DateImportService.php | 30 ++++++++++--- .../Service/ImportTimestampService.php | 44 +++++++++++++++++++ .../Classes/Task/ImportContingentsTask.php | 7 ++- .../Classes/Task/ImportDatesTask.php | 7 ++- 5 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 public/typo3conf/ext/ep_products/Classes/Service/ImportTimestampService.php diff --git a/public/typo3conf/ext/ep_products/Classes/Service/ContingentImportService.php b/public/typo3conf/ext/ep_products/Classes/Service/ContingentImportService.php index 0202fcc4..872cdd99 100644 --- a/public/typo3conf/ext/ep_products/Classes/Service/ContingentImportService.php +++ b/public/typo3conf/ext/ep_products/Classes/Service/ContingentImportService.php @@ -57,14 +57,20 @@ class ContingentImportService implements SingletonInterface, LoggerAwareInterfac * @var Connection */ protected $db; + /** + * @var ImportTimestampService + */ + private $importTimestampService; /** * @param ConfigurationManagerInterface $manager * @throws \Doctrine\DBAL\DBALException */ - public function __construct(ConfigurationManagerInterface $manager) + public function __construct(ConfigurationManagerInterface $manager, ImportTimestampService $importTimestampService) { $this->configurationManager = $manager; + $this->importTimestampService = $importTimestampService; + $settings = GeneralUtility::removeDotsFromTS( $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT) ); @@ -92,6 +98,14 @@ class ContingentImportService implements SingletonInterface, LoggerAwareInterfac return $contingentCount; } + public function isImportRequired(): bool + { + return $this->importTimestampService->isImportRequired( + 'fileadmin/xmlexportzimmer/uebertragung.info', + 'fileadmin/contingents_import.info' + ); + } + /** * @param string $path * @return int diff --git a/public/typo3conf/ext/ep_products/Classes/Service/DateImportService.php b/public/typo3conf/ext/ep_products/Classes/Service/DateImportService.php index a95bb1dc..d5e27804 100644 --- a/public/typo3conf/ext/ep_products/Classes/Service/DateImportService.php +++ b/public/typo3conf/ext/ep_products/Classes/Service/DateImportService.php @@ -85,6 +85,11 @@ class DateImportService implements SingletonInterface, LoggerAwareInterface */ protected $cacheService; + /** + * @var ImportTimestampService + */ + private $importTimestampService; + /** * @var array */ @@ -96,10 +101,15 @@ class DateImportService implements SingletonInterface, LoggerAwareInterface * @param ConfigurationManagerInterface $configurationManager * @param CacheService $cacheService */ - public function __construct(ConfigurationManagerInterface $configurationManager, CacheService $cacheService) - { + public function __construct + ( + ConfigurationManagerInterface $configurationManager, + CacheService $cacheService, + ImportTimestampService $importTimestampService + ) { $this->configurationManager = $configurationManager; $this->cacheService = $cacheService; + $this->importTimestampService = $importTimestampService; $settings = GeneralUtility::removeDotsFromTS( $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT) @@ -133,6 +143,14 @@ class DateImportService implements SingletonInterface, LoggerAwareInterface return $dateCount; } + public function isImportRequired(): bool + { + return $this->importTimestampService->isImportRequired( + 'fileadmin/xmlexport/uebertragung.info', + 'fileadmin/products_import.info' + ); + } + /** * @param $path * @@ -246,7 +264,7 @@ class DateImportService implements SingletonInterface, LoggerAwareInterface WHERE p.deleted = 0 AND p.hidden = 0 AND p.code = :code '; - $products = $this->db->fetchAll($sql, ['code' => $code]); + $products = $this->db->fetchAllAssociative($sql, ['code' => $code]); // Skip import in case product can't be found if (count($products) === 0) { @@ -748,7 +766,7 @@ class DateImportService implements SingletonInterface, LoggerAwareInterface protected function getRoomMappings() { $sql = 'SELECT code, type FROM tx_epproducts_domain_model_roommapping'; - $roomMappings = $this->db->fetchAll($sql); + $roomMappings = $this->db->fetchAllAssociative($sql); foreach ($roomMappings as $mapping) { $this->roomMappings[mb_strtolower($mapping['code'])] = $mapping['type']; @@ -773,7 +791,7 @@ class DateImportService implements SingletonInterface, LoggerAwareInterface ) u LEFT JOIN tx_epproducts_domain_model_hotel h ON u.uid = h.uid '; - $rows = $this->db->fetchAll($sql); + $rows = $this->db->fetchAllAssociative($sql); foreach ($rows as $row) { $this->hotelMappings[$row['code']] = [ @@ -809,7 +827,7 @@ class DateImportService implements SingletonInterface, LoggerAwareInterface LIMIT 0,1 '; - return $this->db->fetchColumn($sql, ['uid' => $uid]); + return $this->db->fetchOne($sql, ['uid' => $uid]); } /** diff --git a/public/typo3conf/ext/ep_products/Classes/Service/ImportTimestampService.php b/public/typo3conf/ext/ep_products/Classes/Service/ImportTimestampService.php new file mode 100644 index 00000000..9f89f33f --- /dev/null +++ b/public/typo3conf/ext/ep_products/Classes/Service/ImportTimestampService.php @@ -0,0 +1,44 @@ +format('d.m.Y H:i:s')); + + return true; + } + + $line = trim(file($lastImportTimestampPath)[0]); + $lastImportAt = \DateTime::createFromFormat('d.m.Y H:i:s', $line); + + if ($lastUploadAt > $lastImportAt) { + $now = new \DateTime('now'); + file_put_contents($lastImportTimestampPath, $now->format('d.m.Y H:i:s')); + + return true; + } + + return false; + } + +} diff --git a/public/typo3conf/ext/ep_products/Classes/Task/ImportContingentsTask.php b/public/typo3conf/ext/ep_products/Classes/Task/ImportContingentsTask.php index ef9f541a..6d5c062a 100644 --- a/public/typo3conf/ext/ep_products/Classes/Task/ImportContingentsTask.php +++ b/public/typo3conf/ext/ep_products/Classes/Task/ImportContingentsTask.php @@ -40,8 +40,11 @@ class ImportContingentsTask extends AbstractTask $objectManager = GeneralUtility::makeInstance(ObjectManager::class); /** @var \EP\EpProducts\Service\ContingentImportService $importService */ $importService = $objectManager->get(ContingentImportService::class); - $path = GeneralUtility::getFileAbsFileName('fileadmin/xmlexportzimmer'); - $importService->import($path); + if ($importService->isImportRequired()) { + $path = GeneralUtility::getFileAbsFileName('fileadmin/xmlexportzimmer'); + $importService->import($path); + } + return true; } } diff --git a/public/typo3conf/ext/ep_products/Classes/Task/ImportDatesTask.php b/public/typo3conf/ext/ep_products/Classes/Task/ImportDatesTask.php index ee2c21c2..266ceb4d 100644 --- a/public/typo3conf/ext/ep_products/Classes/Task/ImportDatesTask.php +++ b/public/typo3conf/ext/ep_products/Classes/Task/ImportDatesTask.php @@ -40,8 +40,11 @@ class ImportDatesTask extends AbstractTask $objectManager = GeneralUtility::makeInstance(ObjectManager::class); /** @var \EP\EpProducts\Service\DateImportService $importService */ $importService = $objectManager->get(DateImportService::class); - $path = GeneralUtility::getFileAbsFileName('fileadmin/xmlexport'); - $importService->import($path); + if ($importService->isImportRequired()) { + $path = GeneralUtility::getFileAbsFileName('fileadmin/xmlexport'); + $importService->import($path); + } + return true; } }