Skip to content

Commit

Permalink
* Add an import method from one database table to another with an out…
Browse files Browse the repository at this point in the history
…er join to a category table.
  • Loading branch information
franzholz committed Dec 3, 2019
1 parent 0eb987e commit 0aad8ae
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 23 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
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.

2019-11-28 Franz Holzinger <[email protected]>

* Add example slots
Expand Down
251 changes: 251 additions & 0 deletions Classes/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,43 @@ class Api {
* @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
*/
protected $signalSlotDispatcher;
protected $time;

/**
* Constructor
*/
public function __construct ()
{
$this->signalSlotDispatcher = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
$time = time();
if (!empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['serverTimeZone'])) {
$time += ($GLOBALS['TYPO3_CONF_VARS']['SYS']['serverTimeZone'] * 3600);
}
$this->setTime($time);
$standardFields = [
'pid', 'tstamp', 'crdate', 'cruser_id', 'hidden', 'starttime', 'endtime', 'sorting'
];
$this->setStandardFields($standardFields);
}

protected function setTime($time)
{
$this->time = $time;
}

protected function getTime()
{
return $this->time;
}

protected function setStandardFields($standardFields)
{
$this->standardFields = $standardFields;
}

protected function getStandardFields()
{
return $this->standardFields;
}

public function importTableFile (
Expand Down Expand Up @@ -117,5 +147,226 @@ public function importTableFile (

return $result;
}

protected function convertToDestination(
$sourceRow,
$recordRelations,
$destinationTableName
) {
$result = array();
foreach ($recordRelations as $relationKey => $relationValue) {
if (strpos($relationKey, 'import-') === 0) {
continue;
}
$result[$relationKey] = $sourceRow[$relationValue];
}

$standardFields = $this->getStandardFields();
foreach ($sourceRow as $field => $value) {
if (
!isset($result[$field]) &&
in_array($field, $standardFields)
) {
$result[$field] = $value;
}
}
$result['tstamp'] = $this->getTime();
foreach ($result as $field => $value) {
if (!isset($GLOBALS['TCA'][$destinationTableName]['columns'][$field])) {
unset($result[$field]);
}
}

return $result;
}

public function importTableFromTable (
$recordRelationFile,
$categoryRelationFile,
$mode = 0
)
{
if (
$recordRelationFile != ''
) {
$xml = file_get_contents($recordRelationFile);
$recordRelations = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($xml);
if (!is_array($recordRelations)) {
throw new \RuntimeException($recordRelationFile . ': ' . $recordRelations);
}

$sourceTableName = $recordRelations['import-source'];
$sourceMMTableName = $recordRelations['import-source-mm-category'];
$destinationMMTableName = $recordRelations['import-destination-mm-category'];
$destinationMMTableField = $recordRelations['import-destination-mm-category-field'];
$destinationTableName = $recordRelations['import-destination'];
$destinationExtension = $recordRelations['import-destination-extension'];

$categoryRelations = null;
$sourceCategoryTable = null;
$destinationCategoryTable = null;

if ($categoryRelationFile != '') {
$xml = file_get_contents($categoryRelationFile);
$categoryRelations = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($xml);
if (!is_array($categoryRelations)) {
throw new \RuntimeException($categoryRelationFile . ': ' . $categoryRelations);
}

$sourceCategoryTable = $categoryRelations['import-source'];
$destinationCategoryTable = $categoryRelations['import-destination'];
}

if (
$sourceTableName != '' &&
$destinationTableName != ''
) {
$where_clause = 'deleted=0';
$allDestinationCategoryRows = [];

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

$where = $where_clause;
$standardFields = $this->getStandardFields();
foreach ($destinationCategoryRow as $field => $value) {
if (in_array($field, $standardFields)) {
continue;
}
$where .= ' AND ' . $field . '=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($value, $destinationCategoryTable);
}

$checkDestinationCategoryRow = // verify that this category has not yet been imported
$GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'*',
$destinationCategoryTable,
$where
);

if ($checkDestinationCategoryRow) {
$allDestinationCategoryRows[$sourceCategoryRow['uid']] = $checkDestinationCategoryRow;
} else {
$GLOBALS['TYPO3_DB']->exec_INSERTquery(
$destinationCategoryTable,
$destinationCategoryRow
);
$insertUid = $GLOBALS['TYPO3_DB']->sql_insert_id();
$destinationCategoryRow['uid'] = $insertUid;
$allDestinationCategoryRows[$sourceCategoryRow['uid']] = $destinationCategoryRow;

}
}
}

$res =
$GLOBALS['TYPO3_DB']->exec_SELECTquery(
'*',
$sourceTableName,
$where_clause
);
$count = 0;
while(
($count >= 0) &&
($sourceRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
) { // $count comparison is changed during test development
$count++;
$destinationRow =
$this->convertToDestination(
$sourceRow,
$recordRelations,
$destinationTableName
);

$where = $where_clause;
$standardFields = $this->getStandardFields();
foreach ($destinationRow as $field => $value) {
if (in_array($field, $standardFields)) {
continue;
}
$where .= ' AND ' . $field . '=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($value, $destinationTableName);
}

$checkDestinationRow = // verify that this category has not yet been imported
$GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'*',
$destinationTableName,
$where
);

if (!$checkDestinationRow) {

$GLOBALS['TYPO3_DB']->exec_INSERTquery(
$destinationTableName,
$destinationRow
);
$destinationUid = $GLOBALS['TYPO3_DB']->sql_insert_id();

if (
$destinationUid &&
$sourceMMTableName != '' &&
$destinationMMTableName != ''
) {
$sourceUid = $sourceRow['uid'];
$where = 'uid_local=' . intval($sourceUid);
$sourceCategoryRows =
$GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
'uid_foreign,tablenames',
$sourceMMTableName,
$where
);
$categoryArray = [];
if ($sourceCategoryRows) {
foreach($sourceCategoryRows as $sourceCategoryRow) {
$categoryArray[] = $sourceCategoryRow['uid_foreign'];
}
}
$destinationCategoryArray = [];
if ($categoryArray) {
foreach ($categoryArray as $category) {
$destinationCategoryArray[] = $allDestinationCategoryRows[$category]['uid'];
}
}

if (!empty($destinationCategoryArray)) {
$destinationMMRow = [];
$destinationMMRow['uid_foreign'] = $destinationUid;
$destinationMMRow['tablenames'] = $destinationTableName;
$destinationMMRow['fieldname'] = $destinationMMTableField;

foreach ($destinationCategoryArray as $category) {
$destinationMMRow['uid_local'] = $category;

$GLOBALS['TYPO3_DB']->exec_INSERTquery(
$destinationMMTableName,
$destinationMMRow
);
}
}

}
}
}
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public function main()
$menu = $slotResult['1'];
}
}

