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