[BUGFIX] Fields of type file_reference are not properly indexed 48/24448/2
authorMartin Borer <mbarts@bluewin.ch>
Mon, 12 Aug 2013 10:44:46 +0000 (12:44 +0200)
committerMarkus Klein <klein.t3@mfc-linz.at>
Mon, 7 Oct 2013 10:43:33 +0000 (12:43 +0200)
sys_refindex entries for TCA-fields of type group->file_reference
are not correct. The fields ref_table and ref_uid are not filled.
This results in a wrong number of references in the Ref column of
the Filelist module and prevents a warning that a resource is still
in use when deleting it.

Resolves: #49538
Releases: 6.2, 6.1, 6.0
Change-Id: I1c44fc98b7ceefd6247ec372e5b28f8682a47bf1
Reviewed-on: https://review.typo3.org/24448
Reviewed-by: Markus Klein
Tested-by: Markus Klein
typo3/sysext/core/Classes/Database/ReferenceIndex.php

index 7eab4ba..afd22db 100644 (file)
@@ -465,41 +465,55 @@ class ReferenceIndex {
         * @param string $value Field value
         * @param array $conf Field configuration array of type "TCA/columns
         * @param integer $uid Field uid
-        * @retur array If field type is OK it will return an array with the files inside. Else FALSE
+        * @return array If field type is OK it will return an array with the files inside. Else FALSE
         * @todo Define visibility
         */
        public function getRelations_procFiles($value, $conf, $uid) {
-               if ($conf['type'] == 'group' && ($conf['internal_type'] == 'file' || $conf['internal_type'] == 'file_reference')) {
-                       // Collect file values in array:
-                       if ($conf['MM']) {
-                               $theFileValues = array();
-                               $dbAnalysis = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
-                               $dbAnalysis->start('', 'files', $conf['MM'], $uid);
-                               foreach ($dbAnalysis->itemArray as $somekey => $someval) {
-                                       if ($someval['id']) {
-                                               $theFileValues[] = $someval['id'];
-                                       }
+               if ($conf['type'] !== 'group' || ($conf['internal_type'] !== 'file' && $conf['internal_type'] !== 'file_reference')) {
+                       return FALSE;
+               }
+
+               // Collect file values in array:
+               if ($conf['MM']) {
+                       $theFileValues = array();
+                       $dbAnalysis = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
+                       $dbAnalysis->start('', 'files', $conf['MM'], $uid);
+                       foreach ($dbAnalysis->itemArray as $someval) {
+                               if ($someval['id']) {
+                                       $theFileValues[] = $someval['id'];
                                }
-                       } else {
-                               $theFileValues = explode(',', $value);
                        }
-                       // Traverse the files and add them:
-                       $uploadFolder = $conf['internal_type'] == 'file' ? $conf['uploadfolder'] : '';
-                       $dest = $this->destPathFromUploadFolder($uploadFolder);
-                       $newValueFiles = array();
-                       foreach ($theFileValues as $file) {
-                               if (trim($file)) {
-                                       $realFile = $dest . '/' . trim($file);
-                                       $newValueFiles[] = array(
-                                               'filename' => basename($file),
-                                               'ID' => md5($realFile),
-                                               'ID_absFile' => $realFile
-                                       );
+               } else {
+                       $theFileValues = explode(',', $value);
+               }
+               // Traverse the files and add them:
+               $uploadFolder = $conf['internal_type'] == 'file' ? $conf['uploadfolder'] : '';
+               $dest = $this->destPathFromUploadFolder($uploadFolder);
+               $newValueFiles = array();
+               foreach ($theFileValues as $file) {
+                       if (trim($file)) {
+                               $realFile = $dest . '/' . trim($file);
+                               $newValueFile = array(
+                                       'filename' => basename($file),
+                                       'ID' => md5($realFile),
+                                       'ID_absFile' => $realFile
+                               );
+                               // Set sys_file and id for referenced files
+                               if ($conf['internal_type'] === 'file_reference') {
+                                       try {
+                                               $file = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($file);
+                                               if ($file instanceof \TYPO3\CMS\Core\Resource\FileInterface) {
+                                                       $newValueFile['table'] = 'sys_file';
+                                                       $newValueFile['id'] = $file->getUid();
+                                               }
+                                       } catch (\Exception $e) {
+
+                                       }
                                }
+                               $newValueFiles[] = $newValueFile;
                        }
-                       return $newValueFiles;
                }
-               return FALSE;
+               return $newValueFiles;
        }
 
        /**
@@ -901,4 +915,4 @@ class ReferenceIndex {
 }
 
 
-?>
\ No newline at end of file
+?>