Completed
Push — master ( f4e97d...0ed595 )
by Jean-Christophe
03:13
created

HtmlForm::run()   B

Complexity

Conditions 8
Paths 30

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 7.1428
c 0
b 0
f 0
cc 8
eloc 15
nc 30
nop 1
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 compile(JsUtils $js=NULL,&$view=NULL){
159
		if(\sizeof($this->_validationParams)>0)
160
			$this->setProperty("novalidate", "");
161
		return parent::compile($js,$view);
162
	}
163
164
	public function run(JsUtils $js) {
165
		$compo=NULL;
166
		foreach ($this->_fields as $field){
167
			if($field instanceof HtmlFormField)
168
				$compo=$this->addCompoValidation($js, $compo, $field);
169
		}
170
		foreach ($this->content as $field){
171
			if($field instanceof HtmlFormFields){
172
				$items=$field->getItems();
173
				foreach ($items as $_field){
174
					if($_field instanceof HtmlFormField)
175
						$compo=$this->addCompoValidation($js, $compo, $_field);
176
				}
177
			}
178
		}
179
		if(isset($compo)===false){
180
			return parent::run($js);
181
		}
182
		$this->_runValidationParams($compo,$js);
183
		return $this->_bsComponent;
184
	}
185
186
	private function _runValidationParams(&$compo,JsUtils $js=NULL){
187
		if(isset($this->_validationParams["_ajaxSubmit"]) && $this->_validationParams["_ajaxSubmit"] instanceof AjaxCall){
188
			$compilation=$this->_validationParams["_ajaxSubmit"]->compile($js);
189
			$compilation=str_ireplace("\"","%quote%", $compilation);
190
			$this->onSuccess($compilation);
191
			unset($this->_validationParams["_ajaxSubmit"]);
192
		}
193
		$compo->addParams($this->_validationParams);
194
		$this->_bsComponent=$compo;
195
		$this->addEventsOnRun($js);
0 ignored issues
show
Bug introduced by
It seems like $js defined by parameter $js on line 186 can be null; however, Ajax\common\html\traits\...Trait::addEventsOnRun() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
196
	}
197
198
	public function addValidationParam($paramName,$paramValue){
199
		$this->_validationParams[$paramName]=$paramValue;
200
		return $this;
201
	}
202
203
	public function setValidationParams(array $_validationParams) {
204
		$this->_validationParams=$_validationParams;
205
		return $this;
206
	}
207
}