‪TYPO3CMS  11.5
CategoryCollection.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 
24 
32 {
43  public static function ‪create(array $collectionRecord, $fillItems = false)
44  {
45  $collection = GeneralUtility::makeInstance(
46  __CLASS__,
47  $collectionRecord['table_name'],
48  $collectionRecord['field_name']
49  );
50  $collection->fromArray($collectionRecord);
51  if ($fillItems) {
52  $collection->loadContents();
53  }
54  return $collection;
55  }
56 
71  public static function ‪load($id, $fillItems = false, $tableName = '', $fieldName = '')
72  {
73  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
74  ->getQueryBuilderForTable(static::$storageTableName);
75  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
76 
77  $collectionRecord = $queryBuilder
78  ->select('*')
79  ->from(static::$storageTableName)
80  ->where(
81  $queryBuilder->expr()->eq(
82  'uid',
83  $queryBuilder->createNamedParameter($id, ‪Connection::PARAM_INT)
84  )
85  )
86  ->setMaxResults(1)
87  ->executeQuery()
88  ->fetchAssociative();
89 
90  $collectionRecord['table_name'] = $tableName;
91  $collectionRecord['field_name'] = $fieldName;
92 
93  return ‪self::create($collectionRecord, $fillItems);
94  }
95 
106  protected function ‪getCollectedRecords()
107  {
108  $relatedRecords = [];
109 
110  $queryBuilder = $this->‪getCollectedRecordsQueryBuilder();
111  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
112  $context = GeneralUtility::makeInstance(Context::class);
113  $pageRepository = GeneralUtility::makeInstance(PageRepository::class, $context);
114  $languageId = $context->getPropertyFromAspect('language', 'contentId', 0);
115 
116  // If language handling is defined for item table, add language condition
117  if (isset(‪$GLOBALS['TCA'][$this->‪getItemTableName()]['ctrl']['languageField'])) {
118  // Consider default or "all" language
119  $languageField = sprintf(
120  '%s.%s',
121  $this->‪getItemTableName(),
122  ‪$GLOBALS['TCA'][$this->‪getItemTableName()]['ctrl']['languageField']
123  );
124 
125  $languageConstraint = $queryBuilder->expr()->in(
126  $languageField,
127  $queryBuilder->createNamedParameter([0, -1], Connection::PARAM_INT_ARRAY)
128  );
129 
130  // If not in default language, also consider items in current language with no original
131  if ($languageId > 0) {
132  $transOrigPointerField = sprintf(
133  '%s.%s',
134  $this->‪getItemTableName(),
135  ‪$GLOBALS['TCA'][$this->‪getItemTableName()]['ctrl']['transOrigPointerField']
136  );
137 
138  $languageConstraint = $queryBuilder->expr()->orX(
139  $languageConstraint,
140  $queryBuilder->expr()->andX(
141  $queryBuilder->expr()->eq(
142  $languageField,
143  $queryBuilder->createNamedParameter($languageId, ‪Connection::PARAM_INT)
144  ),
145  $queryBuilder->expr()->eq(
146  $transOrigPointerField,
147  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
148  )
149  )
150  );
151  }
152 
153  $queryBuilder->andWhere($languageConstraint);
154  }
155 
156  // Get the related records from the database
157  $result = $queryBuilder->executeQuery();
158 
159  while ($record = $result->fetchAssociative()) {
160  // Overlay the record for workspaces
161  $pageRepository->versionOL(
162  $this->‪getItemTableName(),
163  $record
164  );
165 
166  // Overlay the record for translations
167  if (is_array($record)) {
168  $record = $pageRepository->getLanguageOverlay($this->‪getItemTableName(), $record);
169  }
170 
171  // Record may have been unset during the overlay process
172  if (is_array($record)) {
173  $relatedRecords[] = $record;
174  }
175  }
176 
177  return $relatedRecords;
178  }
179 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection\create
‪static TYPO3 CMS Frontend Category Collection CategoryCollection create(array $collectionRecord, $fillItems=false)
Definition: CategoryCollection.php:43
‪TYPO3\CMS\Frontend\Category\Collection
Definition: CategoryCollection.php:16
‪TYPO3\CMS\Core\Category\Collection\CategoryCollection
Definition: CategoryCollection.php:32
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection\getCollectedRecords
‪array getCollectedRecords()
Definition: CategoryCollection.php:106
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getItemTableName
‪string getItemTableName()
Definition: AbstractRecordCollection.php:274
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection\load
‪static TYPO3 CMS Core Collection CollectionInterface load($id, $fillItems=false, $tableName='', $fieldName='')
Definition: CategoryCollection.php:71
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:53
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:31
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection
Definition: CategoryCollection.php:32
‪TYPO3\CMS\Core\Category\Collection\CategoryCollection\getCollectedRecordsQueryBuilder
‪QueryBuilder getCollectedRecordsQueryBuilder()
Definition: CategoryCollection.php:130