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
|
|
|
use Phalcon\Mvc\View; |
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
|
|
View Code Duplication |
if(!$fields instanceof HtmlFormFields){ |
|
|
|
|
26
|
|
|
if(\is_array($fields)===false){ |
27
|
|
|
$fields = \func_get_args(); |
28
|
|
|
$end=\end($fields); |
29
|
|
|
if(\is_string($end)){ |
30
|
|
|
$label=$end; |
31
|
|
|
\array_pop($fields); |
32
|
|
|
}else $label=NULL; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
if(isset($label)) |
36
|
|
|
$this->setLabel($label); |
37
|
|
|
foreach ($fields as $field){ |
|
|
|
|
38
|
|
|
$this->addItem($field); |
39
|
|
|
} |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string|HtmlSemDoubleElement $label |
45
|
|
|
* @return \Ajax\semantic\html\base\HtmlSemDoubleElement |
46
|
|
|
*/ |
47
|
|
|
public function setLabel($label){ |
48
|
|
|
$labelO=$label; |
49
|
|
|
if(\is_string($label)){ |
50
|
|
|
$labelO=new HtmlSemDoubleElement("","label","",$label); |
51
|
|
|
} |
52
|
|
|
$this->insertItem($labelO,0); |
53
|
|
|
return $labelO; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
public function addItem($item){ |
56
|
|
|
$item=parent::addItem($item); |
57
|
|
|
$item->setContainer($this); |
58
|
|
|
return $item; |
59
|
|
|
} |
60
|
|
|
public function compile(JsUtils $js=NULL,View $view=NULL){ |
61
|
|
|
if($this->_equalWidth){ |
62
|
|
|
$count=$this->count(); |
63
|
|
|
$this->addToProperty("class", Wide::getConstants()["W".$count]." fields"); |
64
|
|
|
}else |
65
|
|
|
$this->addToProperty("class","fields"); |
66
|
|
|
return parent::compile($js,$view); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setWidth($index,$width){ |
70
|
|
|
$this->_equalWidth=false; |
71
|
|
|
return $this->getItem($index)->setWidth($width); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function setInline(){ |
75
|
|
|
$this->_equalWidth=false; |
76
|
|
|
$this->addToProperty("class", "inline"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setGrouped(){ |
80
|
|
|
$this->_equalWidth=false; |
81
|
|
|
$this->addToProperty("class", "grouped"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getName() { |
85
|
|
|
return $this->_name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setName($_name) { |
89
|
|
|
$this->_name=$_name; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function radios($name,$items=array(),$label=NULL){ |
94
|
|
|
$fields=array(); |
95
|
|
|
$i=0; |
96
|
|
|
foreach ($items as $item){ |
97
|
|
|
$fields[]=new HtmlFormRadio($name."-".$i++,$name,$item,$item); |
98
|
|
|
} |
99
|
|
|
$radios=new HtmlFormFields("fields-".$name,$fields); |
100
|
|
|
if(isset($label)) |
101
|
|
|
$radios->setLabel($label)->setProperty("for", $name); |
102
|
|
|
return $radios; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function setEqualWidth($_equalWidth) { |
106
|
|
|
$this->_equalWidth=$_equalWidth; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
} |
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.