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