Small changes to load file function

This commit is contained in:
Dirk 2019-08-13 09:12:30 +02:00
parent 011c337078
commit f2fcf646f8

View file

@ -9,6 +9,7 @@ use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\DataHandling\SlugHelper; use TYPO3\CMS\Core\DataHandling\SlugHelper;
use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager; use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository; use TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository;
@ -215,16 +216,6 @@ class DestinationDataImportService {
// Set Configuration // Set Configuration
$this->configurationManager->setConfiguration(array_merge($frameworkConfiguration, $persistenceConfiguration)); $this->configurationManager->setConfiguration(array_merge($frameworkConfiguration, $persistenceConfiguration));
// Init Logger
// Nötig, damit logger arbeitet?
$GLOBALS['TYPO3_CONF_VARS']['LOG']['writerConfiguration'] = [
\TYPO3\CMS\Core\Log\LogLevel::INFO => [
'TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter' => [
'logFile' => 'typo3temp/logs/events_import.log'
]
]
];
$this->logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class)->getLogger(__CLASS__); $this->logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class)->getLogger(__CLASS__);
$this->logger->info('Starting Destination Data Import Service'); $this->logger->info('Starting Destination Data Import Service');
@ -233,8 +224,10 @@ class DestinationDataImportService {
$this->logger->info('Try to get data from ' . $restUrl); $this->logger->info('Try to get data from ' . $restUrl);
if ($jsonResponse = json_decode(file_get_contents($restUrl),true)) { if ($jsonResponse = json_decode(file_get_contents($restUrl),true)) {
$this->logger->info('Received data with ' . count($jsonResponse) . 'items');
return $this->processData($jsonResponse); return $this->processData($jsonResponse);
} else { } else {
$this->logger->error('Could not receive data.');
return 1; return 1;
} }
@ -246,43 +239,33 @@ class DestinationDataImportService {
*/ */
public function processData($data) { public function processData($data) {
$this->logger->info('Received data with ' . count($data) . 'items');
// Get seleceted region // Get seleceted region
$selectedRegion = $this->regionRepository->findByUid($this->regionUid); $selectedRegion = $this->regionRepository->findByUid($this->regionUid);
foreach ($data['items'] as $event) { foreach ($data['items'] as $event) {
$this->logger->info('Processing event ' . substr($event['title'], 0, 20)); $this->logger->info('Processing event ' . substr($event['title'], 0, 20));
// Event already exists? If not create one! // Event already exists? If not create one!
$this->tmpCurrentEvent = $this->getOrCreateEvent($event['global_id'], $event['title']); $this->tmpCurrentEvent = $this->getOrCreateEvent($event['global_id'], $event['title']);
// Set selected Region // Set selected Region
$this->tmpCurrentEvent->setRegion($selectedRegion); $this->tmpCurrentEvent->setRegion($selectedRegion);
// Set Title // Set Title
$this->tmpCurrentEvent->setTitle(substr($event['title'], 0, 254)); $this->tmpCurrentEvent->setTitle(substr($event['title'], 0, 254));
// Set Highlight (Is only set in rest if true) // Set Highlight (Is only set in rest if true)
if($event['highlight']) if($event['highlight'])
$this->tmpCurrentEvent->setHighlight($event['highlight']); $this->tmpCurrentEvent->setHighlight($event['highlight']);
// Set Texts // Set Texts
$this->setTexts($event['texts']); $this->setTexts($event['texts']);
// Set address and geo data // Set address and geo data
$this->setAddress($event['street'], $event['city'], $event['zip'], $event['country'], $event['geo']['main']['latitude'], $event['geo']['main']['longitude']); $this->setAddress($event['street'], $event['city'], $event['zip'], $event['country'], $event['geo']['main']['latitude'], $event['geo']['main']['longitude']);
// Set Categories
$this->setCategories($event['categories']);
// Set Organizer // Set Organizer
$this->setOrganizer($event['addresses']); $this->setOrganizer($event['addresses']);
// Set Dates // Set Dates
$this->setDates($event['timeIntervals']); $this->setDates($event['timeIntervals']);
// Set Assets // Set Assets
$this->setAssets($event['media_objects']); $this->setAssets($event['media_objects']);
// Update and persist // Update and persist
$this->logger->info('Persist database'); $this->logger->info('Persist database');
$this->eventRepository->update($this->tmpCurrentEvent); $this->eventRepository->update($this->tmpCurrentEvent);
@ -293,6 +276,27 @@ class DestinationDataImportService {
return 0; return 0;
} }
/**
* @param array $categories
*/
protected function setCategories(Array $categories) {
$sysParentCategory = $this->sysCategoriesRepository->findByUid($this->categoryParentUid);
foreach ($categories as $categoryTitle) {
$tmpSysCategory = $this->sysCategoriesRepository->findOneByTitle($categoryTitle);
if (!$tmpSysCategory)
{
$this->logger->info('Creating new category: ' . $categoryTitle);
$tmpSysCategory = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Model\\Category');
$tmpSysCategory->setTitle($categoryTitle);
$tmpSysCategory->setParent($sysParentCategory);
$this->sysCategoriesRepository->add($tmpSysCategory);
$this->tmpCurrentEvent->addCategory($tmpSysCategory);
} else {
$this->tmpCurrentEvent->addCategory($tmpSysCategory);
}
}
}
/** /**
* @param array $timeIntervals * @param array $timeIntervals
*/ */
@ -498,14 +502,14 @@ class DestinationDataImportService {
// Check if file already exists // Check if file already exists
if (file_exists($this->environment->getPublicPath() . '/fileadmin/' . $this->filesFolder . strtolower(basename($media_object['url'])))) { if (file_exists($this->environment->getPublicPath() . '/fileadmin/' . $this->filesFolder . strtolower(basename($media_object['url'])))) {
$this->logger->info('[NOTICE] File already exists'); $this->logger->info('File already exists');
} else { } else {
$this->logger->info("[NOTICE] File don't exist"); $this->logger->info("File don't exist");
// Load the file // Load the file
if ($file = $this->loadFile($media_object['url'])) { if ($file = $this->loadFile($media_object['url'])) {
// Move file to defined folder // Move file to defined folder
$this->logger->info('[INFO] Adding file ' . $file); $this->logger->info('Adding file ' . $file);
$this->storage->addFile(PATH_site . "uploads/tx_Events/" . $file, $this->storage->getFolder($this->filesFolder), basename($media_object['url'])); $this->storage->addFile($this->environment->getPublicPath() . "/uploads/tx_events/" . $file, $this->storage->getFolder($this->filesFolder), basename($media_object['url']));
} else { } else {
$error = true; $error = true;
} }
@ -519,7 +523,7 @@ class DestinationDataImportService {
$this->logger->info('No relation found'); $this->logger->info('No relation found');
$file = $this->storage->getFile($this->filesFolder . basename($media_object['url'])); $file = $this->storage->getFile($this->filesFolder . basename($media_object['url']));
$this->metaDataRepository->update($file->getUid(), array('title' => $media_object['value'], 'description' => $media_object['description'], 'alternative' => 'DD Import')); $this->metaDataRepository->update($file->getUid(), array('title' => $media_object['value'], 'description' => $media_object['description'], 'alternative' => 'DD Import'));
$this->createFileRelations($file->getUid(), 'tx_Events_domain_model_event', $this->tmpCurrentEvent->getUid(), 'images', $this->storagePid); $this->createFileRelations($file->getUid(), 'tx_events_domain_model_event', $this->tmpCurrentEvent->getUid(), 'images', $this->storagePid);
} }
} }
@ -533,18 +537,15 @@ class DestinationDataImportService {
* @return bool * @return bool
*/ */
protected function loadFile($file) { protected function loadFile($file) {
$directory = $this->environment->getPublicPath() . "uploads/tx_Events/"; $directory = $this->environment->getPublicPath() . "/uploads/tx_events/";
$filename = basename($file); $filename = basename($file);
$this->logger->info('[INFO] Getting file ' . $file . ' as ' . $filename); $this->logger->info('Getting file ' . $file . ' as ' . $filename);
$asset = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($file); $asset = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($file);
if ($asset) { if ($asset) {
$fp = fopen($directory . $filename, 'w'); file_put_contents($directory . $filename, $asset);
fputs($fp, $asset);
fclose($fp);
return $filename; return $filename;
} }
$this->logger->info('[ERROR] cannot load file ' . $file); $this->logger->error('Cannot load file ' . $file);
return false; return false;
} }