forked from servergrove/KnowledgeBase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CategoriesController.php
139 lines (126 loc) · 4.38 KB
/
CategoriesController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace ServerGrove\KbBundle\Controller;
use Doctrine\Common\Collections\ArrayCollection;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use ServerGrove\KbBundle\Document\Article;
use ServerGrove\KbBundle\Document\Category;
/**
* Class CategoriesController
*
* @Route("/{_locale}/categories", requirements={"_locale"="en|es|pt"})
*
* @author Ismael Ambrosi<ismael@servergrove.com>
*/
class CategoriesController extends Controller
{
/**
* @Route("/", name="sgkb_categories_index")
* @Template
*
* @param array $categories
*
* @return array
*/
public function indexAction($categories = null)
{
return array();
}
/**
* @Template
*
* @param \ServerGrove\KbBundle\Document\Category $category
* @param \ServerGrove\KbBundle\Document\Category $topCategory
* @return array
*/
public function selectorAction(Category $category = null, Category $topCategory = null)
{
$context = $this->get('security.context');
$repository = $this->getCategoryRepository();
if (is_null($category)) {
$categories = $repository->findAllParentsActive($context->isGranted('ROLE_USER'));
} else {
$categories = $category->getChildren()->filter(
$repository->getFilterClosure($context->isGranted('ROLE_USER'))
);
}
$filteredCategories = new ArrayCollection();
/** @var $category \ServerGrove\KbBundle\Document\Category */
foreach ($categories as $subcategory) {
if ($this->shouldCategoryBeDisplayed($subcategory)) {
$filteredCategories->add($subcategory);
}
}
return array('categories' => $filteredCategories, 'topCategory' => $topCategory);
}
/**
* @param \ServerGrove\KbBundle\Document\Category $category
* @return \Symfony\Component\HttpFoundation\Response
*/
public function subcategorySelectorAction(Category $category)
{
return $this->render(
'ServerGroveKbBundle:Categories:subcategorySelector.html.twig',
$this->selectorAction($category)
);
}
/**
*
* @Route("/view/{path}.{_format}",
* name="sgkb_categories_view",
* defaults={"_format"="html"},
* requirements={"_format"="html|json|xml|rss","path":".+"}
* )
* @Template
* @ParamConverter("category", class="ServerGroveKbBundle:Category")
*
* @param Category $category
*
* @return array
*/
public function viewAction(Category $category)
{
$keyword = $this->get('service_container')->getParameter('server_grove_kb.article.top_keyword');
$context = $this->get('security.context');
$repository = $this->getCategoryRepository();
return array(
'category' => $category,
'subcategories' => $category->getChildren()->filter(
$repository->getFilterClosure($context->isGranted('ROLE_USER'))
),
'articles' => $category->getArticles(true),
'topArticles' => $category->getArticles(true)->filter(
function (Article $article) use ($keyword) {
return in_array($keyword, $article->getKeywords()->getValues());
}
)
);
}
/**
* @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;
}
}