Completed
Push — master ( a6a498...20717b )
by
unknown
13s
created

PresentedOptionsField::setResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Forms;
4
5
use SilverStripe\CKANRegistry\Model\Resource;
6
use SilverStripe\Forms\Form;
7
use SilverStripe\Forms\TextField;
8
9
/**
10
 * A PresentedOptionsField renders either a list of options that can be chosen for a {@link DropdownFilter}, or
11
 * a text area allowing free text entry per line.
12
 *
13
 * The values of these options are serialised and saved as JSON.
14
 */
15
class PresentedOptionsField extends TextField
16
{
17
    /**
18
     * @var int
19
     */
20
    const SELECT_TYPE_ALL = 0;
21
22
    /**
23
     * @var int
24
     */
25
    const SELECT_TYPE_CUSTOM = 1;
26
27
    /**
28
     * @var int
29
     */
30
    const SELECT_TYPE_DEFAULT = self::SELECT_TYPE_ALL;
31
32
    protected $schemaComponent = 'PresentedOptions';
33
34
    /**
35
     * The resource that this options field will suggest options from
36
     *
37
     * @var Resource
38
     */
39
    protected $resource;
40
41
    public function __construct(
42
        $name,
43
        Resource $resource,
44
        $title = null,
45
        $value = '',
46
        $maxLength = null,
47
        Form $form = null
48
    ) {
49
        $this->setResource($resource);
50
51
        parent::__construct($name, $title, $value, $maxLength, $form);
52
53
        $this->addExtraClass('ckan-presented-options__container');
54
    }
55
56
    public function Type()
57
    {
58
        return 'ckan-presented-options';
59
    }
60
61
    public function getSchemaDataDefaults()
62
    {
63
        $data = parent::getSchemaDataDefaults();
64
        $data['data']['selectTypeDefault'] = self::SELECT_TYPE_DEFAULT;
65
        $data['data']['selectTypes'] = self::getSelectTypes();
66
        $data['data']['endpoint'] = $this->getResource()->Endpoint;
67
        $data['data']['resource'] = $this->getResource()->Identifier;
68
        $data['data']['fieldMap'] = $this->getResource()->Fields()->map('ID', 'OriginalLabel')->toArray();
69
        return $data;
70
    }
71
72
    /**
73
     * Get a list of options for filtering with a human readable (translated) label
74
     *
75
     * @return array[]
76
     */
77
    public static function getSelectTypes()
78
    {
79
        return [
80
            ['value' => self::SELECT_TYPE_ALL, 'title' => _t(__CLASS__ . '.SELECT_ALL', 'Select from all options')],
81
            ['value' => self::SELECT_TYPE_CUSTOM, 'title' => _t(__CLASS__ . '.SELECT_CUSTOM', 'Manually add options')],
82
        ];
83
    }
84
85
    /**
86
     * @return Resource
87
     */
88
    public function getResource()
89
    {
90
        return $this->resource;
91
    }
92
93
    /**
94
     * @param Resource $resouce
95
     * @return $this
96
     */
97
    public function setResource(Resource $resource)
98
    {
99
        $this->resource = $resource;
100
        return $this;
101
    }
102
}
103