‪TYPO3CMS  ‪main
FileRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
29 
38 {
39  public function ‪__construct(
40  protected readonly ‪ResourceFactory $factory
41  ) {}
42 
46  public function ‪findByUid(int ‪$uid): ‪File
47  {
48  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file');
49  if ($this->‪isFrontend()) {
50  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
51  }
52  $row = $queryBuilder
53  ->select('*')
54  ->from('sys_file')
55  ->where(
56  $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter(‪$uid, ‪Connection::PARAM_INT))
57  )
58  ->executeQuery()
59  ->fetchAssociative();
60  if (!is_array($row)) {
61  throw new \RuntimeException('Could not find row with UID "' . ‪$uid . '" in table "sys_file"', 1314354065);
62  }
63  return $this->‪createDomainObject($row);
64  }
65 
69  protected function ‪createDomainObject(array $databaseRow): ‪File
70  {
71  return $this->factory->getFileObject((int)$databaseRow['uid'], $databaseRow);
72  }
73 
83  public function ‪findByRelation(string $tableName, string $fieldName, int ‪$uid, int $workspaceId = null): array
84  {
85  $itemList = [];
86  $referenceUids = [];
87  if ($this->‪isFrontend()) {
88  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
89  ->getQueryBuilderForTable('sys_file_reference');
90 
91  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
92  $res = $queryBuilder
93  ->select('uid')
94  ->from('sys_file_reference')
95  ->where(
96  $queryBuilder->expr()->eq(
97  'uid_foreign',
98  $queryBuilder->createNamedParameter(‪$uid, ‪Connection::PARAM_INT)
99  ),
100  $queryBuilder->expr()->eq(
101  'tablenames',
102  $queryBuilder->createNamedParameter($tableName)
103  ),
104  $queryBuilder->expr()->eq(
105  'fieldname',
106  $queryBuilder->createNamedParameter($fieldName)
107  )
108  )
109  ->orderBy('sorting_foreign')
110  ->executeQuery();
111 
112  while ($row = $res->fetchAssociative()) {
113  $referenceUids[] = $row['uid'];
114  }
115  } else {
116  $workspaceId ??= GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('workspace', 'id', 0);
117  $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
118  $relationHandler->setWorkspaceId($workspaceId);
119  $relationHandler->start(
120  '',
121  'sys_file_reference',
122  '',
123  ‪$uid,
124  $tableName,
125  BackendUtility::getTcaFieldConfiguration($tableName, $fieldName)
126  );
127  if (!empty($relationHandler->tableArray['sys_file_reference'])) {
128  $relationHandler->processDeletePlaceholder();
129  $referenceUids = $relationHandler->tableArray['sys_file_reference'];
130  }
131  }
132  if (!empty($referenceUids)) {
133  foreach ($referenceUids as $referenceUid) {
134  try {
135  // Just passing the reference uid, the factory is doing workspace
136  // overlays automatically depending on the current environment
137  $itemList[] = $this->factory->getFileReferenceObject($referenceUid);
139  // No handling, just omit the invalid reference uid
140  }
141  }
142  $itemList = $this->‪reapplySorting($itemList);
143  }
144 
145  return $itemList;
146  }
147 
154  protected function ‪reapplySorting(array $itemList): array
155  {
156  uasort(
157  $itemList,
158  static function (‪FileReference $a, ‪FileReference $b) {
159  $sortA = (int)$a->‪getReferenceProperty('sorting_foreign');
160  $sortB = (int)$b->‪getReferenceProperty('sorting_foreign');
161 
162  if ($sortA === $sortB) {
163  return 0;
164  }
165 
166  return ($sortA < $sortB) ? -1 : 1;
167  }
168  );
169  return $itemList;
170  }
171 
176  protected function ‪isFrontend(): bool
177  {
178  return (‪$GLOBALS['TSFE'] ?? null) instanceof ‪TypoScriptFrontendController;
179  }
180 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Core\Database\RelationHandler
Definition: RelationHandler.php:36
‪TYPO3\CMS\Core\Resource\FileRepository\findByRelation
‪FileReference[] findByRelation(string $tableName, string $fieldName, int $uid, int $workspaceId=null)
Definition: FileRepository.php:83
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Core\Resource\FileRepository\createDomainObject
‪createDomainObject(array $databaseRow)
Definition: FileRepository.php:69
‪TYPO3\CMS\Core\Resource\FileRepository
Definition: FileRepository.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:23
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperty
‪mixed getReferenceProperty($key)
Definition: FileReference.php:124
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:58
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Resource\FileRepository\findByUid
‪findByUid(int $uid)
Definition: FileRepository.php:46
‪TYPO3\CMS\Core\Resource\FileRepository\reapplySorting
‪FileReference[] reapplySorting(array $itemList)
Definition: FileRepository.php:154
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:30
‪TYPO3\CMS\Core\Resource\FileRepository\isFrontend
‪isFrontend()
Definition: FileRepository.php:176
‪TYPO3\CMS\Core\Resource\FileRepository\__construct
‪__construct(protected readonly ResourceFactory $factory)
Definition: FileRepository.php:39