[TASK] Cleanup EXT:frontend
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Category / Collection / CategoryCollection.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Category\Collection;
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Extend category collection for the frontend, to collect related records
21 * while respecting language, enable fields, etc.
22 */
23 class CategoryCollection extends \TYPO3\CMS\Core\Category\Collection\CategoryCollection {
24
25 /**
26 * Creates a new collection objects and reconstitutes the
27 * given database record to the new object.
28 *
29 * Overrides the parent method to create a *frontend* category collection.
30 *
31 * @param array $collectionRecord Database record
32 * @param bool $fillItems Populates the entries directly on load, might be bad for memory on large collections
33 * @return \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection
34 */
35 static public function create(array $collectionRecord, $fillItems = FALSE) {
36 /** @var $collection \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection */
37 $collection = GeneralUtility::makeInstance(__CLASS__,
38 $collectionRecord['table_name'],
39 $collectionRecord['field_name']
40 );
41 $collection->fromArray($collectionRecord);
42 if ($fillItems) {
43 $collection->loadContents();
44 }
45 return $collection;
46 }
47
48 /**
49 * Loads the collection with the given id from persistence
50 * For memory reasons, only data for the collection itself is loaded by default.
51 * Entries can be loaded on first access or straightaway using the $fillItems flag.
52 *
53 * Overrides the parent method because of the call to "self::create()" which otherwise calls up
54 * \TYPO3\CMS\Core\Category\Collection\CategoryCollection
55 *
56 * @param int $id Id of database record to be loaded
57 * @param bool $fillItems Populates the entries directly on load, might be bad for memory on large collections
58 * @param string $tableName the table name
59 * @param string $fieldName Name of the categories relation field
60 * @return \TYPO3\CMS\Core\Collection\CollectionInterface
61 */
62 static public function load($id, $fillItems = FALSE, $tableName = '', $fieldName = '') {
63 $collectionRecord = self::getDatabaseConnection()->exec_SELECTgetSingleRow(
64 '*',
65 static::$storageTableName,
66 'uid = ' . (int)$id . self::getTypoScriptFrontendController()->sys_page->enableFields(static::$storageTableName)
67 );
68 $collectionRecord['table_name'] = $tableName;
69 $collectionRecord['field_name'] = $fieldName;
70 return self::create($collectionRecord, $fillItems);
71 }
72
73 /**
74 * Gets the collected records in this collection, by
75 * looking up the MM relations of this record to the
76 * table name defined in the local field 'table_name'.
77 *
78 * Overrides its parent method to implement usage of language,
79 * enable fields, etc. Also performs overlays.
80 *
81 * @return array
82 */
83 protected function getCollectedRecords() {
84 $db = self::getDatabaseConnection();
85
86 $relatedRecords = array();
87 // Assemble where clause
88 $where = 'AND ' . self::$storageTableName . '.uid = ' . (int)$this->getIdentifier();
89 // Add condition on tablenames fields
90 $where .= ' AND sys_category_record_mm.tablenames = ' . $db->fullQuoteStr(
91 $this->getItemTableName(),
92 'sys_category_record_mm'
93 );
94 // Add condition on fieldname field
95 $where .= ' AND sys_category_record_mm.fieldname = ' . $db->fullQuoteStr(
96 $this->getRelationFieldName(),
97 'sys_category_record_mm'
98 );
99 // Add enable fields for item table
100 $tsfe = self::getTypoScriptFrontendController();
101 $where .= $tsfe->sys_page->enableFields($this->getItemTableName());
102 // If language handling is defined for item table, add language condition
103 if (isset($GLOBALS['TCA'][$this->getItemTableName()]['ctrl']['languageField'])) {
104 // Consider default or "all" language
105 $languageField = $this->getItemTableName() . '.' . $GLOBALS['TCA'][$this->getItemTableName()]['ctrl']['languageField'];
106 $languageCondition = $languageField . ' IN (0, -1)';
107 // If not in default language, also consider items in current language with no original
108 if ($tsfe->sys_language_content > 0) {
109 $languageCondition .= '
110 OR (' . $languageField . ' = ' . (int)$tsfe->sys_language_content . '
111 AND ' . $this->getItemTableName() . '.' .
112 $GLOBALS['TCA'][$this->getItemTableName()]['ctrl']['transOrigPointerField'] . ' = 0)
113 ';
114 }
115 $where .= ' AND (' . $languageCondition . ')';
116 }
117 // Get the related records from the database
118 $resource = $db->exec_SELECT_mm_query(
119 $this->getItemTableName() . '.*',
120 self::$storageTableName,
121 'sys_category_record_mm',
122 $this->getItemTableName(),
123 $where
124 );
125
126 if ($resource) {
127 while ($record = $db->sql_fetch_assoc($resource)) {
128 // Overlay the record for workspaces
129 $tsfe->sys_page->versionOL(
130 $this->getItemTableName(),
131 $record
132 );
133 // Overlay the record for translations
134 if (is_array($record) && $tsfe->sys_language_contentOL) {
135 if ($this->getItemTableName() === 'pages') {
136 $record = $tsfe->sys_page->getPageOverlay($record);
137 } else {
138 $record = $tsfe->sys_page->getRecordOverlay(
139 $this->getItemTableName(),
140 $record,
141 $tsfe->sys_language_content,
142 $tsfe->sys_language_contentOL
143 );
144 }
145 }
146 // Record may have been unset during the overlay process
147 if (is_array($record)) {
148 $relatedRecords[] = $record;
149 }
150 }
151 $db->sql_free_result($resource);
152 }
153 return $relatedRecords;
154 }
155
156 /**
157 * Gets the TSFE object.
158 *
159 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
160 */
161 static protected function getTypoScriptFrontendController() {
162 return $GLOBALS['TSFE'];
163 }
164
165 /**
166 * Returns the database connection
167 *
168 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
169 */
170 static protected function getDatabaseConnection() {
171 return $GLOBALS['TYPO3_DB'];
172 }
173
174 }