-
Notifications
You must be signed in to change notification settings - Fork 824
/
GridFieldDataColumns.php
325 lines (293 loc) · 10.1 KB
/
GridFieldDataColumns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
namespace SilverStripe\Forms\GridField;
use SilverStripe\Core\Convert;
use InvalidArgumentException;
use LogicException;
use SilverStripe\Dev\Deprecation;
use SilverStripe\View\ViewableData;
/**
* @see GridField
*/
class GridFieldDataColumns extends AbstractGridFieldComponent implements GridField_ColumnProvider
{
/**
* @var array
*/
public $fieldCasting = [];
/**
* @var array
*/
public $fieldFormatting = [];
/**
* This is the columns that will be visible
*
* @var array
*/
protected $displayFields = [];
/**
* Modify the list of columns displayed in the table.
* See {@link GridFieldDataColumns->getDisplayFields()} and {@link GridFieldDataColumns}.
*
* @param GridField $gridField
* @param array $columns List reference of all column names. (by reference)
*/
public function augmentColumns($gridField, &$columns)
{
$baseColumns = array_keys($this->getDisplayFields($gridField) ?? []);
foreach ($baseColumns as $col) {
$columns[] = $col;
}
$columns = array_unique($columns ?? []);
}
/**
* Names of all columns which are affected by this component.
*
* @param GridField $gridField
* @return array
*/
public function getColumnsHandled($gridField)
{
return array_keys($this->getDisplayFields($gridField) ?? []);
}
/**
* Override the default behaviour of showing the models summaryFields with
* these fields instead
* Example: array( 'Name' => 'Members name', 'Email' => 'Email address')
*
* @param array $fields
* @return $this
*/
public function setDisplayFields($fields)
{
if (!is_array($fields)) {
throw new InvalidArgumentException(
'Arguments passed to GridFieldDataColumns::setDisplayFields() must be an array'
);
}
$this->displayFields = $fields;
return $this;
}
/**
* Get the DisplayFields
*
* @param GridField $gridField
* @return array
* @see GridFieldDataColumns::setDisplayFields
*/
public function getDisplayFields($gridField)
{
if (!$this->displayFields) {
$modelClass = $gridField->getModelClass();
$singleton = singleton($modelClass);
if (!$singleton->hasMethod('summaryFields')) {
throw new LogicException(
'Cannot dynamically determine columns. Pass the column names to setDisplayFields()'
. " or implement a summaryFields() method on $modelClass"
);
}
return $singleton->summaryFields();
}
return $this->displayFields;
}
/**
* Specify castings with fieldname as the key, and the desired casting as value.
* Example: array("MyCustomDate"=>"Date","MyShortText"=>"Text->FirstSentence")
*
* @param array $casting
* @return $this
*/
public function setFieldCasting($casting)
{
$this->fieldCasting = $casting;
return $this;
}
/**
* @return array
*/
public function getFieldCasting()
{
return $this->fieldCasting;
}
/**
* Specify custom formatting for fields, e.g. to render a link instead of pure text.
*
* Caution: Make sure to escape special php-characters like in a normal php-statement.
* Example: "myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'.
*
* Alternatively, pass a anonymous function, which takes two parameters:
* The value and the original list item.
*
* Formatting is applied after field casting, so if you're modifying the string
* to include further data through custom formatting, ensure it's correctly escaped.
*
* @param array $formatting
* @return $this
*/
public function setFieldFormatting($formatting)
{
$this->fieldFormatting = $formatting;
return $this;
}
/**
* @return array
*/
public function getFieldFormatting()
{
return $this->fieldFormatting;
}
/**
* HTML for the column, content of the <td> element.
*
* @param GridField $gridField
* @param ViewableData $record Record displayed in this row
* @param string $columnName
* @return string HTML for the column. Return NULL to skip.
*/
public function getColumnContent($gridField, $record, $columnName)
{
// Find the data column for the given named column
$columns = $this->getDisplayFields($gridField);
$columnInfo = array_key_exists($columnName, $columns ?? []) ? $columns[$columnName] : null;
// Allow callbacks
if (is_array($columnInfo) && isset($columnInfo['callback'])) {
$method = $columnInfo['callback'];
$value = $method($record, $columnName, $gridField);
// This supports simple FieldName syntax
} else {
$value = $gridField->getDataFieldValue($record, $columnName);
}
// Turn $value, whatever it is, into a HTML embeddable string
$value = $this->castValue($gridField, $columnName, $value);
// Make any formatting tweaks
$value = $this->formatValue($gridField, $record, $columnName, $value);
// Do any final escaping
$value = $this->escapeValue($gridField, $value);
return $value;
}
/**
* Attributes for the element containing the content returned by {@link getColumnContent()}.
*
* @param GridField $gridField
* @param ViewableData $record displayed in this row
* @param string $columnName
* @return array
*/
public function getColumnAttributes($gridField, $record, $columnName)
{
return ['class' => 'col-' . preg_replace('/[^\w]/', '-', $columnName ?? '')];
}
/**
* Additional metadata about the column which can be used by other components,
* e.g. to set a title for a search column header.
*
* @param GridField $gridField
* @param string $column
* @return array Map of arbitrary metadata identifiers to their values.
*/
public function getColumnMetadata($gridField, $column)
{
$columns = $this->getDisplayFields($gridField);
$title = null;
if (is_string($columns[$column])) {
$title = $columns[$column];
} elseif (is_array($columns[$column]) && isset($columns[$column]['title'])) {
$title = $columns[$column]['title'];
}
return [
'title' => $title,
];
}
/**
* Translate a Object.RelationName.ColumnName $columnName into the value that ColumnName returns
*
* @param ViewableData $record
* @param string $columnName
* @return string|null - returns null if it could not found a value
* @deprecated 5.4.0 Will be removed without equivalent functionality to replace it.
*/
protected function getValueFromRelation($record, $columnName)
{
Deprecation::notice('5.4.0', 'Will be removed without equivalent functionality to replace it.');
$fieldNameParts = explode('.', $columnName ?? '');
$tmpItem = clone($record);
for ($idx = 0; $idx < sizeof($fieldNameParts ?? []); $idx++) {
$methodName = $fieldNameParts[$idx];
// Last mmethod call from $columnName return what that method is returning
if ($idx == sizeof($fieldNameParts ?? []) - 1) {
return $tmpItem->XML_val($methodName);
}
// else get the object from this $methodName
$tmpItem = $tmpItem->$methodName();
}
return null;
}
/**
* Casts a field to a string which is safe to insert into HTML
*
* @param GridField $gridField
* @param string $fieldName
* @param string $value
* @return string
*/
protected function castValue($gridField, $fieldName, $value)
{
// If a fieldCasting is specified, we assume the result is safe
if (array_key_exists($fieldName, $this->fieldCasting ?? [])) {
$value = $gridField->getCastedValue($value, $this->fieldCasting[$fieldName]);
} elseif (is_object($value)) {
// If the value is an object, we do one of two things
if (method_exists($value, 'Nice')) {
// If it has a "Nice" method, call that & make sure the result is safe
$value = nl2br(Convert::raw2xml($value->Nice()) ?? '');
} else {
// Otherwise call forTemplate - the result of this should already be safe
$value = $value->forTemplate();
}
} else {
// Otherwise, just treat as a text string & make sure the result is safe
$value = nl2br(Convert::raw2xml($value) ?? '');
}
return $value;
}
/**
*
* @param GridField $gridField
* @param ViewableData $item
* @param string $fieldName
* @param string $value
* @return string
*/
protected function formatValue($gridField, $item, $fieldName, $value)
{
if (!array_key_exists($fieldName, $this->fieldFormatting ?? [])) {
return $value;
}
$spec = $this->fieldFormatting[$fieldName];
if (!is_string($spec) && is_callable($spec)) {
return $spec($value, $item);
} else {
$format = str_replace('$value', "__VAL__", $spec ?? '');
$format = preg_replace('/\$([A-Za-z0-9-_]+)/', '$item->$1', $format ?? '');
$format = str_replace('__VAL__', '$value', $format ?? '');
eval('$value = "' . $format . '";');
return $value;
}
}
/**
* Remove values from a value using FieldEscape setter
*
* @param GridField $gridField
* @param string $value
* @return string
*/
protected function escapeValue($gridField, $value)
{
if (!$escape = $gridField->FieldEscape) {
return $value;
}
foreach ($escape as $search => $replace) {
$value = str_replace($search ?? '', $replace ?? '', $value ?? '');
}
return $value;
}
}