‪TYPO3CMS  10.4
FileRepository.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
27 
33 {
41  protected ‪$objectType = File::class;
42 
49  protected ‪$table = 'sys_file';
50 
57  protected function ‪createDomainObject(array $databaseRow)
58  {
59  return $this->factory->getFileObject($databaseRow['uid'], $databaseRow);
60  }
61 
71  public function ‪findByRelation($tableName, $fieldName, $uid)
72  {
73  $itemList = [];
75  throw new \InvalidArgumentException(
76  'UID of related record has to be an integer. UID given: "' . $uid . '"',
77  1316789798
78  );
79  }
80  $referenceUids = [];
81  if ($this->‪getEnvironmentMode() === 'FE') {
82  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
83  ->getQueryBuilderForTable('sys_file_reference');
84 
85  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
86  $res = $queryBuilder
87  ->select('uid')
88  ->from('sys_file_reference')
89  ->where(
90  $queryBuilder->expr()->eq(
91  'uid_foreign',
92  $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
93  ),
94  $queryBuilder->expr()->eq(
95  'tablenames',
96  $queryBuilder->createNamedParameter($tableName, \PDO::PARAM_STR)
97  ),
98  $queryBuilder->expr()->eq(
99  'fieldname',
100  $queryBuilder->createNamedParameter($fieldName, \PDO::PARAM_STR)
101  )
102  )
103  ->orderBy('sorting_foreign')
104  ->execute();
105 
106  while ($row = $res->fetch()) {
107  $referenceUids[] = $row['uid'];
108  }
109  } else {
111  $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
112  $relationHandler->start(
113  '',
114  'sys_file_reference',
115  '',
116  $uid,
117  $tableName,
118  ‪BackendUtility::getTcaFieldConfiguration($tableName, $fieldName)
119  );
120  if (!empty($relationHandler->tableArray['sys_file_reference'])) {
121  $referenceUids = $relationHandler->tableArray['sys_file_reference'];
122  }
123  }
124  if (!empty($referenceUids)) {
125  foreach ($referenceUids as $referenceUid) {
126  try {
127  // Just passing the reference uid, the factory is doing workspace
128  // overlays automatically depending on the current environment
129  $itemList[] = $this->factory->getFileReferenceObject($referenceUid);
130  } catch (‪ResourceDoesNotExistException $exception) {
131  // No handling, just omit the invalid reference uid
132  }
133  }
134  $itemList = $this->‪reapplySorting($itemList);
135  }
136 
137  return $itemList;
138  }
139 
147  public function ‪findFileReferenceByUid($uid)
148  {
150  throw new \InvalidArgumentException('The UID of record has to be an integer. UID given: "' . $uid . '"', 1316889798);
151  }
152  try {
153  $fileReferenceObject = $this->factory->getFileReferenceObject($uid);
154  } catch (\InvalidArgumentException $exception) {
155  $fileReferenceObject = false;
156  }
157  return $fileReferenceObject;
158  }
159 
165  protected function ‪reapplySorting(array $itemList): array
166  {
167  uasort(
168  $itemList,
169  function (‪FileReference $a, ‪FileReference $b) {
170  $sortA = (int)$a->‪getReferenceProperty('sorting_foreign');
171  $sortB = (int)$b->‪getReferenceProperty('sorting_foreign');
172 
173  if ($sortA === $sortB) {
174  return 0;
175  }
176 
177  return ($sortA < $sortB) ? -1 : 1;
178  }
179  );
180  return $itemList;
181  }
182 
192  public function ‪searchByName(‪Folder $folder, $fileName)
193  {
194  trigger_error(__METHOD__ . ' is deprecated. Use ResourceStorage::searchFiles instead', \E_USER_DEPRECATED);
195  $searchDemand = ‪FileSearchDemand::createForSearchTerm($fileName)
196  ->addSearchField('sys_file', 'name');
197 
198  return iterator_to_array($folder->‪searchFiles($searchDemand));
199  }
200 
206  protected function ‪getFileIndexRepository()
207  {
209  }
210 }
‪TYPO3\CMS\Backend\Utility\BackendUtility\getTcaFieldConfiguration
‪static array getTcaFieldConfiguration($table, $field)
Definition: BackendUtility.php:4033
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository
Definition: FileIndexRepository.php:45
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\Resource\AbstractRepository
Definition: AbstractRepository.php:31
‪TYPO3\CMS\Core\Database\RelationHandler
Definition: RelationHandler.php:35
‪TYPO3\CMS\Core\Resource\FileRepository\$table
‪string $table
Definition: FileRepository.php:47
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Core\Resource\FileRepository\reapplySorting
‪reapplySorting(array $itemList)
Definition: FileRepository.php:163
‪TYPO3\CMS\Core\Resource\FileRepository\findByRelation
‪array findByRelation($tableName, $fieldName, $uid)
Definition: FileRepository.php:69
‪TYPO3\CMS\Core\Resource\FileRepository\createDomainObject
‪File createDomainObject(array $databaseRow)
Definition: FileRepository.php:55
‪TYPO3\CMS\Core\Resource\FileRepository
Definition: FileRepository.php:33
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand
Definition: FileSearchDemand.php:26
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository\getInstance
‪static FileIndexRepository getInstance()
Definition: FileIndexRepository.php:78
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:24
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Core\Resource\FileRepository\$objectType
‪string $objectType
Definition: FileRepository.php:40
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperty
‪mixed getReferenceProperty($key)
Definition: FileReference.php:134
‪TYPO3\CMS\Core\Resource\FileRepository\findFileReferenceByUid
‪FileReference bool findFileReferenceByUid($uid)
Definition: FileRepository.php:145
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Resource\Folder\searchFiles
‪FileSearchResultInterface searchFiles(FileSearchDemand $searchDemand, int $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS)
Definition: Folder.php:243
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand\createForSearchTerm
‪static createForSearchTerm(string $searchTerm)
Definition: FileSearchDemand.php:70
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:31
‪TYPO3\CMS\Core\Resource\AbstractRepository\getEnvironmentMode
‪string getEnvironmentMode()
Definition: AbstractRepository.php:296
‪TYPO3\CMS\Core\Resource\FileRepository\getFileIndexRepository
‪FileIndexRepository getFileIndexRepository()
Definition: FileRepository.php:204
‪TYPO3\CMS\Core\Resource\FileRepository\searchByName
‪File[] searchByName(Folder $folder, $fileName)
Definition: FileRepository.php:190