1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @author: nicolaas[at]sunnysideup.co.nz |
5
|
|
|
* @description: adds a SearchForm to all Page Controller classes. |
6
|
|
|
* so that you can add a search form to all pages |
7
|
|
|
* the page is submitted to the SearchPlusPage. |
8
|
|
|
* This is different from a "standard" search form, |
9
|
|
|
* which is always submitted to the page it was submitted from. |
10
|
|
|
* |
11
|
|
|
* |
12
|
|
|
* |
13
|
|
|
**/ |
14
|
|
|
|
15
|
|
|
class SearchPlusSearchForm extends Extension |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
public function SearchPlusForm($name = "SearchForm", $fieldName = "Search", $fieldLabel = '') |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$action = $this->getRequest()->param("Action"); |
|
|
|
|
20
|
|
|
$page = SearchPlusPage::get()->first(); |
21
|
|
|
if ($page) { |
22
|
|
|
//!in_array($action, array("login", "logout")) && |
|
|
|
|
23
|
|
|
if (!$fieldLabel) { |
24
|
|
|
if (isset($_REQUEST[$fieldName])) { |
25
|
|
|
$searchText = $_REQUEST[$fieldName]; |
26
|
|
|
//we set $_REQUEST["Search"] below because it is also used by things like Text::ContextSummary |
27
|
|
|
$_REQUEST["Search"] = $_REQUEST[$fieldName]; |
28
|
|
|
} elseif (isset($_REQUEST["Search"])) { |
29
|
|
|
$searchText = $_REQUEST["Search"]; |
30
|
|
|
} else { |
31
|
|
|
$searchText = 'Search'; |
32
|
|
|
} |
33
|
|
|
} else { |
34
|
|
|
$searchText = ''; |
35
|
|
|
} |
36
|
|
|
$field = new TextField($fieldName, $fieldLabel, $searchText); |
37
|
|
|
$fields = new FieldList($field); |
38
|
|
|
$actions = new FieldList( |
39
|
|
|
new FormAction('results', 'Search') |
40
|
|
|
); |
41
|
|
|
$form = new SearchForm($this, $name, $fields, $actions); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$form->setFormAction($page->Link()."results/"); |
44
|
|
|
$form->setPageLength(Config::inst()->get("SearchPlusPage", "result_length")); |
45
|
|
|
$form->unsetValidator(); |
46
|
|
|
return $form; |
47
|
|
|
} elseif (!$page) { |
48
|
|
|
user_error("You need to create a SearchPlusPage to have a search box", E_USER_NOTICE); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.