1 | <?php |
||
2 | |||
3 | namespace LeKoala\SimpleSearch; |
||
4 | |||
5 | use SilverStripe\Forms\Form; |
||
6 | use SilverStripe\Core\Extension; |
||
7 | use SilverStripe\Forms\FieldList; |
||
8 | use SilverStripe\Forms\TextField; |
||
9 | use SilverStripe\Forms\FormAction; |
||
10 | use SilverStripe\Control\Director; |
||
11 | use SilverStripe\Core\Config\Config; |
||
12 | use SilverStripe\ErrorPage\ErrorPage; |
||
13 | |||
14 | /** |
||
15 | * A simple alternative to full text search |
||
16 | * |
||
17 | * @property \PageController|\LeKoala\SimpleSearch\SimpleSearchControllerExtension $owner |
||
18 | */ |
||
19 | class SimpleSearchControllerExtension extends Extension |
||
20 | { |
||
21 | private static $allowed_actions = [ |
||
22 | 'SimpleSearchForm', |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * Simple site search form |
||
27 | * |
||
28 | * @return Form |
||
29 | */ |
||
30 | public function SimpleSearchForm() |
||
31 | { |
||
32 | $placeholder = _t('SimpleSearch.SEARCH', 'Search'); |
||
33 | $searchText = ''; |
||
34 | $request = $this->owner->getRequest(); |
||
35 | if ($request) { |
||
36 | $searchText = $request->getVar('q'); |
||
37 | } |
||
38 | $fieldsList = []; |
||
39 | |||
40 | $Search = new TextField('q', false, $searchText); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
41 | $Search->setAttribute('placeholder', $placeholder); |
||
42 | $fieldsList[] = $Search; |
||
43 | |||
44 | $fields = new FieldList($fieldsList); |
||
45 | |||
46 | $actionsList = []; |
||
47 | |||
48 | $Go = new FormAction('doSearch', _t('SimpleSearch.GO', 'Go')); |
||
49 | $Go->setName(''); |
||
50 | $Go->setUseButtonTag(true); |
||
51 | $Go->setButtonContent('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M10,18c1.846,0,3.543-0.635,4.897-1.688l4.396,4.396l1.414-1.414l-4.396-4.396C17.365,13.543,18,11.846,18,10 c0-4.411-3.589-8-8-8s-8,3.589-8,8S5.589,18,10,18z M10,4c3.309,0,6,2.691,6,6s-2.691,6-6,6s-6-2.691-6-6S6.691,4,10,4z"/></svg>'); |
||
52 | $actionsList[] = $Go; |
||
53 | |||
54 | $actions = new FieldList($actionsList); |
||
55 | |||
56 | // Create a simple search form |
||
57 | $searchControllerLink = $this->SearchControllerLink(); |
||
58 | $form = Form::create($this->owner, __FUNCTION__, $fields, $actions); |
||
59 | $form->setFormMethod('GET'); |
||
60 | $form->setFormAction($searchControllerLink); |
||
61 | $form->disableSecurityToken(); |
||
62 | |||
63 | return $form; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | public function SearchControllerLink() |
||
70 | { |
||
71 | $directorRules = Config::inst()->get(Director::class, 'rules'); |
||
72 | $searchControllerLink = '/search/'; |
||
73 | foreach ($directorRules as $segment => $controller) { |
||
74 | if ($controller == SearchController::class) { |
||
75 | $searchControllerLink = '/' . $segment . '/'; |
||
76 | } |
||
77 | } |
||
78 | return $searchControllerLink; |
||
79 | } |
||
80 | } |
||
81 |