1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Collection; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\Exception; |
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
7
|
|
|
use SilverStripe\Core\Extension; |
8
|
|
|
use SilverStripe\Forms\DropdownField; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\Forms\Form; |
11
|
|
|
use SilverStripe\Forms\FormAction; |
12
|
|
|
use SilverStripe\ORM\ArrayList; |
13
|
|
|
use SilverStripe\ORM\DataList; |
14
|
|
|
use SilverStripe\ORM\GroupedList; |
15
|
|
|
use SilverStripe\ORM\PaginatedList; |
16
|
|
|
|
17
|
|
|
class CollectionExtension extends Extension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private static $allowed_actions = array( |
|
|
|
|
23
|
|
|
'CollectionSearchForm', |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var DataList|ArrayList |
28
|
|
|
*/ |
29
|
|
|
private $collection; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var |
33
|
|
|
*/ |
34
|
|
|
private $collection_object; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return ArrayList|DataList |
38
|
|
|
*/ |
39
|
3 |
|
public function getCollection() |
40
|
|
|
{ |
41
|
3 |
|
if (!$this->collection) { |
42
|
1 |
|
$this->setCollection($this->owner->request); |
43
|
|
|
} |
44
|
3 |
|
return $this->collection; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param HTTPRequest|null $request |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
1 |
|
public function setCollection(HTTPRequest $request = null) |
52
|
|
|
{ |
53
|
1 |
|
if ($request === null) { |
54
|
|
|
$request = $this->owner->request; |
55
|
|
|
} |
56
|
1 |
|
$searchCriteria = $request->requestVars(); |
57
|
|
|
|
58
|
|
|
// allow $searchCriteria to be updated via extension |
59
|
1 |
|
$this->owner->extend('updateCollectionFilters', $searchCriteria); |
60
|
|
|
|
61
|
1 |
|
$object = $this->getCollectionObject(); |
62
|
|
|
|
63
|
1 |
|
$context = ($object->hasMethod('getCustomSearchContext')) |
64
|
|
|
? $object->getCustomSearchContext() |
65
|
1 |
|
: $object->getDefaultSearchContext(); |
66
|
|
|
|
67
|
1 |
|
$sort = ($request->getVar('Sort')) |
68
|
|
|
? (string) $request->getVar('Sort') |
69
|
1 |
|
: $object->stat('default_sort'); |
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
// check if the sort has an order (ASC or DESC) |
73
|
|
|
// prevents query errors when sorting on relations with an order ('Location.Title DESC') |
74
|
|
|
// no order |
75
|
1 |
|
if (strpos($sort, ' ') === false) { |
76
|
1 |
|
$collection = $context->getResults($searchCriteria)->sort($sort); |
77
|
|
|
// order is given |
78
|
|
|
} else { |
79
|
|
|
$sortParts = explode(' ', $sort); |
80
|
|
|
$collection = $context->getResults($searchCriteria)->sort($sortParts[0], $sortParts[1]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// allow $collection to be updated via extension |
84
|
1 |
|
$this->owner->extend('updateCollectionItems', $collection, $searchCriteria); |
85
|
|
|
|
86
|
1 |
|
$this->collection = $collection; |
87
|
1 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string|\SilverStripe\ORM\DataObject |
92
|
|
|
*/ |
93
|
3 |
|
public function getCollectionObject() |
94
|
|
|
{ |
95
|
3 |
|
if (!$this->collection_object) { |
96
|
1 |
|
$this->setCollectionObject(); |
97
|
|
|
} |
98
|
3 |
|
return $this->collection_object; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
1 |
|
public function setCollectionObject() |
105
|
|
|
{ |
106
|
|
|
try { |
107
|
|
|
/** @var \SilverStripe\ORM\DataObject $collection_object */ |
108
|
1 |
|
$collection_object = $this->owner->config()->get('managed_object'); |
109
|
1 |
|
$this->collection_object = $collection_object::create(); |
110
|
|
|
} catch (Exception $e) { |
111
|
|
|
trigger_error($e, E_USER_NOTICE); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
2 |
|
public function getCollectionSize() |
121
|
|
|
{ |
122
|
2 |
|
if ($object = $this->owner->config()->page_size) { |
123
|
|
|
return (int) $object; |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
return 10; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param HTTPRequest|null $request |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
1 |
|
public function PaginatedList(HTTPRequest $request = null) |
134
|
|
|
{ |
135
|
1 |
|
if ($request === null) { |
136
|
1 |
|
$request = $this->owner->request; |
137
|
|
|
} |
138
|
1 |
|
$start = ($request->getVar('start')) ? (int) $request->getVar('start') : 0; |
139
|
|
|
|
140
|
1 |
|
$records = PaginatedList::create($this->getCollection(), $this->owner->request); |
141
|
1 |
|
$records->setPageStart($start); |
142
|
1 |
|
$records->setPageLength($this->getCollectionSize()); |
143
|
|
|
|
144
|
|
|
// allow $records to be updated via extension |
145
|
1 |
|
$this->owner->extend('updatePaginatedList', $records); |
146
|
|
|
|
147
|
1 |
|
return $records; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return GroupedList |
152
|
|
|
*/ |
153
|
1 |
|
public function GroupedList() |
154
|
|
|
{ |
155
|
1 |
|
$records = GroupedList::create($this->getCollection()); |
156
|
|
|
|
157
|
|
|
// allow $records to be updated via extension |
158
|
1 |
|
$this->owner->extend('updateGroupedList', $records); |
159
|
|
|
|
160
|
1 |
|
return $records; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return Form |
165
|
|
|
*/ |
166
|
1 |
|
public function CollectionSearchForm() |
167
|
|
|
{ |
168
|
1 |
|
$object = $this->getCollectionObject(); |
169
|
|
|
/** @var HTTPRequest $request */ |
170
|
1 |
|
$request = ($this->owner->request) ? $this->owner->request : $this->owner->parentController->getRequest(); |
171
|
1 |
|
$sort = ($request->getVar('Sort')) ? (string) $request->getVar('Sort') : $object->stat('default_sort'); |
|
|
|
|
172
|
|
|
|
173
|
1 |
|
$context = ($object->hasMethod('getCustomSearchContext')) |
174
|
|
|
? $object->getCustomSearchContext() |
175
|
1 |
|
: $object->getDefaultSearchContext(); |
176
|
1 |
|
$fields = $context->getSearchFields(); |
177
|
|
|
|
178
|
|
|
// add sort field if managed object specs getSortOptions() |
179
|
1 |
|
if ($object->hasMethod('getSortOptions')) { |
180
|
1 |
|
$sortOptions = $object->getSortOptions(); |
181
|
1 |
|
if ($object->stat('default_sort')) { |
|
|
|
|
182
|
|
|
$defaultSort = array(str_replace('"', '', $object->stat('default_sort')) => 'Default'); |
|
|
|
|
183
|
|
|
$sortOptions = array_merge($defaultSort, $sortOptions); |
184
|
|
|
} |
185
|
1 |
|
$fields->add( |
186
|
1 |
|
DropdownField::create('Sort', 'Sort by:', $sortOptions, $sort) |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
$actions = new FieldList( |
191
|
1 |
|
new FormAction($this->owner->Link(), 'Search') |
192
|
|
|
); |
193
|
|
|
|
194
|
1 |
|
if (class_exists(BootstrapForm::class)) { |
|
|
|
|
195
|
|
|
$form = BootstrapForm::create( |
196
|
|
|
$this->owner, |
197
|
|
|
'CollectionSearchForm', |
198
|
|
|
$fields, |
199
|
|
|
$actions |
200
|
|
|
); |
201
|
|
|
} else { |
202
|
1 |
|
$form = Form::create( |
203
|
1 |
|
$this->owner, |
204
|
1 |
|
'CollectionSearchForm', |
205
|
1 |
|
$fields, |
206
|
1 |
|
$actions |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// allow $form to be extended via extension |
211
|
1 |
|
$this->owner->extend('updateCollectionForm', $form); |
212
|
|
|
|
213
|
|
|
$form |
214
|
1 |
|
->setFormMethod('get') |
215
|
1 |
|
->disableSecurityToken() |
216
|
1 |
|
->loadDataFrom($request->getVars()) |
217
|
1 |
|
->setFormAction($this->owner->Link()); |
218
|
|
|
|
219
|
1 |
|
return $form; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|