$execute = GeneralUtility::_GP('execute');

if ($execute) {
Expand Down Expand Up @@ -104,14 +103,13 @@ public function main()
$assigns['information'] =
$GLOBALS['LANG']->getLL('wizard.importedTables');
} else {

// CSH
$assigns['cshItem'] = BackendUtility::cshItem('_MOD_web_func', 'tx_import');

$view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
'EXT:' . IMPORT_EXT . '/Resources/Private/Templates/ImportWizard.html'
));

// CSH
$assigns['cshItem'] = BackendUtility::cshItem('_MOD_web_func', 'tx_import');

$assigns['menu'] = $menu;
}

Expand Down
1 change: 1 addition & 0 deletions Classes/Slots/ExampleFunctionSlots.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function processImport (
$mode
)
{
// TODO: implement the import here
$result = array(
$tableName,
$row,
Expand Down
2 changes: 1 addition & 1 deletion Classes/Task/ImportTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function execute ()
$result = true;
$localDefinitionArray = \JambageCom\Import\Api\ImportApi::getLocalDefinitionArray();
$definitionArray = array_merge($localDefinitionArray, $slotDefinitionArray);
//

foreach ($definitionArray as $definition) {
if (
!isset($definition['tables']) ||
Expand Down
2 changes: 1 addition & 1 deletion Resources/Private/Language/locallang_csh.xlf
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<source>Import Tables wizard</source>
</trans-unit>
<trans-unit id="tx_import.description" xml:space="preserve">
<source>Enables you to import files into database tables.
<source>Enables you to import files and other sources into database tables.
Just check the tables you need.</source>
</trans-unit>
</body>
Expand Down
34 changes: 18 additions & 16 deletions ext_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
defined('TYPO3_MODE') || die();

if (TYPO3_MODE == 'BE') {
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::insertModuleFunction(
'web_func',
\JambageCom\Import\Controller\ImportTablesWizardModuleFunctionController::class,
null,
'LLL:EXT:' . IMPORT_EXT . '/Resources/Private/Language/locallang.xlf:moduleFunction.import'
);
call_user_func(function () {
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::insertModuleFunction(
'web_func',
\JambageCom\Import\Controller\ImportTablesWizardModuleFunctionController::class,
null,
'LLL:EXT:' . IMPORT_EXT . '/Resources/Private/Language/locallang.xlf:moduleFunction.import'
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addLLrefForTCAdescr(
'_MOD_web_func',
'EXT:' . IMPORT_EXT . '/Resources/Private/Language/locallang_csh.xlf'
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addLLrefForTCAdescr(
'_MOD_web_func',
'EXT:' . IMPORT_EXT . '/Resources/Private/Language/locallang_csh.xlf'
);

// Add context sensitive help (csh) to the backend module
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addLLrefForTCAdescr(
IMPORT_CSHKEY,
'EXT:' . IMPORT_EXT . '/Resources/Private/Language/locallang_csh_import.xlf'
);
// Add context sensitive help (csh) to the backend module
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addLLrefForTCAdescr(
IMPORT_CSHKEY,
'EXT:' . IMPORT_EXT . '/Resources/Private/Language/locallang_csh_import.xlf'
);
});
}

0 comments on commit 0aad8ae

Please sign in to comment.