Completed
Push — master ( 460f9e...2c7b4a )
by Jean-Christophe
03:15
created

HtmlForm::getValidationParams()   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\semantic\html\collections\form\traits\FormTrait;
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,FormTrait;
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->addItems($elements);
39
	}
40
41
	protected function getForm(){
42
		return $this;
43
	}
44
45
	/**
46
	 * @param string $title
47
	 * @param number $niveau
48
	 * @param string $dividing
49
	 * @return HtmlHeader
50
	 */
51
	public function addHeader($title, $niveau=1, $dividing=true) {
52
		$header=new HtmlHeader("", $niveau, $title);
53
		if ($dividing)
54
			$header->setDividing();
55
		return $this->addItem($header);
56
	}
57
58
	/**
59
	 * @param string $caption
60
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
61
	 */
62
	public function addDivider($caption=NULL){
63
		return $this->addContent(new HtmlDivider("",$caption));
64
	}
65
66
	public function addFields($fields=NULL, $label=NULL) {
67
		if (isset($fields)) {
68
			if (!$fields instanceof HtmlFormFields) {
69
				if (!\is_array($fields)) {
70
					$fields=\func_get_args();
71
					$end=\end($fields);
72
					if (\is_string($end)) {
73
						$label=$end;
74
						\array_pop($fields);
75
					} else
76
						$label=NULL;
77
				}
78
				$this->_fields=\array_merge($this->_fields, $fields);
79
				$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count(), $fields);
80
			}
81
			if (isset($label))
82
				$fields=new HtmlFormField("", $fields, $label);
83
		} else {
84
			$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count());
85
		}
86
		$this->addItem($fields);
87
		return $fields;
88
	}
89
90
	public function addItem($item) {
91
		$item=parent::addItem($item);
92
		if (\is_subclass_of($item, HtmlFormField::class) === true) {
93
			$this->_fields[]=$item;
94
		}
95
		return $item;
96
	}
97
98
	public function getField($index) {
99
		if (\is_string($index)) {
100
			$field=$this->getElementById($index, $this->_fields);
101
		} else {
102
			$field=$this->_fields[$index];
103
		}
104
		return $field;
105
	}
106
107
	/**
108
	 * automatically divide fields to be equal width
109
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
110
	 */
111
	public function setEqualWidth() {
112
		return $this->addToProperty("class", "equal width");
113
	}
114
115
	/**
116
	 * Adds a field (alias for addItem)
117
	 * @param HtmlFormField $field
118
	 * @return \Ajax\common\html\HtmlDoubleElement
119
	 */
120
	public function addField($field) {
121
		return $this->addItem($field);
122
	}
123
124
	/**
125
	 *
126
	 * @param string $identifier
127
	 * @param string $content
128
	 * @param string $header
129
	 * @param string $icon
130
	 * @param string $type
131
	 * @return \Ajax\semantic\html\collections\HtmlMessage
132
	 */
133
	public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) {
134
		$message=new HtmlMessage($identifier, $content);
135
		if (isset($header))
136
			$message->addHeader($header);
137
		if (isset($icon))
138
			$message->setIcon($icon);
139
		if (isset($type))
140
			$message->setStyle($type);
141
		return $this->addItem($message);
142
	}
143
144
145
146
	public function compile(JsUtils $js=NULL,&$view=NULL){
147
		if(\sizeof($this->_validationParams)>0)
148
			$this->setProperty("novalidate", "");
149
		return parent::compile($js,$view);
150
	}
151
152
	public function run(JsUtils $js) {
153
		$compo=NULL;
154
		foreach ($this->_fields as $field){
155
			if($field instanceof HtmlFormField)
156
				$compo=$this->addCompoValidation($js, $compo, $field);
157
		}
158
		foreach ($this->content as $field){
159
			if($field instanceof HtmlFormFields){
160
				$items=$field->getItems();
161
				foreach ($items as $_field){
162
					if($_field instanceof HtmlFormField)
163
						$compo=$this->addCompoValidation($js, $compo, $_field);
164
				}
165
			}
166
		}
167
		if(isset($compo)===false){
168
			return parent::run($js);
169
		}
170
		$this->_runValidationParams($compo,$js);
171
		return $this->_bsComponent;
172
	}
173
174
	public function addValidationParam($paramName,$paramValue){
175
		$this->_validationParams[$paramName]=$paramValue;
176
		return $this;
177
	}
178
179
	public function setValidationParams(array $_validationParams) {
180
		$this->_validationParams=$_validationParams;
181
		return $this;
182
	}
183
184
	public function getValidationParams() {
185
		return $this->_validationParams;
186
	}
187
188
}