/** * @param \ServerGrove\KbBundle\Document\Category $category * @param \Doctrine\Common\Collections\ArrayCollection $articles * @return \Doctrine\Common\Collections\ArrayCollection */ public function getArticlesFromCategoryHierarchy(Category $category, ArrayCollection $articles) { if (is_null($articles)) { $articles = new \Doctrine\Common\Collections\ArrayCollection(); } foreach ($category->getArticles() as $article) { $articles->add($article); } foreach ($category->getChildren() as $child) { $this->getArticlesFromCategoryHierarchy($child, $articles); } return $articles; }
/** * @param \ServerGrove\KbBundle\Document\Category $category * * @return bool */ private function shouldCategoryBeDisplayed(Category $category) { if (0 < $category->getArticles()->count()) { /** @var $article \ServerGrove\KbBundle\Document\Article */ foreach ($category->getArticles() as $article) { // If the category has at least one article active, // it should be displayed if ($article->getIsActive()) { return true; } } } elseif (0 < $category->getChildren()->count()) { /** @var $child \ServerGrove\KbBundle\Document\Category */ foreach ($category->getChildren() as $child) { if ($this->shouldCategoryBeDisplayed($child)) { return true; } } } return false; }
public function removeChildrenFromCategory(Category $category) { $controller = $this; foreach ($category->getChildren() as $child) { call_user_func(array($controller, 'removeArticlesFromCategory'), $child); call_user_func(array($controller, 'removeChildrenFromCategory'), $child); /** @var $category Category */ $category->getChildren()->removeElement($child); } }