|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\CKANRegistry\Model; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldGroup; |
|
6
|
|
|
use SilverStripe\Forms\FieldList; |
|
7
|
|
|
use SilverStripe\ORM\DataObject; |
|
8
|
|
|
use SilverStripe\ORM\FieldType\DBString; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Represents a generic field on a CKAN Resource, e.g. a column in a spreadsheet. |
|
12
|
|
|
* It is intentionally generic, as the resource may not be a tabular one, e.g. geospatal data to be rendered in a map. |
|
13
|
|
|
* |
|
14
|
|
|
* @method Resource Resource |
|
15
|
|
|
*/ |
|
16
|
|
|
class ResourceField extends DataObject |
|
17
|
|
|
{ |
|
18
|
|
|
private static $table_name = 'CKANResourceField'; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
private static $db = [ |
|
|
|
|
|
|
21
|
|
|
'Name' => 'Varchar', |
|
22
|
|
|
'Type' => 'Varchar', |
|
23
|
|
|
'ReadableName' => 'Varchar', |
|
24
|
|
|
'ShowInSummaryView' => 'Boolean', |
|
25
|
|
|
'ShowInDetailView' => 'Boolean', |
|
26
|
|
|
'RemoveDuplicates' => 'Boolean', |
|
27
|
|
|
'Order' => 'Int', |
|
28
|
|
|
'DisplayConditions' => 'Text', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
private static $has_one = [ |
|
|
|
|
|
|
32
|
|
|
'Resource' => Resource::class, |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
private static $summary_fields = [ |
|
|
|
|
|
|
36
|
|
|
'Order', |
|
37
|
|
|
'ReadableName', |
|
38
|
|
|
'Name', |
|
39
|
|
|
'Type', |
|
40
|
|
|
'ShowInSummaryView', |
|
41
|
|
|
'ShowInDetailView', |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
public function getCMSFields() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
47
|
|
|
$fields->removeByName('Name'); |
|
48
|
|
|
|
|
49
|
|
|
$fields->removeByName('Type'); |
|
50
|
|
|
$fields->dataFieldByName('ReadableName') |
|
51
|
|
|
->setAttribute('placeholder', $this->Name); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$summary = $fields->dataFieldByName('ShowInSummaryView'); |
|
54
|
|
|
$detail = $fields->dataFieldByName('ShowInDetailView'); |
|
55
|
|
|
$duplicates = $fields->dataFieldByName('RemoveDuplicates'); |
|
56
|
|
|
|
|
57
|
|
|
$fields->removeByName(['ShowInSummaryView', 'ShowInDetailView', 'RemoveDuplicates',]); |
|
58
|
|
|
$fields->insertBefore(FieldGroup::create('Visibility', [$summary, $detail, $duplicates]), 'Order'); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$fields->removeByName('ResourceID'); |
|
61
|
|
|
}); |
|
62
|
|
|
return parent::getCMSFields(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function onBeforeWrite() |
|
66
|
|
|
{ |
|
67
|
|
|
parent::onBeforeWrite(); |
|
68
|
|
|
|
|
69
|
|
|
if (empty($this->ReadableName) && !empty($this->Name)) { |
|
|
|
|
|
|
70
|
|
|
$this->generateReadableName(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Generate a readable name from the Name |
|
76
|
|
|
* |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function generateReadableName() |
|
80
|
|
|
{ |
|
81
|
|
|
$readableName = str_replace(['_', '-'], ' ', $this->Name); |
|
|
|
|
|
|
82
|
|
|
$readableName = ucfirst(strtolower($readableName)); |
|
83
|
|
|
$this->ReadableName = $readableName; |
|
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|