|
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
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
private static $has_many = [ |
|
|
|
|
|
|
34
|
|
|
'Fields' => ResourceField::class, |
|
35
|
|
|
'Filters' => ResourceFilter::class, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Whenever the CKAN resource identifier is changed we should clear any existing field and filter configurations |
|
40
|
|
|
* |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function onAfterWrite() |
|
44
|
|
|
{ |
|
45
|
|
|
if ($this->isChanged('Identifier')) { |
|
46
|
|
|
$this->Fields()->removeAll(); |
|
47
|
|
|
|
|
48
|
|
|
/** @var ResourcePopulatorInterface $populator */ |
|
49
|
|
|
$populator = Injector::inst()->get(ResourcePopulatorInterface::class); |
|
50
|
|
|
$populator->populateFields($this); |
|
51
|
|
|
|
|
52
|
|
|
// Remove the existing filters and add a default text entry to search all ResourceFields |
|
53
|
|
|
$this->Filters() |
|
54
|
|
|
->removeAll() |
|
55
|
|
|
->add(ResourceFilter::create()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
parent::onAfterWrite(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Whenever the CKAN resource identifier is changed, populate a new set of metadata for the new resource |
|
63
|
|
|
* |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function onBeforeWrite() |
|
67
|
|
|
{ |
|
68
|
|
|
if ($this->isChanged('Identifier')) { |
|
69
|
|
|
/** @var ResourcePopulatorInterface $populator */ |
|
70
|
|
|
$populator = Injector::inst()->get(ResourcePopulatorInterface::class); |
|
71
|
|
|
$populator->populateMetadata($this); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
parent::onBeforeWrite(); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|