Completed
Push — master ( 364809...26095a )
by Jean-Christophe
10:25
created

HtmlForm::addHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
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
14
/**
15
 * Semantic Form component
16
 * @see http://semantic-ui.com/collections/form.html
17
 * @author jc
18
 * @version 1.001
19
 */
20
class HtmlForm extends HtmlSemCollection {
21
22
	use FieldsTrait;
23
	/**
24
	 * @var array
25
	 */
26
	protected $_fields;
27
28
	/**
29
	 * @var array
30
	 */
31
	protected $_validationParams;
32
33
	public function __construct($identifier, $elements=array()) {
34
		parent::__construct($identifier, "form", "ui form");
35
		$this->_states=[ State::ERROR,State::SUCCESS,State::WARNING,State::DISABLED ];
36
		$this->setProperty("name", $this->identifier);
37
		$this->_fields=array ();
38
		$this->_validationParams=[];
39
		$this->addItems($elements);
40
	}
41
42
	/**
43
	 * @param string $title
44
	 * @param number $niveau
45
	 * @param string $dividing
46
	 * @return HtmlHeader
47
	 */
48
	public function addHeader($title, $niveau=1, $dividing=true) {
49
		$header=new HtmlHeader("", $niveau, $title);
50
		if ($dividing)
51
			$header->setDividing();
52
		return $this->addItem($header);
53
	}
54
55
	/**
56
	 * @param string $caption
57
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
58
	 */
59
	public function addDivider($caption=NULL){
60
		return $this->addContent(new HtmlDivider("",$caption));
61
	}
62
63
	public function addFields($fields=NULL, $label=NULL) {
64
		if (isset($fields)) {
65
			if (!$fields instanceof HtmlFormFields) {
66
				if (\is_array($fields) === false) {
67
					$fields=\func_get_args();
68
					$end=\end($fields);
69
					if (\is_string($end)) {
70
						$label=$end;
71
						\array_pop($fields);
72
					} else
73
						$label=NULL;
74
				}
75
				$this->_fields=\array_merge($this->_fields, $fields);
76
				$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count(), $fields);
77
			}
78
			if (isset($label))
79
				$fields=new HtmlFormField("", $fields, $label);
80
		} else {
81
			$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count());
82
		}
83
		$this->addItem($fields);
84
		return $fields;
85
	}
86
87
	public function addItem($item) {
88
		$item=parent::addItem($item);
89
		if (\is_subclass_of($item, HtmlFormField::class) === true) {
90
			$this->_fields[]=$item;
91
		}
92
		return $item;
93
	}
94
95
	public function getField($index) {
96
		if (\is_string($index)) {
97
			$field=$this->getElementById($index, $this->_fields);
98
		} else {
99
			$field=$this->_fields[$index];
100
		}
101
		return $field;
102
	}
103
104
	/**
105
	 * automatically divide fields to be equal width
106
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
107
	 */
108
	public function setEqualWidth() {
109
		return $this->addToProperty("class", "equal width");
110
	}
111
112
	/**
113
	 * Adds a field (alias for addItem)
114
	 * @param HtmlFormField $field
115
	 * @return \Ajax\common\html\HtmlDoubleElement
116
	 */
117
	public function addField($field) {
118
		return $this->addItem($field);
119
	}
120
121
	public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){
122
		$field=$this->getItem($index);
123
		if(isset($field)){
124
			$field->addRule($type,$prompt,$value);
125
		}
126
		return $this;
127
	}
128
129
	public function addFieldRules($index,$rules){
130
		$field=$this->getItem($index);
131
		if(isset($field)){
132
			$field->addRules($rules);
133
		}
134
		return $this;
135
	}
136
137
	/**
138
	 *
139
	 * @param string $identifier
140
	 * @param string $content
141
	 * @param string $header
142
	 * @param string $icon
143
	 * @param string $type
144
	 * @return \Ajax\semantic\html\collections\HtmlMessage
145
	 */
