|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Zikula package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright Zikula Foundation - http://zikula.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zikula\SearchModule\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
15
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
19
|
|
|
use Zikula\Core\Controller\AbstractController; |
|
20
|
|
|
use Zikula\Core\Response\PlainResponse; |
|
21
|
|
|
|
|
22
|
|
|
class SearchController extends AbstractController |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @Route("/{page}", requirements={"page"="\d+"}) |
|
26
|
|
|
* @Template |
|
27
|
|
|
* |
|
28
|
|
|
* @param Request $request |
|
29
|
|
|
* @param int $page |
|
30
|
|
|
* @return array|Response |
|
31
|
|
|
*/ |
|
32
|
|
|
public function executeAction(Request $request, $page = -1) |
|
33
|
|
|
{ |
|
34
|
|
|
if (!$this->hasPermission('ZikulaSearchModule::', '::', ACCESS_READ)) { |
|
35
|
|
|
throw new AccessDeniedException(); |
|
36
|
|
|
} |
|
37
|
|
|
$setActiveDefaults = false; |
|
38
|
|
|
if (!$request->query->has('active')) { |
|
39
|
|
|
$setActiveDefaults = true; |
|
40
|
|
|
$activeModules = []; |
|
41
|
|
|
} else { |
|
42
|
|
|
$activeModules = $request->query->get('active'); |
|
43
|
|
|
if (!is_array($activeModules)) { |
|
44
|
|
|
$activeModules = [$activeModules]; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
$searchableModules = $this->get('zikula_search_module.internal.searchable_module_collector')->getAll(); |
|
48
|
|
|
|
|
49
|
|
|
if (count($searchableModules) == 0) { |
|
50
|
|
|
return $this->render('@ZikulaSearchModule/Search/unsearchable.html.twig'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$moduleFormBuilder = $this->get('form.factory') |
|
54
|
|
|
->createNamedBuilder('modules', 'Symfony\Component\Form\Extension\Core\Type\FormType', [], [ |
|
55
|
|
|
'auto_initialize' => false, |
|
56
|
|
|
'required' => false |
|
57
|
|
|
]); |
|
58
|
|
|
foreach ($searchableModules as $moduleName => $searchableInstance) { |
|
59
|
|
|
if ($setActiveDefaults) { |
|
60
|
|
|
$activeModules[$moduleName] = 1; |
|
61
|
|
|
} |
|
62
|
|
|
if ($this->getVar('disable_' . $moduleName, false)) { |
|
63
|
|
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
if (!$this->hasPermission('ZikulaSearchModule::Item', $moduleName . '::', ACCESS_READ)) { |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
$moduleFormBuilder->add($moduleName, 'Zikula\SearchModule\Form\Type\AmendableModuleSearchType', [ |
|
69
|
|
|
'label' => $this->get('kernel')->getModule($moduleName)->getMetaData()->getDisplayName(), |
|
70
|
|
|
'translator' => $this->getTranslator(), |
|
71
|
|
|
'active' => !$setActiveDefaults || (isset($activeModules[$moduleName]) && ($activeModules[$moduleName] == 1)), |
|
72
|
|
|
'permissionApi' => $this->get('zikula_permissions_module.api.permission') |
|
73
|
|
|
]); |
|
74
|
|
|
$searchableInstance->amendForm($moduleFormBuilder->get($moduleName)); |
|
75
|
|
|
} |
|
76
|
|
|
$form = $this->createForm('Zikula\SearchModule\Form\Type\SearchType', [], [ |
|
77
|
|
|
'translator' => $this->get('translator.default'), |
|
78
|
|
|
]); |
|
79
|
|
|
$form->add($moduleFormBuilder->getForm()); |
|
80
|
|
|
$q = $request->query->get('q', ''); |
|
81
|
|
|
if ($request->isMethod('GET') && !empty($q)) { |
|
82
|
|
|
$token = $request->query->get('_token'); |
|
83
|
|
|
$form->submit(['q' => $q, '_token' => $token]); |
|
84
|
|
|
} else { |
|
85
|
|
|
$form->handleRequest($request); |
|
86
|
|
|
} |
|
87
|
|
|
$noResultsFound = false; |
|
88
|
|
|
|
|
89
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
90
|
|
|
$searchApi = $this->get('zikula_search_module.api.search_api'); |
|
91
|
|
|
$formData = $form->getData(); |
|
92
|
|
|
$formData['numlimit'] = $this->getVar('itemsperpage', 25); |
|
93
|
|
|
$formData['firstPage'] = $page < 1; |
|
94
|
|
|
$formData['page'] = $page < 1 ? 1 : $page; |
|
95
|
|
|
$result = $searchApi->search($formData['q'], $page < 1, $formData['searchType'], $formData['searchOrder'], $this->getVar('itemsperpage', 25), $page, $formData['modules']); |
|
96
|
|
|
$searchApiErrors = $result['errors']; |
|
97
|
|
|
if ($result['resultCount'] > 0) { |
|
98
|
|
|
$templateParameters = array_merge($formData, [ |
|
99
|
|
|
'resultCount' => $result['resultCount'], |
|
100
|
|
|
'results' => $result['sqlResult'], |
|
101
|
|
|
'limitSummary' => $this->getVar('limitsummary', 200), |
|
102
|
|
|
'errors' => isset($searchApiErrors) ? $searchApiErrors : [] |
|
103
|
|
|
]); |
|
104
|
|
|
// log the search if on first page |
|
105
|
|
|
if ($formData['firstPage']) { |
|
106
|
|
|
$searchApi->log($formData['q']); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this->render('@ZikulaSearchModule/Search/results.html.twig', $templateParameters); |
|
110
|
|
|
} else { |
|
111
|
|
|
$noResultsFound = true; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return [ |
|
116
|
|
|
'noResultsFound' => $noResultsFound, |
|
117
|
|
|
'form' => $form->createView(), |
|
118
|
|
|
]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @Route("/recent") |
|
123
|
|
|
* @Template |
|
124
|
|
|
* |
|
125
|
|
|
* Display a list of recent searches |
|
126
|
|
|
* |
|
127
|
|
|
* @return array |
|
128
|
|
|
* |
|
129
|
|
|
* @throws AccessDeniedException Thrown if the user doesn't have read access |
|
130
|
|
|
*/ |
|
131
|
|
|
public function recentAction(Request $request) |
|
132
|
|
|
{ |
|
133
|
|
|
// security check |
|
134
|
|
|
if (!$this->hasPermission('ZikulaSearchModule::', '::', ACCESS_READ)) { |
|
135
|
|
|
throw new AccessDeniedException(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$startnum = $request->query->getInt('startnum', 0); |
|
139
|
|
|
$itemsPerPage = $this->getVar('itemsperpage', 25); |
|
140
|
|
|
$statRepo = $this->get('zikula_search_module.search_stat_repository'); |
|
141
|
|
|
$items = $statRepo->getStats([], ['date' => 'DESC'], $itemsPerPage, $startnum); |
|
142
|
|
|
|
|
143
|
|
|
$templateParameters = [ |
|
144
|
|
|
'recentSearches' => $items, |
|
145
|
|
|
'pager' => [ |
|
146
|
|
|
'amountOfItems' => $statRepo->countStats(), |
|
147
|
|
|
'itemsPerPage' => $itemsPerPage |
|
148
|
|
|
] |
|
149
|
|
|
]; |
|
150
|
|
|
|
|
151
|
|
|
return $templateParameters; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @Route("/opensearch", options={"i18n"=false}) |
|
156
|
|
|
* |
|
157
|
|
|
* Generate xml for opensearch syndication |
|
158
|
|
|
* |
|
159
|
|
|
* @return PlainResponse Thrown if the user doesn't have read access to the module |
|
160
|
|
|
*/ |
|
161
|
|
|
public function opensearchAction() |
|
162
|
|
|
{ |
|
163
|
|
|
if (!$this->hasPermission('ZikulaSearchModule::', '::', ACCESS_READ)) { |
|
164
|
|
|
throw new AccessDeniedException(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$variableApi = $this->get('zikula_extensions_module.api.variable'); |
|
168
|
|
|
$templateParameters = [ |
|
169
|
|
|
'siteName' => $variableApi->getSystemVar('sitename', $variableApi->getSystemVar('sitename_en')), |
|
170
|
|
|
'slogan' => $variableApi->getSystemVar('slogan', $variableApi->getSystemVar('slogan_en')), |
|
171
|
|
|
'metaKeywords' => $variableApi->getSystemVar('metakeywords', $variableApi->getSystemVar('metakeywords_en')), |
|
172
|
|
|
'adminMail' => $variableApi->getSystemVar('adminmail'), |
|
173
|
|
|
'hasAdultContent' => $variableApi->get('ZikulaSearchModule', 'opensearch_adult_content', false) |
|
174
|
|
|
]; |
|
175
|
|
|
|
|
176
|
|
|
return new PlainResponse($this->renderView('@ZikulaSearchModule/Search/opensearch.xml.twig', $templateParameters), Response::HTTP_OK, ['Content-Type' => 'text/xml']); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|