TYPO3 CMS  TYPO3_6-2
DatabaseTreeDataProvider.php
Go to the documentation of this file.
1 <?php
3 
19 
26 
27  const SIGNAL_PostProcessTreeData = 'PostProcessTreeData';
28  const MODE_CHILDREN = 1;
29  const MODE_PARENT = 2;
33  protected $tableName = '';
34 
38  protected $treeId = '';
39 
43  protected $labelField = '';
44 
48  protected $tableWhere = '';
49 
53  protected $lookupMode = self::MODE_CHILDREN;
54 
58  protected $lookupField = '';
59 
63  protected $rootUid = 0;
64 
68  protected $idCache = array();
69 
76 
82  protected $nodeSortValues = array();
83 
87  protected $generatedTSConfig = array();
88 
93 
100  public function setLabelField($labelField) {
101  $this->labelField = $labelField;
102  }
103 
109  public function getLabelField() {
110  return $this->labelField;
111  }
112 
119  public function setTableName($tableName) {
120  $this->tableName = $tableName;
121  }
122 
128  public function getTableName() {
129  return $this->tableName;
130  }
131 
138  public function setLookupField($lookupField) {
139  $this->lookupField = $lookupField;
140  }
141 
147  public function getLookupField() {
148  return $this->lookupField;
149  }
150 
157  public function setLookupMode($lookupMode) {
158  $this->lookupMode = $lookupMode;
159  }
160 
166  public function getLookupMode() {
167  return $this->lookupMode;
168  }
169 
176  public function getNodes(\TYPO3\CMS\Backend\Tree\TreeNode $node) {
177 
178  }
179 
185  public function getRoot() {
186  return $this->buildRepresentationForNode($this->treeData);
187  }
188 
195  public function setRootUid($rootUid) {
196  $this->rootUid = $rootUid;
197  }
198 
204  public function getRootUid() {
205  return $this->rootUid;
206  }
207 
214  public function setTableWhere($tableWhere) {
215  $this->tableWhere = $tableWhere;
216  }
217 
223  public function getTableWhere() {
224  return $this->tableWhere;
225  }
226 
235  protected function buildRepresentationForNode(\TYPO3\CMS\Backend\Tree\TreeNode $basicNode, \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeNode $parent = NULL, $level = 0) {
237  $node = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Tree\\TableConfiguration\\DatabaseTreeNode');
238  $row = array();
239  if ($basicNode->getId() == 0) {
240  $node->setSelected(FALSE);
241  $node->setExpanded(TRUE);
242  $node->setLabel($GLOBALS['LANG']->sL($GLOBALS['TCA'][$this->tableName]['ctrl']['title']));
243  } else {
244  $row = BackendUtility::getRecordWSOL($this->tableName, $basicNode->getId(), '*', '', FALSE);
245  $node->setLabel(BackendUtility::getRecordTitle($this->tableName, $row) ?: $basicNode->getId());
246  $node->setSelected(GeneralUtility::inList($this->getSelectedList(), $basicNode->getId()));
247  $node->setExpanded($this->isExpanded($basicNode));
248  }
249  $node->setId($basicNode->getId());
250  $node->setSelectable(!GeneralUtility::inList($this->getNonSelectableLevelList(), $level) && !in_array($basicNode->getId(), $this->getItemUnselectableList()));
251  $node->setSortValue($this->nodeSortValues[$basicNode->getId()]);
252  $node->setIcon(\TYPO3\CMS\Backend\Utility\IconUtility::mapRecordTypeToSpriteIconClass($this->tableName, $row));
253  $node->setParentNode($parent);
254  if ($basicNode->hasChildNodes()) {
255  $node->setHasChildren(TRUE);
257  $childNodes = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Tree\\SortedTreeNodeCollection');
258  foreach ($basicNode->getChildNodes() as $child) {
259  $childNodes->append($this->buildRepresentationForNode($child, $node, $level + 1));
260  }
261  $node->setChildNodes($childNodes);
262  }
263  return $node;
264  }
265 
271  public function initializeTreeData() {
272  parent::initializeTreeData();
273  $this->nodeSortValues = array_flip($this->itemWhiteList);
274  $this->columnConfiguration = $GLOBALS['TCA'][$this->getTableName()]['columns'][$this->getLookupField()]['config'];
275  if (isset($this->columnConfiguration['foreign_table']) && $this->columnConfiguration['foreign_table'] != $this->getTableName()) {
276  throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for different node-Tables is not implemented yet', 1290944650);
277  }
278  $this->treeData = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Tree\\TreeNode');
279  $this->treeData->setId($this->getRootUid());
280  $this->treeData->setParentNode(NULL);
281  $childNodes = $this->getChildrenOf($this->treeData, 0);
282  if ($childNodes !== NULL) {
283  $this->treeData->setChildNodes($childNodes);
284  }
285 
287  }
288 
296  protected function getChildrenOf(\TYPO3\CMS\Backend\Tree\TreeNode $node, $level) {
297  $nodeData = NULL;
298  if ($node->getId() !== 0) {
299  $nodeData = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', $this->tableName, 'uid=' . $node->getId());
300  }
301  if ($nodeData == NULL) {
302  $nodeData = array(
303  'uid' => 0,
304  $this->getLookupField() => ''
305  );
306  }
307  $storage = NULL;
308  $children = $this->getRelatedRecords($nodeData);
309  if (count($children)) {
311  $storage = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Tree\\TreeNodeCollection');
312  foreach ($children as $child) {
313  $node = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Tree\\TreeNode');
314  $node->setId($child);
315  if ($level <= $this->levelMaximum) {
316  $children = $this->getChildrenOf($node, $level + 1);
317  if ($children !== NULL) {
318  $node->setChildNodes($children);
319  }
320  }
321  $storage->append($node);
322  }
323  }
324  return $storage;
325  }
326 
333  protected function getRelatedRecords(array $row) {
335  $children = $this->getChildrenUidsFromParentRelation($row);
336  } else {
337  $children = $this->getChildrenUidsFromChildrenRelation($row);
338  }
339  $allowedArray = array();
340  foreach ($children as $child) {
341  if (!in_array($child, $this->idCache) && in_array($child, $this->itemWhiteList)) {
342  $allowedArray[] = $child;
343  }
344  }
345  $this->idCache = array_merge($this->idCache, $allowedArray);
346  return $allowedArray;
347  }
348 
355  protected function getChildrenUidsFromParentRelation(array $row) {
356  $uid = $row['uid'];
357  switch ((string) $this->columnConfiguration['type']) {
358  case 'inline':
359 
360  case 'select':
361  if ($this->columnConfiguration['MM']) {
363  $dbGroup = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
364  // Dummy field for setting "look from other site"
365  $this->columnConfiguration['MM_oppositeField'] = 'children';
366  $dbGroup->start($row[$this->getLookupField()], $this->getTableName(), $this->columnConfiguration['MM'], $uid, $this->getTableName(), $this->columnConfiguration);
367  $relatedUids = $dbGroup->tableArray[$this->getTableName()];
368  } elseif ($this->columnConfiguration['foreign_field']) {
369  $relatedUids = $this->listFieldQuery($this->columnConfiguration['foreign_field'], $uid);
370  } else {
371  $relatedUids = $this->listFieldQuery($this->getLookupField(), $uid);
372  }
373  break;
374  default:
375  $relatedUids = $this->listFieldQuery($this->getLookupField(), $uid);
376  }
377  return $relatedUids;
378  }
379 
386  protected function getChildrenUidsFromChildrenRelation(array $row) {
387  $relatedUids = array();
388  $uid = $row['uid'];
389  $value = $row[$this->getLookupField()];
390  switch ((string) $this->columnConfiguration['type']) {
391  case 'inline':
392 
393  case 'select':
394  if ($this->columnConfiguration['MM']) {
396  $dbGroup = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
397  $dbGroup->start($value, $this->getTableName(), $this->columnConfiguration['MM'], $uid, $this->getTableName(), $this->columnConfiguration);
398  $relatedUids = $dbGroup->tableArray[$this->getTableName()];
399  } elseif ($this->columnConfiguration['foreign_field']) {
400  $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $this->getTableName(), $this->columnConfiguration['foreign_field'] . '=' . (int)$uid);
401  foreach ($records as $record) {
402  $relatedUids[] = $record['uid'];
403  }
404  } else {
405  $relatedUids = GeneralUtility::intExplode(',', $value, TRUE);
406  }
407  break;
408  default:
409  $relatedUids = GeneralUtility::intExplode(',', $value, TRUE);
410  }
411  return $relatedUids;
412  }
413 
421  protected function listFieldQuery($fieldName, $queryId) {
422  $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $this->getTableName(), $GLOBALS['TYPO3_DB']->listQuery($fieldName, (int)$queryId, $this->getTableName()) . ((int)$queryId === 0 ? ' OR CAST(' . $fieldName . ' AS CHAR) = \'\'' : ''));
423  $uidArray = array();
424  foreach ($records as $record) {
425  $uidArray[] = $record['uid'];
426  }
427  return $uidArray;
428  }
429 
435  protected function emitPostProcessTreeDataSignal() {
436  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Tree\\TableConfiguration\\TableConfiguration\\DatabaseTreeDataProvider',
437  self::SIGNAL_PostProcessTreeData,
438  array($this, $this->treeData)
439  );
440  }
441 
447  protected function getSignalSlotDispatcher() {
448  if (!isset($this->signalSlotDispatcher)) {
449  $this->signalSlotDispatcher = $this->getObjectManager()->get('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
450  }
452  }
453 
459  protected function getObjectManager() {
460  return GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
461  }
462 
463 }
static getRecordWSOL($table, $uid, $fields=' *', $where='', $useDeleteClause=TRUE, $unsetMovePointers=FALSE)
static intExplode($delimiter, $string, $removeEmptyValues=FALSE, $limit=0)
$uid
Definition: server.php:36
static getRecordTitle($table, $row, $prep=FALSE, $forceResult=TRUE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]