1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\CKANRegistry\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CKANRegistry\Page\CKANRegistryPage; |
6
|
|
|
use SilverStripe\CKANRegistry\Service\ResourcePopulatorInterface; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
use SilverStripe\ORM\HasManyList; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A CKAN Resource that belongs to a DataSet/Package, as to be accessed via the CKAN API. |
13
|
|
|
* |
14
|
|
|
* @property string Name |
15
|
|
|
* @property string ResourceName |
16
|
|
|
* @property string Endpoint |
17
|
|
|
* @property string DataSet |
18
|
|
|
* @property string Identifier |
19
|
|
|
* @method HasManyList Fields |
20
|
|
|
* @method HasManyList Filters |
21
|
|
|
*/ |
22
|
|
|
class Resource extends DataObject |
23
|
|
|
{ |
24
|
|
|
private static $table_name = 'CKANResource'; |
|
|
|
|
25
|
|
|
|
26
|
|
|
private static $db = [ |
|
|
|
|
27
|
|
|
'Name' => 'Varchar', |
28
|
|
|
'ResourceName' => 'Varchar', |
29
|
|
|
'Endpoint' => 'Varchar', |
30
|
|
|
'DataSet' => 'Varchar', |
31
|
|
|
'Identifier' => 'Varchar', |
32
|
|
|
'ItemsPerPage' => 'Int', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
private static $belongs_to = [ |
|
|
|
|
36
|
|
|
'Page' => CKANRegistryPage::class . '.DataResource', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
private static $has_many = [ |
|
|
|
|
40
|
|
|
'Fields' => ResourceField::class, |
41
|
|
|
'Filters' => ResourceFilter::class, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
private static $defaults = [ |
|
|
|
|
45
|
|
|
'ItemsPerPage' => 30, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Whenever the CKAN resource identifier is changed we should clear any existing field and filter configurations |
50
|
|
|
* |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function onAfterWrite() |
54
|
|
|
{ |
55
|
|
|
if ($this->isChanged('Identifier')) { |
56
|
|
|
$this->Fields()->each(function (ResourceField $field) { |
57
|
|
|
$field->delete(); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
/** @var ResourcePopulatorInterface $populator */ |
61
|
|
|
$populator = Injector::inst()->get(ResourcePopulatorInterface::class); |
62
|
|
|
$populator->populateFields($this); |
63
|
|
|
|
64
|
|
|
// Remove the existing filters and add a default text entry to search all ResourceFields |
65
|
|
|
$this->Filters() |
66
|
|
|
->each(function (ResourceFilter $filter) { |
67
|
|
|
$filter->delete(); |
68
|
|
|
}) |
69
|
|
|
->add(ResourceFilter::create()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
parent::onAfterWrite(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Whenever the CKAN resource identifier is changed, populate a new set of metadata for the new resource |
77
|
|
|
* |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function onBeforeWrite() |
81
|
|
|
{ |
82
|
|
|
if ($this->isChanged('Identifier')) { |
83
|
|
|
/** @var ResourcePopulatorInterface $populator */ |
84
|
|
|
$populator = Injector::inst()->get(ResourcePopulatorInterface::class); |
85
|
|
|
$populator->populateMetadata($this); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
parent::onBeforeWrite(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Loads model data encapsulated as JSON in order to power front end technologies used to render that |
93
|
|
|
* data. Includes critical info such as the CKAN site to query (e.g. which domain, datastore, etc.) |
94
|
|
|
* but also can be extended to be used for configuring the component used to show this (e.g. React.js |
95
|
|
|
* or Vue.js component configuration). |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function getCKANClientConfig() |
100
|
|
|
{ |
101
|
|
|
$config = [ |
102
|
|
|
'spec' => [ |
103
|
|
|
'endpoint' => $this->Endpoint, |
104
|
|
|
'dataset' => $this->DataSet, |
105
|
|
|
'identifier' => $this->Identifier, |
106
|
|
|
], |
107
|
|
|
'name' => $this->Name, |
108
|
|
|
'resourceName' => $this->ResourceName, |
109
|
|
|
'basePath' => $this->getPageBasePath(), |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
$this->extend('updateCKANClientConfig', $config); |
113
|
|
|
|
114
|
|
|
return $config; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns the base path for the resource's page with a leading slash |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getPageBasePath() |
123
|
|
|
{ |
124
|
|
|
/** @var CKANRegistryPage $page */ |
125
|
|
|
$page = $this->getComponent('Page'); |
126
|
|
|
$pagePath = $page->RelativeLink(); |
127
|
|
|
return '/' . trim($pagePath, '/'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|