TYPO3 CMS  TYPO3_8-7
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 
25 
31 {
39  protected $objectType = File::class;
40 
47  protected $table = 'sys_file';
48 
55  protected function createDomainObject(array $databaseRow)
56  {
57  return $this->factory->getFileObject($databaseRow['uid'], $databaseRow);
58  }
59 
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' && !empty($GLOBALS['TSFE']->sys_page)) {
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 
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 
190  public function searchByName(Folder $folder, $fileName)
191  {
193  $fileFactory = GeneralUtility::makeInstance(ResourceFactory::class);
194 
195  $storage = $folder->getStorage();
196  $folders = $storage->getFoldersInFolder($folder, 0, 0, true, true);
197  $folders[$folder->getIdentifier()] = $folder;
198 
199  $fileRecords = $this->getFileIndexRepository()->findByFolders($folders, false, $fileName);
200 
201  $files = [];
202  foreach ($fileRecords as $fileRecord) {
203  try {
204  $file = $fileFactory->getFileObject($fileRecord['uid'], $fileRecord);
205  if ($storage->checkFileAndFolderNameFilters($file)) {
206  $files[] = $file;
207  }
208  } catch (Exception\FileDoesNotExistException $ignoredException) {
209  continue;
210  }
211  }
212 
213  return $files;
214  }
215 
221  protected function getFileIndexRepository()
222  {
224  }
225 }
static makeInstance($className,... $constructorArguments)
static getTcaFieldConfiguration($table, $field)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']