|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\collections\form; |
|
4
|
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemCollection; |
|
6
|
|
|
use Ajax\semantic\html\elements\HtmlHeader; |
|
7
|
|
|
use Ajax\semantic\html\collections\HtmlMessage; |
|
8
|
|
|
use Ajax\semantic\html\base\constants\State; |
|
9
|
|
|
use Ajax\semantic\html\collections\form\traits\FieldsTrait; |
|
10
|
|
|
use Ajax\semantic\html\elements\HtmlDivider; |
|
11
|
|
|
use Ajax\JsUtils; |
|
12
|
|
|
use Ajax\semantic\html\collections\form\traits\FormTrait; |
|
13
|
|
|
use Ajax\semantic\components\Form; |
|
14
|
|
|
use Ajax\common\html\BaseHtml; |
|
15
|
|
|
use Ajax\common\html\HtmlDoubleElement; |
|
16
|
|
|
use Ajax\semantic\components\validation\FieldValidation; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Semantic Form component |
|
20
|
|
|
* @see http://semantic-ui.com/collections/form.html |
|
21
|
|
|
* @author jc |
|
22
|
|
|
* @version 1.001 |
|
23
|
|
|
*/ |
|
24
|
|
|
class HtmlForm extends HtmlSemCollection { |
|
25
|
|
|
|
|
26
|
|
|
use FieldsTrait,FormTrait; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_fields; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $_validationParams; |
|
36
|
|
|
|
|
37
|
|
|
protected $_extraFieldRules; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct($identifier, $elements=array()) { |
|
40
|
|
|
parent::__construct($identifier, "form", "ui form"); |
|
41
|
|
|
$this->_states=[ State::ERROR,State::SUCCESS,State::WARNING,State::DISABLED ]; |
|
42
|
|
|
$this->setProperty("name", $this->identifier); |
|
43
|
|
|
$this->_fields=array (); |
|
44
|
|
|
$this->addItems($elements); |
|
45
|
|
|
$this->_validationParams=[]; |
|
46
|
|
|
$this->_extraFieldRules=[]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function getForm(){ |
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $title |
|
55
|
|
|
* @param number $niveau |
|
56
|
|
|
* @param string $dividing |
|
57
|
|
|
* @return HtmlHeader |
|
58
|
|
|
*/ |
|
59
|
|
|
public function addHeader($title, $niveau=1, $dividing=true) { |
|
60
|
|
|
$header=new HtmlHeader("", $niveau, $title); |
|
61
|
|
|
if ($dividing) |
|
62
|
|
|
$header->setDividing(); |
|
63
|
|
|
return $this->addItem($header); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Adds a divider |
|
68
|
|
|
* @param string $caption |
|
69
|
|
|
* @return HtmlForm |
|
70
|
|
|
*/ |
|
71
|
|
|
public function addDivider($caption=NULL){ |
|
72
|
|
|
return $this->addContent(new HtmlDivider("",$caption)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Adds a group of fields |
|
77
|
|
|
* @param array $fields |
|
78
|
|
|
* @param string $label |
|
79
|
|
|
* @return HtmlFormFields |
|
80
|
|
|
*/ |
|
81
|
|
|
public function addFields($fields=NULL, $label=NULL) { |
|
82
|
|
|
if (isset($fields)) { |
|
83
|
|
|
if (!$fields instanceof HtmlFormFields) { |
|
84
|
|
|
if (!\is_array($fields)) { |
|
85
|
|
|
$fields=\func_get_args(); |
|
86
|
|
|
$end=\end($fields); |
|
87
|
|
|
if (\is_string($end)) { |
|
88
|
|
|
$label=$end; |
|
89
|
|
|
\array_pop($fields); |
|
90
|
|
|
} else |
|
91
|
|
|
$label=NULL; |
|
92
|
|
|
} |
|
93
|
|
|
$this->_fields=\array_merge($this->_fields, $fields); |
|
94
|
|
|
$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count(), $fields); |
|
95
|
|
|
} |
|
96
|
|
|
if (isset($label)){ |
|
97
|
|
|
$fields->wrap("<div class='field'><label>{$label}</label>","</div>"); |
|
98
|
|
|
} |
|
99
|
|
|
} else { |
|
100
|
|
|
$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count()); |
|
101
|
|
|
} |
|
102
|
|
|
$this->addItem($fields); |
|
103
|
|
|
return $fields; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function addItem($item) { |
|
107
|
|
|
$item=parent::addItem($item); |
|
108
|
|
|
if (\is_subclass_of($item, HtmlFormField::class) === true) { |
|
109
|
|
|
$this->_fields[]=$item; |
|
110
|
|
|
} |
|
111
|
|
|
return $item; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param int $index |
|
116
|
|
|
* @return mixed|NULL|BaseHtml |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getField($index) { |
|
119
|
|
|
if (\is_string($index)) { |
|
|
|
|
|
|
120
|
|
|
$field=$this->getElementById($index, $this->_fields); |
|
121
|
|
|
} else { |
|
122
|
|
|
$field=$this->_fields[$index]; |
|
123
|
|
|
} |
|
124
|
|
|
return $field; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* automatically divide fields to be equal width |
|
129
|
|
|
* @return HtmlForm |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setEqualWidth() { |
|
132
|
|
|
return $this->addToProperty("class", "equal width"); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Adds a field (alias for addItem) |
|
137
|
|
|
* @param HtmlFormField $field |
|
138
|
|
|
* @return HtmlDoubleElement |
|
139
|
|
|
*/ |
|
140
|
|
|
public function addField($field) { |
|
141
|
|
|
return $this->addItem($field); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $identifier |
|
147
|
|
|
* @param string $content |
|
148
|
|
|
* @param string $header |
|
149
|
|
|
* @param string $icon |
|
150
|
|
|
* @param string $type |
|
151
|
|
|
* @return HtmlMessage |
|
152
|
|
|
*/ |
|
153
|
|
|
public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) { |
|
154
|
|
|
$message=new HtmlMessage($identifier, $content); |
|
155
|
|
|
if (isset($header)) |
|
156
|
|
|
$message->addHeader($header); |
|
157
|
|
|
if (isset($icon)) |
|
158
|
|
|
$message->setIcon($icon); |
|
159
|
|
|
if (isset($type)) |
|
160
|
|
|
$message->setStyle($type); |
|
161
|
|
|
return $this->addItem($message); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
public function compile(JsUtils $js=NULL,&$view=NULL){ |
|
167
|
|
|
if(\sizeof($this->_validationParams)>0) |
|
168
|
|
|
$this->setProperty("novalidate", ""); |
|
169
|
|
|
return parent::compile($js,$view); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function run(JsUtils $js) { |
|
173
|
|
|
if(isset($js)){ |
|
174
|
|
|
$compo=$js->semantic()->form("#".$this->identifier); |
|
175
|
|
|
}else{ |
|
176
|
|
|
$compo=new Form(); |
|
177
|
|
|
$compo->attach("#".$this->identifier); |
|
178
|
|
|
} |
|
179
|
|
|
foreach ($this->_fields as $field){ |
|
180
|
|
|
if($field instanceof HtmlFormField){ |
|
181
|
|
|
$this->addCompoValidation($compo, $field); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
foreach ($this->content as $field){ |
|
185
|
|
|
if($field instanceof HtmlFormFields){ |
|
186
|
|
|
$items=$field->getItems(); |
|
187
|
|
|
foreach ($items as $_field){ |
|
188
|
|
|
if($_field instanceof HtmlFormField) |
|
189
|
|
|
$this->addCompoValidation($compo, $_field); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
foreach ($this->_extraFieldRules as $field=>$fieldValidation){ |
|
194
|
|
|
$this->addExtraCompoValidation($compo, $fieldValidation); |
|
195
|
|
|
} |
|
196
|
|
|
$this->_runValidationParams($compo,$js); |
|
197
|
|
|
return $this->_bsComponent; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function getExtraFieldValidation($fieldname){ |
|
201
|
|
|
if(!isset($this->_extraFieldRules[$fieldname])){ |
|
202
|
|
|
$this->_extraFieldRules[$fieldname]=new FieldValidation($fieldname); |
|
203
|
|
|
} |
|
204
|
|
|
return $this->_extraFieldRules[$fieldname]; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function addValidationParam($paramName,$paramValue,$before="",$after=""){ |
|
208
|
|
|
$this->addBehavior($this->_validationParams, $paramName, $paramValue,$before,$after); |
|
209
|
|
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function setValidationParams(array $_validationParams) { |
|
213
|
|
|
$this->_validationParams=$_validationParams; |
|
214
|
|
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function hasValidationParams(){ |
|
218
|
|
|
return sizeof($this->_validationParams)>0; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function getValidationParams() { |
|
222
|
|
|
return $this->_validationParams; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function removeValidationParam($param){ |
|
226
|
|
|
unset($this->_validationParams[$param]); |
|
227
|
|
|
return $this; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
} |
|
231
|
|
|
|