/** * At the end of the import process all file and DB relations should be set properly (that is relations to imported records are all re-created so imported records are correctly related again) * Relations in flexform fields are processed in setFlexFormRelations() after this function * * @return void * @see setFlexFormRelations() * @todo Define visibility */ public function setRelations() { $updateData = array(); // import_newId contains a register of all records that was in the import memorys "records" key foreach ($this->import_newId as $nId => $dat) { $table = $dat['table']; $uid = $dat['uid']; // original UID - NOT the new one! // If the record has been written and received a new id, then proceed: if (is_array($this->import_mapId[$table]) && isset($this->import_mapId[$table][$uid])) { $thisNewUid = BackendUtility::wsMapId($table, $this->import_mapId[$table][$uid]); if (is_array($this->dat['records'][$table . ':' . $uid]['rels'])) { if ($this->legacyImport) { if ($table != 'pages') { $oldPid = $this->dat['records'][$table . ':' . $uid]['data']['pid']; $thisNewPageUid = \TYPO3\CMS\Backend\Utility\BackendUtility::wsMapId($table, $this->import_mapId['pages'][$oldPid]); } else { $thisNewPageUid = $thisNewUid; } } // Traverse relation fields of each record foreach ($this->dat['records'][$table . ':' . $uid]['rels'] as $field => $config) { // uid_local of sys_file_reference needs no update because the correct reference uid was already written // @see ImportExport::fixUidLocalInSysFileReferenceRecords() if ($table === 'sys_file_reference' && $field === 'uid_local') { continue; } switch ((string) $config['type']) { case 'db': if (is_array($config['itemArray']) && count($config['itemArray'])) { $itemConfig = $GLOBALS['TCA'][$table]['columns'][$field]['config']; $valArray = $this->setRelations_db($config['itemArray'], $itemConfig); $updateData[$table][$thisNewUid][$field] = implode(',', $valArray); } break; case 'file': if (is_array($config['newValueFiles']) && count($config['newValueFiles'])) { $valArr = array(); foreach ($config['newValueFiles'] as $fI) { $valArr[] = $this->import_addFileNameToBeCopied($fI); } if ($this->legacyImport && $this->legacyImportFolder === NULL && isset($this->legacyImportMigrationTables[$table][$field])) { // Do nothing - the legacy import folder is missing } elseif ($this->legacyImport && $this->legacyImportFolder !== NULL && isset($this->legacyImportMigrationTables[$table][$field])) { $refIds = array(); foreach ($valArr as $tempFile) { $fileName = $this->alternativeFileName[$tempFile]; $fileObject = NULL; try { // check, if there is alreay the same file in the folder if ($this->legacyImportFolder->hasFile($fileName)) { $file = $this->legacyImportFolder->getFile($fileName); if ($file->getSha1() === sha1_file($tempFile)) { $fileObject = $file; } } } catch (Exception $e) { } if ($fileObject === NULL) { try { $fileObject = $this->legacyImportFolder->addFile($tempFile, $fileName, 'changeName'); } catch (\TYPO3\CMS\Core\Exception $e) { $this->error('Error: no file could be added to the storage for file name' . $this->alternativeFileName[$tempFile]); } } if ($fileObject !== NULL) { $refId = uniqid('NEW'); $refIds[] = $refId; $updateData['sys_file_reference'][$refId] = array('uid_local' => $fileObject->getUid(), 'uid_foreign' => $thisNewUid, 'tablenames' => $table, 'fieldname' => $field, 'pid' => $thisNewPageUid, 'table_local' => 'sys_file'); } } $updateData[$table][$thisNewUid][$field] = implode(',', $refIds); if (!empty($this->legacyImportMigrationTables[$table][$field])) { $this->legacyImportMigrationRecords[$table][$thisNewUid][$field] = $refIds; } } else { $updateData[$table][$thisNewUid][$field] = implode(',', $valArr); } } break; } } } else { $this->error('Error: no record was found in data array!', 1); } } else { $this->error('Error: this records is NOT created it seems! (' . $table . ':' . $uid . ')', 1); } } if (count($updateData)) { $tce = $this->getNewTCE(); $tce->isImporting = TRUE; $this->callHook('before_setRelation', array('tce' => &$tce, 'data' => &$updateData)); $tce->start($updateData, array()); $tce->process_datamap(); // Replace the temporary "NEW" ids with the final ones. foreach ($this->legacyImportMigrationRecords as $table => $records) { foreach ($records as $uid => $fields) { foreach ($fields as $field => $referenceIds) { foreach ($referenceIds as $key => $referenceId) { $this->legacyImportMigrationRecords[$table][$uid][$field][$key] = $tce->substNEWwithIDs[$referenceId]; } } } } $this->callHook('after_setRelations', array('tce' => &$tce)); } }