Completed
Push — master ( 411df4...9281ef )
by Jean-Christophe
03:04
created

HtmlForm::addErrorMessage()   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
13
/**
14
 * Semantic Form component
15
 * @see http://semantic-ui.com/collections/form.html
16
 * @author jc
17
 * @version 1.001
18
 */
19
class HtmlForm extends HtmlSemCollection {
20
21
	use FieldsTrait;
22
	/**
23
	 * @var array
24
	 */
25
	protected $_fields;
26
27
	/**
28
	 * @var array
29
	 */
30
	protected $_validationParams;
31
32
	public function __construct($identifier, $elements=array()) {
33
		parent::__construct($identifier, "form", "ui form");
34
		$this->_states=[ State::ERROR,State::SUCCESS,State::WARNING,State::DISABLED ];
35
		$this->setProperty("name", $this->identifier);
36
		$this->_fields=array ();
37
		$this->_validationParams=[];
38
		$this->addItems($elements);
39
	}
40
41
	/**
42
	 * @param string $title
43
	 * @param number $niveau
44
	 * @param string $dividing
45
	 * @return HtmlHeader
46
	 */
47
	public function addHeader($title, $niveau=1, $dividing=true) {
48
		$header=new HtmlHeader("", $niveau, $title);
49
		if ($dividing)
50
			$header->setDividing();
51
		return $this->addItem($header);
52
	}
53
54
	/**
55
	 * @param string $caption
56
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
57
	 */
58
	public function addDivider($caption=NULL){
59
		return $this->addContent(new HtmlDivider("",$caption));
60
	}
61
62
	public function addFields($fields=NULL, $label=NULL) {
63
		if (isset($fields)) {
64
			if (!$fields instanceof HtmlFormFields) {
65 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...
66
					$fields=\func_get_args();
67
					$end=\end($fields);
68
					if (\is_string($end)) {
69
						$label=$end;
70
						\array_pop($fields);
71
					} else
72
						$label=NULL;
73
				}
74
				$this->_fields=\array_merge($this->_fields, $fields);
75
				$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count(), $fields);
76
			}
77
			if (isset($label))
78
				$fields=new HtmlFormField("", $fields, $label);
79
		} else {
80
			$fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count());
81
		}
82
		$this->addItem($fields);
83
		return $fields;
84
	}
85
86
	public function addItem($item) {
87
		$item=parent::addItem($item);
88
		if (\is_subclass_of($item, HtmlFormField::class) === true) {
89
			$this->_fields[]=$item;
90
		}
91
		return $item;
92
	}
93
94
	public function getField($index) {
95
		if (\is_string($index)) {
96
			$field=$this->getElementById($index, $this->_fields);
97
		} else {
98
			$field=$this->_fields[$index];
99
		}
100
		return $field;
101
	}
102
103
	/**
104
	 * automatically divide fields to be equal width
105
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
106
	 */
107
	public function setEqualWidth() {
108
		return $this->addToProperty("class", "equal width");
109
	}
110
111
	/**
112
	 * Adds a field (alias for addItem)
113
	 * @param HtmlFormField $field
114
	 * @return \Ajax\common\html\HtmlDoubleElement
115
	 */
116
	public function addField($field) {
117
		return $this->addItem($field);
118
	}
119
120
	public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){
121
		$field=$this->getItem($index);
122
		if(isset($field)){
123
			$field->addRule($type,$prompt,$value);
124
		}
125
		return $this;
126
	}
127
128
	/**
129
	 *
130
	 * @param string $identifier
131
	 * @param string $content
132
	 * @param string $header
133
	 * @param string $icon
134
	 * @param string $type
135
	 * @return \Ajax\semantic\html\collections\HtmlMessage
136
	 */
137
	public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) {
138
		$message=new HtmlMessage($identifier, $content);
139
		if (isset($header))
140
			$message->addHeader($header);
141
		if (isset($icon))
142
			$message->setIcon($icon);
143
		if (isset($type))
144
			$message->setStyle($type);
145
		return $this->addItem($message);
146
	}
147
148
	public function run(JsUtils $js) {
149
		$compo=NULL;
150
		foreach ($this->_fields as $field){
151
			$validation=$field->getValidation();
152
			if(isset($validation)){
153
				if(isset($compo)===false){
154
					$compo=$js->semantic()->form("#".$this->identifier);
155
				}
156
				$validation->setIdentifier($field->getField()->getIdentifier());
157
				$compo->addFieldValidation($validation);
158
			}
159
		}
160
		if(isset($compo)===false){
161
			return parent::run($js);
162
		}
163
		$compo->addParams($this->_validationParams);
164
		$this->_bsComponent=$compo;
165
		$this->addEventsOnRun($js);
166
		return $this->_bsComponent;
167
	}
168
169
	public function setLoading() {
170
		return $this->addToProperty("class", "loading");
171
	}
172
173
	public function addErrorMessage(){
174
		return $this->addContent((new HtmlMessage(""))->setError());
175
	}
176
177
	public function jsState($state) {
178
		return $this->jsDoJquery("addClass", $state);
179
	}
180
181
	public function setValidationParams(array $_validationParams) {
182
		$this->_validationParams=$_validationParams;
183
		return $this;
184
	}
185
186
}