forked from jaedb/Search
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSearchControllerExtension.php
More file actions
executable file
·255 lines (202 loc) · 7.35 KB
/
SearchControllerExtension.php
File metadata and controls
executable file
·255 lines (202 loc) · 7.35 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace PlasticStudio\Search;
use Exception;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\CheckboxSetField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Core\Config\Config;
class SearchControllerExtension extends Extension {
private static $allowed_actions = array(
'BasicSearchForm',
'AdvancedSearchForm'
);
/**
* Default form (ie in menus and footers)
*
* @return Form
**/
public function BasicSearchForm(){
// create our search form fields
$fields = FieldList::create();
$placeholder_text = 'Keywords';
if (Config::inst()->get('PlasticStudio\Search\SearchPageController', 'search_form_placeholder_text')) {
$placeholder_text = Config::inst()->get('PlasticStudio\Search\SearchPageController', 'search_form_placeholder_text');
}
$fields->push( TextField::create('query','',SearchPageController::get_query())->addExtraClass('query')->setAttribute('placeholder', $placeholder_text) );
// create the form actions (we only need a submit button)
$submit_button_text = 'Search';
if (Config::inst()->get('PlasticStudio\Search\SearchPageController', 'submit_button_text')) {
$submit_button_text = Config::inst()->get('PlasticStudio\Search\SearchPageController', 'submit_button_text');
}
// don't do action here, set below for 404 error page fix
// fix breaks pagination, reinstating
$actions = FieldList::create(
FormAction::create("doBasicSearchForm")->setTitle($submit_button_text)
);
// now build the actual form object
$form = Form::create(
$controller = $this->owner,
$name = 'BasicSearchForm',
$fields = $fields,
$actions = $actions
)->addExtraClass('search-form')
->disableSecurityToken();
// $page = SearchPage::get()->first();
// $form->setFormAction($page->Link());
return $form;
}
/**
* Build the advanced search form (ie results page)
*
* @return Form
**/
public function AdvancedSearchForm(){
// create our search form fields
$fields = FieldList::create();
// search keywords
$placeholder_text = 'Keywords';
if (Config::inst()->get('PlasticStudio\Search\SearchPageController', 'search_form_placeholder_text')) {
$placeholder_text = Config::inst()->get('PlasticStudio\Search\SearchPageController', 'search_form_placeholder_text');
}
$fields->push( TextField::create('query','',SearchPageController::get_query())->addExtraClass('query')->setAttribute('placeholder', $placeholder_text) );
// classes to search
if ($types_available = SearchPageController::get_types_available()){
if ($types = SearchPageController::get_types()){
$value = $types;
$select_all_types = false;
} else {
$value = [];
$select_all_types = true;
}
// Construct the array of options for the field
foreach ($types_available as $key => $type){
$source[$key] = $type['Label'];
if ($select_all_types){
$value[] = $key;
}
}
$fields->push(CheckboxSetField::create('types', 'Types', $source, $value));
}
// Filters that we need to map
if ($filters_available = SearchPageController::get_filters_available()){
// Grab our already-set filters
$filters = SearchPageController::get_filters();
foreach ($filters_available as $key => $filter){
// Identify any existing values (ie if we're on the results page with values already set)
$value = null;
if (isset($filters[$key])){
$value = $filters[$key];
}
switch ($filter['Structure']){
/**
* Plain column value field
**/
case 'db':
if (isset($filter['Field'])){
$field = $filter['Field'];
} else {
$field = "SilverStripe\Forms\TextField";
}
$fields->push($field::create($key, $filter['Label'], $value));
break;
/**
* Simple relation field
**/
case 'has_one':
$source = $filter['ClassName']::get();
// We need to apply a filter to the displayed relational options (based on config)
if (isset($filter['Filters'])){
$source = $source->filter($filter['Filters']);
}
$empty_string = 'All '.$filter['Label'];
if (substr($empty_string, -1) != 's'){
$empty_string.= 's';
}
$fields->push(DropdownField::create($key, $filter['Label'], $source->map('ID','Title','All'), $value)->setEmptyString($empty_string));
break;
/**
* Complex relational field
**/
case 'many_many':
$source = $filter['ClassName']::get();
// We need to apply a filter to the displayed relational options (based on config)
if (isset($filter['Filters'])){
$source = $source->filter($filter['Filters']);
}
if ($value == null) {
$default = '';
} else {
$default = explode(',', $value);
}
$fields->push(CheckboxSetField::create($key, $filter['Label'], $source->map('ID','Title','All'), $default)->addExtraClass('chosen-select'));
break;
}
}
}
// Sorting rules
if ($sorts_available = SearchPageController::get_sorts_available()){
// Default to the first option
$source = [];
// Construct the array of options for the field
foreach ($sorts_available as $key => $type){
$source[$key] = $type['Label'];
}
$fields->push(DropdownField::create('sort', 'Sort', $source, SearchPageController::get_mapped_sort()['Key']));
}
// create the form actions (we only need a submit button)
$submit_button_text = 'Search';
if (Config::inst()->get('PlasticStudio\Search\SearchPageController', 'submit_button_text')) {
$submit_button_text = Config::inst()->get('PlasticStudio\Search\SearchPageController', 'submit_button_text');
}
$actions = FieldList::create(
FormAction::create("doBasicSearchForm")->setTitle($submit_button_text)
);
// now build the actual form object
$form = Form::create(
$controller = $this->owner,
$name = 'AdvancedSearchForm',
$fields = $fields,
$actions = $actions
)->addExtraClass('search-form advanced-search-form')
->disableSecurityToken();
return $form;
}
/**
* Process the submitted search form. All we're really doing is redirecting to our structured URL
* @param $data = array (post data)
* @param $form = obj (the originating BasicSearchForm object)
* @return HTTPRedirect
**/
public function doBasicSearchForm($data, $form){
$page = SearchPage::get()->first();
if (!$page){
throw new Exception("The required SearchPage record does not exist");
die();
}
$filters_available = SearchPageController::get_filters_available();
$vars = '';
foreach ($data as $key => $value){
// Make sure we only carry configured filters
// This begins to protect us against malicious use :-)
if ((isset($filters_available[$key]) || $key == 'query' || $key == 'types' || $key == 'sort') && $value && $value !== ''){
// Concat into a URL string
if ($vars == ''){
$vars .= '?'.$key.'=';
} else {
$vars .= '&'.$key.'=';
}
// And merge any arrays into comma-separated values
if (is_array($value)){
$vars .= join(',',$value);
} else {
$vars .= $value;
}
}
}
return $this->owner->redirect($page->Link().$vars);
}
}