Issues (50)

src/Model/Resource.php (4 issues)

Severity
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';
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
24
25
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
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 = [
0 ignored issues
show
The private property $has_many is not used, and could be removed.
Loading history...
35
        'Fields' => ResourceField::class,
36
        'Filters' => ResourceFilter::class,
37
    ];
38
39
    private static $defaults = [
0 ignored issues
show
The private property $defaults is not used, and could be removed.
Loading history...
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->Identifier && $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
                    'FilterLabel' => _t(self::class . '.DEFAULT_FILTER_LABEL', 'Search'),
66
                ]));
67
        }
68
69
        parent::onAfterWrite();
70
    }
71
72
    /**
73
     * Whenever the CKAN resource identifier is changed, populate a new set of metadata for the new resource
74
     *
75
     * {@inheritdoc}
76
     */
77
    public function onBeforeWrite()
78
    {
79
        if ($this->Identifier && $this->isChanged('Identifier')) {
80
            /** @var ResourcePopulatorInterface $populator */
81
            $populator = Injector::inst()->get(ResourcePopulatorInterface::class);
82
            $populator->populateMetadata($this);
83
        }
84
85
        parent::onBeforeWrite();
86
    }
87
}
88