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\service\AjaxCall; |
13
|
|
|
use Ajax\semantic\html\collections\form\traits\FormTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Semantic Form component |
17
|
|
|
* @see http://semantic-ui.com/collections/form.html |
18
|
|
|
* @author jc |
19
|
|
|
* @version 1.001 |
20
|
|
|
*/ |
21
|
|
|
class HtmlForm extends HtmlSemCollection { |
22
|
|
|
|
23
|
|
|
use FieldsTrait,FormTrait; |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $_fields; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $_validationParams; |
33
|
|
|
|
34
|
|
|
public function __construct($identifier, $elements=array()) { |
35
|
|
|
parent::__construct($identifier, "form", "ui form"); |
36
|
|
|
$this->_states=[ State::ERROR,State::SUCCESS,State::WARNING,State::DISABLED ]; |
37
|
|
|
$this->setProperty("name", $this->identifier); |
38
|
|
|
$this->_fields=array (); |
39
|
|
|
$this->_validationParams=[]; |
40
|
|
|
$this->addItems($elements); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function getForm(){ |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $title |
49
|
|
|
* @param number $niveau |
50
|
|
|
* @param string $dividing |
51
|
|
|
* @return HtmlHeader |
52
|
|
|
*/ |
53
|
|
|
public function addHeader($title, $niveau=1, $dividing=true) { |
54
|
|
|
$header=new HtmlHeader("", $niveau, $title); |
55
|
|
|
if ($dividing) |
56
|
|
|
$header->setDividing(); |
57
|
|
|
return $this->addItem($header); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $caption |
62
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlForm |
63
|
|
|
*/ |
64
|
|
|
public function addDivider($caption=NULL){ |
65
|
|
|
return $this->addContent(new HtmlDivider("",$caption)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function addFields($fields=NULL, $label=NULL) { |
69
|
|
|
if (isset($fields)) { |
70
|
|
|
if (!$fields instanceof HtmlFormFields) { |
71
|
|
View Code Duplication |
if (\is_array($fields) === false) { |
|
|
|
|
72
|
|
|
$fields=\func_get_args(); |
73
|
|
|
$end=\end($fields); |
74
|
|
|
if (\is_string($end)) { |
75
|
|
|
$label=$end; |
76
|
|
|
\array_pop($fields); |
77
|
|
|
} else |
78
|
|
|
$label=NULL; |
79
|
|
|
} |
80
|
|
|
$this->_fields=\array_merge($this->_fields, $fields); |
81
|
|
|
$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count(), $fields); |
82
|
|
|
} |
83
|
|
|
if (isset($label)) |
84
|
|
|
$fields=new HtmlFormField("", $fields, $label); |
85
|
|
|
} else { |
86
|
|
|
$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count()); |
87
|
|
|
} |
88
|
|
|
$this->addItem($fields); |
89
|
|
|
return $fields; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function addItem($item) { |
93
|
|
|
$item=parent::addItem($item); |
94
|
|
|
if (\is_subclass_of($item, HtmlFormField::class) === true) { |
95
|
|
|
$this->_fields[]=$item; |
96
|
|
|
} |
97
|
|
|
return $item; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getField($index) { |
101
|
|
|
if (\is_string($index)) { |
102
|
|
|
$field=$this->getElementById($index, $this->_fields); |
103
|
|
|
} else { |
104
|
|
|
$field=$this->_fields[$index]; |
105
|
|
|
} |
106
|
|
|
return $field; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* automatically divide fields to be equal width |
111
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlForm |
112
|
|
|
*/ |
113
|
|
|
public function setEqualWidth() { |
114
|
|
|
return $this->addToProperty("class", "equal width"); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Adds a field (alias for addItem) |
119
|
|
|
* @param HtmlFormField $field |
120
|
|
|
* @return \Ajax\common\html\HtmlDoubleElement |
121
|
|
|
*/ |
122
|
|
|
public function addField($field) { |
123
|
|
|
return $this->addItem($field); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* |
128
|
|
|
* @param string $identifier |
129
|
|
|
* @param string $content |
130
|
|
|
* @param string $header |
131
|
|
|
* @param string $icon |
132
|
|
|
* @param string $type |
133
|
|
|
* @return \Ajax\semantic\html\collections\HtmlMessage |
134
|
|
|
*/ |
135
|
|
|
public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) { |
136
|
|
|
$message=new HtmlMessage($identifier, $content); |
137
|
|
|
if (isset($header)) |
138
|
|
|
$message->addHeader($header); |
139
|
|
|
if (isset($icon)) |
140
|
|
|
$message->setIcon($icon); |
141
|
|
|
if (isset($type)) |
142
|
|
|
$message->setStyle($type); |
143
|
|
|
return $this->addItem($message); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function addCompoValidation($js,$compo,$field){ |
147
|
|
|
$validation=$field->getValidation(); |
148
|
|
|
if(isset($validation)){ |
149
|
|
|
if(isset($compo)===false){ |
150
|
|
|
$compo=$js->semantic()->form("#".$this->identifier); |
151
|
|
|
} |
152
|
|
|
$validation->setIdentifier($field->getDataField()->getIdentifier()); |
153
|
|
|
$compo->addFieldValidation($validation); |
154
|
|
|
} |
155
|
|
|
return $compo; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function compile(JsUtils $js=NULL,&$view=NULL){ |
159
|
|
|
if(\sizeof($this->_validationParams)>0) |
160
|
|
|
$this->setProperty("novalidate", ""); |
161
|
|
|
return parent::compile($js,$view); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function run(JsUtils $js) { |
165
|
|
|
$compo=NULL; |
166
|
|
|
foreach ($this->_fields as $field){ |
167
|
|
|
if($field instanceof HtmlFormField) |
168
|
|
|
$compo=$this->addCompoValidation($js, $compo, $field); |
169
|
|
|
} |
170
|
|
|
foreach ($this->content as $field){ |
171
|
|
|
if($field instanceof HtmlFormFields){ |
172
|
|
|
$items=$field->getItems(); |
173
|
|
|
foreach ($items as $_field){ |
174
|
|
|
if($_field instanceof HtmlFormField) |
175
|
|
|
$compo=$this->addCompoValidation($js, $compo, $_field); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
if(isset($compo)===false){ |
180
|
|
|
return parent::run($js); |
181
|
|
|
} |
182
|
|
|
if(isset($this->_validationParams["_ajaxSubmit"])){ |
183
|
|
|
if($this->_validationParams["_ajaxSubmit"] instanceof AjaxCall){ |
184
|
|
|
$compilation=$this->_validationParams["_ajaxSubmit"]->compile($js); |
185
|
|
|
$compilation=str_ireplace("\"","%quote%", $compilation); |
186
|
|
|
$this->onSuccess($compilation); |
187
|
|
|
unset($this->_validationParams["_ajaxSubmit"]); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
$compo->addParams($this->_validationParams); |
191
|
|
|
$this->_bsComponent=$compo; |
192
|
|
|
$this->addEventsOnRun($js); |
193
|
|
|
return $this->_bsComponent; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function addValidationParam($paramName,$paramValue){ |
197
|
|
|
$this->_validationParams[$paramName]=$paramValue; |
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function setValidationParams(array $_validationParams) { |
202
|
|
|
$this->_validationParams=$_validationParams; |
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.