Skip to content
Snippets Groups Projects
Commit 0cba5ced authored by Torsten Oppermann's avatar Torsten Oppermann
Browse files

[TASK] Fixing file reference issues

parent e35644c1
No related branches found
No related tags found
No related merge requests found
......@@ -31,6 +31,8 @@ use SGalinski\SgNews\Domain\Model\Tag;
use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
......@@ -178,21 +180,22 @@ class MigrateNewsCommandController extends CommandController {
$newsPage->addTag($matchingTag);
}
/** @var FileReference $image */
$image = $this->getMatchingImage($row);
if ($image !== NULL) {
$newsPage->addTeaser1Image($image);
$originalResource = $image->getOriginalResource();
if ($originalResource !== NULL) {
$originalFile = $originalResource->getOriginalFile();
if ($originalFile !== NULL) {
$teaserImage2 = $this->fileReferenceRepository->addFileReferenceFromFile(
$originalFile, $this->newsPagesMap[$row['uid']],
$this->newsPagesMap[$row['uid']], 'pages', 'tx_sgnews_teaser2_image'
);
$newsPage->addTeaser2Image($teaserImage2);
}
/** @var File $image */
$file = $this->getMatchingFile($row);
if ($file instanceof File) {
$teaserImage1 = $this->fileReferenceRepository->addFileReferenceFromFile(
$file, $this->newsPagesMap[$row['uid']],
$this->newsPagesMap[$row['uid']], 'pages', 'tx_sgnews_teaser1_image'
);
if ($teaserImage1) {
$newsPage->addTeaser1Image($teaserImage1);
$teaserImage2 = $this->fileReferenceRepository->addFileReferenceFromFile(
$file, $this->newsPagesMap[$row['uid']],
$this->newsPagesMap[$row['uid']], 'pages', 'tx_sgnews_teaser2_image'
);
$newsPage->addTeaser2Image($teaserImage2);
}
}
......@@ -213,33 +216,37 @@ class MigrateNewsCommandController extends CommandController {
* Get the image file matching the news
*
* @param array $row
* @return null|FileReference
* @return null|FileInterface
* @throws \Exception
*/
private function getMatchingImage(array $row) {
private function getMatchingFile(array $row) {
// match old page id with old file reference id
/** @var DatabaseConnection $db */
$db = $GLOBALS['TYPO3_DB'];
$where = 'tablenames = "tx_news_domain_model_news" AND fieldname = "fal_media" AND uid_foreign = ' . $row['uid'];
$where = 'tablenames = "tx_news_domain_model_news" AND fieldname = "fal_media" AND uid_foreign = ' . $this->newsPagesMap[$row['uid']];
/** @var \mysqli_result $result */
$result = $db->exec_SELECTgetSingleRow('uid, uid_local', 'sys_file_reference', $where);
if (!$result) {
$fileReferenceResult = $db->exec_SELECTgetSingleRow(
'uid, uid_local', 'sys_file_reference_news_migration', $where
);
if (!$fileReferenceResult) {
return NULL;
}
$where = 'uid = ' . $result['uid'];
$db->exec_UPDATEquery(
'sys_file_reference',
$where,
['pid' => $this->newsPagesMap[$row['uid']], 'uid_foreign' => $this->newsPagesMap[$row['uid']], 'tablenames' => 'pages', 'fieldname' => 'tx_sgnews_teaser1_image']
);
$where = 'uid = ' . $fileReferenceResult['uid_local'];
/** @var \mysqli_result $result */
$fileResult = $db->exec_SELECTgetSingleRow('identifier', 'sys_file_news_migration', $where);
if (!$fileResult) {
return NULL;
}
$result = $this->fileReferenceRepository->findByUid((int) $result['uid']);
if ($result instanceof FileReference) {
$result->setPid($this->newsPagesMap[$row['uid']]);
return $result;
$oldIdentifier = $fileResult['identifier'];
$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
$storage = $resourceFactory->getStorageObject(1);
if (!$storage->hasFile($oldIdentifier)) {
return NULL;
}
return NULL;
return $storage->getFile($oldIdentifier);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment