|
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
|
|
|
if (strpos($sort, ',') === false) { |
|
80
|
|
|
$sortParts = explode(' ', $sort); |
|
81
|
|
|
$collection = $context->getResults($searchCriteria)->sort($sortParts[0], $sortParts[1]); |
|
82
|
|
|
} else { |
|
83
|
|
|
// multiple orders are given |
|
84
|
|
|
$sortParts = []; |
|
85
|
|
|
$sortArgs = explode(',', $sort); |
|
86
|
|
|
foreach ($sortArgs as $arg) { |
|
87
|
|
|
$arg = trim($arg); |
|
88
|
|
|
if (strpos($arg, ' ') === false) { |
|
89
|
|
|
$sortParts[$arg] = 'ASC'; |
|
90
|
|
|
} else { |
|
91
|
|
|
$part = explode(' ', $arg); |
|
92
|
|
|
$sortParts[$part[0]] = $part[1]; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
$collection = $context->getResults($searchCriteria)->sort($sortParts); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// allow $collection to be updated via extension |
|
100
|
1 |
|
$this->owner->extend('updateCollectionItems', $collection, $searchCriteria); |
|
101
|
|
|
|
|
102
|
1 |
|
$this->collection = $collection; |
|
103
|
1 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return string|\SilverStripe\ORM\DataObject |
|
108
|
|
|
*/ |
|
109
|
3 |
|
public function getCollectionObject() |
|
110
|
|
|
{ |
|
111
|
3 |
|
if (!$this->collection_object) { |
|
112
|
1 |
|
$this->setCollectionObject(); |
|
113
|
|
|
} |
|
114
|
3 |
|
return $this->collection_object; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return $this |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function setCollectionObject() |
|
121
|
|
|
{ |
|
122
|
1 |
|
$this->owner->extend('updateCollectionObject', $this->collection_object); |
|
123
|
|
|
|
|
124
|
1 |
|
if (!$this->collection_object) { |
|
125
|
|
|
try { |
|
126
|
|
|
/** @var \SilverStripe\ORM\DataObject $collection_object */ |
|
127
|
1 |
|
$collection_object = $this->owner->config()->get('managed_object'); |
|
128
|
1 |
|
$this->collection_object = $collection_object::create(); |
|
129
|
|
|
} catch (Exception $e) { |
|
130
|
|
|
trigger_error($e, E_USER_NOTICE); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return int |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function getCollectionSize() |
|
141
|
|
|
{ |
|
142
|
2 |
|
if ($object = $this->owner->config()->page_size) { |
|
143
|
|
|
return (int) $object; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
2 |
|
return 10; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param HTTPRequest|null $request |
|
151
|
|
|
* @return mixed |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function PaginatedList(HTTPRequest $request = null) |
|
154
|
|
|
{ |
|
155
|
1 |
|
if ($request === null) { |
|
156
|
1 |
|
$request = $this->owner->request; |
|
157
|
|
|
} |
|
158
|
1 |
|
$start = ($request->getVar('start')) ? (int) $request->getVar('start') : 0; |
|
159
|
|
|
|
|
160
|
1 |
|
$records = PaginatedList::create($this->getCollection(), $this->owner->request); |
|
161
|
1 |
|
$records->setPageStart($start); |
|
162
|
1 |
|
$records->setPageLength($this->getCollectionSize()); |
|
163
|
|
|
|
|
164
|
|
|
// allow $records to be updated via extension |
|
165
|
1 |
|
$this->owner->extend('updatePaginatedList', $records); |
|
166
|
|
|
|
|
167
|
1 |
|
return $records; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return GroupedList |
|
172
|
|
|
*/ |
|
173
|
1 |
|
public function GroupedList() |
|
174
|
|
|
{ |
|
175
|
1 |
|
$records = GroupedList::create($this->getCollection()); |
|
176
|
|
|
|
|
177
|
|
|
// allow $records to be updated via extension |
|
178
|
1 |
|
$this->owner->extend('updateGroupedList', $records); |
|
179
|
|
|
|
|
180
|
1 |
|
return $records; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return Form |
|
185
|
|
|
*/ |
|
186
|
1 |
|
public function CollectionSearchForm() |
|
187
|
|
|
{ |
|
188
|
1 |
|
$object = $this->getCollectionObject(); |
|
189
|
|
|
/** @var HTTPRequest $request */ |
|
190
|
1 |
|
$request = ($this->owner->request) ? $this->owner->request : $this->owner->parentController->getRequest(); |
|
191
|
1 |
|
$sort = ($request->getVar('Sort')) ? (string) $request->getVar('Sort') : $object->stat('default_sort'); |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
1 |
|
$context = ($object->hasMethod('getCustomSearchContext')) |
|
194
|
|
|
? $object->getCustomSearchContext() |
|
195
|
1 |
|
: $object->getDefaultSearchContext(); |
|
196
|
1 |
|
$fields = $context->getSearchFields(); |
|
197
|
|
|
|
|
198
|
|
|
// add sort field if managed object specs getSortOptions() |
|
199
|
1 |
|
if ($object->hasMethod('getSortOptions')) { |
|
200
|
1 |
|
$sortOptions = $object->getSortOptions(); |
|
201
|
1 |
|
if ($object->stat('default_sort')) { |
|
|
|
|
|
|
202
|
|
|
$defaultSort = array(str_replace('"', '', $object->stat('default_sort')) => 'Default'); |
|
|
|
|
|
|
203
|
|
|
$sortOptions = array_merge($defaultSort, $sortOptions); |
|
204
|
|
|
} |
|
205
|
1 |
|
$fields->add( |
|
206
|
1 |
|
DropdownField::create('Sort', 'Sort by:', $sortOptions, $sort) |
|
207
|
|
|
); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
1 |
|
$actions = new FieldList( |
|
211
|
1 |
|
new FormAction($this->owner->Link(), 'Search') |
|
212
|
|
|
); |
|
213
|
|
|
|
|
214
|
1 |
|
if (class_exists(BootstrapForm::class)) { |
|
|
|
|
|
|
215
|
|
|
$form = BootstrapForm::create( |
|
216
|
|
|
$this->owner, |
|
217
|
|
|
'CollectionSearchForm', |
|
218
|
|
|
$fields, |
|
219
|
|
|
$actions |
|
220
|
|
|
); |
|
221
|
|
|
} else { |
|
222
|
1 |
|
$form = Form::create( |
|
223
|
1 |
|
$this->owner, |
|
224
|
1 |
|
'CollectionSearchForm', |
|
225
|
1 |
|
$fields, |
|
226
|
1 |
|
$actions |
|
227
|
|
|
); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// allow $form to be extended via extension |
|
231
|
1 |
|
$this->owner->extend('updateCollectionForm', $form); |
|
232
|
|
|
|
|
233
|
|
|
$form |
|
234
|
1 |
|
->setFormMethod('get') |
|
235
|
1 |
|
->disableSecurityToken() |
|
236
|
1 |
|
->loadDataFrom($request->getVars()) |
|
237
|
1 |
|
->setFormAction($this->owner->Link()); |
|
238
|
|
|
|
|
239
|
1 |
|
return $form; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|