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

HtmlForm::setEqualWidth()   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 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
/**
9
 * Semantic Form component
10
 * @see http://semantic-ui.com/collections/form.html
11
 * @author jc
12
 * @version 1.001
13
 */
14
class HtmlForm extends HtmlSemCollection{
15
16
	use FieldsTrait;
17
18
	protected $_fields;
19
20
	public function __construct( $identifier, $elements=array()){
21
		parent::__construct( $identifier, "form", "ui form");
22
		$this->setProperty("name", $this->identifier);
23
		$this->_fields=array();
24
		$this->addItems($elements);
25
	}
26
27
	public function addHeader($title,$niveau=1,$dividing=true){
28
		$header=new HtmlHeader("",$niveau,$title);
29
		if($dividing)
30
			$header->setDividing();
31
		return $this->addItem($header);
32
	}
33
34
	public function addFields($fields,$label=NULL){
35
		if(!$fields instanceof HtmlFormFields){
36
			if(\is_array($fields)===false){
37
				$fields = \func_get_args();
38
				$end=\end($fields);
39
				if(\is_string($end)){
40
					$label=$end;
41
					\array_pop($fields);
42
				}else $label=NULL;
43
			}
44
			$this->_fields=\array_merge($this->_fields,$fields);
45
			$fields=new HtmlFormFields("fields-".$this->identifier."-".$this->count(),$fields);
46
		}
47
		if(isset($label))
48
		 $fields=new HtmlFormField("", $fields,$label);
49
		$this->addItem($fields);
50
		return $fields;
51
	}
52
53
	public function addItem($item){
54
		$item=parent::addItem($item);
55
		if(\is_subclass_of($item, HtmlFormField::class)===true){
56
			$this->_fields[]=$item;
57
		}
58
		return $item;
59
	}
60
61
	public function getField($index){
62
		if(\is_string($index)){
63
			$field=$this->getElementById($index, $this->_fields);
64
		}else{
65
			$field=$this->_fields[$index];
66
		}
67
		return $field;
68
	}
69
70
	/**
71
	 * automatically divide fields to be equal width
72
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
73
	 */
74
	public function setEqualWidth(){
75
		return $this->addToProperty("class", "equal width");
76
	}
77
78
	/**
79
	 * Adds a field (alias for addItem)
80
	 * @param HtmlFormField $field
81
	 * @return \Ajax\common\html\HtmlDoubleElement
82
	 */
83
	public function addField($field){
84
		return $this->addItem($field);
85
	}
86
87
	/**
88
	 * @param string $identifier
89
	 * @param string $content
90
	 * @param string $header
91
	 * @param string $icon
92
	 * @return \Ajax\semantic\html\collections\HtmlMessage
93
	 */
94
	public function addMessage($identifier,$content,$header=NULL,$icon=NULL){
95
		$message=new HtmlMessage($identifier,$content);
96
		if(isset($header))
97
			$message->addHeader($header);
98
		if(isset($icon))
99
			$message->setIcon($icon);
100
		return $this->addItem($message);
101
	}
102
103
	public function addInputs($inputs,$fieldslabel=null){
104
		$fields=array();
105
		foreach ($inputs as $input){
106
			\extract($input);
107
			$f=new HtmlFormInput("","");
108
			$f->fromArray($input);
109
			$fields[]=$f;
110
		}
111
		return $this->addFields($fields,$fieldslabel);
112
	}
113
}