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
|
|
|
try { |
123
|
|
|
/** @var \SilverStripe\ORM\DataObject $collection_object */ |
124
|
1 |
|
$collection_object = $this->owner->config()->get('managed_object'); |
125
|
1 |
|
$this->collection_object = $collection_object::create(); |
126
|
|
|
} catch (Exception $e) { |
127
|
|
|
trigger_error($e, E_USER_NOTICE); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
2 |
|
public function getCollectionSize() |
137
|
|
|
{ |
138
|
2 |
|
if ($object = $this->owner->config()->page_size) { |
139
|
|
|
return (int) $object; |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
return 10; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param HTTPRequest|null $request |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
1 |
|
public function PaginatedList(HTTPRequest $request = null) |
150
|
|
|
{ |
151
|
1 |
|
if ($request === null) { |
152
|
1 |
|
$request = $this->owner->request; |
153
|
|
|
} |
154
|
1 |
|
$start = ($request->getVar('start')) ? (int) $request->getVar('start') : 0; |
155
|
|
|
|
156
|
1 |
|
$records = PaginatedList::create($this->getCollection(), $this->owner->request); |
157
|
1 |
|
$records->setPageStart($start); |
158
|
1 |
|
$records->setPageLength($this->getCollectionSize()); |
159
|
|
|
|
160
|
|
|
// allow $records to be updated via extension |
161
|
1 |
|
$this->owner->extend('updatePaginatedList', $records); |
162
|
|
|
|
163
|
1 |
|
return $records; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return GroupedList |
168
|
|
|
*/ |
169
|
1 |
|
public function GroupedList() |
170
|
|
|
{ |
171
|
1 |
|
$records = GroupedList::create($this->getCollection()); |
172
|
|
|
|
173
|
|
|
// allow $records to be updated via extension |
174
|
1 |
|
$this->owner->extend('updateGroupedList', $records); |
175
|
|
|
|
176
|
1 |
|
return $records; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return Form |
181
|
|
|
*/ |
182
|
1 |
|
public function CollectionSearchForm() |
183
|
|
|
{ |
184
|
1 |
|
$object = $this->getCollectionObject(); |
185
|
|
|
/** @var HTTPRequest $request */ |
186
|
1 |
|
$request = ($this->owner->request) ? $this->owner->request : $this->owner->parentController->getRequest(); |
187
|
1 |
|
$sort = ($request->getVar('Sort')) ? (string) $request->getVar('Sort') : $object->stat('default_sort'); |
|
|
|
|
188
|
|
|
|
189
|
1 |
|
$context = ($object->hasMethod('getCustomSearchContext')) |
190
|
|
|
? $object->getCustomSearchContext() |
191
|
1 |
|
: $object->getDefaultSearchContext(); |
192
|
1 |
|
$fields = $context->getSearchFields(); |
193
|
|
|
|
194
|
|
|
// add sort field if managed object specs getSortOptions() |
195
|
1 |
|
if ($object->hasMethod('getSortOptions')) { |
196
|
1 |
|
$sortOptions = $object->getSortOptions(); |
197
|
1 |
|
if ($object->stat('default_sort')) { |
|
|
|
|
198
|
|
|
$defaultSort = array(str_replace('"', '', $object->stat('default_sort')) => 'Default'); |
|
|
|
|
199
|
|
|
$sortOptions = array_merge($defaultSort, $sortOptions); |
200
|
|
|
} |
201
|
1 |
|
$fields->add( |
202
|
1 |
|
DropdownField::create('Sort', 'Sort by:', $sortOptions, $sort) |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
1 |
|
$actions = new FieldList( |
207
|
1 |
|
new FormAction($this->owner->Link(), 'Search') |
208
|
|
|
); |
209
|
|
|
|
210
|
1 |
|
if (class_exists(BootstrapForm::class)) { |
|
|
|
|
211
|
|
|
$form = BootstrapForm::create( |
212
|
|
|
$this->owner, |
213
|
|
|
'CollectionSearchForm', |
214
|
|
|
$fields, |
215
|
|
|
$actions |
216
|
|
|
); |
217
|
|
|
} else { |
218
|
1 |
|
$form = Form::create( |
219
|
1 |
|
$this->owner, |
220
|
1 |
|
'CollectionSearchForm', |
221
|
1 |
|
$fields, |
222
|
1 |
|
$actions |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// allow $form to be extended via extension |
227
|
1 |
|
$this->owner->extend('updateCollectionForm', $form); |
228
|
|
|
|
229
|
|
|
$form |
230
|
1 |
|
->setFormMethod('get') |
231
|
1 |
|
->disableSecurityToken() |
232
|
1 |
|
->loadDataFrom($request->getVars()) |
233
|
1 |
|
->setFormAction($this->owner->Link()); |
234
|
|
|
|
235
|
1 |
|
return $form; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|