[!!!][TASK] Remove deprecated code from sysext core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / FileRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
18 use TYPO3\CMS\Core\Resource\Index\FileIndexRepository;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Repository for accessing files
23 * it also serves as the public API for the indexing part of files in general
24 *
25 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
26 * @author Ingmar Schlecht <ingmar@typo3.org>
27 */
28 class FileRepository extends AbstractRepository {
29
30 /**
31 * The main object type of this class. In some cases (fileReference) this
32 * repository can also return FileReference objects, implementing the
33 * common FileInterface.
34 *
35 * @var string
36 */
37 protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\File';
38
39 /**
40 * Main File object storage table. Note that this repository also works on
41 * the sys_file_reference table when returning FileReference objects.
42 *
43 * @var string
44 */
45 protected $table = 'sys_file';
46
47 /**
48 * Creates an object managed by this repository.
49 *
50 * @param array $databaseRow
51 * @return File
52 */
53 protected function createDomainObject(array $databaseRow) {
54 return $this->factory->getFileObject($databaseRow['uid'], $databaseRow);
55 }
56
57 /**
58 * Find FileReference objects by relation to other records
59 *
60 * @param int $tableName Table name of the related record
61 * @param int $fieldName Field name of the related record
62 * @param int $uid The UID of the related record (needs to be the localized uid, as translated IRRE elements relate to them)
63 * @return array An array of objects, empty if no objects found
64 * @throws \InvalidArgumentException
65 * @api
66 */
67 public function findByRelation($tableName, $fieldName, $uid) {
68 $itemList = array();
69 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
70 throw new \InvalidArgumentException('Uid of related record has to be an integer.', 1316789798);
71 }
72 $referenceUids = NULL;
73 if ($this->getEnvironmentMode() === 'FE' && !empty($GLOBALS['TSFE']->sys_page)) {
74 /** @var $frontendController \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController */
75 $frontendController = $GLOBALS['TSFE'];
76 $references = $this->getDatabaseConnection()->exec_SELECTgetRows(
77 'uid',
78 'sys_file_reference',
79 'tablenames=' . $this->getDatabaseConnection()->fullQuoteStr($tableName, 'sys_file_reference') .
80 ' AND uid_foreign=' . (int)$uid .
81 ' AND fieldname=' . $this->getDatabaseConnection()->fullQuoteStr($fieldName, 'sys_file_reference')
82 . $frontendController->sys_page->enableFields('sys_file_reference', $frontendController->showHiddenRecords),
83 '',
84 'sorting_foreign',
85 '',
86 'uid'
87 );
88 if (!empty($references)) {
89 $referenceUids = array_keys($references);
90 }
91 } else {
92 /** @var $relationHandler \TYPO3\CMS\Core\Database\RelationHandler */
93 $relationHandler = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
94 $relationHandler->start(
95 '', 'sys_file_reference', '', $uid, $tableName,
96 \TYPO3\CMS\Backend\Utility\BackendUtility::getTcaFieldConfiguration($tableName, $fieldName)
97 );
98 if (!empty($relationHandler->tableArray['sys_file_reference'])) {
99 $referenceUids = $relationHandler->tableArray['sys_file_reference'];
100 }
101 }
102 if (!empty($referenceUids)) {
103 foreach ($referenceUids as $referenceUid) {
104 try {
105 // Just passing the reference uid, the factory is doing workspace
106 // overlays automatically depending on the current environment
107 $itemList[] = $this->factory->getFileReferenceObject($referenceUid);
108 } catch (ResourceDoesNotExistException $exception) {
109 // No handling, just omit the invalid reference uid
110 }
111 }
112 }
113 return $itemList;
114 }
115
116 /**
117 * Find FileReference objects by uid
118 *
119 * @param int $uid The UID of the sys_file_reference record
120 * @return FileReference|boolean
121 * @throws \InvalidArgumentException
122 * @api
123 */
124 public function findFileReferenceByUid($uid) {
125 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
126 throw new \InvalidArgumentException('uid of record has to be an integer.', 1316889798);
127 }
128 try {
129 $fileReferenceObject = $this->factory->getFileReferenceObject($uid);
130 } catch (\InvalidArgumentException $exception) {
131 $fileReferenceObject = FALSE;
132 }
133 return $fileReferenceObject;
134 }
135
136 /**
137 * Return a file index repository
138 *
139 * @return FileIndexRepository
140 */
141 protected function getFileIndexRepository() {
142 return FileIndexRepository::getInstance();
143 }
144
145 }