TYPO3 CMS  TYPO3_7-6
CategoryCollection.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 
22 
27 {
33  protected static $storageTableName = 'sys_category';
34 
40  protected $relationFieldName = 'categories';
41 
49  public function __construct($tableName = null, $fieldName = null)
50  {
51  parent::__construct();
52  if (!empty($tableName)) {
53  $this->setItemTableName($tableName);
54  } elseif (empty($this->itemTableName)) {
55  throw new \RuntimeException(self::class . ' needs a valid itemTableName.', 1341826168);
56  }
57  if (!empty($fieldName)) {
58  $this->setRelationFieldName($fieldName);
59  }
60  }
61 
70  public static function create(array $collectionRecord, $fillItems = false)
71  {
73  $collection = GeneralUtility::makeInstance(
74  self::class,
75  $collectionRecord['table_name'],
76  $collectionRecord['field_name']
77  );
78  $collection->fromArray($collectionRecord);
79  if ($fillItems) {
80  $collection->loadContents();
81  }
82  return $collection;
83  }
84 
97  public static function load($id, $fillItems = false, $tableName = '', $fieldName = '')
98  {
99  $collectionRecord = self::getDatabaseConnection()->exec_SELECTgetSingleRow(
100  '*',
101  static::$storageTableName,
102  'uid = ' . (int)$id . BackendUtility::deleteClause(static::$storageTableName)
103  );
104  $collectionRecord['table_name'] = $tableName;
105  $collectionRecord['field_name'] = $fieldName;
106  return self::create($collectionRecord, $fillItems);
107  }
108 
116  protected function getCollectedRecords()
117  {
118  $db = self::getDatabaseConnection();
119 
120  $relatedRecords = [];
121  // Assemble where clause
122  $where = 'AND ' . self::$storageTableName . '.uid = ' . (int)$this->getIdentifier();
123  // Add condition on tablenames fields
124  $where .= ' AND sys_category_record_mm.tablenames = ' . $db->fullQuoteStr(
125  $this->getItemTableName(),
126  'sys_category_record_mm'
127  );
128  // Add condition on fieldname field
129  $where .= ' AND sys_category_record_mm.fieldname = ' . $db->fullQuoteStr(
130  $this->getRelationFieldName(),
131  'sys_category_record_mm'
132  );
133  $resource = $db->exec_SELECT_mm_query(
134  $this->getItemTableName() . '.*',
135  self::$storageTableName,
136  'sys_category_record_mm',
137  $this->getItemTableName(),
138  $where
139  );
140  if ($resource) {
141  while ($record = $db->sql_fetch_assoc($resource)) {
142  $relatedRecords[] = $record;
143  }
144  $db->sql_free_result($resource);
145  }
146  return $relatedRecords;
147  }
148 
159  public function loadContents()
160  {
161  $entries = $this->getCollectedRecords();
162  $this->removeAll();
163  foreach ($entries as $entry) {
164  $this->add($entry);
165  }
166  }
167 
175  protected function getPersistableDataArray()
176  {
177  return [
178  'title' => $this->getTitle(),
179  'description' => $this->getDescription(),
180  'items' => $this->getItemUidList(true)
181  ];
182  }
183 
190  public function add($data)
191  {
192  $this->storage->push($data);
193  }
194 
201  public function addAll(CollectionInterface $other)
202  {
203  foreach ($other as $value) {
204  $this->add($value);
205  }
206  }
207 
215  public function remove($data)
216  {
217  $offset = 0;
218  foreach ($this->storage as $value) {
219  if ($value == $data) {
220  break;
221  }
222  $offset++;
223  }
224  $this->storage->offsetUnset($offset);
225  }
226 
233  public function removeAll()
234  {
235  $this->storage = new \SplDoublyLinkedList();
236  }
237 
243  public function getItems()
244  {
245  $itemArray = [];
247  foreach ($this->storage as $item) {
248  $itemArray[] = $item;
249  }
250  return $itemArray;
251  }
252 
258  public function setRelationFieldName($field)
259  {
260  $this->relationFieldName = $field;
261  }
262 
268  public function getRelationFieldName()
269  {
271  }
272 
278  public static function getStorageTableName()
279  {
280  return self::$storageTableName;
281  }
282 
288  public static function getStorageItemsField()
289  {
290  return self::$storageItemsField;
291  }
292 
298  protected static function getDatabaseConnection()
299  {
300  return $GLOBALS['TYPO3_DB'];
301  }
302 }
static load($id, $fillItems=false, $tableName='', $fieldName='')
static create(array $collectionRecord, $fillItems=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static deleteClause($table, $tableAlias='')