[TASK] Refactor file references relations retrieval 87/31287/3
authorOliver Hader <oliver@typo3.org>
Wed, 2 Jul 2014 10:20:27 +0000 (12:20 +0200)
committerOliver Hader <oliver.hader@typo3.org>
Thu, 14 Aug 2014 09:38:22 +0000 (11:38 +0200)
The retrieval of file references in BackendUtility
is currently bound to the thumbCode() method but
is useful for other components as well.

Resolves: #60054
Releases: master, 6.2
Change-Id: Id92bdeaed24d8924285a640f905ffd55db21da4d
Reviewed-on: http://review.typo3.org/31287
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
typo3/sysext/backend/Classes/Utility/BackendUtility.php
typo3/sysext/core/Classes/Resource/ResourceFactory.php
typo3/sysext/frontend/Tests/Unit/ContentObject/FilesContentObjectTest.php

index d5f509d..76e55bd 100644 (file)
@@ -1469,6 +1469,43 @@ class BackendUtility {
        }
 
        /**
+        * Resolves file references for a given record.
+        *
+        * @param string $tableName Name of the table of the record
+        * @param string $fieldName Name of the field of the record
+        * @param array $element Record data
+        * @param NULL|int $workspaceId Workspace to fetch data for
+        * @return NULL|array|\TYPO3\CMS\Core\Resource\FileReference[]
+        */
+       static public function resolveFileReferences($tableName, $fieldName, $element, $workspaceId = NULL) {
+               if (empty($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'])) {
+                       return NULL;
+               }
+               $configuration = $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'];
+               if (empty($configuration['type']) || $configuration['type'] !== 'inline'
+                       || empty($configuration['foreign_table']) || $configuration['foreign_table'] !== 'sys_file_reference') {
+                       return NULL;
+               }
+
+               $fileReferences = array();
+               /** @var $relationHandler \TYPO3\CMS\Core\Database\RelationHandler */
+               $relationHandler = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
+               if ($workspaceId !== NULL) {
+                       $relationHandler->setWorkspaceId($workspaceId);
+               }
+               $relationHandler->start($element[$fieldName], $configuration['foreign_table'], $configuration['MM'], $element['uid'], $tableName, $configuration);
+               $relationHandler->processDeletePlaceholder();
+               $referenceUids = $relationHandler->tableArray[$configuration['foreign_table']];
+
+               foreach ($referenceUids as $referenceUid) {
+                       $fileReference = ResourceFactory::getInstance()->getFileReferenceObject($referenceUid, array(), ($workspaceId === 0));
+                       $fileReferences[$fileReference->getUid()] = $fileReference;
+               }
+
+               return $fileReferences;
+       }
+
+       /**
         * Returns a linked image-tag for thumbnail(s)/fileicons/truetype-font-previews from a database row with a list of image files in a field
         * All $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] extension are made to thumbnails + ttf file (renders font-example)
         * Thumbsnails are linked to the show_item.php script which will display further details.
@@ -1486,7 +1523,6 @@ class BackendUtility {
         * @return string Thumbnail image tag.
         */
        static public function thumbCode($row, $table, $field, $backPath, $thumbScript = '', $uploaddir = NULL, $abs = 0, $tparams = '', $size = '', $linkInfoPopup = TRUE) {
-               $tcaConfig = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
                // Check and parse the size parameter
                $sizeParts = array(64, 64);
                if ($size = trim($size)) {
@@ -1496,16 +1532,10 @@ class BackendUtility {
                        }
                }
                $thumbData = '';
+               $fileReferences = static::resolveFileReferences($table, $field, $row);
                // FAL references
-               if ($tcaConfig['type'] === 'inline' && $tcaConfig['foreign_table'] === 'sys_file_reference') {
-                       /** @var $relationHandler \TYPO3\CMS\Core\Database\RelationHandler */
-                       $relationHandler = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
-                       $relationHandler->start($row[$field], $tcaConfig['foreign_table'], $tcaConfig['MM'], $row['uid'], $table, $tcaConfig);
-                       $relationHandler->processDeletePlaceholder();
-                       $referenceUids = $relationHandler->tableArray[$tcaConfig['foreign_table']];
-
-                       foreach ($referenceUids as $referenceUid) {
-                               $fileReferenceObject = ResourceFactory::getInstance()->getFileReferenceObject($referenceUid);
+               if ($fileReferences !== NULL) {
+                       foreach ($fileReferences as $fileReferenceObject) {
                                $fileObject = $fileReferenceObject->getOriginalFile();
 
                                if ($fileObject->isMissing()) {
index 3ad5fca..db93134 100644 (file)
@@ -560,26 +560,19 @@ class ResourceFactory implements ResourceFactoryInterface, \TYPO3\CMS\Core\Singl
         *
         * @param integer $uid The uid of the file usage (sys_file_reference) to instantiate.
         * @param array $fileReferenceData The record row from database.
+        * @param bool $raw Whether to get raw results without performing overlays
         *
         * @throws \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
         * @return FileReference
         */
-       public function getFileReferenceObject($uid, array $fileReferenceData = array()) {
+       public function getFileReferenceObject($uid, array $fileReferenceData = array(), $raw = FALSE) {
                if (!is_numeric($uid)) {
                        throw new \InvalidArgumentException('uid of file usage (sys_file_reference) has to be numeric.', 1300086584);
                }
                if (!$this->fileReferenceInstances[$uid]) {
                        // Fetches data in case $fileData is empty
                        if (empty($fileReferenceData)) {
-                               // fetch the reference record of the current workspace
-                               if (TYPO3_MODE === 'BE') {
-                                       $fileReferenceData = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordWSOL('sys_file_reference', $uid);
-                               } elseif (is_object($GLOBALS['TSFE'])) {
-                                       $fileReferenceData = $GLOBALS['TSFE']->sys_page->checkRecord('sys_file_reference', $uid);
-                               } else {
-                                       /** @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
-                                       $fileReferenceData = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', 'sys_file_reference', 'uid=' . (int)$uid . ' AND deleted=0');
-                               }
+                               $fileReferenceData = $this->getFileReferenceData($uid, $raw);
                                if (!is_array($fileReferenceData)) {
                                        throw new \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException('No file usage (sys_file_reference) found for given UID.', 1317178794);
                                }
@@ -604,6 +597,25 @@ class ResourceFactory implements ResourceFactoryInterface, \TYPO3\CMS\Core\Singl
        }
 
        /**
+        * Gets data for the given uid of the file reference record.
+        *
+        * @param int $uid The uid of the file usage (sys_file_reference) to be fetched
+        * @param bool $raw Whether to get raw results without performing overlays
+        * @return NULL|array
+        */
+       protected function getFileReferenceData($uid, $raw = FALSE) {
+               if (!$raw && TYPO3_MODE === 'BE') {
+                       $fileReferenceData = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordWSOL('sys_file_reference', $uid);
+               } elseif (!$raw && is_object($GLOBALS['TSFE'])) {
+                       $fileReferenceData = $GLOBALS['TSFE']->sys_page->checkRecord('sys_file_reference', $uid);
+               } else {
+                       /** @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
+                       $fileReferenceData = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', 'sys_file_reference', 'uid=' . (int)$uid . ' AND deleted=0');
+               }
+               return $fileReferenceData;
+       }
+
+       /**
         * Returns an instance of the FileIndexRepository
         *
         * @return FileIndexRepository
@@ -630,4 +642,4 @@ class ResourceFactory implements ResourceFactoryInterface, \TYPO3\CMS\Core\Singl
                return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\Indexer', $storage);
        }
 
-}
\ No newline at end of file
+}
index 7f95b68..36682e3 100644 (file)
@@ -207,7 +207,7 @@ class FilesContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                                ->with('name')
                                ->will($this->returnValue('File ' . $i));
 
-                       $fileReferenceMap[] = array($i, array(), $fileReference);
+                       $fileReferenceMap[] = array($i, array(), FALSE, $fileReference);
                }
 
                $resourceFactory = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');