Completed
Push — master ( aa0ebb...030ef0 )
by Jean-Christophe
03:09
created

HtmlForm::getForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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) {
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...
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 run(JsUtils $js) {
159
		$compo=NULL;
160
		foreach ($this->_fields as $field){
161
			if($field instanceof HtmlFormField)
162
				$compo=$this->addCompoValidation($js, $compo, $field);
163
		}
164
		foreach ($this->content as $field){
165
			if($field instanceof HtmlFormFields){
166
				$items=$field->getItems();
167
				foreach ($items as $_field){
168
					if($_field instanceof HtmlFormField)
169
						$compo=$this->addCompoValidation($js, $compo, $_field);
170
				}
171
			}
172
		}
173
		if(isset($compo)===false){
174
			return parent::run($js);
175
		}
176
		if(isset($this->_validationParams["_ajaxSubmit"])){
177
			if($this->_validationParams["_ajaxSubmit"] instanceof AjaxCall){
178
				$compilation=$this->_validationParams["_ajaxSubmit"]->compile($js);
179
				$compilation=str_ireplace("\"","%quote%", $compilation);
180
				$this->onSuccess($compilation);
181
				unset($this->_validationParams["_ajaxSubmit"]);
182
			}
183
		}
184
		$compo->addParams($this->_validationParams);
185
		$this->_bsComponent=$compo;
186
		$this->addEventsOnRun($js);
187
		return $this->_bsComponent;
188
	}
189
190
	public function addValidationParam($paramName,$paramValue){
191
		$this->_validationParams[$paramName]=$paramValue;
192
		return $this;
193
	}
194
195
	public function setValidationParams(array $_validationParams) {
196
		$this->_validationParams=$_validationParams;
197
		return $this;
198
	}
199
}