1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\collections\form; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemCollection; |
6
|
|
|
use Ajax\semantic\html\base\constants\Wide; |
7
|
|
|
use Ajax\JsUtils; |
8
|
|
|
|
9
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
10
|
|
|
use Ajax\semantic\html\collections\form\traits\FieldsTrait; |
11
|
|
|
|
12
|
|
|
class HtmlFormFields extends HtmlSemCollection { |
13
|
|
|
|
14
|
|
|
use FieldsTrait; |
15
|
|
|
protected $_equalWidth; |
16
|
|
|
protected $_name; |
17
|
|
|
|
18
|
|
|
public function __construct($identifier, $fields=array(), $equalWidth=true) { |
19
|
|
|
parent::__construct($identifier, "div"); |
20
|
|
|
$this->_equalWidth=$equalWidth; |
21
|
|
|
$this->addItems($fields); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function addFields($fields=NULL, $label=NULL) { |
25
|
|
|
if (!$fields instanceof HtmlFormFields) { |
26
|
|
|
if (!\is_array($fields)) { |
27
|
|
|
$fields=\func_get_args(); |
28
|
|
|
$end=\end($fields); |
29
|
|
|
if (\is_string($end)) { |
30
|
|
|
$label=$end; |
31
|
|
|
\array_pop($fields); |
32
|
|
|
} else |
33
|
|
|
$label=NULL; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
if (isset($label)) |
37
|
|
|
$this->setLabel($label); |
38
|
|
|
foreach ( $fields as $field ) { |
39
|
|
|
$this->addItem($field); |
40
|
|
|
} |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @param string|HtmlSemDoubleElement $label |
47
|
|
|
* @return \Ajax\semantic\html\base\HtmlSemDoubleElement |
48
|
|
|
*/ |
49
|
|
|
public function setLabel($label) { |
50
|
|
|
$labelO=$label; |
51
|
|
|
if (\is_string($label)) { |
52
|
|
|
$labelO=new HtmlSemDoubleElement("", "label", "", $label); |
53
|
|
|
} |
54
|
|
|
$this->insertItem($labelO, 0); |
55
|
|
|
return $labelO; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function addItem($item) { |
59
|
|
|
$item=parent::addItem($item); |
60
|
|
|
if($item instanceof HtmlFormField) |
61
|
|
|
$item->setContainer($this); |
62
|
|
|
return $item; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return HtmlFormField |
67
|
|
|
*/ |
68
|
|
|
public function getItem($index){ |
69
|
|
|
return parent::getItem($index); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function compile(JsUtils $js=NULL, &$view=NULL) { |
73
|
|
|
if ($this->_equalWidth) { |
74
|
|
|
$count=$this->count(); |
75
|
|
|
$this->addToProperty("class", Wide::getConstants()["W".$count]." fields"); |
76
|
|
|
} else |
77
|
|
|
$this->addToProperty("class", "fields"); |
78
|
|
|
return parent::compile($js, $view); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setWidth($index, $width) { |
82
|
|
|
$this->_equalWidth=false; |
83
|
|
|
return $this->getItem($index)->setWidth($width); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function setInline() { |
87
|
|
|
$this->_equalWidth=false; |
88
|
|
|
$this->addToProperty("class", "inline"); |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function setGrouped() { |
93
|
|
|
$this->_equalWidth=false; |
94
|
|
|
$this->addToProperty("class", "grouped"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getName() { |
98
|
|
|
return $this->_name; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function setName($_name) { |
102
|
|
|
$this->_name=$_name; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
public static function radios($name, $items=array(), $label=NULL, $value=null, $type=NULL) { |
108
|
|
|
$fields=array (); |
109
|
|
|
$i=0; |
110
|
|
|
foreach ( $items as $val => $caption ) { |
111
|
|
|
$itemO=new HtmlFormRadio($name."-".$i++, $name, $caption, $val, $type); |
112
|
|
|
if ($val===$value) { |
113
|
|
|
$itemO->getDataField()->setProperty("checked", ""); |
114
|
|
|
} |
115
|
|
|
$fields[]=$itemO; |
116
|
|
|
} |
117
|
|
|
$radios=new HtmlFormFields("fields-".$name, $fields); |
118
|
|
|
if (isset($label)) |
119
|
|
|
$radios->setLabel($label)->setProperty("for", $name); |
120
|
|
|
return $radios; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public static function checkeds($name, $items=array(), $label=NULL, $values=array(), $type=NULL) { |
124
|
|
|
$fields=array (); |
125
|
|
|
$i=0; |
126
|
|
|
foreach ( $items as $val => $caption ) { |
127
|
|
|
$itemO=new HtmlFormCheckbox($name."-".$i++, $caption, $val, $type); |
128
|
|
|
$itemO->setName($name); |
129
|
|
|
if (\array_search($val, $values)!==false) { |
130
|
|
|
$itemO->getDataField()->setProperty("checked", ""); |
131
|
|
|
} |
132
|
|
|
$fields[]=$itemO; |
133
|
|
|
} |
134
|
|
|
$radios=new HtmlFormFields("fields-".$name, $fields); |
135
|
|
|
if (isset($label)) |
136
|
|
|
$radios->setLabel($label)->setProperty("for", $name); |
137
|
|
|
return $radios; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function setEqualWidth($_equalWidth) { |
141
|
|
|
$this->_equalWidth=$_equalWidth; |
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|