Completed
Push — master ( c6c18d...79b035 )
by Jean-Christophe
03:25
created

HtmlForm::addButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 4
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\elements\HtmlButton;
9
use Ajax\semantic\html\base\constants\State;
10
/**
11
 * Semantic Form component
12
 * @see http://semantic-ui.com/collections/form.html
13
 * @author jc
14
 * @version 1.001
15
 */
16
class HtmlForm extends HtmlSemCollection{
17
18
	use FieldsTrait;
19
20
	protected $_fields;
21
22
	public function __construct( $identifier, $elements=array()){
23
		parent::__construct( $identifier, "form", "ui form");
24
		$this->_states=[State::ERROR,State::SUCCESS,State::WARNING,State::DISABLED];
25
		$this->setProperty("name", $this->identifier);
26
		$this->_fields=array();
27
		$this->addItems($elements);
28
	}
29
30
	public function addHeader($title,$niveau=1,$dividing=true){
31
		$header=new HtmlHeader("",$niveau,$title);
32
		if($dividing)
33
			$header->setDividing();
34
		return $this->addItem($header);
35
	}
36
37
	public function addFields($fields,$label=NULL){
38
		if(!$fields instanceof HtmlFormFields){
39
			if(\is_array($fields)===false){
40
				$fields = \func_get_args();
41
				$end=\end($fields);
42
				if(\is_string($end)){
43
					$label=$end;
44
					\array_pop($fields);
45
				}else $label=NULL;
46
			}
47
			$this->_fields=\array_merge($this->_fields,$fields);
48
			$fields=new HtmlFormFields("fields-".$this->identifier."-".$this->count(),$fields);
49
		}
50
		if(isset($label))
51
		 $fields=new HtmlFormField("", $fields,$label);
52
		$this->addItem($fields);
53
		return $fields;
54
	}
55
56
	public function addItem($item){
57
		$item=parent::addItem($item);
58
		if(\is_subclass_of($item, HtmlFormField::class)===true){
59
			$this->_fields[]=$item;
60
		}
61
		return $item;
62
	}
63
64
	public function getField($index){
65
		if(\is_string($index)){
66
			$field=$this->getElementById($index, $this->_fields);
67
		}else{
68
			$field=$this->_fields[$index];
69
		}
70
		return $field;
71
	}
72
73
	/**
74
	 * automatically divide fields to be equal width
75
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
76
	 */
77
	public function setEqualWidth(){
78
		return $this->addToProperty("class", "equal width");
79
	}
80
81
	/**
82
	 * Adds a field (alias for addItem)
83
	 * @param HtmlFormField $field
84
	 * @return \Ajax\common\html\HtmlDoubleElement
85
	 */
86
	public function addField($field){
87
		return $this->addItem($field);
88
	}
89
90
	/**
91
	 * @param string $identifier
92
	 * @param string $content
93
	 * @param string $header
94
	 * @param string $icon
95
	 * @param string $type
96
	 * @return \Ajax\semantic\html\collections\HtmlMessage
97
	 */
98
	public function addMessage($identifier,$content,$header=NULL,$icon=NULL,$type=NULL){
99
		$message=new HtmlMessage($identifier,$content);
100
		if(isset($header))
101
			$message->addHeader($header);
102
		if(isset($icon))
103
			$message->setIcon($icon);
104
		if(isset($type))
105
			$message->setStyle($type);
106
		return $this->addItem($message);
107
	}
108
109
	public function addInputs($inputs,$fieldslabel=null){
110
		$fields=array();
111
		foreach ($inputs as $input){
112
			\extract($input);
113
			$f=new HtmlFormInput("","");
114
			$f->fromArray($input);
115
			$fields[]=$f;
116
		}
117
		return $this->addFields($fields,$fieldslabel);
118
	}
119
120
	public function addDropdown($identifier,$items=array(), $label=NULL,$value=NULL,$multiple=false){
121
		return $this->addItem(new HtmlFormDropdown($identifier,$items,$label,$value,$multiple));
122
	}
123
124
	public function addInput($identifier, $label=NULL,$type="text",$value=NULL,$placeholder=NULL){
125
		return $this->addItem(new HtmlFormInput($identifier,$label,$type,$value,$placeholder));
126
	}
127
128
	public function addButton($identifier,$value,$cssStyle=NULL,$onClick=NULL){
129
		return $this->addItem(new HtmlButton($identifier,$value,$cssStyle,$onClick));
130
	}
131
132
	public function addCheckbox($identifier, $label=NULL,$value=NULL,$type=NULL){
133
		return $this->addItem(new HtmlFormCheckbox($identifier,$label,$value,$type));
134
	}
135
136
	public function setLoading(){
137
		return $this->addToProperty("class", "loading");
138
	}
139
140
	public function jsState($state){
141
		return $this->jsDoJquery("addClass",$state);
142
	}
143
}