-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathDefaultController.php
More file actions
104 lines (85 loc) · 3.16 KB
/
DefaultController.php
File metadata and controls
104 lines (85 loc) · 3.16 KB
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
<?php
namespace ServerGrove\KbBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use ServerGrove\KbBundle\Document\Article;
use ServerGrove\KbBundle\Util\Sluggable;
class DefaultController extends Controller
{
/**
* @Route("/", name="sgkb_index")
* @Route("/{_locale}", name="sgkb_index_locale", requirements={"_locale"="en|es|pt"})
* @return array
*/
public function indexAction()
{
$categoryName = $this->get('service_container')->getParameter('server_grove_kb.article.front_page_category');
/** @var $category \ServerGrove\KbBundle\Document\Category */
$category = $this->getCategoryRepository()->find(sprintf('/categories/%s', Sluggable::urlize($categoryName)));
if ($category && $category->getArticles()->count()) {
$keyword = $this->get('service_container')->getParameter('server_grove_kb.article.front_page_keyword');
$article = $category->getArticles()->filter(
function (Article $article) use ($keyword) {
return in_array($keyword, $article->getKeywords()->toArray());
}
)->first();
if (!$article) {
$article = $category->getArticles()->first();
}
return $this->forward(
'ServerGroveKbBundle:Articles:view',
array(
'article' => $article,
'category' => $article->getDefaultCategory(),
'registerView' => false,
'showComments' => false
)
);
}
return $this->forward('ServerGroveKbBundle:Categories:index');
}
/**
* @Template("ServerGroveKbBundle:Default:searchForm.html.twig")
* @return array
*/
public function searchFormAction()
{
$form = $this->getSearchForm();
return array('form' => $form->createView());
}
/**
* @Route("/{_locale}/search", name="sgkb_search", requirements={"_locale"="en|es|pt"})
* @Template
*
* @return array
*/
public function searchAction()
{
$form = $this->getSearchForm();
/** @var $request \Symfony\Component\HttpFoundation\Request */
$request = $this->getRequest();
$results = array();
if ('POST' == $request->getMethod()) {
$form->bind($request);
if ($form->isValid()) {
$data = $form->getData();
$category = null;
if (!empty($data['category'])) {
$category = $this->getCategoryRepository()->find($data['category']);
}
$results = $this->getArticleRepository()->search($data['keywords'], $category);
}
}
return array(
'form' => $form->createView(),
'results' => $results
);
}
/**
* @return \Symfony\Component\Form\Form
*/
private function getSearchForm()
{
return $this->createForm(new \ServerGrove\KbBundle\Form\SearchArticleType($this->getCategoryRepository()));
}
}