/** * Find category tree * * @param array $rootIdList list of id s * @return QueryInterface */ public function findTree(array $rootIdList) { $subCategories = CategoryService::getChildrenCategories(implode(',', $rootIdList)); $ordering = array('sorting' => QueryInterface::ORDER_ASCENDING); $categories = $this->findByIdList(explode(',', $subCategories), $ordering); $flatCategories = array(); foreach ($categories as $category) { $flatCategories[$category->getUid()] = array('item' => $category, 'parent' => $category->getParentcategory() ? $category->getParentcategory()->getUid() : null); } $tree = array(); // If leaves are selected without its parents selected, those are shown as parent foreach ($flatCategories as $id => &$flatCategory) { if (!isset($flatCategories[$flatCategory['parent']])) { $flatCategory['parent'] = null; } } foreach ($flatCategories as $id => &$node) { if ($node['parent'] === null) { $tree[$id] =& $node; } else { $flatCategories[$node['parent']]['children'][$id] =& $node; } } return $tree; }
/** * Builds a complete node including children * * @param \TYPO3\CMS\Backend\Tree\TreeNode|\TYPO3\CMS\Backend\Tree\TreeNode $basicNode * @param NULL|\TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeNode $parent * @param integer $level * @param bool $restriction * @return \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeNode node */ protected function buildRepresentationForNode(\TYPO3\CMS\Backend\Tree\TreeNode $basicNode, \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeNode $parent = null, $level = 0, $restriction = false) { $iconFactory = GeneralUtility::makeInstance(IconFactory::class); /**@param $node \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeNode */ $node = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeNode::class); $row = array(); if ($basicNode->getId() == 0) { $node->setSelected(false); $node->setExpanded(true); $node->setLabel($GLOBALS['LANG']->sL($GLOBALS['TCA'][$this->tableName]['ctrl']['title'])); } else { $row = BackendUtility::getRecordWSOL($this->tableName, $basicNode->getId(), '*', '', false); if ($this->getLabelField() !== '') { $label = CategoryService::translateCategoryRecord($row[$this->getLabelField()], $row); $node->setLabel($label); } else { $node->setLabel($basicNode->getId()); } $node->setSelected(GeneralUtility::inList($this->getSelectedList(), $basicNode->getId())); $node->setExpanded($this->isExpanded($basicNode)); $node->setLabel($node->getLabel()); } $node->setId($basicNode->getId()); // Break to force single category activation if ($parent != null && $level != 0 && $this->isSingleCategoryAclActivated() && !$this->isCategoryAllowed($node)) { return null; } $node->setSelectable(!GeneralUtility::inList($this->getNonSelectableLevelList(), $level) && !in_array($basicNode->getId(), $this->getItemUnselectableList())); $node->setSortValue($this->nodeSortValues[$basicNode->getId()]); $node->setIcon($iconFactory->getIconForRecord($this->tableName, $row, Icon::SIZE_SMALL)->render()); $node->setParentNode($parent); if ($basicNode->hasChildNodes()) { $node->setHasChildren(true); /** @var \TYPO3\CMS\Backend\Tree\SortedTreeNodeCollection $childNodes */ $childNodes = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\SortedTreeNodeCollection::class); $foundSomeChild = false; foreach ($basicNode->getChildNodes() as $child) { // Change in custom TreeDataProvider by adding the if clause if ($restriction || $this->isCategoryAllowed($child)) { $returnedChild = $this->buildRepresentationForNode($child, $node, $level + 1, true); if (!is_null($returnedChild)) { $foundSomeChild = true; $childNodes->append($returnedChild); } else { $node->setParentNode(null); $node->setHasChildren(false); } } // Change in custom TreeDataProvider end } if ($foundSomeChild) { $node->setChildNodes($childNodes); } } return $node; }
/** * Generate additional label for category records * including the title of the parent category * * @param array $params * @return void */ public function getUserLabelCategory(array &$params) { // In list view: show normal label $listView = strpos(GeneralUtility::getIndpEnv('REQUEST_URI'), 'typo3/sysext/list/mod1/db_list.php') || strpos(GeneralUtility::getIndpEnv('REQUEST_URI'), 'typo3/mod.php?&M=web_list') || strpos(GeneralUtility::getIndpEnv('REQUEST_URI'), 'typo3/mod.php?M=web_list'); // No overlay if language of category is not base or no language yet selected if ($listView || !is_array($params['row'])) { $params['title'] = $params['row']['title']; } else { $params['title'] = CategoryService::translateCategoryRecord($params['row']['title'], $params['row']); } }
/** * Generate additional label for category records * including the title of the parent category * * @param array $params * @return void */ public function getUserLabelCategory(array &$params) { $showTranslationInformation = false; $getVars = GeneralUtility::_GET(); if (isset($getVars['route']) && $getVars['route'] === '/record/edit' && isset($getVars['edit']) && is_array($getVars['edit']) && (isset($getVars['edit']['tt_content']) || isset($getVars['edit']['tx_news_domain_model_news']) || isset($getVars['edit']['sys_category']))) { $showTranslationInformation = true; } if ($showTranslationInformation && is_array($params['row'])) { $params['title'] = CategoryService::translateCategoryRecord($params['row']['title'], $params['row']); } else { $params['title'] = $params['row']['title']; } }
/** * Returns a category constraint created by * a given list of categories and a junction string * * @param QueryInterface $query * @param array $categories * @param string $conjunction * @param boolean $includeSubCategories * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface|null */ protected function createCategoryConstraint(QueryInterface $query, $categories, $conjunction, $includeSubCategories = false) { $constraint = null; $categoryConstraints = []; // If "ignore category selection" is used, nothing needs to be done if (empty($conjunction)) { return $constraint; } if (!is_array($categories)) { $categories = GeneralUtility::intExplode(',', $categories, true); } foreach ($categories as $category) { if ($includeSubCategories) { $subCategories = GeneralUtility::trimExplode(',', CategoryService::getChildrenCategories($category, 0, '', true), true); $subCategoryConstraint = []; $subCategoryConstraint[] = $query->contains('categories', $category); if (count($subCategories) > 0) { foreach ($subCategories as $subCategory) { $subCategoryConstraint[] = $query->contains('categories', $subCategory); } } if ($subCategoryConstraint) { $categoryConstraints[] = $query->logicalOr($subCategoryConstraint); } } else { $categoryConstraints[] = $query->contains('categories', $category); } } if ($categoryConstraints) { switch (strtolower($conjunction)) { case 'or': $constraint = $query->logicalOr($categoryConstraints); break; case 'notor': $constraint = $query->logicalNot($query->logicalOr($categoryConstraints)); break; case 'notand': $constraint = $query->logicalNot($query->logicalAnd($categoryConstraints)); break; case 'and': default: $constraint = $query->logicalAnd($categoryConstraints); } } return $constraint; }
/** * @test * @dataProvider removeValuesFromStringDataProvider */ public function removeValuesFromString($expected, $given) { $result = CategoryService::removeValuesFromString($given[0], $given[1]); $this->assertEquals($expected, $result); }