Passed
Push — master ( cd7b92...8b3dbf )
by Jean-Christophe
03:06
created

HtmlFormFields::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\semantic\html\collections\form;
4
5
use Ajax\semantic\html\base\HtmlSemCollection;
6
use Ajax\semantic\html\base\constants\Wide;
7
use Ajax\JsUtils;
8
9
use Ajax\semantic\html\base\HtmlSemDoubleElement;
10
use Ajax\semantic\html\collections\form\traits\FieldsTrait;
11
12
class HtmlFormFields extends HtmlSemCollection {
13
14
	use FieldsTrait;
15
	protected $_equalWidth;
16
	protected $_name;
17
18
	public function __construct($identifier, $fields=array(), $equalWidth=true) {
19
		parent::__construct($identifier, "div");
20
		$this->_equalWidth=$equalWidth;
21
		$this->addItems($fields);
22
	}
23
24
	public function addFields($fields=NULL, $label=NULL) {
25
		if (!$fields instanceof HtmlFormFields) {
26
			if (!\is_array($fields)) {
27
				$fields=\func_get_args();
28
				$end=\end($fields);
29
				if (\is_string($end)) {
30
					$label=$end;
31
					\array_pop($fields);
32
				} else
33
					$label=NULL;
34
			}
35
		}
36
		if (isset($label))
37
			$this->setLabel($label);
38
		foreach ( $fields as $field ) {
39
			$this->addItem($field);
40
		}
41
		return $this;
42
	}
43
44
	/**
45
	 *
46
	 * @param string|HtmlSemDoubleElement $label
47
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
48
	 */
49
	public function setLabel($label) {
50
		$labelO=$label;
51
		if (\is_string($label)) {
52
			$labelO=new HtmlSemDoubleElement("", "label", "", $label);
53
		}
54
		$this->insertItem($labelO, 0);
55
		return $labelO;
56
	}
57
58
	public function addItem($item) {
59
		$item=parent::addItem($item);
60
		if($item instanceof HtmlFormField)
61
			$item->setContainer($this);
62
		return $item;
63
	}
64
	
65
	/**
66
	 * @return HtmlFormField
67
	 */
68
	public function getItem($index){
69
		return parent::getItem($index);
70
	}
71
72
	public function compile(JsUtils $js=NULL, &$view=NULL) {
73
		if ($this->_equalWidth) {
74
			$count=$this->count();
75
			$this->addToProperty("class", Wide::getConstants()["W".$count]." fields");
76
		} else
77
			$this->addToProperty("class", "fields");
78
		return parent::compile($js, $view);
79
	}
80
81
	public function setWidth($index, $width) {
82
		$this->_equalWidth=false;
83
		return $this->getItem($index)->setWidth($width);
84
	}
85
86
	public function setInline() {
87
		$this->_equalWidth=false;
88
		$this->addToProperty("class", "inline");
89
		return $this;
90
	}
91
92
	public function setGrouped() {
93
		$this->_equalWidth=false;
94
		$this->addToProperty("class", "grouped");
95
	}
96
97
	public function getName() {
98
		return $this->_name;
99
	}
100
101
	public function setName($_name) {
102
		$this->_name=$_name;
103
		return $this;
104
	}
105
106
107
	public static function radios($identifier,$name, $items=array(), $label=NULL, $value=null, $type=NULL) {
108
		$fields=array ();
109
		$i=0;
110
		foreach ( $items as $val => $caption ) {
111
			$itemO=new HtmlFormRadio($name."-".$i++, $name, $caption, $val, $type);
112
			if ($val===$value) {
113
				$itemO->getDataField()->setProperty("checked", "");
114
			}
115
			$fields[]=$itemO;
116
		}
117
		$radios=new HtmlFormFields($identifier, $fields);
118
		if (isset($label))
119
			$radios->setLabel($label)->setProperty("for", $name);
120
		return $radios;
121
	}
122
123
	public static function checkeds($identifier,$name, $items=array(), $label=NULL, $values=array(), $type=NULL) {
124
		$fields=array ();
125
		$i=0;
126
		foreach ( $items as $val => $caption ) {
127
			$itemO=new HtmlFormCheckbox($name."-".$i++, $caption, $val, $type);
128
			$itemO->setName($name);
129
			if (\array_search($val, $values)!==false) {
130
				$itemO->getDataField()->setProperty("checked", "");
131
			}
132
			$fields[]=$itemO;
133
		}
134
		$radios=new HtmlFormFields($identifier, $fields);
135
		if (isset($label))
136
			$radios->setLabel($label)->setProperty("for", $name);
137
		return $radios;
138
	}
139
140
	public function setEqualWidth($_equalWidth) {
141
		$this->_equalWidth=$_equalWidth;
142
		return $this;
143
	}
144
	
145
	public function run(JsUtils $js){
146
		$result= parent::run($js);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
147
		//return $result->setItemSelector("[data-value]");
148
	}
149
}
150