/
form.php
182 lines (157 loc) · 5.9 KB
/
form.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* DokuWiki Plugin elasticsearch (Form Helper Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <gohr@cosmocode.de>
* @author Anna Dabrowska <dabrowska@cosmocode.de>
*/
use dokuwiki\Form\Form;
/**
* Search form helper
*/
class helper_plugin_elasticsearch_form extends DokuWiki_Plugin
{
/**
* Replacement for the standard search form
*
* @param array $aggregations
*/
public function tpl($aggregations)
{
global $lang;
global $QUERY;
$searchForm = (new Form(['method' => 'get'], true))->addClass('search-results-form');
$searchForm->setHiddenField('do', 'search');
$searchForm->addFieldsetOpen()->addClass('search-form');
$searchForm->addTextInput('q')->val($QUERY)->useInput(false);
$searchForm->addButton('', $lang['btn_search'])->attr('type', 'submit');
$this->addAdvancedSearch($searchForm, $aggregations);
$searchForm->addFieldsetClose();
echo $searchForm->toHTML();
}
/**
* Advanced search
*
* @param Form $searchForm
* @param array $aggregations
*/
protected function addAdvancedSearch(Form $searchForm, array $aggregations)
{
$searchForm->addTagOpen('div')
->addClass('advancedOptions')
->attr('style', 'display: none;')
->attr('aria-hidden', 'true');
foreach ($aggregations as $term => $aggregation) {
// keep canonical 'ns' search parameter for namespaces
$param = $term === 'namespace' ? 'ns' : $term;
$this->addCheckboxSelector($searchForm, $aggregation['buckets'], $param);
}
$this->addDateSelector($searchForm);
$this->addLanguageSelector($searchForm);
$searchForm->addTagClose('div');
}
/**
* Filter with checkboxes
*
* @param Form $searchForm
* @param array $aggregations Namespace aggregations
* @param string $param Prefix to use in input names
*/
protected function addCheckboxSelector(Form $searchForm, array $aggregations, $param)
{
if (!empty($aggregations)) {
$pluginSearchConfigs = \action_plugin_elasticsearch_search::getRawPluginSearchConfigs();
$selectorId = !empty($pluginSearchConfigs[$param]['id']) ? $pluginSearchConfigs[$param]['id'] : 'plugin__elasticsearch-' . $param;
$searchForm->addTagOpen('div')
->addClass('toggle')
->id($selectorId)
->attr('aria-haspopup', 'true');
// popup toggler
$searchForm->addTagOpen('div')->addClass('current');
$label = $param === 'ns' ? $this->getLang('nsp') : $pluginSearchConfigs[$param]['label'];
$searchForm->addHTML($label);
$searchForm->addTagClose('div');
// options
$i = 0;
$searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
foreach ($aggregations as $agg) {
$searchForm->addTagOpen('li');
$searchForm->addCheckbox($param . '[]')->val($agg['key'])->id("__$param-" . $i);
$searchForm->addLabel(shorten('', $agg['key'], 25) . ' (' . $agg['doc_count'] . ')', "__$param-" . $i)
->attr('title', $agg['key']);
$searchForm->addTagClose('li');
$i++;
}
$searchForm->addTagClose('ul');
$searchForm->addTagClose('div');
}
}
/**
* Date range filter
*
* @param Form $searchForm
*/
protected function addDateSelector(Form $searchForm)
{
global $lang;
$options = [
'any' => $lang['search_any_time'],
'week' => $lang['search_past_7_days'],
'month' => $lang['search_past_month'],
'year' => $lang['search_past_year'],
];
$searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true');
// popup toggler
$searchForm->addTagOpen('div')->addClass('current');
$searchForm->addHTML($this->getLang('lastmod'));
$searchForm->addTagClose('div');
// options
$i = 0;
$searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
foreach ($options as $opt => $label) {
$searchForm->addTagOpen('li');
$searchForm->addRadioButton('min')->val($opt)->id('__min-' . $i);
$searchForm->addLabel($label, '__min-' . $i);
$searchForm->addTagClose('li');
$i++;
}
$searchForm->addTagClose('ul');
$searchForm->addTagClose('div');
}
/**
* Language filter based on the translation plugin
*
* @param Form $searchForm
*/
protected function addLanguageSelector(Form $searchForm)
{
/** @var helper_plugin_translation $transplugin */
$transplugin = plugin_load('helper', 'translation');
if (!$transplugin) return;
$translations = $transplugin->translations;
if (empty($translations)) return;
$searchForm->addTagOpen('div')
->addClass('toggle')
->id('plugin__elasticsearch-lang')
->attr('aria-haspopup', 'true');
// popup toggler
$searchForm->addTagOpen('div')->addClass('current');
$label = $this->getLang('lang');
$searchForm->addHTML($label);
$searchForm->addTagClose('div');
// options
$i = 0;
$searchForm->addTagOpen('ul')->attr('aria-expanded', 'false');
foreach ($translations as $lang) {
$searchForm->addTagOpen('li');
$searchForm->addCheckbox('lang[]')->val($lang)->id("__lang-" . $i);
$searchForm->addLabel($lang, "__lang-" . $i)
->attr('title', $lang);
$searchForm->addTagClose('li');
$i++;
}
$searchForm->addTagClose('ul');
$searchForm->addTagClose('div');
}
}