|
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\Block; |
|
13
|
|
|
|
|
14
|
|
|
use Zikula\BlocksModule\AbstractBlockHandler; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Block to display a search form |
|
18
|
|
|
*/ |
|
19
|
|
|
class SearchBlock extends AbstractBlockHandler |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* display block |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $properties |
|
25
|
|
|
* @return string the rendered bock |
|
26
|
|
|
*/ |
|
27
|
|
|
public function display(array $properties) |
|
28
|
|
|
{ |
|
29
|
|
|
$title = !empty($properties['title']) ? $properties['title'] : ''; |
|
30
|
|
|
if (!$this->hasPermission('Searchblock::', "$title::", ACCESS_READ)) { |
|
31
|
|
|
return ''; |
|
32
|
|
|
} |
|
33
|
|
|
// set defaults |
|
34
|
|
|
$properties['displaySearchBtn'] = isset($properties['displaySearchBtn']) ? $properties['displaySearchBtn'] : false; |
|
35
|
|
|
$properties['active'] = isset($properties['active']) ? $properties['active'] : []; |
|
36
|
|
|
|
|
37
|
|
|
// get Core-2.0 searchable modules |
|
38
|
|
|
$searchableModules = $this->get('zikula_search_module.internal.searchable_module_collector')->getAll(); |
|
39
|
|
|
$moduleFormBuilder = $this->get('form.factory') |
|
40
|
|
|
->createNamedBuilder('modules', 'Symfony\Component\Form\Extension\Core\Type\FormType', [], [ |
|
41
|
|
|
'auto_initialize' => false, |
|
42
|
|
|
'required' => false |
|
43
|
|
|
]); |
|
44
|
|
|
foreach ($searchableModules as $moduleName => $searchableInstance) { |
|
45
|
|
|
if (!in_array($moduleName, $properties['active'])) { |
|
46
|
|
|
continue; |
|
47
|
|
|
} |
|
48
|
|
|
if (!$this->hasPermission('ZikulaSearchModule::Item', $moduleName . '::', ACCESS_READ)) { |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
$moduleFormBuilder->add($moduleName, 'Zikula\SearchModule\Form\Type\AmendableModuleSearchType', [ |
|
52
|
|
|
'label' => $this->get('kernel')->getModule($moduleName)->getMetaData()->getDisplayName(), |
|
53
|
|
|
'translator' => $this->getTranslator(), |
|
54
|
|
|
'active' => true, |
|
55
|
|
|
'permissionApi' => $this->get('zikula_permissions_module.api.permission') |
|
56
|
|
|
]); |
|
57
|
|
|
$searchableInstance->amendForm($moduleFormBuilder->get($moduleName)); |
|
58
|
|
|
} |
|
59
|
|
|
$form = $this->get('form.factory')->create('Zikula\SearchModule\Form\Type\SearchType', [], [ |
|
60
|
|
|
'translator' => $this->get('translator.default'), |
|
61
|
|
|
'action' => $this->get('router')->generate('zikulasearchmodule_search_execute') |
|
62
|
|
|
]); |
|
63
|
|
|
$form->add($moduleFormBuilder->getForm()); |
|
64
|
|
|
|
|
65
|
|
|
$templateParameters = [ |
|
66
|
|
|
'form' => $form->createView(), |
|
67
|
|
|
'properties' => $properties, |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
return $this->renderView('@ZikulaSearchModule/Block/search.html.twig', $templateParameters); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns the fully qualified class name of the block's form class. |
|
75
|
|
|
* |
|
76
|
|
|
* @return string Template path |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getFormClassName() |
|
79
|
|
|
{ |
|
80
|
|
|
return 'Zikula\SearchModule\Block\Form\Type\SearchBlockType'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns any array of form options. |
|
85
|
|
|
* |
|
86
|
|
|
* @return array Options array |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getFormOptions() |
|
89
|
|
|
{ |
|
90
|
|
|
$searchModules = []; |
|
91
|
|
|
$searchableModules = $this->get('zikula_search_module.internal.searchable_module_collector')->getAll(); |
|
92
|
|
View Code Duplication |
foreach (array_keys($searchableModules) as $moduleName) { |
|
|
|
|
|
|
93
|
|
|
$displayName = $this->get('kernel')->getModule($moduleName)->getMetaData()->getDisplayName(); |
|
94
|
|
|
$searchModules[$displayName] = $moduleName; |
|
95
|
|
|
} |
|
96
|
|
|
// remove disabled |
|
97
|
|
|
foreach ($searchModules as $displayName => $moduleName) { |
|
98
|
|
|
if ((bool) $this->getVar('disable_' . $moduleName, false)) { |
|
99
|
|
|
unset($searchModules[$displayName]); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// get all the search plugins |
|
104
|
|
|
return [ |
|
105
|
|
|
'activeModules' => $searchModules |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the template used for rendering the editing form. |
|
111
|
|
|
* |
|
112
|
|
|
* @return string Template path |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getFormTemplate() |
|
115
|
|
|
{ |
|
116
|
|
|
return '@ZikulaSearchModule/Block/search_modify.html.twig'; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.