Completed
Push — master ( f5a6ee...8e0a6c )
by
unknown
15s queued 12s
created

Dropdown::getClientConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Model\ResourceFilter;
4
5
use InvalidArgumentException;
6
use SilverStripe\CKANRegistry\Forms\PresentedOptionsField;
7
use SilverStripe\CKANRegistry\Model\ResourceFilter;
8
use SilverStripe\Forms\DropdownField;
9
use SilverStripe\Forms\FieldList;
10
11
/**
12
 * Provides a single select option for CKAN resources to be filtered by
13
 */
14
class Dropdown extends ResourceFilter
15
{
16
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
17
        'Options' => 'Text', // JSON blob
18
    ];
19
20
    private static $table_name = 'CKANFilter_Dropdown';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
21
22
    private static $singular_name = 'Dropdown';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
23
24
    protected $fieldType = DropdownField::class;
25
26
    public function getCMSFields()
27
    {
28
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
29
            $fields->addFieldToTab('Root.Main', PresentedOptionsField::create(
30
                'Options',
31
                $this->FilterFor,
0 ignored issues
show
Bug Best Practice introduced by
The property FilterFor does not exist on SilverStripe\CKANRegistr...ResourceFilter\Dropdown. Since you implemented __get, consider adding a @property annotation.
Loading history...
32
                _t(__CLASS__ . '.OPTIONS', 'Presented options')
33
            ));
34
        });
35
36
        return parent::getCMSFields();
37
    }
38
39
    public function getClientConfig()
40
    {
41
        $config = parent::getClientConfig();
42
43
        try {
44
            $config['options'] = $this->getConfiguredOptions();
45
        } catch (InvalidArgumentException $e) {
46
            $config['options'] = [];
47
        }
48
49
        return $config;
50
    }
51
52
    /**
53
     * Get the options that have been configured for this dropdown by the CMS author. ie. parse the "Options" value
54
     *
55
     * @throws InvalidArgumentException When the configured "selectType" is unknown
56
     */
57
    protected function getConfiguredOptions()
58
    {
59
        $spec = json_decode($this->Options, true) ?: [];
0 ignored issues
show
Bug Best Practice introduced by
The property Options does not exist on SilverStripe\CKANRegistr...ResourceFilter\Dropdown. Since you implemented __get, consider adding a @property annotation.
Loading history...
60
61
        if (!isset($spec['selectType'])) {
62
            return [];
63
        }
64
65
        $selectType = (int) $spec['selectType'];
66
67
        if ($selectType === PresentedOptionsField::SELECT_TYPE_ALL) {
68
            return $spec['selections'];
69
        }
70
        if ($selectType === PresentedOptionsField::SELECT_TYPE_CUSTOM) {
71
            return $spec['customOptions'];
72
        }
73
74
        throw new InvalidArgumentException('Unknown "selectType" is configured for ' . static::class);
75
    }
76
}
77