1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amarkal\UI; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Implements a Toggle UI component. |
7
|
|
|
*/ |
8
|
|
|
class Component_toggle |
9
|
|
|
extends AbstractComponent |
|
|
|
|
10
|
|
|
implements ValueComponentInterface, |
|
|
|
|
11
|
|
|
FilterableComponentInterface, |
|
|
|
|
12
|
|
|
DisableableComponentInterface |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
public $component_type = 'toggle'; |
15
|
|
|
|
16
|
|
|
protected function on_created() |
17
|
|
|
{ |
18
|
|
|
if($this->multi && !\is_array($this->default) |
|
|
|
|
19
|
|
|
|| !$this->multi && \is_array($this->default)) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
throw new \RuntimeException("The default value must be an array if multi is set to true, and must be a string otherwise."); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function default_model() |
26
|
|
|
{ |
27
|
|
|
return array( |
28
|
|
|
'name' => '', |
29
|
|
|
'id' => '', |
30
|
|
|
'disabled' => false, |
31
|
|
|
'required' => false, |
32
|
|
|
'readonly' => false, |
33
|
|
|
'multi' => false, |
34
|
|
|
'data' => array(), |
35
|
|
|
'default' => array(), |
36
|
|
|
'filter' => array( $this, 'filter' ), |
37
|
|
|
'show' => null |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function required_arguments() |
42
|
|
|
{ |
43
|
|
|
return array('name','data'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function get_template_path() |
47
|
|
|
{ |
48
|
|
|
return __DIR__.'/template.phtml'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* This filter is needed when the form is submitted without $.amarkalUIForm, |
53
|
|
|
* since the value of the toggle needs to be splitted into an array (when 'multi' is set to true). |
54
|
|
|
* When using $.amarkalUIForm to submit the form, the component's getValue/setValue will |
55
|
|
|
* handle this. |
56
|
|
|
* |
57
|
|
|
* @param [string|array] $v |
|
|
|
|
58
|
|
|
* @return [string|array] |
|
|
|
|
59
|
|
|
*/ |
60
|
|
|
public function filter($v) |
61
|
|
|
{ |
62
|
|
|
if($this->multi && !\is_array($v)) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
return \explode(',', $v); |
65
|
|
|
} |
66
|
|
|
return $v; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function is_selected( $value ) |
70
|
|
|
{ |
71
|
|
|
if($this->multi) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
return \in_array($value, $this->value); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
return $value === $this->value; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |