1
|
|
|
<?php namespace Rocket\UI\Forms\Fields; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Renders radio buttons |
5
|
|
|
* |
6
|
|
|
* @method $this checked(boolean $checked) |
7
|
|
|
* @method $this values(array $config) |
8
|
|
|
* @method $this check_value(string $value) |
9
|
|
|
*/ |
10
|
|
|
class Radio extends Field |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Extends the type |
14
|
|
|
* @param string $id |
15
|
|
|
* @param array $data |
16
|
|
|
*/ |
17
|
|
|
public function __construct($id, $data = []) |
18
|
|
|
{ |
19
|
|
|
$this->type = 'radio'; |
20
|
|
|
|
21
|
|
|
parent::__construct($id, $data); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Adds input attributes |
26
|
|
|
*/ |
27
|
|
|
protected function inputAttributes() |
28
|
|
|
{ |
29
|
|
|
$this->input_attributes['name'] = $this->name; |
30
|
|
|
$this->input_attributes['type'] = 'radio'; |
31
|
|
|
$this->input_attributes['id'] = $this->id; |
32
|
|
|
$this->input_attributes['class'] = []; //removes elm |
33
|
|
|
|
34
|
|
|
if (array_key_exists('check_value', $this->params) && $this->params['check_value']) { |
35
|
|
|
$this->input_attributes['value'] = $this->params['check_value']; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($this->getValidator()->getValue($this->name) == $this->input_attributes['value']) { |
39
|
|
|
$this->input_attributes['checked'] = 'checked'; |
40
|
|
|
} elseif (array_key_exists('checked', $this->params)) { |
41
|
|
|
if ($this->params['checked'] !== false) { |
42
|
|
|
$this->input_attributes['checked'] = 'checked'; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!array_key_exists('values', $this->params)) { |
47
|
|
|
$this->params['label_position'] = 'after'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (array_key_exists('enabled', $this->params) && $this->params['enabled'] == false) { |
51
|
|
|
$this->input_attributes['disabled'] = 'disabled'; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Renders the boxes |
57
|
|
|
*/ |
58
|
|
|
protected function renderInner() |
59
|
|
|
{ |
60
|
|
|
if (array_key_exists('values', $this->params)) { |
61
|
|
|
unset($this->input_attributes['id']); |
62
|
|
|
|
63
|
|
|
$this->input_attributes['class'][] = 'form_multiple'; |
64
|
|
|
foreach ($this->params['values'] as $key => $val) { |
65
|
|
|
$this->input_attributes['value'] = $key; |
66
|
|
|
if ($key == $this->params['value']) { |
67
|
|
|
$this->input_attributes['checked'] = 'checked'; |
68
|
|
|
} else { |
69
|
|
|
unset($this->input_attributes['checked']); |
70
|
|
|
} |
71
|
|
|
$this->result .= '<label class=radio-inline>'; |
72
|
|
|
$this->result .= '<input' . $this->renderAttributes($this->input_attributes) . ' />'; |
73
|
|
|
$this->result .= '<span>' . $val . '</span>'; |
74
|
|
|
$this->result .= '</label>'; |
75
|
|
|
} |
76
|
|
|
} else { |
77
|
|
|
parent::renderInner(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function classes() |
82
|
|
|
{ |
83
|
|
|
parent::classes(); |
84
|
|
|
|
85
|
|
|
if (!array_key_exists('values', $this->params)) { |
86
|
|
|
$this->label_attributes['class'][] = 'radio'; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|