/
ResourceField.php
123 lines (107 loc) · 4.35 KB
/
ResourceField.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
<?php
namespace SilverStripe\CKANRegistry\Model;
use SilverStripe\CKANRegistry\Forms\ResultConditionsField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\ReadonlyField;
use SilverStripe\ORM\DataObject;
/**
* Represents a generic field on a CKAN Resource, e.g. a column in a spreadsheet.
* It is intentionally generic, as the resource may not be a tabular one, e.g. geospatial data to be rendered in a map.
*
* @property Resource Resource
* @method static ResourceField create()
* @property string OriginalLabel
* @property string Type
* @property string ReadableLabel
* @property bool ShowInResultsView
* @property bool ShowInDetailView
* @property bool RemoveDuplicates
* @property int Position
* @property string DisplayConditions
*/
class ResourceField extends DataObject
{
private static $table_name = 'CKANResourceField';
private static $db = [
'OriginalLabel' => 'Varchar',
'Type' => 'Varchar',
'ReadableLabel' => 'Varchar',
'ShowInResultsView' => 'Boolean',
'ShowInDetailView' => 'Boolean(1)',
'RemoveDuplicates' => 'Boolean',
'Position' => 'Int',
'DisplayConditions' => 'Text',
];
private static $has_one = [
'Resource' => Resource::class,
];
private static $summary_fields = [
'Position',
'ReadableLabel',
'OriginalLabel',
'Type',
'ShowInResultsView',
'ShowInDetailView',
];
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$originalLabel = ReadonlyField::create('OriginalLabel')
->setDescription(_t(
__CLASS__ . '.ORIGINAL_LABEL_DESCRIPTION',
'Title of this field as provided by the CKAN resource'
));
$fields->replaceField('OriginalLabel', $originalLabel);
$readableLabel = $fields->dataFieldByName('ReadableLabel');
$readableLabel->setAttribute('placeholder', $this->OriginalLabel);
$fields->removeByName('Type');
$positionField = NumericField::create('Position')
->setTitle(_t(__CLASS__ . '.ORDER_LABEL', 'Presented order'))
->setDescription(_t(
__CLASS__ . '.ORDER_DENOMINATOR',
'of {count} columns',
['count' => static::get()->filter('ResourceID', $this->ResourceID)->count()]
))
->addExtraClass('ckan-resource__order');
$fields->replaceField('Position', $positionField);
$summary = $fields->dataFieldByName('ShowInResultsView')
->addExtraClass('visibility-options__option');
$detail = $fields->dataFieldByName('ShowInDetailView')
->addExtraClass('visibility-options__option');
$duplicates = $fields->dataFieldByName('RemoveDuplicates')
->setTitle(
_t(__CLASS__ . '.REMOVE_DUPLICATES_LABEL', 'Only show one value if duplicates exist')
)
->addExtraClass('visibility-options__option');
// Present the visibility fields in a group
$fields->removeByName(['ShowInResultsView', 'ShowInDetailView', 'RemoveDuplicates']);
$visibilityOptions = FieldGroup::create('Visibility', [$summary, $detail, $duplicates])
->addExtraClass('visibility-options');
$fields->insertBefore($visibilityOptions, 'Position');
$fields->removeByName('ResourceID');
$fields->replaceField(
'DisplayConditions',
ResultConditionsField::create(
'DisplayConditions',
_t(__CLASS__ . '.RESULT_CONDITIONS', 'Result conditions')
)
);
// See https://github.com/silverstripe/silverstripe-framework/issues/8696
foreach ([$summary, $detail, $readableLabel, $originalLabel] as $field) {
$field->setTitle(ucfirst(strtolower($field->Title() ?? '')));
}
});
return parent::getCMSFields();
}
/**
* Use the readable label for GridField CRUD operation result messages
*
* @return string
*/
public function getTitle()
{
return $this->ReadableLabel;
}
}