1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: sjhc1170 |
5
|
|
|
* Date: 17/05/2018 |
6
|
|
|
* Time: 15:23 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Iriven\Plugins\Form\Elements; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use \Iriven\Plugins\Form\Core\Interfaces\FormElementInterface; |
13
|
|
|
|
14
|
|
|
class Checkbox extends Text |
15
|
|
|
{ |
16
|
|
|
protected $options; |
17
|
|
|
private $hiddenInput; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Checkbox constructor. |
21
|
|
|
* @param $label |
22
|
|
|
* @param array $options |
23
|
|
|
* @param array $attributes |
24
|
|
|
*/ |
25
|
|
|
public function __construct($label, array $options, array $attributes=[]) |
26
|
|
|
{ |
27
|
|
|
parent::__construct($label, $attributes); |
28
|
|
|
$this->options = $options; |
29
|
|
|
$this->Attributes()->set('type','checkbox'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function RenderHtml() |
36
|
|
|
{ |
37
|
|
|
$html = ''; |
38
|
|
|
if($this->Types()->has($this->Attributes()->get('type'))) |
39
|
|
|
{ |
40
|
|
|
if(!$this->Attributes()->has('id')) |
41
|
|
|
$this->Attributes()->createElementID($this->Attributes()->get('name',$this->label()->getItemID())); |
42
|
|
|
$this->label()->Attribute()->set('fieldtype',$this->Attributes()->get('type')); |
43
|
|
|
$this->label()->Attribute()->set('for',$this->Attributes()->get('id')); |
44
|
|
|
if(sizeof($this->options)<2) |
45
|
|
|
{ |
46
|
|
|
if($this->Attributes()->get('value')) |
47
|
|
|
$this->Attributes()->set('checked','checked'); |
48
|
|
|
$this->Attributes()->set('value','1'); |
49
|
|
|
$this->hiddenInput = new Hidden($this->Label(),['value'=>'0']); |
50
|
|
|
} |
51
|
|
|
if($this->hiddenInput instanceof FormElementInterface) |
52
|
|
|
return parent::RenderHtml().$this->hiddenInput->RenderHtml(); |
53
|
|
|
if($this->Attributes()->has('value')) |
54
|
|
|
{ |
55
|
|
|
if(!is_array($this->Attributes()->get('value'))) |
56
|
|
|
$this->Attributes()->set('value',[$this->Attributes()->get('value')]); |
57
|
|
|
} |
58
|
|
|
else |
59
|
|
|
$this->Attributes()->set('value',[]); |
60
|
|
|
if(substr($this->Attributes()->get('name'), -2) !== '[]') |
61
|
|
|
$this->Attributes()->set('name',$this->Attributes()->get('name').'[]'); |
62
|
|
|
$count = 0; |
63
|
|
|
$html .= $this->Label()->RenderHtml(); |
64
|
|
|
$this->Attributes()->Ignore(['id', 'value', 'checked', 'required']); |
65
|
|
|
foreach($this->options as $index => $details) |
66
|
|
|
{ |
67
|
|
|
$html .= '<label for="'.$this->Attributes()->get('id').'-'.$count.'">'; |
68
|
|
|
$html .= '<input id="'.$this->Attributes()->get('id').'-'.$count.'"'; |
69
|
|
|
$html .= ' value="'.$index.'"'; |
70
|
|
|
$html .= $this->Attributes()->RenderHtml(); |
71
|
|
|
if(in_array($index , $this->Attributes()->get('value'))) $html .= ' checked="checked"'; |
72
|
|
|
$html .= ' >'.$details.'</label>'; |
73
|
|
|
++$count; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return $html; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|