Passed
Pull Request — master (#32)
by
unknown
02:14
created

ResourceLocatorField::getDatasetFieldName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace SilverStripe\CKANRegistry\Forms;
3
4
use InvalidArgumentException;
5
use SilverStripe\Forms\FormField;
6
use SilverStripe\ORM\DataObjectInterface;
7
8
class ResourceLocatorField extends FormField
9
{
10
    /**
11
     * The default CKAN endpoint to be used in a default isn't provided on construction
12
     * @see ResourceLocatorField::$defaultEndpoint
13
     *
14
     * @config
15
     * @var string
16
     */
17
    private static $default_endpoint = 'https://catalogue.data.govt.nz/';
18
19
    /**
20
     * The default CKAN endpoint to be used in this field. This will allow consumers of the field to only provide
21
     * package or dataset IDs and still work. If not set the configured default will instead be used.
22
     *
23
     * @var string|null
24
     */
25
    protected $defaultEndpoint;
26
27
    /**
28
     * Set a site name that can be used to refer to the CKAN endpoint. By default this will be "a CKAN website".
29
     *
30
     * @var string|null
31
     */
32
    protected $siteName = null;
33
34
    /**
35
     * The name of the subfield to save the endpoint value of this field into
36
     *
37
     * @var string
38
     */
39
    protected $endpointFieldName = 'Endpoint';
40
41
    /**
42
     * The name of the subfield to save the dataset value of this field into
43
     *
44
     * @var string
45
     */
46
    protected $datasetFieldName = 'DataSet';
47
48
    /**
49
     * The name of the subfield to save the resource value of this field into
50
     *
51
     * @var string
52
     */
53
    protected $resourceFieldName = 'Resource';
54
55
    /**
56
     * @param string $name
57
     * @param string $title
58
     * @param string $value
59
     * @param string $defaultEndpoint
60
     */
61
    public function __construct($name, $title = null, $value = null, $defaultEndpoint = null)
62
    {
63
        parent::__construct($name, $title, $value);
64
        $this->setDefaultEndpoint($defaultEndpoint);
65
66
        // Set a default description
67
        $this->setDescription(_t(
68
            __CLASS__ . '.DESCRIPTION',
69
            'Connect to a data source from {site}. Once added and saved you can configure the appearance and add search'
70
            . ' filters.',
71
            [ 'site' => $this->getSiteName() ]
72
        ));
73
74
        $this->addExtraClass('ckan-resource-locator__container');
75
    }
76
77
    public function getSchemaDataDefaults()
78
    {
79
        $schemaData = parent::getSchemaDataDefaults();
80
81
        $schemaData['defaultEndpoint'] = $this->getDefaultEndpoint();
82
83
        return $schemaData;
84
    }
85
86
    public function setSubmittedValue($value, $data = null)
87
    {
88
        return $this->setValue(json_decode($value, true));
89
    }
90
91
    public function saveInto(DataObjectInterface $dataObject)
92
    {
93
        // Duplicate existing logic where the field is skipped given there's no name on this field.
94
        if (!$this->name) {
95
            return;
96
        }
97
98
        // Find what we're actually saving into
99
        $child = $this->getSaveTarget($dataObject);
100
101
        if (!$child || !$child instanceof DataObjectInterface) {
102
            throw new InvalidArgumentException('Could not determine where to save the value of ' . __CLASS__);
103
        }
104
105
        // Pull the value that'll be null or an associative array of our specification
106
        $value = $this->Value();
107
        $child->setCastedField($this->getEndpointFieldName(), $value ? $value['endpoint'] : null);
108
        $child->setCastedField($this->getDatasetFieldName(), $value ? $value['dataset'] : null);
109
        $child->setCastedField($this->getResourceFieldName(), $value ? $value['resource'] : null);
110
    }
111
112
    /**
113
     * Provide the object that this field actually saves into.
114
     * By default this is a relation access with __get. Eg. given $dataObject is a page; $page->CKANResource where
115
     * "CKANResource" is the name of this field.
116
     *
117
     * @param DataObjectInterface $dataObject
118
     * @return mixed
119
     */
120
    public function getSaveTarget(DataObjectInterface $dataObject)
121
    {
122
        $target = $dataObject->{$this->name};
123
124
        $this->extend('updateSaveTarget', $target);
125
126
        return $target;
127
    }
128
129
    /**
130
     * @see ResourceLocatorField::$defaultEndpoint
131
     * @return string
132
     */
133
    public function getDefaultEndpoint()
134
    {
135
        if (!$this->defaultEndpoint) {
136
            return self::config()->get('default_endpoint');
137
        }
138
139
        return $this->defaultEndpoint;
140
    }
141
142
    /**
143
     * @see ResourceLocatorField::$defaultEndpoint
144
     * @param string $defaultEndpoint
145
     * @return $this
146
     */
147
    public function setDefaultEndpoint($defaultEndpoint)
148
    {
149
        $this->defaultEndpoint = $defaultEndpoint;
150
        return $this;
151
    }
152
153
    /**
154
     * @return null|string
155
     */
156
    public function getSiteName()
157
    {
158
        // Allow empty site names
159
        if ($this->siteName === null) {
160
            return _t(__CLASS__ . '.GENERIC_SITE_NAME', 'a CKAN website');
161
        }
162
163
        return $this->siteName;
164
    }
165
166
    /**
167
     * @param null|string $siteName
168
     * @return $this
169
     */
170
    public function setSiteName($siteName)
171
    {
172
        $this->siteName = $siteName;
173
        return $this;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getEndpointFieldName()
180
    {
181
        return $this->endpointFieldName;
182
    }
183
184
    /**
185
     * @param string $endpointFieldName
186
     * @return $this
187
     */
188
    public function setEndpointFieldName($endpointFieldName)
189
    {
190
        $this->endpointFieldName = $endpointFieldName;
191
        return $this;
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getDatasetFieldName()
198
    {
199
        return $this->datasetFieldName;
200
    }
201
202
    /**
203
     * @param string $datasetFieldName
204
     * @return $this
205
     */
206
    public function setDatasetFieldName($datasetFieldName)
207
    {
208
        $this->datasetFieldName = $datasetFieldName;
209
        return $this;
210
    }
211
212
    /**
213
     * @return string
214
     */
215
    public function getResourceFieldName()
216
    {
217
        return $this->resourceFieldName;
218
    }
219
220
    /**
221
     * @param string $resourceFieldName
222
     * @return $this
223
     */
224
    public function setResourceFieldName($resourceFieldName)
225
    {
226
        $this->resourceFieldName = $resourceFieldName;
227
        return $this;
228
    }
229
}
230