events/Classes/Service/DestinationDataImportService.php

711 lines
26 KiB
PHP
Raw Normal View History

2019-08-12 07:43:37 +02:00
<?php
namespace Wrm\Events\Service;
2019-08-12 15:49:52 +02:00
use TYPO3\CMS\Core\Core\Environment;
2019-08-12 07:43:37 +02:00
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\Resource\Index\MetaDataRepository;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\DataHandling\SlugHelper;
use TYPO3\CMS\Core\Database\ConnectionPool;
2019-08-13 11:17:34 +02:00
use TYPO3\CMS\Core\Log\LogManager;
2019-08-13 09:12:30 +02:00
2019-08-12 07:43:37 +02:00
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use Wrm\Events\Domain\Repository\DateRepository;
use Wrm\Events\Domain\Repository\EventRepository;
use Wrm\Events\Domain\Repository\OrganizerRepository;
use Wrm\Events\Domain\Repository\RegionRepository;
2019-08-13 11:17:34 +02:00
2019-08-12 07:43:37 +02:00
class DestinationDataImportService {
/**
* @var
*/
protected $restUrl;
/**
* @var
*/
protected $restLicenseKey;
/**
* @var
*/
protected $restType;
/**
* @var
*/
protected $restLimit;
/**
* @var
*/
protected $restMode;
2019-08-12 07:43:37 +02:00
/**
* @var
*/
protected $restTemplate;
/**
* @var
*/
protected $restExperience;
/**
* @var
*/
protected $storagePid;
2019-08-13 10:10:00 +02:00
/**
* @var
*/
protected $categoriesPid;
2019-08-12 07:43:37 +02:00
/**
* @var
*/
protected $regionUid;
/**
* @var
*/
protected $categoryParentUid;
/**
* @var
*/
protected $filesFolder;
/**
* @var
*/
protected $storage;
/**
* @var array
*/
protected $settings = [];
2019-08-12 15:49:52 +02:00
/*
* @var
*/
2019-08-12 16:12:45 +02:00
protected $environment;
2019-08-12 07:43:37 +02:00
/**
2020-08-19 11:30:13 +02:00
* @var
2019-08-12 07:43:37 +02:00
*/
2020-08-19 11:30:13 +02:00
protected $tmpCurrentEvent;
2019-08-12 07:43:37 +02:00
/**
* @var
*/
protected $logger;
/**
* @var EventRepository
*/
protected $eventRepository;
/**
* @var RegionRepository
*/
protected $regionRepository;
/**
* @var OrganizerRepository
*/
protected $organizerRepository;
/**
* @var DateRepository
*/
protected $dateRepository;
/**
* @var CategoryRepository
*/
protected $sysCategoriesRepository;
/**
* @var FileRepository
*/
protected $fileRepository;
/**
* @var MetaDataRepository
*/
protected $metaDataRepository;
/**
* @var ConfigurationManager
*/
protected $configurationManager;
/**
* @var ObjectManager
*/
protected $objectManager;
/**
* @var PersistenceManager
*/
protected $persistenceManager;
/**
* @var ResourceFactory
*/
protected $resourceFactory;
/**
* ImportService constructor.
* @param EventRepository $eventRepository
* @param RegionRepository $regionRepository
* @param OrganizerRepository $organizerRepository
* @param DateRepository $dateRepository
* @param CategoryRepository $sysCategoriesRepository
* @param FileRepository $fileRepository
* @param MetaDataRepository $metaDataRepository
* @param ConfigurationManager $configurationManager
* @param PersistenceManager $persistenceManager
* @param ResourceFactory $resourceFactory
* @param ObjectManager $objectManager
2019-08-12 15:49:52 +02:00
* @param Environment $environment
2019-08-12 07:43:37 +02:00
*/
public function __construct(
EventRepository $eventRepository,
RegionRepository $regionRepository,
OrganizerRepository $organizerRepository,
DateRepository $dateRepository,
CategoryRepository $sysCategoriesRepository,
FileRepository $fileRepository,
MetaDataRepository $metaDataRepository,
ConfigurationManager $configurationManager,
PersistenceManager $persistenceManager,
ResourceFactory $resourceFactory,
2019-08-12 15:49:52 +02:00
ObjectManager $objectManager,
Environment $environment
2019-08-12 07:43:37 +02:00
) {
$this->eventRepository = $eventRepository;
$this->regionRepository = $regionRepository;
$this->organizerRepository = $organizerRepository;
$this->dateRepository = $dateRepository;
$this->sysCategoriesRepository = $sysCategoriesRepository;
$this->fileRepository = $fileRepository;
$this->metaDataRepository = $metaDataRepository;
$this->configurationManager = $configurationManager;
$this->persistenceManager = $persistenceManager;
$this->resourceFactory = $resourceFactory;
2019-08-12 15:49:52 +02:00
$this->objectManager = $objectManager;
$this->environment = $environment;
2019-08-12 07:43:37 +02:00
// Get Typoscript Settings
$this->settings = $this->configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'Events',
'Pi1'
);
// Set properties
2019-11-07 10:25:09 +01:00
$this->restUrl = $this->settings['destinationData']['restUrl'];
$this->restLicenseKey = $this->settings['destinationData']['license'];
$this->restType = $this->settings['destinationData']['restType'];
$this->restLimit = $this->settings['destinationData']['restLimit'];
$this->restMode = $this->settings['destinationData']['restMode'];
2019-11-07 10:25:09 +01:00
$this->restTemplate = $this->settings['destinationData']['restTemplate'];
$this->sysCategoriesPid = $this->settings['destinationData']['categoriesPid'];
$this->categoryParentUid = $this->settings['destinationData']['categoryParentUid'];
2019-08-12 15:49:52 +02:00
}
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
/**
* @param $restExperience
* @param $storagePid
* @param $regionUid
* @param $filesFolder
*/
2019-11-07 10:25:09 +01:00
public function import($restExperience, $storagePid, $regionUid, $filesFolder) {
2019-08-12 15:49:52 +02:00
$this->restExperience = $restExperience;
$this->storagePid = $storagePid;
$this->regionUid = $regionUid;
$this->filesFolder = $filesFolder;
// Get configuration
2019-08-12 07:43:37 +02:00
$frameworkConfiguration = $this->configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK
);
2019-08-12 15:49:52 +02:00
// Set storage pid
2019-08-12 07:43:37 +02:00
$persistenceConfiguration = [
'persistence' => [
'storagePid' => $this->storagePid,
],
];
2019-08-12 15:49:52 +02:00
// Set Configuration
2019-08-12 07:43:37 +02:00
$this->configurationManager->setConfiguration(array_merge($frameworkConfiguration, $persistenceConfiguration));
2019-08-13 11:17:34 +02:00
$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
2019-08-12 07:43:37 +02:00
2019-10-09 15:03:58 +02:00
$this->logger->info('Starting Destination Data Import Service');
$restUrl = $this->restUrl . '?experience=' . $this->restExperience . '&licensekey=' . $this->restLicenseKey . '&type=' . $this->restType . '&mode=' . $this->restMode . '&limit=' . $this->restLimit . '&template=' . $this->restTemplate;
2019-08-12 07:43:37 +02:00
$this->logger->info('Try to get data from ' . $restUrl);
if ($jsonResponse = json_decode(file_get_contents($restUrl),true)) {
2019-10-09 15:03:58 +02:00
$this->logger->info('Received data with ' . count($jsonResponse['items']) . ' items');
2019-08-12 07:43:37 +02:00
return $this->processData($jsonResponse);
} else {
2019-08-13 09:12:30 +02:00
$this->logger->error('Could not receive data.');
2019-08-12 07:43:37 +02:00
return 1;
}
}
/**
* @param $data
* @return int
*/
public function processData($data) {
2019-10-09 15:03:58 +02:00
$this->logger->info('Processing json ' . count($data['items']));
// Get selected region
2019-08-12 07:43:37 +02:00
$selectedRegion = $this->regionRepository->findByUid($this->regionUid);
foreach ($data['items'] as $event) {
2019-08-12 15:49:52 +02:00
$this->logger->info('Processing event ' . substr($event['title'], 0, 20));
2019-08-12 15:49:52 +02:00
// Event already exists? If not create one!
$this->tmpCurrentEvent = $this->getOrCreateEvent($event['global_id'], $event['title']);
2019-08-12 07:43:37 +02:00
// Set selected Region
$this->tmpCurrentEvent->setRegion($selectedRegion);
2019-08-12 07:43:37 +02:00
// Set Title
$this->tmpCurrentEvent->setTitle(substr($event['title'], 0, 254));
2019-08-12 07:43:37 +02:00
// Set Highlight (Is only set in rest if true)
if($event['highlight'])
$this->tmpCurrentEvent->setHighlight($event['highlight']);
2019-08-12 07:43:37 +02:00
// Set Texts
if($event['texts'])
$this->setTexts($event['texts']);
2019-08-12 15:49:52 +02:00
// Set address and geo data
2020-08-19 11:30:13 +02:00
if($event['name'] || $event['street'] || $event['city'] || $event['zip'] || $event['country'] || $event['web'])
2020-03-16 13:52:58 +01:00
$this->setAddress($event);
2019-09-23 15:41:41 +02:00
// Set LatLng
if($event['geo']['main']['latitude'] && $event['geo']['main']['longitude'])
$this->setLatLng($event['geo']['main']['latitude'], $event['geo']['main']['longitude']);
2019-08-13 09:12:30 +02:00
// Set Categories
if($event['categories'])
$this->setCategories($event['categories']);
2019-08-12 07:43:37 +02:00
// Set Organizer
if($event['addresses'])
$this->setOrganizer($event['addresses']);
2020-08-19 11:30:13 +02:00
// Set Social and Tickets
if($event['media_objects'])
$this->setSocial($event['media_objects']);
2019-08-12 07:43:37 +02:00
// Set Dates
if($event['timeIntervals'])
$this->setDates($event['timeIntervals']);
2019-08-12 15:49:52 +02:00
// Set Assets
if($event['media_objects'])
$this->setAssets($event['media_objects']);
2019-08-12 15:49:52 +02:00
// Update and persist
$this->logger->info('Persist database');
$this->eventRepository->update($this->tmpCurrentEvent);
$this->persistenceManager->persistAll();
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
}
$this->doSlugUpdate();
2019-10-09 15:03:58 +02:00
$this->logger->info('Finished import');
2019-08-12 15:49:52 +02:00
return 0;
}
2019-08-12 07:43:37 +02:00
2019-08-13 09:12:30 +02:00
/**
2019-10-09 15:03:58 +02:00
*
2019-08-13 09:12:30 +02:00
* @param array $categories
*/
protected function setCategories(Array $categories) {
$sysParentCategory = $this->sysCategoriesRepository->findByUid($this->categoryParentUid);
foreach ($categories as $categoryTitle) {
$tmpSysCategory = $this->sysCategoriesRepository->findOneByTitle($categoryTitle);
2019-08-13 11:17:34 +02:00
if (!$tmpSysCategory) {
2019-08-13 09:12:30 +02:00
$this->logger->info('Creating new category: ' . $categoryTitle);
2019-08-13 11:17:34 +02:00
$tmpSysCategory = $this->objectManager->get(\TYPO3\CMS\Extbase\Domain\Model\Category::class);
2019-08-13 09:12:30 +02:00
$tmpSysCategory->setTitle($categoryTitle);
$tmpSysCategory->setParent($sysParentCategory);
2019-08-13 10:10:00 +02:00
$tmpSysCategory->setPid($this->sysCategoriesPid);
2019-08-13 09:12:30 +02:00
$this->sysCategoriesRepository->add($tmpSysCategory);
$this->tmpCurrentEvent->addCategory($tmpSysCategory);
} else {
$this->tmpCurrentEvent->addCategory($tmpSysCategory);
}
}
}
2019-08-12 15:49:52 +02:00
/**
* @param array $timeIntervals
2019-08-13 11:17:34 +02:00
* @TODO: split into functions
2019-08-12 15:49:52 +02:00
*/
protected function setDates(Array $timeIntervals) {
2019-08-12 07:43:37 +02:00
2019-10-09 15:03:58 +02:00
// @TODO: does not seem to work -->
2019-08-12 15:49:52 +02:00
//$currentEventDates = $this->tmpCurrentEvent->getDates();
//$this->tmpCurrentEvent->removeAllDates($currentEventDates);
// <--
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
// TODO: Workaround delete dates
$currentEventDates = $this->tmpCurrentEvent->getDates();
$this->logger->info('Found ' . count($currentEventDates) . ' to delete');
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
foreach ($currentEventDates as $currentDate) {
$this->dateRepository->remove($currentDate);
}
2019-08-12 07:43:37 +02:00
2019-10-11 15:12:07 +02:00
$today = new \DateTime('today');
$today = $today->getTimestamp();
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
foreach ($timeIntervals as $date) {
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
// Check if dates are given as interval or not
if (empty($date['interval'])) {
2019-08-12 07:43:37 +02:00
2019-10-11 15:12:07 +02:00
if (strtotime($date['start']) > $today) {
2019-08-12 15:49:52 +02:00
$this->logger->info('Setup single date');
2019-08-13 11:17:34 +02:00
$dateObj = $this->objectManager->get(\Wrm\Events\Domain\Model\Date::class);
2019-08-12 15:49:52 +02:00
$start = new \DateTime($date['start'], new \DateTimeZone($date['tz']));
$end = new \DateTime($date['end'], new \DateTimeZone($date['tz']));
$this->logger->info('Start transformed ' . $start->format('Y-m-d H:i'));
$this->logger->info('End transformed ' . $end->format('Y-m-d H:i'));
$dateObj->setStart($start);
$dateObj->setEnd($end);
$this->tmpCurrentEvent->addDate($dateObj);
}
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
} else {
2019-08-12 07:43:37 +02:00
2020-03-16 12:10:01 +01:00
if ($date['freq'] == 'Daily' && empty($date['weekdays']) && !empty($date['repeatUntil'])) {
2019-08-12 15:49:52 +02:00
$this->logger->info('Setup daily interval dates');
$this->logger->info('Start ' . $date['start']);
$this->logger->info('End ' . $date['repeatUntil']);
$start = new \DateTime($date['start'], new \DateTimeZone($date['tz']));
$until = new \DateTime($date['repeatUntil'], new \DateTimeZone($date['tz']));
2020-03-16 12:10:01 +01:00
2019-08-12 15:49:52 +02:00
for($i = strtotime($start->format('l'), $start->getTimestamp()); $i <= $until->getTimestamp(); $i = strtotime('+1 day', $i)) {
2019-10-11 15:12:07 +02:00
if ($i >= $today) {
2019-08-12 15:49:52 +02:00
$eventStart = new \DateTime();
$eventStart->setTimestamp($i);
$eventStart->setTime($start->format('H'), $start->format('i'));
$eventEnd = new \DateTime();
$eventEnd->setTimestamp($i);
$eventEnd->setTime($until->format('H'), $until->format('i'));
2019-08-13 11:17:34 +02:00
$dateObj = $this->objectManager->get(\Wrm\Events\Domain\Model\Date::class);
2019-08-12 15:49:52 +02:00
$dateObj->setStart($eventStart);
$dateObj->setEnd($eventEnd);
$this->tmpCurrentEvent->addDate($dateObj);
}
2019-08-12 07:43:37 +02:00
}
2019-08-12 15:49:52 +02:00
}
2019-08-12 07:43:37 +02:00
2020-03-16 12:10:01 +01:00
else if ($date['freq'] == 'Weekly' && !empty($date['weekdays']) && !empty($date['repeatUntil'])) {
2019-08-12 15:49:52 +02:00
foreach ($date['weekdays'] as $day) {
$this->logger->info('Setup weekly interval dates for ' . $day);
2019-08-12 07:43:37 +02:00
$this->logger->info('Start ' . $date['start']);
$this->logger->info('End ' . $date['repeatUntil']);
$start = new \DateTime($date['start'], new \DateTimeZone($date['tz']));
$until = new \DateTime($date['repeatUntil'], new \DateTimeZone($date['tz']));
2019-08-12 15:49:52 +02:00
for($i = strtotime($day, $start->getTimestamp()); $i <= $until->getTimestamp(); $i = strtotime('+1 week', $i)) {
2019-10-11 15:12:07 +02:00
if ($i >= $today) {
2019-08-12 07:43:37 +02:00
$eventStart = new \DateTime();
$eventStart->setTimestamp($i);
$eventStart->setTime($start->format('H'), $start->format('i'));
$eventEnd = new \DateTime();
$eventEnd->setTimestamp($i);
$eventEnd->setTime($until->format('H'), $until->format('i'));
2019-08-13 11:17:34 +02:00
$dateObj = $this->objectManager->get(\Wrm\Events\Domain\Model\Date::class);
2019-08-12 07:43:37 +02:00
$dateObj->setStart($eventStart);
$dateObj->setEnd($eventEnd);
$this->tmpCurrentEvent->addDate($dateObj);
}
}
}
2019-08-12 15:49:52 +02:00
}
}
}
2019-10-11 15:12:07 +02:00
$this->logger->info('Finished setup dates');
2019-08-12 15:49:52 +02:00
}
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
/**
* @param array $addresses
*/
protected function setOrganizer(Array $addresses) {
foreach ($addresses as $address)
{
if ($address['rel'] == "organizer") {
$tmpOrganizer = $this->organizerRepository->findOneByName($address['name']);
2019-08-13 11:17:34 +02:00
if ($tmpOrganizer) {
2019-08-12 15:49:52 +02:00
$this->tmpCurrentEvent->setOrganizer($tmpOrganizer);
2019-08-13 11:17:34 +02:00
continue;
2019-08-12 15:49:52 +02:00
}
2019-08-13 11:17:34 +02:00
$tmpOrganizer = $this->objectManager->get(\Wrm\Events\Domain\Model\Organizer::class);
$tmpOrganizer->setName($address['name']);
$tmpOrganizer->setCity($address['city']);
$tmpOrganizer->setZip($address['zip']);
$tmpOrganizer->setStreet($address['street']);
$tmpOrganizer->setPhone($address['phone']);
$tmpOrganizer->setWeb($address['web']);
$tmpOrganizer->setEmail($address['email']);
$tmpOrganizer->setDistrict($address['district']);
$this->organizerRepository->add($tmpOrganizer);
$this->tmpCurrentEvent->setOrganizer($tmpOrganizer);
2019-08-12 15:49:52 +02:00
}
}
}
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
/**
2020-03-16 13:52:58 +01:00
* @param array $event
*/
protected function setAddress(Array $event) {
2020-08-17 14:26:38 +02:00
if (!empty($event['name']))
$this->tmpCurrentEvent->setName($event['name']);
2020-03-16 13:52:58 +01:00
if (!empty($event['street']))
$this->tmpCurrentEvent->setStreet($event['street']);
if (!empty($event['city']))
$this->tmpCurrentEvent->setCity($event['city']);
if (!empty($event['zip']))
$this->tmpCurrentEvent->setZip($event['zip']);
if (!empty($event['country']))
$this->tmpCurrentEvent->setCountry($event['country']);
if (!empty($event['phone']))
$this->tmpCurrentEvent->setPhone($event['phone']);
if (!empty($event['web']))
$this->tmpCurrentEvent->setWeb($event['web']);
2019-09-23 15:41:41 +02:00
}
2020-08-19 11:30:13 +02:00
/**
* @param array $media
*/
protected function setSocial(Array $media) {
foreach ($media as $link)
{
if ($link['rel'] == "socialmedia" && $link['value'] == "Facebook")
$this->tmpCurrentEvent->setFacebook($link['url']);
if ($link['rel'] == "socialmedia" && $link['value'] == "YouTube")
$this->tmpCurrentEvent->setYouTube($link['url']);
if ($link['rel'] == "socialmedia" && $link['value'] == "Instagram")
$this->tmpCurrentEvent->setInstagram($link['url']);
if ($link['rel'] == "booking")
$this->tmpCurrentEvent->setTicket($link['url']);
}
}
2019-09-23 15:41:41 +02:00
/**
* @param string $lat
* @param string $lng
*/
protected function setLatLng(String $lat, String $lng) {
2019-08-12 15:49:52 +02:00
$this->tmpCurrentEvent->setLatitude($lat);
$this->tmpCurrentEvent->setLongitude($lng);
}
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
/**
* Set Texts
* @param Array $texts
*/
protected function setTexts(Array $texts) {
foreach ($texts as $text)
{
if ($text['rel'] == "details" && $text['type'] == "text/plain") {
2020-08-19 11:30:13 +02:00
$this->tmpCurrentEvent->setDetails(str_replace('\n\n', '\n', $text['value']));
2019-08-12 15:49:52 +02:00
}
if ($text['rel'] == "teaser" && $text['type'] == "text/plain") {
2020-08-19 11:30:13 +02:00
$this->tmpCurrentEvent->setTeaser(str_replace('\n\n', '\n', $text['value']));
2019-08-12 15:49:52 +02:00
}
2020-03-16 14:06:03 +01:00
if ($text['rel'] == "PRICE_INFO" && $text['type'] == "text/plain") {
2020-08-19 11:30:13 +02:00
$this->tmpCurrentEvent->setPriceInfo(str_replace('\n\n', '\n', $text['value']));
2020-03-16 14:06:03 +01:00
}
2019-08-12 15:49:52 +02:00
}
}
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
/**
* Load File
* @param String $globalId
* @param String $title
*/
protected function getOrCreateEvent(String $globalId, String $title) {
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
$event = $this->eventRepository->findOneByGlobalId($globalId);
2019-08-12 07:43:37 +02:00
2019-08-13 11:17:34 +02:00
if ($event) {
2019-08-12 15:49:52 +02:00
// Global ID is found and events gets updated
$event = $this->eventRepository->findOneByGlobalId($globalId);
$this->logger->info('Found "' . substr($title, 0, 20) . '..." with global id ' . $globalId . ' in database');
2019-08-13 11:17:34 +02:00
return $event;
2019-08-12 15:49:52 +02:00
}
2019-08-12 07:43:37 +02:00
2019-08-13 11:17:34 +02:00
// New event is created
$this->logger->info(substr($title, 0, 20) . ' does not exist');
$event = $this->objectManager->get(\Wrm\Events\Domain\Model\Event::class);
// Create event and persist
$event->setGlobalId($globalId);
$event->setCategories(new ObjectStorage());
$this->eventRepository->add($event);
$this->persistenceManager->persistAll();
$this->logger->info('Not found "' . substr($title, 0, 20) . '..." with global id ' . $globalId . ' in database. Created new one.');
2019-08-12 15:49:52 +02:00
return $event;
}
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
/**
* @param array $assets
*/
protected function setAssets(Array $assets) {
2019-08-13 11:17:34 +02:00
2019-10-09 15:03:58 +02:00
$this->logger->info("Set assets");
2019-08-13 11:17:34 +02:00
$error = false;
2019-08-12 15:49:52 +02:00
foreach ($assets as $media_object)
{
if($media_object['rel'] == "default" && $media_object['type'] == "image/jpeg") {
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
$this->storage = $this->resourceFactory->getDefaultStorage();
2019-08-12 07:43:37 +02:00
2019-10-09 15:03:58 +02:00
if ($this->storage == null) {
$this->logger->error('No default storage defined. Cancel import.');
die();
}
2019-08-12 15:49:52 +02:00
// Check if file already exists
if (file_exists($this->environment->getPublicPath() . '/fileadmin/' . $this->filesFolder . strtolower(basename($media_object['url'])))) {
2019-08-13 09:12:30 +02:00
$this->logger->info('File already exists');
2019-08-12 15:49:52 +02:00
} else {
2019-08-13 09:12:30 +02:00
$this->logger->info("File don't exist");
2019-08-12 15:49:52 +02:00
// Load the file
if ($file = $this->loadFile($media_object['url'])) {
// Move file to defined folder
2019-08-13 09:12:30 +02:00
$this->logger->info('Adding file ' . $file);
$this->storage->addFile($this->environment->getPublicPath() . "/uploads/tx_events/" . $file, $this->storage->getFolder($this->filesFolder), basename($media_object['url']));
2019-08-12 07:43:37 +02:00
} else {
2019-08-12 15:49:52 +02:00
$error = true;
2019-08-12 07:43:37 +02:00
}
2019-08-12 15:49:52 +02:00
}
2019-08-12 07:43:37 +02:00
2019-08-12 15:49:52 +02:00
if ($error !== true) {
if ($this->tmpCurrentEvent->getImages() !== null) {
$this->logger->info('Relation found');
// TODO: How to delete file references?
} else {
$this->logger->info('No relation found');
$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'));
2019-08-13 09:12:30 +02:00
$this->createFileRelations($file->getUid(), 'tx_events_domain_model_event', $this->tmpCurrentEvent->getUid(), 'images', $this->storagePid);
2019-08-12 07:43:37 +02:00
}
}
2019-08-12 15:49:52 +02:00
}
2019-08-13 11:17:34 +02:00
$error = false;
2019-08-12 07:43:37 +02:00
}
}
/**
* Load File
* @param string $file
2019-08-13 11:17:34 +02:00
* @return string
2019-08-12 07:43:37 +02:00
*/
protected function loadFile($file) {
2019-08-13 09:12:30 +02:00
$directory = $this->environment->getPublicPath() . "/uploads/tx_events/";
2019-08-12 07:43:37 +02:00
$filename = basename($file);
2019-08-13 09:12:30 +02:00
$this->logger->info('Getting file ' . $file . ' as ' . $filename);
2019-08-13 11:17:34 +02:00
$asset = GeneralUtility::getUrl($file);
2019-08-12 07:43:37 +02:00
if ($asset) {
2019-08-13 09:12:30 +02:00
file_put_contents($directory . $filename, $asset);
2019-08-12 07:43:37 +02:00
return $filename;
}
2019-08-13 09:12:30 +02:00
$this->logger->error('Cannot load file ' . $file);
2019-08-12 07:43:37 +02:00
return false;
}
/**
* Build relations for FAL
* @param int $uid_local
* @param string $tablenames
* @param int $uid_foreign
* @param string $fieldname
* @param string $storagePid
* @return bool
*/
protected function createFileRelations($uid_local, $tablenames, $uid_foreign, $fieldname, $storagePid) {
$newId = 'NEW1234';
$data = array();
$data['sys_file_reference'][$newId] = array(
'table_local' => 'sys_file',
'uid_local' => $uid_local,
'tablenames' => $tablenames,
'uid_foreign' => $uid_foreign,
'fieldname' => $fieldname,
'pid' => $storagePid
);
$data[$tablenames][$uid_foreign] = array(
'pid' => $storagePid,
$fieldname => $newId
);
2019-08-13 11:17:34 +02:00
$dataHandler = $this->objectManager->get(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
2019-08-12 07:43:37 +02:00
$dataHandler->start($data, array());
$dataHandler->process_datamap();
if (count($dataHandler->errorLog) === 0) {
return true;
}
2019-08-13 11:17:34 +02:00
foreach($dataHandler->errorLog as $error) {
$this->logger->info($error);
}
return false;
2019-08-12 07:43:37 +02:00
}
/**
* Performs slug update
* @return bool
*/
protected function doSlugUpdate()
{
$this->logger->info('Update slugs');
$slugHelper = GeneralUtility::makeInstance(
SlugHelper::class,
'tx_events_domain_model_event',
'slug',
$GLOBALS['TCA']['tx_events_domain_model_event']['columns']['slug']['config']
);
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_events_domain_model_event');
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll();
$statement = $queryBuilder->select('uid', 'global_id')
->from('tx_events_domain_model_event')
->where(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->eq('slug', $queryBuilder->createNamedParameter('', \PDO::PARAM_STR)),
$queryBuilder->expr()->isNull('slug')
)
)
->execute();
while ($record = $statement->fetch()) {
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->update('tx_events_domain_model_event')
->where(
$queryBuilder->expr()->eq(
'uid',
$queryBuilder->createNamedParameter($record['uid'], \PDO::PARAM_INT)
)
)
->set('slug', $slugHelper->sanitize((string)$record['global_id']));
$queryBuilder->getSQL();
$queryBuilder->execute();
}
return true;
}
2019-10-09 15:03:58 +02:00
}