/**
  * Find category tree
  *
  * @param array $rootIdList list of id s
  * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
  */
 public function findTree(array $rootIdList)
 {
     $subCategories = Tx_News_Service_CategoryService::getChildrenCategories(implode(',', $rootIdList));
     $ordering = array('sorting' => \TYPO3\CMS\Extbase\Persistence\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;
 }
 /**
  * Returns a category constraint created by
  * a given list of categories and a junction string
  *
  * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
  * @param  array $categories
  * @param  string $conjunction
  * @param  boolean $includeSubCategories
  * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface|null
  */
 protected function createCategoryConstraint(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, $categories, $conjunction, $includeSubCategories = FALSE)
 {
     $constraint = NULL;
     $categoryConstraints = array();
     // If "ignore category selection" is used, nothing needs to be done
     if (empty($conjunction)) {
         return $constraint;
     }
     if (!is_array($categories)) {
         $categories = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $categories, TRUE);
     }
     foreach ($categories as $category) {
         if ($includeSubCategories) {
             $subCategories = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', Tx_News_Service_CategoryService::getChildrenCategories($category, 0, '', TRUE), TRUE);
             $subCategoryConstraint = array();
             $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;
 }