1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\SelectedColourPicker\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\TextField; |
6
|
|
|
use SilverStripe\Forms\Validator; |
7
|
|
|
use SilverStripe\ORM\ArrayList; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
9
|
|
|
use SilverStripe\View\ArrayData; |
10
|
|
|
use Sunnysideup\SelectedColourPicker\ViewableData\SelectedColourPickerFormFieldSwatches; |
11
|
|
|
|
12
|
|
|
class SelectedColourPickerFormField extends TextField |
13
|
|
|
{ |
14
|
|
|
protected $inputType = 'color'; |
15
|
|
|
|
16
|
|
|
protected $source = []; |
17
|
|
|
|
18
|
|
|
protected $limitedToOptions = true; |
19
|
|
|
|
20
|
|
|
protected $isBgColour = true; |
21
|
|
|
|
22
|
|
|
public function setOptions(array $array) |
23
|
|
|
{ |
24
|
|
|
$this->source = $array; |
25
|
|
|
|
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setLimitedToOptions(bool $bool) |
30
|
|
|
{ |
31
|
|
|
$this->limitedToOptions = $bool; |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setIsBgColour(bool $bool) |
37
|
|
|
{ |
38
|
|
|
$this->isBgColour = $bool; |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Validate this field. |
45
|
|
|
* |
46
|
|
|
* @param Validator $validator |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function validate($validator) |
51
|
|
|
{ |
52
|
|
|
if ($this->limitedToOptions && $this->value && ! isset($this->source[$this->value])) { |
53
|
|
|
$validator->validationError( |
54
|
|
|
$this->name, |
55
|
|
|
'Please selected from suggested options only', |
56
|
|
|
'validation' |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function Field($properties = []) |
66
|
|
|
{ |
67
|
|
|
$this->setAttribute('list', $this->ID() . '_List'); |
68
|
|
|
$this->setDescription( |
69
|
|
|
DBField::create_field( |
70
|
|
|
'HTMLText', |
71
|
|
|
SelectedColourPickerFormFieldSwatches::get_swatches_html( |
72
|
|
|
$this->name, |
73
|
|
|
$this->value, |
74
|
|
|
$this->source, |
75
|
|
|
$this->isBgColour |
76
|
|
|
) |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
return parent::Field(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function ColourOptionsAsArrayList(): ArrayList |
84
|
|
|
{ |
85
|
|
|
$al = new ArrayList(); |
86
|
|
|
foreach ($this->source as $colour => $label) { |
87
|
|
|
$al->push( |
88
|
|
|
new ArrayData( |
89
|
|
|
[ |
90
|
|
|
'Colour' => $colour, |
91
|
|
|
'Label' => $label, |
92
|
|
|
] |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $al; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function Type() |
101
|
|
|
{ |
102
|
|
|
return 'selected-colour-picker'; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|