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
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Semantic Form component |
15
|
|
|
* @see http://semantic-ui.com/collections/form.html |
16
|
|
|
* @author jc |
17
|
|
|
* @version 1.001 |
18
|
|
|
*/ |
19
|
|
|
class HtmlForm extends HtmlSemCollection { |
20
|
|
|
|
21
|
|
|
use FieldsTrait; |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $_fields; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $_validationParams; |
31
|
|
|
|
32
|
|
|
public function __construct($identifier, $elements=array()) { |
33
|
|
|
parent::__construct($identifier, "form", "ui form"); |
34
|
|
|
$this->_states=[ State::ERROR,State::SUCCESS,State::WARNING,State::DISABLED ]; |
35
|
|
|
$this->setProperty("name", $this->identifier); |
36
|
|
|
$this->_fields=array (); |
37
|
|
|
$this->_validationParams=[]; |
38
|
|
|
$this->addItems($elements); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $title |
43
|
|
|
* @param number $niveau |
44
|
|
|
* @param string $dividing |
45
|
|
|
* @return HtmlHeader |
46
|
|
|
*/ |
47
|
|
|
public function addHeader($title, $niveau=1, $dividing=true) { |
48
|
|
|
$header=new HtmlHeader("", $niveau, $title); |
49
|
|
|
if ($dividing) |
50
|
|
|
$header->setDividing(); |
51
|
|
|
return $this->addItem($header); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $caption |
56
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlForm |
57
|
|
|
*/ |
58
|
|
|
public function addDivider($caption=NULL){ |
59
|
|
|
return $this->addContent(new HtmlDivider("",$caption)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function addFields($fields=NULL, $label=NULL) { |
63
|
|
|
if (isset($fields)) { |
64
|
|
|
if (!$fields instanceof HtmlFormFields) { |
65
|
|
View Code Duplication |
if (\is_array($fields) === false) { |
|
|
|
|
66
|
|
|
$fields=\func_get_args(); |
67
|
|
|
$end=\end($fields); |
68
|
|
|
if (\is_string($end)) { |
69
|
|
|
$label=$end; |
70
|
|
|
\array_pop($fields); |
71
|
|
|
} else |
72
|
|
|
$label=NULL; |
73
|
|
|
} |
74
|
|
|
$this->_fields=\array_merge($this->_fields, $fields); |
75
|
|
|
$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count(), $fields); |
76
|
|
|
} |
77
|
|
|
if (isset($label)) |
78
|
|
|
$fields=new HtmlFormField("", $fields, $label); |
79
|
|
|
} else { |
80
|
|
|
$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count()); |
81
|
|
|
} |
82
|
|
|
$this->addItem($fields); |
83
|
|
|
return $fields; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function addItem($item) { |
87
|
|
|
$item=parent::addItem($item); |
88
|
|
|
if (\is_subclass_of($item, HtmlFormField::class) === true) { |
89
|
|
|
$this->_fields[]=$item; |
90
|
|
|
} |
91
|
|
|
return $item; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getField($index) { |
95
|
|
|
if (\is_string($index)) { |
96
|
|
|
$field=$this->getElementById($index, $this->_fields); |
97
|
|
|
} else { |
98
|
|
|
$field=$this->_fields[$index]; |
99
|
|
|
} |
100
|
|
|
return $field; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* automatically divide fields to be equal width |
105
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlForm |
106
|
|
|
*/ |
107
|
|
|
public function setEqualWidth() { |
108
|
|
|
return $this->addToProperty("class", "equal width"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Adds a field (alias for addItem) |
113
|
|
|
* @param HtmlFormField $field |
114
|
|
|
* @return \Ajax\common\html\HtmlDoubleElement |
115
|
|
|
*/ |
116
|
|
|
public function addField($field) { |
117
|
|
|
return $this->addItem($field); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){ |
121
|
|
|
$field=$this->getItem($index); |
122
|
|
|
if(isset($field)){ |
123
|
|
|
$field->addRule($type,$prompt,$value); |
124
|
|
|
} |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
* @param string $identifier |
131
|
|
|
* @param string $content |
132
|
|
|
* @param string $header |
133
|
|
|
* @param string $icon |
134
|
|
|
* @param string $type |
135
|
|
|
* @return \Ajax\semantic\html\collections\HtmlMessage |
136
|
|
|
*/ |
137
|
|
|
public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) { |
138
|
|
|
$message=new HtmlMessage($identifier, $content); |
139
|
|
|
if (isset($header)) |
140
|
|
|
$message->addHeader($header); |
141
|
|
|
if (isset($icon)) |
142
|
|
|
$message->setIcon($icon); |
143
|
|
|
if (isset($type)) |
144
|
|
|
$message->setStyle($type); |
145
|
|
|
return $this->addItem($message); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function run(JsUtils $js) { |
149
|
|
|
$hasValidation=false; |
150
|
|
|
$compo=$js->semantic()->form("#".$this->identifier); |
151
|
|
|
foreach ($this->_fields as $field){ |
152
|
|
|
$validation=$field->getValidation(); |
153
|
|
|
if(isset($validation)){ |
154
|
|
|
$validation->setIdentifier($field->getField()->getIdentifier()); |
155
|
|
|
$compo->addFieldValidation($validation); |
156
|
|
|
$hasValidation=true; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
if($hasValidation===false){ |
160
|
|
|
return parent::run($js); |
161
|
|
|
} |
162
|
|
|
$compo->addParams($this->_validationParams); |
163
|
|
|
$this->_bsComponent=$compo; |
164
|
|
|
$this->addEventsOnRun($js); |
165
|
|
|
return $this->_bsComponent; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function setLoading() { |
169
|
|
|
return $this->addToProperty("class", "loading"); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function addErrorMessage(){ |
173
|
|
|
return $this->addContent((new HtmlMessage(""))->setError()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function jsState($state) { |
177
|
|
|
return $this->jsDoJquery("addClass", $state); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function setValidationParams(array $_validationParams) { |
181
|
|
|
$this->_validationParams=$_validationParams; |
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
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.