Skip to content

Commit

Permalink
* Add support for the import into image FAL records referencing to th…
Browse files Browse the repository at this point in the history
…e table sys_file_reference
  • Loading branch information
franzholz committed Dec 12, 2019
1 parent 0aad8ae commit 4ac8f03
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 13 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2019-12-12 Franz Holzinger <[email protected]>

* Add support for the import into image FAL records referencing to the table sys_file_reference

2019-12-03 Franz Holzinger <[email protected]>

* Add an import method from one database table to another with an outer join to a category table.
Expand Down
94 changes: 82 additions & 12 deletions Classes/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

use TYPO3\CMS\Core\Utility\GeneralUtility;

use JambageCom\Import\Api\ImportFal;


class Api {
/**
Expand Down Expand Up @@ -148,14 +150,16 @@ public function importTableFile (
return $result;
}


protected function convertToDestination(
$sourceRow,
$recordRelations,
array &$falRow,
array $sourceRow,
array $recordRelations,
$destinationTableName
) {
$result = array();
foreach ($recordRelations as $relationKey => $relationValue) {
if (strpos($relationKey, 'import-') === 0) {
if (strpos($relationKey, 'import-') === 0) { // do not use the configuration part
continue;
}
$result[$relationKey] = $sourceRow[$relationValue];
Expand All @@ -172,13 +176,61 @@ protected function convertToDestination(
}
$result['tstamp'] = $this->getTime();
foreach ($result as $field => $value) {
if (!isset($GLOBALS['TCA'][$destinationTableName]['columns'][$field])) {
if (
$GLOBALS['TCA'][$destinationTableName]['columns'][$field]['config']['foreign_table'] == 'sys_file_reference' ||
in_array($field, array('pid', 'tstamp', 'crdate'))
) {
// FAL records will be added after the insertion of the record
$falRow[$field] = $value;
}

if (
$field != 'pid' &&
(
!isset($GLOBALS['TCA'][$destinationTableName]['columns'][$field]) ||
isset($falRow[$field])
)
) {
unset($result[$field]);
}
}

$falRow['crdate'] = $falRow['tstamp'];

return $result;
}

public function addFal (
$destinationTableName,
$destinationFalRow,
$sourceRow,
$imageFolder
) {
$falApi = GeneralUtility::makeInstance(ImportFal::class);

foreach ($destinationFalRow as $field => $value) {
if (
$imageFolder != '' &&
strlen($value) &&
$GLOBALS['TCA'][$destinationTableName]['columns'][$field]['config']['foreign_table'] == 'sys_file_reference'
) {
$falRow = array();
$falRow['uid'] = $destinationFalRow['uid'];
$falRow['pid'] = $destinationFalRow['pid'];
$falRow[$field] = $sourceRow[$field];
$files = GeneralUtility::trimExplode(',', $falRow[$field]);

$falApi->add(
$destinationTableName,
$falRow,
$field,
$files,
$GLOBALS['TCA']['tt_address']['columns'][$field]['config'],
$imageFolder
);
}
}
}

public function importTableFromTable (
$recordRelationFile,
Expand Down Expand Up @@ -221,29 +273,33 @@ public function importTableFromTable (
$sourceTableName != '' &&
$destinationTableName != ''
) {
$where_clause = 'deleted=0';
$where_clause = 'deleted=0';
$allDestinationCategoryRows = [];

if (
$sourceMMTableName != '' &&
!empty($categoryRelations)
) {
) { // process the categories
$sourceCategoryRows =
$GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
'*',
$sourceCategoryTable,
$where_clause
);
$count = 0;
$imageFolder = '';
foreach ($sourceCategoryRows as $sourceCategoryRow) {
$count++;
if ($count < 0) // changed during test development
break;
$falRow = array(); // TODO: FAL for categories
$destinationCategoryRow =
$this->convertToDestination(
$falRow,
$sourceCategoryRow,
$categoryRelations,
$destinationCategoryTable
$destinationCategoryTable,
$imageFolder
);

$where = $where_clause;
Expand Down Expand Up @@ -285,12 +341,14 @@ public function importTableFromTable (
);
$count = 0;
while(
($count >= 0) &&
($count > -1) &&
($sourceRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
) { // $count comparison is changed during test development
) { // the $count comparison is changed during testing and development
$count++;
$falRow = array();
$destinationRow =
$this->convertToDestination(
$falRow,
$sourceRow,
$recordRelations,
$destinationTableName
Expand All @@ -305,7 +363,7 @@ public function importTableFromTable (
$where .= ' AND ' . $field . '=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($value, $destinationTableName);
}

$checkDestinationRow = // verify that this category has not yet been imported
$checkDestinationRow = // verify that this record has not yet been imported
$GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'*',
$destinationTableName,
Expand All @@ -319,6 +377,19 @@ public function importTableFromTable (
$destinationRow
);
$destinationUid = $GLOBALS['TYPO3_DB']->sql_insert_id();

if ($destinationUid) {
$imageFolder = $recordRelations['import-source-image-folder'];

// add FAL records
$falRow['uid'] = $destinationUid;
$this->addFal(
$destinationTableName,
$falRow,
$sourceRow,
$imageFolder
);
}

if (
$destinationUid &&
Expand Down Expand Up @@ -360,8 +431,7 @@ public function importTableFromTable (
$destinationMMRow
);
}
}

}
}
}
}
Expand Down
153 changes: 153 additions & 0 deletions Classes/Api/ImportFal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
namespace JambageCom\Import\Api;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

/**
* Part of the import extension.
*
* FAL functions
*
* @author Franz Holzinger <[email protected]>
* @maintainer Franz Holzinger <[email protected]>
* @package TYPO3
* @subpackage import
*
*/

use TYPO3\CMS\Core\Utility\GeneralUtility;




class ImportFal {
const TARGET_DIRECTORY = 'user_upload/';

/**
* Returns the "AND NOT deleted" clause for the tablename given IF $GLOBALS['TCA'] configuration points to such a field.
*
* @param string Tablename
* @return string
* @see enableFields()
*/
static public function deleteClause ($table) {
if (!strcmp($table, 'pages')) { // Hardcode for pages because TCA might not be loaded yet (early frontend initialization)
$result = ' AND deleted=0';
} else {
$result = $GLOBALS['TCA'][$table]['ctrl']['delete'] ? ' AND ' . $GLOBALS['TCA'][$table]['ctrl']['delete'] . '=0' : '';
}
return $result;
}

public function add (
$tablename,
array $row,
$falFieldname,
array $files,
array $config,
$falFolder
) {
if (!empty($files) && $falFolder != '') {

/** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
$storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
$storage = $storageRepository->findByUid(1);
$targetDirectory = self::TARGET_DIRECTORY;
$targetFolder = null;
if (!$storage->hasFolder('/' . $targetDirectory)) {
$targetFolder = $storage->createFolder('/' . $targetDirectory);
} else {
$targetFolder = $storage->getFolder('/' . $targetDirectory);
}

$sysfileRowArray = array();
$falTable = 'sys_file_reference';
$where_clause = 'uid_foreign=' . intval($row['uid']) . ' AND tablenames=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tablename, $falTable) . ' AND fieldname=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($falFieldname, $falTable);
$where_clause .= $this->deleteClause('sys_file_reference');

$sysfileRowArray =
$GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
'*',
$falTable,
$where_clause,
'',
'sorting',
'',
'uid_local'
);

$imageCount = count($files);
$needsCountUpdate = true;
foreach ($files as $imageKey => $image) {
$imageFile = $targetDirectory . $image;
$fileIdentifier = '1:' . $imageFile;

// Check if the file is already known by FAL, if not add it
$targetFileName = 'fileadmin/' . $imageFile;

if (!file_exists(PATH_site . $targetFileName)) {
$fullSourceFileName = PATH_site . $falFolder . '/' . $image;
// Move the file to the storage and index it (indexing creates the sys_file entry)
$file = $storage->addFile($fullSourceFileName, $targetFolder, '', 'cancel');
}

$fac = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory'); // create instance to storage repository

$file = $fac->getFileObjectFromCombinedIdentifier($fileIdentifier);
if ($file instanceof \TYPO3\CMS\Core\Resource\File) {
$fileUid = $file->getUid();
if (
empty($sysfileRowArray) ||
!isset($sysfileRowArray[$fileUid])
) {
$data = array();
$data['sys_file_reference']['NEW1234'] = array(
'uid_local' => $fileUid,
'uid_foreign' => $row['uid'], // uid of your table record
'tablenames' => $tablename,
'fieldname' => $falFieldname,
'pid' => $row['pid'], // parent id of the parent page
'table_local' => 'sys_file',
);

if (!isset($sysfileRowArray[$fileUid])) {
$data[$tablename][$row['uid']] = array($falFieldname => 'NEW1234');
}

/** @var \TYPO3\CMS\Core\DataHandling\DataHandler $tce */
$tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\DataHandling\DataHandler'); // create TCE instance
$tce->start($data, array());
$tce->process_datamap();

if ($tce->errorLog) {
$content .= 'TCE->errorLog:' . \TYPO3\CMS\Core\Utility\DebugUtility::viewArray($tce->errorLog);
} else {
// nothing
}
}
}
$imageCount++;
} // foreach ($files)

if ($needsCountUpdate) {
$GLOBALS['TYPO3_DB']->exec_UPDATEquery(
$tablename,
'uid=' . intval($row['uid']),
array($falFieldname => $imageCount)
);
}
}
}
}

2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'createDirs' => '',
'clearCacheOnLoad' => 0,
'author_company' => '',
'version' => '0.3.0',
'version' => '0.4.0',
'constraints' => array(
'depends' => array(
'php' => '5.5.0-7.99.99',
Expand Down

0 comments on commit 4ac8f03

Please sign in to comment.