146
	public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) {
147
		$message=new HtmlMessage($identifier, $content);
148
		if (isset($header))
149
			$message->addHeader($header);
150
		if (isset($icon))
151
			$message->setIcon($icon);
152
		if (isset($type))
153
			$message->setStyle($type);
154
		return $this->addItem($message);
155
	}
156
157
	private function addCompoValidation($js,$compo,$field){
158
		$validation=$field->getValidation();
159
		if(isset($validation)){
160
			if(isset($compo)===false){
161
				$compo=$js->semantic()->form("#".$this->identifier);
162
			}
163
			$validation->setIdentifier($field->getField()->getIdentifier());
164
			$compo->addFieldValidation($validation);
165
		}
166
		return $compo;
167
	}
168
169
	public function run(JsUtils $js) {
170
		$compo=NULL;
171
		foreach ($this->_fields as $field){
172
			if($field instanceof HtmlFormField)
173
				$compo=$this->addCompoValidation($js, $compo, $field);
174
		}
175
		foreach ($this->content as $field){
176
			if($field instanceof HtmlFormFields){
177
				$items=$field->getItems();
178
				foreach ($items as $_field){
179
					if($_field instanceof HtmlFormField)
180
						$compo=$this->addCompoValidation($js, $compo, $_field);
181
				}
182
			}
183
		}
184
		if(isset($compo)===false){
185
			return parent::run($js);
186
		}
187
		if(isset($this->_validationParams["_ajaxSubmit"])){
188
			if($this->_validationParams["_ajaxSubmit"] instanceof AjaxCall){
189
				$compilation=$this->_validationParams["_ajaxSubmit"]->compile($js);
190
				$compilation=str_ireplace("\"","%quote%", $compilation);
191
				$this->onSuccess($compilation);
192
				unset($this->_validationParams["_ajaxSubmit"]);
193
			}
194
		}
195
		$compo->addParams($this->_validationParams);
196
		$this->_bsComponent=$compo;
197
		$this->addEventsOnRun($js);
198
		return $this->_bsComponent;
199
	}
200
201
	public function setLoading() {
202
		return $this->addToProperty("class", "loading");
203
	}
204
205
	public function addErrorMessage(){
206
		return $this->addContent((new HtmlMessage(""))->setError());
207
	}
208
209
	public function jsState($state) {
210
		return $this->jsDoJquery("addClass", $state);
211
	}
212
213
	public function setValidationParams(array $_validationParams) {
214
		$this->_validationParams=$_validationParams;
215
		return $this;
216
	}
217
218
	public function submitOn($event,$identifier,$url,$responseElement){
219
		$elem=$this->getElementById($identifier, $this->content);
220
		if(isset($elem)){
221
			$elem->addEvent($event, "$('#".$this->identifier."').form('validate form');");
222
			$this->_validationParams["_ajaxSubmit"]=new AjaxCall("postForm", ["form"=>$this->identifier,"responseElement"=>$responseElement,"url"=>$url]);
223
		}
224
		return $this;
225
	}
226
227
	public function submitOnClick($identifier,$url,$responseElement){
228
		return $this->submitOn("click", $identifier, $url, $responseElement);
229
	}
230
231
	public function addSubmit($identifier,$value,$CssStyle=NULL,$url=NULL,$responseElement=NULL){
232
		$bt=$this->addButton($identifier, $value,$CssStyle);
233
		if(isset($url) && isset($responseElement))
234
			$this->submitOnClick($identifier, $url, $responseElement);
235
		return $bt;
236
	}
237
238
	/**
239
	 * Callback on each valid field
240
	 * @param string $jsCode
241
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
242
	 */
243
	public function onValid($jsCode){
244
		$this->_validationParams["onValid"]="%function(){".$jsCode."}%";
245
		return $this;
246
	}
247
248
	/**
249
	 * Callback if a form is all valid
250
	 * @param string $jsCode can use event and fields parameters
251
	 * @return HtmlForm
252
	 */
253
	public function onSuccess($jsCode){
254
		$this->_validationParams["onSuccess"]="%function(evt,fields){".$jsCode."}%";
255
		return $this;
256
	}
257
258
}