2021-02-01 14:14:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace WerkraumMedia\ThueCat\Domain\Import\Importer;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool;
|
|
|
|
use TYPO3\CMS\Core\DataHandling\DataHandler;
|
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\Model\Entity;
|
2021-02-03 17:20:01 +01:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Import\Model\EntityCollection;
|
2021-02-01 14:14:05 +01:00
|
|
|
use WerkraumMedia\ThueCat\Domain\Model\Backend\ImportLog;
|
|
|
|
use WerkraumMedia\ThueCat\Domain\Model\Backend\ImportLogEntry;
|
|
|
|
|
|
|
|
class SaveData
|
|
|
|
{
|
|
|
|
private DataHandler $dataHandler;
|
|
|
|
private ConnectionPool $connectionPool;
|
2021-02-03 17:20:01 +01:00
|
|
|
private array $errorLog;
|
2021-02-01 14:14:05 +01:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
DataHandler $dataHandler,
|
|
|
|
ConnectionPool $connectionPool
|
|
|
|
) {
|
|
|
|
$this->dataHandler = $dataHandler;
|
|
|
|
$this->connectionPool = $connectionPool;
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:20:01 +01:00
|
|
|
public function import(EntityCollection $entityCollection, ImportLog $log): void
|
2021-02-01 14:14:05 +01:00
|
|
|
{
|
2021-02-03 17:20:01 +01:00
|
|
|
$this->errorLog = [];
|
2021-02-01 14:14:05 +01:00
|
|
|
|
2021-02-16 17:12:47 +01:00
|
|
|
$this->updateKnownData($entityCollection);
|
|
|
|
$this->createEntities($entityCollection);
|
|
|
|
$this->updateKnownData($entityCollection);
|
|
|
|
$this->updateEntities($entityCollection);
|
2021-02-01 14:14:05 +01:00
|
|
|
|
2021-02-03 17:20:01 +01:00
|
|
|
foreach ($entityCollection->getEntities() as $entity) {
|
|
|
|
$log->addEntry(new ImportLogEntry($entity, $this->errorLog));
|
2021-02-01 14:14:05 +01:00
|
|
|
}
|
2021-02-03 17:20:01 +01:00
|
|
|
}
|
2021-02-01 14:14:05 +01:00
|
|
|
|
2021-02-16 17:12:47 +01:00
|
|
|
private function updateKnownData(EntityCollection $entities): void
|
2021-02-03 17:20:01 +01:00
|
|
|
{
|
2021-02-16 17:12:47 +01:00
|
|
|
foreach ($entities->getEntities() as $entity) {
|
|
|
|
if ($entity->exists()) {
|
2021-02-03 17:20:01 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-02-16 17:12:47 +01:00
|
|
|
|
|
|
|
$identifier = $this->getIdentifier($entity);
|
2021-02-03 17:20:01 +01:00
|
|
|
if (is_numeric($identifier)) {
|
|
|
|
$entity->setExistingTypo3Uid((int) $identifier);
|
|
|
|
}
|
2021-02-16 17:12:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createEntities(EntityCollection $entities): void
|
|
|
|
{
|
|
|
|
$this->createDefaultLanguageEntities($entities);
|
|
|
|
$this->createTranslationEntities($entities);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createDefaultLanguageEntities(EntityCollection $entities): void
|
|
|
|
{
|
|
|
|
$identifierMapping = [];
|
2021-02-03 17:20:01 +01:00
|
|
|
|
2021-02-16 17:12:47 +01:00
|
|
|
$entity = $entities->getDefaultLanguageEntity();
|
|
|
|
if ($entity === null) {
|
|
|
|
return;
|
2021-02-03 17:20:01 +01:00
|
|
|
}
|
2021-02-16 17:12:47 +01:00
|
|
|
$identifier = $this->getIdentifier($entity);
|
|
|
|
$identifierMapping[spl_object_id($entity)] = $identifier;
|
|
|
|
$dataArray[$entity->getTypo3DatabaseTableName()][$identifier] = $this->getEntityData($entity);
|
2021-02-03 17:20:01 +01:00
|
|
|
|
|
|
|
$dataHandler = clone $this->dataHandler;
|
|
|
|
$dataHandler->start($dataArray, []);
|
|
|
|
$dataHandler->process_datamap();
|
|
|
|
$this->errorLog = array_merge($this->errorLog, $dataHandler->errorLog);
|
|
|
|
|
2021-02-16 17:12:47 +01:00
|
|
|
foreach ($entities->getEntities() as $entity) {
|
2021-02-03 17:20:01 +01:00
|
|
|
if (
|
|
|
|
isset($identifierMapping[spl_object_id($entity)])
|
|
|
|
&& isset($dataHandler->substNEWwithIDs[$identifierMapping[spl_object_id($entity)]])
|
|
|
|
) {
|
|
|
|
$entity->setImportedTypo3Uid($dataHandler->substNEWwithIDs[$identifierMapping[spl_object_id($entity)]]);
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 14:14:05 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 17:12:47 +01:00
|
|
|
private function createTranslationEntities(EntityCollection $entities): void
|
|
|
|
{
|
|
|
|
$commandMap = [];
|
|
|
|
|
|
|
|
foreach ($entities->getEntitiesToTranslate() as $entity) {
|
|
|
|
$identifier = $this->getDefaultLanguageIdentifier($entity);
|
|
|
|
if (
|
|
|
|
$entity->isForDefaultLanguage()
|
|
|
|
|| $identifier === 0
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$commandMap[$entity->getTypo3DatabaseTableName()][$identifier]['localize'] = $entity->getTypo3SystemLanguageUid();
|
|
|
|
$dataHandler = clone $this->dataHandler;
|
|
|
|
$dataHandler->start([], $commandMap);
|
|
|
|
$dataHandler->process_cmdmap();
|
|
|
|
$this->errorLog = array_merge($this->errorLog, $dataHandler->errorLog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function updateEntities(EntityCollection $entities): void
|
|
|
|
{
|
|
|
|
$dataArray = [];
|
|
|
|
|
|
|
|
foreach ($entities->getExistingEntities() as $entity) {
|
|
|
|
$dataArray[$entity->getTypo3DatabaseTableName()][$entity->getTypo3Uid()] = $this->getEntityData($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dataHandler = clone $this->dataHandler;
|
|
|
|
$dataHandler->start($dataArray, []);
|
|
|
|
$dataHandler->process_datamap();
|
|
|
|
$this->errorLog = array_merge($this->errorLog, $dataHandler->errorLog);
|
|
|
|
}
|
|
|
|
|
2021-02-01 14:14:05 +01:00
|
|
|
private function getIdentifier(Entity $entity): string
|
|
|
|
{
|
|
|
|
$existingUid = $this->getExistingUid($entity);
|
|
|
|
|
|
|
|
if ($existingUid > 0) {
|
|
|
|
return (string) $existingUid;
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:20:01 +01:00
|
|
|
$identifier = 'NEW_' . sha1($entity->getRemoteId() . $entity->getTypo3SystemLanguageUid());
|
|
|
|
// Ensure new ID is max 30, as this is max volumn of the sys_log column
|
|
|
|
return substr($identifier, 0, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getEntityData(Entity $entity): array
|
|
|
|
{
|
|
|
|
return array_merge($entity->getData(), [
|
|
|
|
'pid' => $entity->getTypo3StoragePid(),
|
|
|
|
'remote_id' => $entity->getRemoteId(),
|
|
|
|
]);
|
2021-02-01 14:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getExistingUid(Entity $entity): int
|
|
|
|
{
|
2021-02-16 17:12:47 +01:00
|
|
|
$tableColumns = $this->connectionPool
|
|
|
|
->getConnectionForTable($entity->getTypo3DatabaseTableName())
|
|
|
|
->getSchemaManager()
|
|
|
|
->listTableColumns($entity->getTypo3DatabaseTableName());
|
|
|
|
|
2021-02-01 14:14:05 +01:00
|
|
|
$queryBuilder = $this->connectionPool->getQueryBuilderForTable($entity->getTypo3DatabaseTableName());
|
|
|
|
$queryBuilder->getRestrictions()->removeAll();
|
|
|
|
$queryBuilder->select('uid');
|
|
|
|
$queryBuilder->from($entity->getTypo3DatabaseTableName());
|
|
|
|
$queryBuilder->where($queryBuilder->expr()->eq(
|
|
|
|
'remote_id',
|
|
|
|
$queryBuilder->createNamedParameter($entity->getRemoteId())
|
|
|
|
));
|
2021-02-16 17:12:47 +01:00
|
|
|
if (isset($tableColumns['sys_language_uid'])) {
|
|
|
|
$queryBuilder->andWhere($queryBuilder->expr()->eq(
|
|
|
|
'sys_language_uid',
|
|
|
|
$queryBuilder->createNamedParameter($entity->getTypo3SystemLanguageUid())
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $queryBuilder->execute()->fetchColumn();
|
|
|
|
if (is_numeric($result)) {
|
|
|
|
return (int) $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getDefaultLanguageIdentifier(Entity $entity): int
|
|
|
|
{
|
|
|
|
$queryBuilder = $this->connectionPool->getQueryBuilderForTable($entity->getTypo3DatabaseTableName());
|
|
|
|
$queryBuilder->getRestrictions()->removeAll();
|
|
|
|
$queryBuilder->select('uid');
|
|
|
|
$queryBuilder->from($entity->getTypo3DatabaseTableName());
|
|
|
|
$queryBuilder->where($queryBuilder->expr()->eq(
|
|
|
|
'remote_id',
|
|
|
|
$queryBuilder->createNamedParameter($entity->getRemoteId())
|
|
|
|
));
|
|
|
|
$queryBuilder->andWhere($queryBuilder->expr()->eq(
|
|
|
|
'sys_language_uid',
|
|
|
|
$queryBuilder->createNamedParameter(0)
|
|
|
|
));
|
2021-02-01 14:14:05 +01:00
|
|
|
|
|
|
|
$result = $queryBuilder->execute()->fetchColumn();
|
|
|
|
if (is_numeric($result)) {
|
|
|
|
return (int) $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|