Completed
Push — master ( a79a4a...ec2f1c )
by Jean-Christophe
02:56
created

HtmlForm::addFieldRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
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 View Code Duplication
				if (\is_array($fields) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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
	/**
122
	 *
123
	 * @param string $identifier
124
	 * @param string $content
125
	 * @param string $header
126
	 * @param string $icon
127
	 * @param string $type
128
	 * @return \Ajax\semantic\html\collections\HtmlMessage
129
	 */
130
	public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) {
131
		$message=new HtmlMessage($identifier, $content);
132
		if (isset($header))
133
			$message->addHeader($header);
134
		if (isset($icon))
135
			$message->setIcon($icon);
136
		if (isset($type))
137
			$message->setStyle($type);
138
		return $this->addItem($message);
139
	}
140
141
	private function addCompoValidation($js,$compo,$field){
142
		$validation=$field->getValidation();
143
		if(isset($validation)){
144
			if(isset($compo)===false){
145
				$compo=$js->semantic()->form("#".$this->identifier);
146
			}
147
			$validation->setIdentifier($field->getDataField()->getIdentifier());
148
			$compo->addFieldValidation($validation);
149
		}
150
		return $compo;
151
	}
152
153
	public function run(JsUtils $js) {
154
		$compo=NULL;
155
		foreach ($this->_fields as $field){
156
			if($field instanceof HtmlFormField)
157
				$compo=$this->addCompoValidation($js, $compo, $field);
158
		}
159
		foreach ($this->content as $field){
160
			if($field instanceof HtmlFormFields){
161
				$items=$field->getItems();
162
				foreach ($items as $_field){
163
					if($_field instanceof HtmlFormField)
164
						$compo=$this->addCompoValidation($js, $compo, $_field);
165
				}
166
			}
167
		}
168
		if(isset($compo)===false){
169
			return parent::run($js);
170
		}
171
		if(isset($this->_validationParams["_ajaxSubmit"])){
172
			if($this->_validationParams["_ajaxSubmit"] instanceof AjaxCall){
173
				$compilation=$this->_validationParams["_ajaxSubmit"]->compile($js);
174
				$compilation=str_ireplace("\"","%quote%", $compilation);
175
				$this->onSuccess($compilation);
176
				unset($this->_validationParams["_ajaxSubmit"]);
177
			}
178
		}
179
		$compo->addParams($this->_validationParams);
180
		$this->_bsComponent=$compo;
181
		$this->addEventsOnRun($js);
182
		return $this->_bsComponent;
183
	}
184
185
	public function setLoading() {
186
		return $this->addToProperty("class", "loading");
187
	}
188
189
	public function addErrorMessage(){
190
		return $this->addContent((new HtmlMessage(""))->setError());
191
	}
192
193
	public function jsState($state) {
194
		return $this->jsDoJquery("addClass", $state);
195
	}
196
197
	public function setValidationParams(array $_validationParams) {
198
		$this->_validationParams=$_validationParams;
199
		return $this;
200
	}
201
202
	public function submitOn($event,$identifier,$url,$responseElement){
203
		$elem=$this->getElementById($identifier, $this->content);
204
		if(isset($elem)){
205
			$elem->addEvent($event, "$('#".$this->identifier."').form('validate form');");
206
			$this->_validationParams["_ajaxSubmit"]=new AjaxCall("postForm", ["form"=>$this->identifier,"responseElement"=>$responseElement,"url"=>$url]);
207
		}
208
		return $this;
209
	}
210
211
	public function submitOnClick($identifier,$url,$responseElement){
212
		return $this->submitOn("click", $identifier, $url, $responseElement);
213
	}
214
215
	public function addSubmit($identifier,$value,$CssStyle=NULL,$url=NULL,$responseElement=NULL){
216
		$bt=$this->addButton($identifier, $value,$CssStyle);
217
		if(isset($url) && isset($responseElement))
218
			$this->submitOnClick($identifier, $url, $responseElement);
219
		return $bt;
220
	}
221
222
	/**
223
	 * Callback on each valid field
224
	 * @param string $jsCode
225
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
226
	 */
227
	public function onValid($jsCode){
228
		$this->_validationParams["onValid"]="%function(){".$jsCode."}%";
229
		return $this;
230
	}
231
232
	/**
233
	 * Callback if a form is all valid
234
	 * @param string $jsCode can use event and fields parameters
235
	 * @return HtmlForm
236
	 */
237
	public function onSuccess($jsCode){
238
		$this->_validationParams["onSuccess"]="%function(evt,fields){".$jsCode."}%";
239
		return $this;
240
	}
241
242
}