|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: gordon |
|
6
|
|
|
* Date: 25/3/2561 |
|
7
|
|
|
* Time: 17:01 น. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Suilven\FreeTextSearch\Page; |
|
11
|
|
|
|
|
12
|
|
|
use SilverStripe\Forms\DropdownField; |
|
13
|
|
|
use SilverStripe\Forms\NumericField; |
|
14
|
|
|
use SilverStripe\Forms\TextField; |
|
15
|
|
|
use Suilven\FreeTextSearch\Container\SearchResults; |
|
16
|
|
|
use Suilven\FreeTextSearch\Indexes; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class SearchPage |
|
20
|
|
|
* |
|
21
|
|
|
* @package Suilven\FreeTextSearch\Page |
|
22
|
|
|
* @property string $IndexToSearch - the name of the index to search, defaults to SiteTree |
|
23
|
|
|
* @property int $PageSize the number of results to show on each page |
|
24
|
|
|
* @property string $PageLandingMode - the mode to render when landing on the search page |
|
25
|
|
|
* @property string $ShowTagCloudFor - show a tag cloud |
|
26
|
|
|
*/ |
|
27
|
|
|
class SearchPage extends \Page |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var \Suilven\FreeTextSearch\Container\SearchResults */ |
|
30
|
|
|
private $searchResults; |
|
31
|
|
|
|
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
private static $table_name = 'SearchPage'; |
|
34
|
|
|
|
|
35
|
|
|
/** @var array database fields */ |
|
36
|
|
|
private static $db = [ |
|
37
|
|
|
// an index filter for this page, not user selectable. Use case here is to restrict to likes of searching |
|
38
|
|
|
// within a specific blog only |
|
39
|
|
|
'IndexToSearch' => 'Varchar(255)', |
|
40
|
|
|
|
|
41
|
|
|
// page size |
|
42
|
|
|
'PageSize' => 'Int', |
|
43
|
|
|
|
|
44
|
|
|
// show all results if the search page has facets (optionally) |
|
45
|
|
|
'ShowTagCloudFor' => 'Varchar', |
|
46
|
|
|
|
|
47
|
|
|
'MaximumNumberOfFacets' => 'Int', |
|
48
|
|
|
|
|
49
|
|
|
'PageLandingMode' => "Enum('DoNothing, ShowResultsForStar,ShowTagCloud', 'DoNothing')", |
|
50
|
|
|
|
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
/** @var array<string,int|bool|string|float> */ |
|
54
|
|
|
private static $defaults = [ |
|
55
|
|
|
'IndexToSearch' => 'sitetree', |
|
56
|
|
|
'ShowInsearch' => false, |
|
57
|
|
|
|
|
58
|
|
|
// same as Laravel |
|
59
|
|
|
'PageSize' => 15, |
|
60
|
|
|
|
|
61
|
|
|
'MaximumNumberOfFacets' => 100, |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Accessor to the search results object |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getSearchResults(): SearchResults |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->searchResults; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
public function setSearchResults(SearchResults $newSearchResults): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->searchResults = $newSearchResults; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get the fields to facet on |
|
81
|
|
|
* |
|
82
|
|
|
* @return array<string> |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getFacetFields(): array |
|
85
|
|
|
{ |
|
86
|
|
|
$indexesService = new Indexes(); |
|
87
|
|
|
|
|
88
|
|
|
return $indexesService->getFacetFields($this->IndexToSearch); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get the has many fields |
|
94
|
|
|
* |
|
95
|
|
|
* @return array<string> |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getHasManyFields(): array |
|
98
|
|
|
{ |
|
99
|
|
|
$indexesService = new Indexes(); |
|
100
|
|
|
|
|
101
|
|
|
$fieldDetails = $indexesService->getHasManyFields($this->IndexToSearch); |
|
102
|
|
|
$result = []; |
|
103
|
|
|
foreach ($fieldDetails as $detail) { |
|
104
|
|
|
$result[] = $detail['name']; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $result; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
public function getCMSFields(): \SilverStripe\Forms\FieldList |
|
112
|
|
|
{ |
|
113
|
|
|
$fields = parent::getCMSFields(); |
|
114
|
|
|
$indexesService = new Indexes(); |
|
115
|
|
|
$indexes = $indexesService->getIndexes(); |
|
116
|
|
|
$indexNames = []; |
|
117
|
|
|
foreach ($indexes as $index) { |
|
118
|
|
|
$indexNames[$index->getName()] = $index->getName(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$fields->addFieldToTab('Root.Index', DropdownField::create( |
|
122
|
|
|
'IndexToSearch', |
|
123
|
|
|
'Index to Search', |
|
124
|
|
|
$indexNames |
|
125
|
|
|
)); |
|
126
|
|
|
|
|
127
|
|
|
$fields->addFieldToTab('Root.Index', NumericField::create('PageSize', 'Number of Results Per Page')); |
|
128
|
|
|
$fields->addFieldToTab('Root.Index', NumericField::create('MaximumNumberOfFacets', 'Number of Facets To Show')); |
|
129
|
|
|
|
|
130
|
|
|
$ddf = DropdownField::create('PageLandingMode', 'PageLandingMode', \singleton($this->getClassName())-> |
|
131
|
|
|
dbObject('PageLandingMode')->enumValues()); |
|
132
|
|
|
$fields->addFieldToTab('Root.Index', $ddf); |
|
133
|
|
|
|
|
134
|
|
|
$fields->addFieldToTab('Root.Index', TextField::create( |
|
135
|
|
|
'ShowTagCloudFor', |
|
136
|
|
|
'Show a tag cloud for the named facet' |
|
137
|
|
|
)); |
|
138
|
|
|
|
|
139
|
|
|
return $fields; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|