Passed
Push — master ( a6eb21...a5e5c2 )
by Jean-Christophe
02:03
created

FieldsTrait::setFieldsPropertyValues()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 2
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
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
use Ajax\semantic\html\collections\form\HtmlFormFields;
18
19
/**
20
 * @author jc
21
 * @property string $identifier
22
 */
23
trait FieldsTrait {
24
	abstract public function addFields($fields=NULL,$label=NULL);
25
	abstract public function addItem($item);
26
27
	protected function createItem($value){
28
		if(\is_array($value)){
29
			$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)));
30
			return $itemO;
31
		}elseif(\is_object($value)){
32
			$itemO=new HtmlFormField("field-".$this->identifier, $value);
33
			return $itemO;
34
		}else
35
			return new HtmlFormInput($value);
36
	}
37
38
	protected function createCondition($value){
39
		return \is_object($value)===false || $value instanceof \Ajax\semantic\html\elements\HtmlInput;
40
	}
41
42
	public function addInputs($inputs,$fieldslabel=null){
43
		$fields=array();
44
		foreach ($inputs as $input){
45
			\extract($input);
46
			$f=new HtmlFormInput("","");
47
			$f->fromArray($input);
48
			$fields[]=$f;
49
		}
50
		return $this->addFields($fields,$fieldslabel);
51
	}
52
53
	/**
54
	 * Sets the values of a property for each Field of each item in the collection
55
	 * @param string $property
56
	 * @param array $values
57
	 * @return HtmlFormFields
58
	 */
59
	public function setFieldsPropertyValues($property,$values){
60
		$i=0;
61
		if(\is_array($values)===false){
0 ignored issues
show
introduced by
The condition is_array($values) === false can never be true.
Loading history...
62
			$values=\array_fill(0, $this->count(),$values);
63
		}
64
		foreach ($values as $value){
65
			$c=$this->content[$i++];
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist on Ajax\semantic\html\colle...form\traits\FieldsTrait. Did you maybe forget to declare it?
Loading history...
66
			if(isset($c)){
67
				$df=$c->getDataField();
68
				$df->setProperty($property,$value);
69
			}
70
			else{
71
				return $this;
72
			}
73
		}
74
		return $this;
75
	}
76
77
	public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){
78
		$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? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
		/** @scrutinizer ignore-call */ 
79
  $field=$this->getItem($index);
Loading history...
79
		if(isset($field)){
80
			$field->addRule($type,$prompt,$value);
81
		}
82
		return $this;
83
	}
84
85
	public function addFieldRules($index,$rules){
86
		$field=$this->getItem($index);
87
		if(isset($field)){
88
			$field->addRules($rules);
89
		}
90
		return $this;
91
	}
92
93
	/**
94
	 * Adds a new dropdown element
95
	 * @param string $identifier
96
	 * @param array $items
97
	 * @param string $label
98
	 * @param string $value
99
	 * @param boolean $multiple
100
	 * @return HtmlFormDropdown
101
	 */
102
	public function addDropdown($identifier,$items=array(), $label=NULL,$value=NULL,$multiple=false){
103
		return $this->addItem(new HtmlFormDropdown($identifier,$items,$label,$value,$multiple));
104
	}
105
106
	/**
107
	 * Adds a new button groups
108
	 * @param string $identifier
109
	 * @param array $elements
110
	 * @param boolean $asIcons
111
	 * @return HtmlButtonGroups
112
	 */
113
	public function addButtonGroups($identifier,$elements=[],$asIcons=false){
114
		return $this->addItem(new HtmlButtonGroups($identifier,$elements,$asIcons));
115
	}
116
117
	/**
118
	 * Adds a button with a dropdown button
119
	 * @param string $identifier
120
	 * @param string $value
121
	 * @param array $items
122
	 * @param boolean $asCombo
123
	 * @param string $icon
124
	 * @return HtmlButtonGroups
125
	 */
126
	public function addDropdownButton($identifier,$value,$items=[],$asCombo=false,$icon=null){
127
		return $this->addItem(HtmlButton::dropdown($identifier, $value,$items,$asCombo,$icon));
128
	}
129
130
	/**
131
	 * @param string $identifier
132
	 * @param string $label
133
	 * @param string $type
134
	 * @param string $value
135
	 * @param string $placeholder
136
	 * @return HtmlFormInput
137
	 */
138
	public function addInput($identifier, $label=NULL,$type="text",$value=NULL,$placeholder=NULL){
139
		return $this->addItem(new HtmlFormInput($identifier,$label,$type,$value,$placeholder));
140
	}
141
142
	/**
143
	 * @param string $identifier
144
	 * @param string $label
145
	 * @param string $value
146
	 * @param string $placeholder
147
	 * @param int $rows
148
	 * @return HtmlTextarea
149
	 */
150
	public function addTextarea($identifier, $label,$value=NULL,$placeholder=NULL,$rows=5){
151
		return $this->addItem(new HtmlFormTextarea($identifier,$label,$value,$placeholder,$rows));
152
	}
153
154
	public function addPassword($identifier, $label=NULL){
155
		return $this->addItem(new HtmlFormInput($identifier,$label,"password","",""));
156
	}
157
158
	public function addButton($identifier,$value,$cssStyle=NULL,$onClick=NULL){
159
		return $this->addItem(new HtmlButton($identifier,$value,$cssStyle,$onClick));
160
	}
161
162
	/**
163
	 * @param string $identifier
164
	 * @param string $label
165
	 * @param string $value
166
	 * @param string $type
167
	 * @return HtmlFormCheckbox
168
	 */
169
	public function addCheckbox($identifier, $label=NULL,$value=NULL,$type=NULL){
170
		return $this->addItem(new HtmlFormCheckbox($identifier,$label,$value,$type));
171
	}
172
173
	public function addRadio($identifier, $name,$label=NULL,$value=NULL){
174
		return $this->addItem(new HtmlFormRadio($identifier,$name,$label,$value));
175
	}
176
177
	public function addElement($identifier,$content,$label,$tagName="div",$baseClass=""){
178
		$div=new HtmlSemDoubleElement($identifier,$tagName,$baseClass,$content);
179
		return $this->addItem(new HtmlFormField("field-".$identifier, $div,$label));
180
	}
181
}
182