Completed
Push — master ( 7abd10...04d259 )
by Jean-Christophe
03:18
created

HtmlForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
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\semantic\html\collections\form\traits\FormTrait;
13
use Ajax\semantic\components\Form;
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->addItems($elements);
40
		$this->_validationParams=[];
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
				if (!\is_array($fields)) {
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
147
148
	public function compile(JsUtils $js=NULL,&$view=NULL){
149
		if(\sizeof($this->_validationParams)>0)
150
			$this->setProperty("novalidate", "");
151
		return parent::compile($js,$view);
152
	}
153
154
	public function run(JsUtils $js) {
155
		if(isset($js)){
156
			$compo=$js->semantic()->form("#".$this->identifier);
157
		}else{
158
			$compo=new Form();
159
			$compo->attach("#".$this->identifier);
160
		}
161
		foreach ($this->_fields as $field){
162
			if($field instanceof HtmlFormField){
163
				$compo=$this->addCompoValidation($compo, $field);
164
			}
165
		}
166
		foreach ($this->content as $field){
167
			if($field instanceof HtmlFormFields){
168
				$items=$field->getItems();
169
				foreach ($items as $_field){
170
					if($_field instanceof HtmlFormField)
171
						$compo=$this->addCompoValidation($compo, $_field);
172
				}
173
			}
174
		}
175
		$this->_runValidationParams($compo,$js);
176
		return $this->_bsComponent;
177
	}
178
179
	public function addValidationParam($paramName,$paramValue){
180
		$this->_validationParams[$paramName]=$paramValue;
181
		return $this;
182
	}
183
184
	public function setValidationParams(array $_validationParams) {
185
		$this->_validationParams=$_validationParams;
186
		return $this;
187
	}
188
189
	public function getValidationParams() {
190
		return $this->_validationParams;
191
	}
192
193
	public function removeValidationParam($param){
194
		unset($this->_validationParams[$param]);
195
		return $this;
196
	}
197
198
}