TYPO3 CMS  TYPO3_6-2
CategoryCollection.php
Go to the documentation of this file.
1 <?php
3 
22 
28  static protected $storageTableName = 'sys_category';
29 
35  protected $relationFieldName = 'categories';
36 
44  public function __construct($tableName = NULL, $fieldName = NULL) {
45  parent::__construct();
46  if (!empty($tableName)) {
47  $this->setItemTableName($tableName);
48  } elseif (empty($this->itemTableName)) {
49  throw new \RuntimeException('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection needs a valid itemTableName.', 1341826168);
50  }
51  if (!empty($fieldName)) {
52  $this->setRelationFieldName($fieldName);
53  }
54  }
55 
64  static public function create(array $collectionRecord, $fillItems = FALSE) {
67  'TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection',
68  $collectionRecord['table_name'],
69  $collectionRecord['field_name']
70  );
71  $collection->fromArray($collectionRecord);
72  if ($fillItems) {
73  $collection->loadContents();
74  }
75  return $collection;
76  }
77 
90  static public function load($id, $fillItems = FALSE, $tableName = '', $fieldName = '') {
91  $collectionRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
92  '*',
93  static::$storageTableName,
94  'uid = ' . (int)$id . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause(static::$storageTableName)
95  );
96  $collectionRecord['table_name'] = $tableName;
97  $collectionRecord['field_name'] = $fieldName;
98  return self::create($collectionRecord, $fillItems);
99  }
100 
108  protected function getCollectedRecords() {
109  $relatedRecords = array();
110  // Assemble where clause
111  $where = 'AND ' . self::$storageTableName . '.uid = ' . (int)$this->getIdentifier();
112  // Add condition on tablenames fields
113  $where .= ' AND sys_category_record_mm.tablenames = ' . $this->getDatabaseConnection()->fullQuoteStr(
114  $this->getItemTableName(),
115  'sys_category_record_mm'
116  );
117  // Add condition on fieldname field
118  $where .= ' AND sys_category_record_mm.fieldname = ' . $this->getDatabaseConnection()->fullQuoteStr(
119  $this->getRelationFieldName(),
120  'sys_category_record_mm'
121  );
122  $resource = $this->getDatabaseConnection()->exec_SELECT_mm_query(
123  $this->getItemTableName() . '.*',
124  self::$storageTableName,
125  'sys_category_record_mm',
126  $this->getItemTableName(),
127  $where
128  );
129  if ($resource) {
130  while ($record = $this->getDatabaseConnection()->sql_fetch_assoc($resource)) {
131  $relatedRecords[] = $record;
132  }
133  $this->getDatabaseConnection()->sql_free_result($resource);
134  }
135  return $relatedRecords;
136  }
137 
148  public function loadContents() {
149  $entries = $this->getCollectedRecords();
150  $this->removeAll();
151  foreach ($entries as $entry) {
152  $this->add($entry);
153  }
154  }
155 
163  protected function getPersistableDataArray() {
164  return array(
165  'title' => $this->getTitle(),
166  'description' => $this->getDescription(),
167  'items' => $this->getItemUidList(TRUE)
168  );
169  }
170 
177  public function add($data) {
178  $this->storage->push($data);
179  }
180 
187  public function addAll(\TYPO3\CMS\Core\Collection\CollectionInterface $other) {
188  foreach ($other as $value) {
189  $this->add($value);
190  }
191  }
192 
200  public function remove($data) {
201  $offset = 0;
202  foreach ($this->storage as $value) {
203  if ($value == $data) {
204  break;
205  }
206  $offset++;
207  }
208  $this->storage->offsetUnset($offset);
209  }
210 
217  public function removeAll() {
218  $this->storage = new \SplDoublyLinkedList();
219  }
220 
226  public function getItems() {
227  $itemArray = array();
229  foreach ($this->storage as $item) {
230  $itemArray[] = $item;
231  }
232  return $itemArray;
233  }
234 
240  public function setRelationFieldName($field) {
241  $this->relationFieldName = $field;
242  }
243 
249  public function getRelationFieldName() {
251  }
252 
258  static public function getStorageTableName() {
259  return self::$storageTableName;
260  }
261 
267  static public function getStorageItemsField() {
268  return self::$storageItemsField;
269  }
270 
276  protected function getDatabaseConnection() {
277  return $GLOBALS['TYPO3_DB'];
278  }
279 
280 }
addAll(\TYPO3\CMS\Core\Collection\CollectionInterface $other)
static load($id, $fillItems=FALSE, $tableName='', $fieldName='')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static create(array $collectionRecord, $fillItems=FALSE)