Ejemplo n.º 1
0
 /**
  * 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()
  */
 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'])) {
                 $thisNewPageUid = 0;
                 if ($this->legacyImport) {
                     if ($table != 'pages') {
                         $oldPid = $this->dat['records'][$table . ':' . $uid]['data']['pid'];
                         $thisNewPageUid = 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']) && !empty($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']) && !empty($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)) {
                                                 $fileStorage = $this->legacyImportFolder->getStorage();
                                                 $file = $fileStorage->getFile($this->legacyImportFolder->getIdentifier() . $fileName);
                                                 if ($file->getSha1() === sha1_file($tempFile)) {
                                                     $fileObject = $file;
                                                 }
                                             }
                                         } catch (Exception $e) {
                                         }
                                         if ($fileObject === null) {
                                             try {
                                                 $fileObject = $this->legacyImportFolder->addFile($tempFile, $fileName, DuplicationBehavior::RENAME);
                                             } catch (Exception $e) {
                                                 $this->error('Error: no file could be added to the storage for file name' . $this->alternativeFileName[$tempFile]);
                                             }
                                         }
                                         if ($fileObject !== null) {
                                             $refId = StringUtility::getUniqueId('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!');
             }
         } else {
             $this->error('Error: this records is NOT created it seems! (' . $table . ':' . $uid . ')');
         }
     }
     if (!empty($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));
     }
 }
 /**
  * Create new OnlineMedia item container file
  *
  * @param Folder $targetFolder
  * @param string $fileName
  * @param string $onlineMediaId
  * @return File
  */
 protected function createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
 {
     $tempFilePath = GeneralUtility::tempnam('online_media');
     file_put_contents($tempFilePath, $onlineMediaId);
     return $targetFolder->addFile($tempFilePath, $fileName, 'changeName');
 }
 /**
  * Create new OnlineMedia item container file.
  * This is created inside typo3temp/ and then moved from FAL to the proper storage.
  *
  * @param Folder $targetFolder
  * @param string $fileName
  * @param string $onlineMediaId
  * @return File
  */
 protected function createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
 {
     $temporaryFile = PATH_site . 'typo3temp/assets/transient/' . GeneralUtility::tempnam('online_media');
     GeneralUtility::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
     return $targetFolder->addFile($temporaryFile, $fileName, 'changeName');
 }
 /**
  * Create new OnlineMedia item container file.
  * This is created inside typo3temp/ and then moved from FAL to the proper storage.
  *
  * @param Folder $targetFolder
  * @param string $fileName
  * @param string $onlineMediaId
  * @return File
  */
 protected function createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
 {
     $temporaryFile = GeneralUtility::tempnam('online_media');
     GeneralUtility::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
     $file = $targetFolder->addFile($temporaryFile, $fileName, DuplicationBehavior::RENAME);
     GeneralUtility::unlink_tempfile($temporaryFile);
     return $file;
 }