Index: t3lib/tree/tca/class.t3lib_tree_tca_databasetreedataprovider.php =================================================================== --- t3lib/tree/tca/class.t3lib_tree_tca_databasetreedataprovider.php (Revision 9326) +++ t3lib/tree/tca/class.t3lib_tree_tca_databasetreedataprovider.php (Arbeitskopie) @@ -38,6 +38,7 @@ const MODE_CHILDREN = 1; const MODE_PARENT = 2; + /** * @var string */ @@ -74,6 +75,10 @@ */ protected $rootUid = 0; + /** + * @var array + */ + protected $idCache = array(); /** * Sets the label field @@ -275,30 +280,22 @@ * @return A|null|object */ protected function getChildrenOf(t3lib_tree_Node $node, $level) { - if ($this->getLookupMode() == t3lib_tree_tca_DatabaseTreeDataProvider::MODE_CHILDREN) { - $nodeData = NULL; - if ($node->getId() !== 0) { - $nodeData = current($GLOBALS['TYPO3_DB']->exec_SELECTgetRows( - '*', - $this->tableName, - 'uid=' . $node->getId() - )); - } - if ($nodeData == NULL) { - $nodeData = array('uid' => 0); - } - $children = $this->getRelatedRecords($nodeData); - } else { - $childrenDB = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( - 'uid', + $nodeData = NULL; + if ($node->getId() !== 0) { + $nodeData = current($GLOBALS['TYPO3_DB']->exec_SELECTgetRows( + '*', $this->tableName, - $this->getLookupField() . '=' . intval($node->getId()) . $this->getTableWhere() + 'uid=' . $node->getId() + )); + } + if ($nodeData == NULL) { + $nodeData = array( + 'uid' => 0, + $this->getLookupField() => '' ); - $children = array(); - foreach ($childrenDB as $child) { - $children[] = $child['uid']; - } } + $storage = NULL; + $children = $this->getRelatedRecords($nodeData); if (count($children)) { $storage = t3lib_div::makeInstance('t3lib_tree_NodeCollection'); foreach ($children as $child) { @@ -307,17 +304,17 @@ if ($level <= $this->levelMaximum) { $children = $this->getChildrenOf($node, $level + 1); if ($children !== NULL) { - $node->setChildNodes($this->getChildrenOf($node, $level + 1)); + $node->setChildNodes($children); } } $storage->append($node); } - } else { - $storage = NULL; } return $storage; } + + /** * Gets related records depending on TCA configuration * @@ -325,34 +322,114 @@ * @return array */ protected function getRelatedRecords(array $row) { + if ($this->getLookupMode() == t3lib_tree_tca_DatabaseTreeDataProvider::MODE_PARENT) { + $children = $this->getChildrenUidsFromParentRelation($row); + } else { + $children = $this->getChildrenUidsFromChildrenRelation($row); + } + $allowedArray = array(); + foreach ($children AS $child) { + if (!in_array($child, $this->idCache)) { + $allowedArray[] = $child; + } + } + $this->idCache = array_merge($this->idCache, $allowedArray); + return $allowedArray; + } + + /** + * Gets related records depending on TCA configuration + * + * @param $row + * @return array + */ + protected function getChildrenUidsFromParentRelation(array $row) { $relatedUids = array(); $uid = $row['uid']; - $value = $row[$this->getLookupField()]; - $theColConf = $GLOBALS['TCA'][$this->getTableName()]['columns'][$this->getLookupField()]['config']; - switch ((string)$theColConf['type']) { + $columnConfiguration = $GLOBALS['TCA'][$this->getTableName()]['columns'][$this->getLookupField()]['config']; + switch ((string)$columnConfiguration['type']) { case 'inline': case 'select': - if ($theColConf['MM']) { + if ($columnConfiguration['MM']) { $dbGroup = t3lib_div::makeInstance('t3lib_loadDBGroup'); - $dbGroup->start($value, $theColConf['foreign_table'], $theColConf['MM'], $uid, $this->getLookupField(), $theColConf); - $relatedUids = $dbGroup->tableArray[$theColConf['foreign_table']]; - } elseif ($theColConf['foreign_table'] && $GLOBALS['TCA'][$theColConf['foreign_table']] && $theColConf['foreign_field']) { - $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $theColConf['foreign_table'], $theColConf['foreign_field'] . '=' . intval($uid) . ' ' . t3lib_BEfunc::deleteClause( $theColConf['foreign_table'])); + $columnConfiguration['MM_oppositeField'] = 'children'; // dummy field for setting "look from other site" + + $dbGroup->start($row[$this->getLookupField()], $columnConfiguration['foreign_table'], $columnConfiguration['MM'], $uid, $this->getTableName(), $columnConfiguration); + + $relatedUids = $dbGroup->tableArray[$columnConfiguration['foreign_table']]; + } elseif ($columnConfiguration['foreign_table'] && $GLOBALS['TCA'][$columnConfiguration['foreign_table']] && $columnConfiguration['foreign_field']) { + $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( + 'uid', + $columnConfiguration['foreign_table'], + " ( concat(','," . $columnConfiguration['foreign_field'] . ",',') LIKE '%," . intval($uid) . ",%' " + . (intval($uid) == 0 ? " OR " . $columnConfiguration['foreign_field'] . " = ''" : '' ) + . " ) " . t3lib_BEfunc::deleteClause( $columnConfiguration['foreign_table']) + ); foreach ($records as $record) { $relatedUids[] = $record['uid']; } } else { - $relatedUids = t3lib_div::trimExplode(',', $value); + $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( + 'uid', $columnConfiguration['foreign_table'], + " ( concat(','," . $this->getLookupField() . ",',') LIKE '%," . intval($uid) . ",%' " + . (intval($uid) == 0 ? " OR " . $this->getLookupField() . " = ''" : '' ) + . " ) " . t3lib_BEfunc::deleteClause( $columnConfiguration['foreign_table']) + ); + foreach ($records as $record) { + $relatedUids[] = $record['uid']; + } } break; case 'group': - $relatedUids = t3lib_div::trimExplode(',', $value, 1); + $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( + 'uid', $columnConfiguration['foreign_table'], + " ( concat(','," . $this->getLookupField() . ",',') LIKE '%," . intval($uid) . ",%' " + . (intval($uid) == 0 ? " OR " . $this->getLookupField() . " = ''" : '' ) + . " ) " . t3lib_BEfunc::deleteClause( $columnConfiguration['foreign_table']) + ); + foreach ($records as $record) { + $relatedUids[] = $record['uid']; + } break; } return $relatedUids; } + /** + * Gets related children records depending on TCA configuration + * + * @param $row + * @return array + */ + protected function getChildrenUidsFromChildrenRelation(array $row) { + $relatedUids = array(); + $uid = $row['uid']; + $value = $row[$this->getLookupField()]; + $columnConfiguration = $GLOBALS['TCA'][$this->getTableName()]['columns'][$this->getLookupField()]['config']; + switch ((string)$columnConfiguration['type']) { + case 'inline': + case 'select': + if ($columnConfiguration['MM']) { + $dbGroup->start($value, $columnConfiguration['foreign_table'], $columnConfiguration['MM'], $uid, $this->getTableName(), $columnConfiguration); + + $relatedUids = $dbGroup->tableArray[$columnConfiguration['foreign_table']]; + } elseif ($columnConfiguration['foreign_table'] && $GLOBALS['TCA'][$columnConfiguration['foreign_table']] && $columnConfiguration['foreign_field']) { + $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $columnConfiguration['foreign_table'], $columnConfiguration['foreign_field'] . '=' . intval($uid) . ' ' . t3lib_BEfunc::deleteClause( $columnConfiguration['foreign_table'])); + foreach ($records as $record) { + $relatedUids[] = $record['uid']; + } + } else { + $relatedUids = t3lib_div::intExplode(',', $value, true); + } + break; + case 'group': + $relatedUids = t3lib_div::intExplode(',', $value, true); + break; + } + return $relatedUids; + } + } ?> \ No newline at end of file