1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ilateral\SilverStripe\ModelAdminPlus; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\Form; |
6
|
|
|
use SilverStripe\ORM\ArrayLib; |
7
|
|
|
use SilverStripe\ORM\ArrayList; |
8
|
|
|
use SilverStripe\Core\ClassInfo; |
9
|
|
|
use SilverStripe\Forms\TextField; |
10
|
|
|
use SilverStripe\Admin\ModelAdmin; |
11
|
|
|
use SilverStripe\View\Requirements; |
12
|
|
|
use Colymba\BulkManager\BulkManager; |
13
|
|
|
use SilverStripe\Control\Controller; |
14
|
|
|
use SilverStripe\Core\Config\Config; |
15
|
|
|
use SilverStripe\Forms\DatetimeField; |
16
|
|
|
use SilverStripe\Core\Injector\Injector; |
17
|
|
|
use Colymba\BulkManager\BulkAction\UnlinkHandler; |
18
|
|
|
use SilverStripe\Forms\GridField\GridFieldPaginator; |
19
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
20
|
|
|
use SilverStripe\Forms\GridField\GridFieldSortableHeader; |
21
|
|
|
use SilverStripe\Forms\GridField\GridField_ColumnProvider; |
22
|
|
|
use ilateral\SilverStripe\ModelAdminPlus\AutoCompleteField; |
|
|
|
|
23
|
|
|
use Symbiote\GridFieldExtensions\GridFieldConfigurablePaginator; |
24
|
|
|
use SilverStripe\Forms\GridField\GridFieldFilterHeader as SSGridFieldFilterHeader; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Custom version of model admin that adds extra features |
28
|
|
|
* (such as submitting search results via a POST, saving the query |
29
|
|
|
* as a session and automatic Bulk Editing support) |
30
|
|
|
* |
31
|
|
|
* @author ilateral |
32
|
|
|
* @package ModelAdminPlus |
33
|
|
|
*/ |
34
|
|
|
abstract class ModelAdminPlus extends ModelAdmin |
35
|
|
|
{ |
36
|
|
|
const EXPORT_FIELDS = "export_fields"; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Automatically convert date fields on gridfields |
40
|
|
|
* to use `Date.Nice`. |
41
|
|
|
* |
42
|
|
|
* @var boolean |
43
|
|
|
*/ |
44
|
|
|
private static $auto_convert_dates = true; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Automatically convert DB text fields to AutoComplete fields |
49
|
|
|
* |
50
|
|
|
* @var boolean |
51
|
|
|
*/ |
52
|
|
|
private static $convert_to_autocomplete = true; |
|
|
|
|
53
|
|
|
|
54
|
|
|
private static $allowed_actions = [ |
|
|
|
|
55
|
|
|
"SearchForm" |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* List of currently registered ModelAdminSnippets, that is represented as |
60
|
|
|
* a list of classnames. |
61
|
|
|
* |
62
|
|
|
* These snippets are then setup when ModelAdminPlus is initilised and |
63
|
|
|
* rendered into the ModelAdminPlus content template. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
private static $registered_snippets = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Setup |
71
|
|
|
*/ |
72
|
|
|
public function getSnippets() |
73
|
|
|
{ |
74
|
|
|
$snippets = ArrayList::create(); |
75
|
|
|
|
76
|
|
|
// Setup any model admin plus snippets |
77
|
|
|
foreach ($this->config()->registered_snippets as $snippet) { |
78
|
|
|
$snippet = Injector::inst()->create($snippet); |
79
|
|
|
$snippet->setParent($this); |
80
|
|
|
$snippets->add($snippet); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$snippets = $snippets->sort("Order", "DESC"); |
84
|
|
|
|
85
|
|
|
$this->extend("updateSnippets", $snippets); |
86
|
|
|
|
87
|
|
|
return $snippets; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function init() |
91
|
|
|
{ |
92
|
|
|
parent::init(); |
93
|
|
|
|
94
|
|
|
// Require additional CSS |
95
|
|
|
Requirements::css("i-lateral/silverstripe-modeladminplus:client/dist/css/admin.css"); |
96
|
|
|
|
97
|
|
|
$clear = $this->getRequest()->getVar("clear"); |
98
|
|
|
|
99
|
|
|
if (isset($clear) && $clear == 1) { |
100
|
|
|
$this->clearSearchSession(); |
101
|
|
|
// Remove clear flag |
102
|
|
|
return $this->redirect( |
103
|
|
|
$this->Link( |
104
|
|
|
$this->sanitiseClassName($this->modelClass) |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the default export fields for the current model. |
112
|
|
|
* |
113
|
|
|
* First this checks if there is an `export_fields` config variable set on |
114
|
|
|
* the model class, if not, it reverts to the default behaviour. |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function getExportFields() |
119
|
|
|
{ |
120
|
|
|
$export_fields = Config::inst()->get( |
121
|
|
|
$this->modelClass, |
122
|
|
|
self::EXPORT_FIELDS |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
if (isset($export_fields) && is_array($export_fields)) { |
126
|
|
|
$fields = $export_fields; |
127
|
|
|
} else { |
128
|
|
|
$fields = parent::getExportFields(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->extend("updateExportFields", $fields); |
132
|
|
|
|
133
|
|
|
return $fields; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the name of the session to be useed by this model admin's search |
138
|
|
|
* form. |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function getSearchSessionName() |
143
|
|
|
{ |
144
|
|
|
$curr = $this->sanitiseClassName(self::class); |
145
|
|
|
$model = $this->sanitiseClassName($this->modelClass); |
146
|
|
|
return $curr . "." . $model; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Empty the current search session |
151
|
|
|
* |
152
|
|
|
* @return Session |
|
|
|
|
153
|
|
|
*/ |
154
|
|
|
public function clearSearchSession() |
155
|
|
|
{ |
156
|
|
|
$session = $this->getRequest()->getSession(); |
157
|
|
|
return $session->clear($this->getSearchSessionName()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the current search session |
162
|
|
|
* |
163
|
|
|
* @return Session |
164
|
|
|
*/ |
165
|
|
|
public function getSearchSession() |
166
|
|
|
{ |
167
|
|
|
$session = $this->getRequest()->getSession(); |
168
|
|
|
return $session->get($this->getSearchSessionName()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set some data to a search session. This needs to be an array of |
173
|
|
|
* data (like the data submitted by a form). |
174
|
|
|
* |
175
|
|
|
* @param array $data An array of data to store in the session |
176
|
|
|
* |
177
|
|
|
* @return self |
178
|
|
|
*/ |
179
|
|
|
public function setSearchSession($data) |
180
|
|
|
{ |
181
|
|
|
$session = $this->getRequest()->getSession(); |
182
|
|
|
return $session->set($this->getSearchSessionName(), $data); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the current search results, combined with any saved |
187
|
|
|
* search results and resturn (as an array). |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
public function getSearchData() |
192
|
|
|
{ |
193
|
|
|
$data = $this->getSearchSession(); |
194
|
|
|
|
195
|
|
|
if (!$data || $data && !is_array($data)) { |
|
|
|
|
196
|
|
|
$data = []; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $data; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Add bulk editor to Edit Form |
204
|
|
|
* |
205
|
|
|
* @param int|null $id |
206
|
|
|
* @param FieldList $fields |
|
|
|
|
207
|
|
|
* |
208
|
|
|
* @return Form A Form object |
209
|
|
|
*/ |
210
|
|
|
public function getEditForm($id = null, $fields = null) |
211
|
|
|
{ |
212
|
|
|
$form = parent::getEditForm($id, $fields); |
213
|
|
|
$grid_field = $form |
214
|
|
|
->Fields() |
215
|
|
|
->fieldByName($this->sanitiseClassName($this->modelClass)); |
216
|
|
|
|
217
|
|
|
// Add bulk editing to gridfield |
218
|
|
|
$manager = new BulkManager(); |
219
|
|
|
$manager->removeBulkAction(UnlinkHandler::class); |
220
|
|
|
|
221
|
|
|
$config = $grid_field->getConfig(); |
222
|
|
|
|
223
|
|
|
$config |
224
|
|
|
->removeComponentsByType(GridFieldPaginator::class) |
225
|
|
|
->addComponent($manager) |
226
|
|
|
->addComponent(new GridFieldConfigurablePaginator()); |
227
|
|
|
|
228
|
|
|
// Switch to custom filter header |
229
|
|
|
$config |
230
|
|
|
->removeComponentsByType(SSGridFieldFilterHeader::class) |
231
|
|
|
->addComponent(new GridFieldFilterHeader( |
232
|
|
|
false, |
233
|
|
|
function ($context) { |
234
|
|
|
$this->extend('updateSearchContext', $context); |
235
|
|
|
}, |
236
|
|
|
function ($form) { |
237
|
|
|
$this->extend('updateSearchForm', $form); |
238
|
|
|
} |
239
|
|
|
)); |
240
|
|
|
|
241
|
|
|
if (!$this->showSearchForm || |
242
|
|
|
(is_array($this->showSearchForm) && !in_array($this->modelClass, $this->showSearchForm)) |
243
|
|
|
) { |
244
|
|
|
$config->removeComponentsByType(GridFieldFilterHeader::class); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if ($this->config()->auto_convert_dates) { |
248
|
|
|
GridFieldDateFinder::create($grid_field)->convertDateFields(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $form; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Set the session from the submitted form data (and redirect back) |
256
|
|
|
* |
257
|
|
|
* @param array $data Submitted form |
258
|
|
|
* @param Form $form The current form |
259
|
|
|
* |
260
|
|
|
* @return HTTPResponse |
|
|
|
|
261
|
|
|
*/ |
262
|
|
|
public function search($data, $form) |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
foreach ($data as $key => $value) { |
265
|
|
|
// Ensure we clear any null values |
266
|
|
|
// so they don't mess up the list |
267
|
|
|
if (empty($data[$key])) { |
268
|
|
|
unset($data[$key]); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// Ensure we clear any null values |
272
|
|
|
// so they don't mess up the list |
273
|
|
|
if (strpos($key, "action_") !== false) { |
274
|
|
|
unset($data[$key]); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$this->setSearchSession($data); |
279
|
|
|
|
280
|
|
|
return $this->redirectBack(); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths