‪TYPO3CMS  9.5
FileRepository.php
Go to the documentation of this file.
1 <?php
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 
26 
32 {
40  protected ‪$objectType = File::class;
41 
48  protected ‪$table = 'sys_file';
49 
56  protected function ‪createDomainObject(array $databaseRow)
57  {
58  return $this->factory->getFileObject($databaseRow['uid'], $databaseRow);
59  }
60 
70  public function ‪findByRelation($tableName, $fieldName, $uid)
71  {
72  $itemList = [];
74  throw new \InvalidArgumentException(
75  'UID of related record has to be an integer. UID given: "' . $uid . '"',
76  1316789798
77  );
78  }
79  $referenceUids = [];
80  if ($this->‪getEnvironmentMode() === 'FE') {
81  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
82  ->getQueryBuilderForTable('sys_file_reference');
83 
84  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
85  $res = $queryBuilder
86  ->select('uid')
87  ->from('sys_file_reference')
88  ->where(
89  $queryBuilder->expr()->eq(
90  'uid_foreign',
91  $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
92  ),
93  $queryBuilder->expr()->eq(
94  'tablenames',
95  $queryBuilder->createNamedParameter($tableName, \PDO::PARAM_STR)
96  ),
97  $queryBuilder->expr()->eq(
98  'fieldname',
99  $queryBuilder->createNamedParameter($fieldName, \PDO::PARAM_STR)
100  )
101  )
102  ->orderBy('sorting_foreign')
103  ->execute();
104 
105  while ($row = $res->fetch()) {
106  $referenceUids[] = $row['uid'];
107  }
108  } else {
110  $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
111  $relationHandler->start(
112  '',
113  'sys_file_reference',
114  '',
115  $uid,
116  $tableName,
117  ‪BackendUtility::getTcaFieldConfiguration($tableName, $fieldName)
118  );
119  if (!empty($relationHandler->tableArray['sys_file_reference'])) {
120  $referenceUids = $relationHandler->tableArray['sys_file_reference'];
121  }
122  }
123  if (!empty($referenceUids)) {
124  foreach ($referenceUids as $referenceUid) {
125  try {
126  // Just passing the reference uid, the factory is doing workspace
127  // overlays automatically depending on the current environment
128  $itemList[] = $this->factory->getFileReferenceObject($referenceUid);
129  } catch (‪ResourceDoesNotExistException $exception) {
130  // No handling, just omit the invalid reference uid
131  }
132  }
133  $itemList = $this->‪reapplySorting($itemList);
134  }
135 
136  return $itemList;
137  }
138 
146  public function ‪findFileReferenceByUid($uid)
147  {
149  throw new \InvalidArgumentException('The UID of record has to be an integer. UID given: "' . $uid . '"', 1316889798);
150  }
151  try {
152  $fileReferenceObject = $this->factory->getFileReferenceObject($uid);
153  } catch (\InvalidArgumentException $exception) {
154  $fileReferenceObject = false;
155  }
156  return $fileReferenceObject;
157  }
158 
164  protected function ‪reapplySorting(array $itemList): array
165  {
166  uasort(
167  $itemList,
168  function (‪FileReference $a, ‪FileReference $b) {
169  $sortA = (int)$a->‪getReferenceProperty('sorting_foreign');
170  $sortB = (int)$b->‪getReferenceProperty('sorting_foreign');
171 
172  if ($sortA === $sortB) {
173  return 0;
174  }
175 
176  return ($sortA < $sortB) ? -1 : 1;
177  }
178  );
179  return $itemList;
180  }
181 
191  public function ‪searchByName(‪Folder $folder, $fileName)
192  {
193  trigger_error(__METHOD__ . ' is deprecated. Use ResourceStorage::searchFiles instead', \E_USER_DEPRECATED);
194  $searchDemand = ‪FileSearchDemand::createForSearchTerm($fileName)
195  ->addSearchField('sys_file', 'name');
196 
197  return iterator_to_array($folder->‪searchFiles($searchDemand));
198  }
199 
205  protected function ‪getFileIndexRepository()
206  {
208  }
209 }
‪TYPO3\CMS\Backend\Utility\BackendUtility\getTcaFieldConfiguration
‪static array getTcaFieldConfiguration($table, $field)
Definition: BackendUtility.php:4505
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository
Definition: FileIndexRepository.php:41
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Core\Resource\AbstractRepository
Definition: AbstractRepository.php:27
‪TYPO3\CMS\Core\Database\RelationHandler
Definition: RelationHandler.php:32
‪TYPO3\CMS\Core\Resource\FileRepository\$table
‪string $table
Definition: FileRepository.php:46
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:31
‪TYPO3\CMS\Core\Resource\FileRepository\reapplySorting
‪reapplySorting(array $itemList)
Definition: FileRepository.php:162
‪TYPO3\CMS\Core\Resource\FileRepository\findByRelation
‪array findByRelation($tableName, $fieldName, $uid)
Definition: FileRepository.php:68
‪TYPO3\CMS\Core\Resource\FileRepository\createDomainObject
‪File createDomainObject(array $databaseRow)
Definition: FileRepository.php:54
‪TYPO3\CMS\Core\Resource\FileRepository
Definition: FileRepository.php:32
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand
Definition: FileSearchDemand.php:24
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository\getInstance
‪static FileIndexRepository getInstance()
Definition: FileIndexRepository.php:70
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:21
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Core\Resource\FileRepository\$objectType
‪string $objectType
Definition: FileRepository.php:39
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:37
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperty
‪mixed getReferenceProperty($key)
Definition: FileReference.php:133
‪TYPO3\CMS\Core\Resource\FileRepository\findFileReferenceByUid
‪FileReference bool findFileReferenceByUid($uid)
Definition: FileRepository.php:144
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Resource\Folder\searchFiles
‪FileSearchResultInterface searchFiles(FileSearchDemand $searchDemand, int $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS)
Definition: Folder.php:240
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand\createForSearchTerm
‪static createForSearchTerm(string $searchTerm)
Definition: FileSearchDemand.php:68
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:29
‪TYPO3\CMS\Core\Resource\AbstractRepository\getEnvironmentMode
‪string getEnvironmentMode()
Definition: AbstractRepository.php:289
‪TYPO3\CMS\Core\Resource\FileRepository\getFileIndexRepository
‪FileIndexRepository getFileIndexRepository()
Definition: FileRepository.php:203
‪TYPO3\CMS\Core\Resource\FileRepository\searchByName
‪File[] searchByName(Folder $folder, $fileName)
Definition: FileRepository.php:189