Completed
Push — master ( 7ebf5f...c286d3 )
by Jean-Christophe
02:51
created

FieldsTrait::addDropdownButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 5
1
<?php
2
3
namespace Ajax\semantic\html\collections\form\traits;
4
5
use Ajax\service\JArray;
6
use Ajax\semantic\html\collections\form\HtmlFormInput;
7
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
8
use Ajax\semantic\html\elements\HtmlButton;
9
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
10
use Ajax\semantic\html\collections\form\HtmlFormRadio;
11
use Ajax\semantic\html\collections\form\HtmlFormField;
12
use Ajax\common\html\html5\HtmlTextarea;
13
use Ajax\common\html\HtmlDoubleElement;
14
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
15
use Ajax\semantic\html\base\HtmlSemDoubleElement;
16
use Ajax\semantic\html\elements\HtmlButtonGroups;
17
18
/**
19
 * @author jc
20
 * @property string $identifier
21
 */
22
trait FieldsTrait {
23
	abstract public function addFields($fields=NULL,$label=NULL);
24
	abstract public function addItem($item);
25
26
	protected function createItem($value){
27
		if(\is_array($value)){
28
			$itemO=new HtmlFormInput(JArray::getDefaultValue($value, "id",""),JArray::getDefaultValue($value, "label",null),JArray::getDefaultValue($value, "type", "text"),JArray::getDefaultValue($value, "value",""),JArray::getDefaultValue($value, "placeholder",JArray::getDefaultValue($value, "label",null)));
29
			return $itemO;
30
		}elseif(\is_object($value)){
31
			$itemO=new HtmlFormField("field-".$this->identifier, $value);
32
			return $itemO;
33
		}else
34
			return new HtmlFormInput($value);
35
	}
36
37
	protected function createCondition($value){
38
		return \is_object($value)===false || $value instanceof \Ajax\semantic\html\elements\HtmlInput;
39
	}
40
41
	public function addInputs($inputs,$fieldslabel=null){
42
		$fields=array();
43
		foreach ($inputs as $input){
44
			\extract($input);
45
			$f=new HtmlFormInput("","");
46
			$f->fromArray($input);
47
			$fields[]=$f;
48
		}
49
		return $this->addFields($fields,$fieldslabel);
50
	}
51
52
	public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){
53
		$field=$this->getItem($index);
0 ignored issues
show
Bug introduced by
It seems like getItem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
		if(isset($field)){
55
			$field->addRule($type,$prompt,$value);
56
		}
57
		return $this;
58
	}
59
60
	public function addFieldRules($index,$rules){
61
		$field=$this->getItem($index);
0 ignored issues
show
Bug introduced by
It seems like getItem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
		if(isset($field)){
63
			$field->addRules($rules);
64
		}
65
		return $this;
66
	}
67
68
	/**
69
	 * Adds a new dropdown element
70
	 * @param string $identifier
71
	 * @param array $items
72
	 * @param string $label
73
	 * @param string $value
74
	 * @param boolean $multiple
75
	 * @return HtmlFormDropdown
76
	 */
77
	public function addDropdown($identifier,$items=array(), $label=NULL,$value=NULL,$multiple=false){
78
		return $this->addItem(new HtmlFormDropdown($identifier,$items,$label,$value,$multiple));
79
	}
80
81
	/**
82
	 * Adds a new button groups
83
	 * @param string $identifier
84
	 * @param array $elements
85
	 * @param boolean $asIcons
86
	 * @return HtmlButtonGroups
87
	 */
88
	public function addButtonGroups($identifier,$elements=[],$asIcons=false){
89
		return $this->addItem(new HtmlButtonGroups($identifier,$elements,$asIcons));
90
	}
91
92
	/**
93
	 * Adds a button with a dropdown button
94
	 * @param string $identifier
95
	 * @param string $value
96
	 * @param array $items
97
	 * @param boolean $asCombo
98
	 * @param string $icon
99
	 * @return HtmlButtonGroups
100
	 */
101
	public function addDropdownButton($identifier,$value,$items=[],$asCombo=false,$icon=null){
102
		return $this->addItem(HtmlButton::dropdown($identifier, $value,$items,$asCombo,$icon));
103
	}
104
105
	/**
106
	 * @param string $identifier
107
	 * @param string $label
108
	 * @param string $type
109
	 * @param string $value
110
	 * @param string $placeholder
111
	 * @return HtmlFormInput
112
	 */
113
	public function addInput($identifier, $label=NULL,$type="text",$value=NULL,$placeholder=NULL){
114
		return $this->addItem(new HtmlFormInput($identifier,$label,$type,$value,$placeholder));
115
	}
116
117
	/**
118
	 * @param string $identifier
119
	 * @param string $label
120
	 * @param string $value
121
	 * @param string $placeholder
122
	 * @param int $rows
123
	 * @return HtmlTextarea
124
	 */
125
	public function addTextarea($identifier, $label,$value=NULL,$placeholder=NULL,$rows=5){
126
		return $this->addItem(new HtmlFormTextarea($identifier,$label,$value,$placeholder,$rows));
127
	}
128
129
	public function addPassword($identifier, $label=NULL){
130
		return $this->addItem(new HtmlFormInput($identifier,$label,"password","",""));
131
	}
132
133
	public function addButton($identifier,$value,$cssStyle=NULL,$onClick=NULL){
134
		return $this->addItem(new HtmlButton($identifier,$value,$cssStyle,$onClick));
135
	}
136
137
	public function addCheckbox($identifier, $label=NULL,$value=NULL,$type=NULL){
138
		return $this->addItem(new HtmlFormCheckbox($identifier,$label,$value,$type));
139
	}
140
141
	public function addRadio($identifier, $name,$label=NULL,$value=NULL){
142
		return $this->addItem(new HtmlFormRadio($identifier,$name,$label,$value));
143
	}
144
145
	public function addElement($identifier,$content,$label,$tagName="div",$baseClass=""){
146
		$div=new HtmlSemDoubleElement($identifier,$tagName,$baseClass,$content);
147
		return $this->addItem(new HtmlFormField("field-".$identifier, $div,$label));
148
	}
149
}
150