Implement automatic xml import controlled by uploaded timestamp files
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace EP\EpProducts\Service;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
class ImportTimestampService
|
||||
{
|
||||
public function isImportRequired(string $uploadTimestampFile, string $importTimestampFile): bool
|
||||
{
|
||||
$updatedTimestampPath = GeneralUtility::getFileAbsFileName($uploadTimestampFile);
|
||||
|
||||
// No upload yet so no import required
|
||||
if (!$updatedTimestampPath || !file_exists($updatedTimestampPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$line = trim(file($updatedTimestampPath)[0]);
|
||||
$lastUploadAt = \DateTime::createFromFormat('d.m.Y H:i:s', $line);
|
||||
|
||||
$lastImportTimestampPath = GeneralUtility::getFileAbsFileName($importTimestampFile);
|
||||
|
||||
// No import yet so import required
|
||||
if (!$lastImportTimestampPath || !file_exists($lastImportTimestampPath)) {
|
||||
$now = new \DateTime('now');
|
||||
file_put_contents($lastImportTimestampPath, $now->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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user