Completed
Push — master ( 2bc7fe...701cf4 )
by Jean-Christophe
03:09
created

FieldAsTrait::_prepareFormFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
namespace Ajax\semantic\widgets\base;
3
use Ajax\service\JString;
4
use Ajax\semantic\html\elements\HtmlImage;
5
use Ajax\semantic\html\modules\checkbox\HtmlRadio;
6
use Ajax\semantic\html\base\constants\Size;
7
use Ajax\semantic\html\elements\HtmlLabel;
8
use Ajax\semantic\html\modules\HtmlProgress;
9
use Ajax\semantic\html\modules\HtmlRating;
10
use Ajax\semantic\html\elements\HtmlHeader;
11
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
12
use Ajax\semantic\html\collections\form\HtmlFormInput;
13
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
14
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
15
use Ajax\semantic\html\collections\form\HtmlFormFields;
16
use Ajax\semantic\html\collections\HtmlMessage;
17
use Ajax\semantic\html\elements\HtmlButton;
18
19
/**
20
 * @author jc
21
 * @property InstanceViewer $_instanceViewer
22
 * @property boolean $_edition
23
 * @property mixed _modelInstance
24
 */
25
trait FieldAsTrait{
26
27
	abstract protected function _getFieldIdentifier($prefix,$name="");
28
	abstract public function setValueFunction($index,$callback);
29
	abstract protected function _getFieldName($index);
30
	abstract protected function _getFieldCaption($index);
31
	abstract protected function _buttonAsSubmit(&$button,$event,$url,$responseElement=NULL);
32
33
	/**
34
	 * @param HtmlFormField $element
35
	 * @param array $attributes
36
	 */
37
	protected function _applyAttributes($element,&$attributes,$index){
38
		if(isset($attributes["callback"])){
39
			$callback=$attributes["callback"];
40
			if(\is_callable($callback)){
41
				$callback($element,$this->_modelInstance,$index);
42
				unset($attributes["callback"]);
43
			}
44
		}
45
		unset($attributes["rules"]);
46
		$element->fromArray($attributes);
47
	}
48
49
	private function _getLabelField($caption,$icon=NULL){
50
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
51
		return $label;
52
	}
53
54
	protected function _addRules($element,&$attributes){
55
		if(isset($attributes["rules"])){
56
			$rules=$attributes["rules"];
57
			if(\is_array($rules)){
58
				$element->addRules($rules);
59
			}
60
			else{
61
				$element->addRule($rules);
62
			}
63
			unset($attributes["rules"]);
64
		}
65
	}
66
67
	protected function _prepareFormFields(&$field,$name,&$attributes){
68
		$field->setName($name);
69
		$this->_addRules($field, $attributes);
70
		return $field;
71
	}
72
73
	protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
74
		$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){
75
			$caption=$this->_getFieldCaption($index);
76
			$name=$this->_getFieldName($index);
77
			$id=$this->_getFieldIdentifier($prefix,$name);
78
			if(isset($attributes["name"])){
79
				$name=$attributes["name"];
80
				unset($attributes["name"]);
81
			}
82
			$element=$elementCallback($id,$name,$value,$caption);
83
			if(\is_array($attributes)){
84
				$this->_applyAttributes($element, $attributes,$index);
85
			}
86
			$element->setDisabled(!$this->_edition);
87
			return $element;
88
		});
89
			return $this;
90
	}
91
92
93 View Code Duplication
	public function fieldAsProgress($index,$label=NULL, $attributes=array()){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
94
		$this->setValueFunction($index,function($value) use($label,$attributes){
95
			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
96
			return $pb;
97
		});
98
			return $this;
99
	}
100
101 View Code Duplication
	public function fieldAsRating($index,$max=5, $icon=""){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
102
		$this->setValueFunction($index,function($value) use($max,$icon){
103
			$rating=new HtmlRating($this->_getFieldIdentifier("rat"),$value,$max,$icon);
104
			return $rating;
105
		});
106
			return $this;
107
	}
108
109
	public function fieldAsLabel($index,$icon=NULL){
110
		$this->setValueFunction($index,function($caption) use($icon){
111
			$lbl=$this->_getLabelField($caption,$icon);
112
			return $lbl;
113
		});
114
			return $this;
115
	}
116
117
	public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){
118
		return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){
119
			$header=new HtmlHeader($id,$niveau,$value);
120
			if(isset($icon))
121
				$header->asIcon($icon, $value);
122
			return $header;
123
		}, $index,$attributes,"header");
124
	}
125
126
127
	public function fieldAsImage($index,$size=Size::MINI,$circular=false){
128
		$this->setValueFunction($index,function($img) use($size,$circular){
129
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
130
			return $image;
131
		});
132
			return $this;
133
	}
134
135
	public function fieldAsAvatar($index,$attributes=NULL){
136
		return $this->_fieldAs(function($id,$name,$value){
137
			$img=new HtmlImage($id,$value);
138
			$img->asAvatar();
139
			return $img;
140
		}, $index,$attributes,"avatar");
141
	}
142
143
	public function fieldAsRadio($index,$attributes=NULL){
144
		return $this->_fieldAs(function($id,$name,$value){
145
			$input= new HtmlRadio($id,$name,$value,$value);
146
			return $input;
147
		}, $index,$attributes,"radio");
148
	}
149
150
	public function fieldAsRadios($index,$elements=[],$attributes=NULL){
151
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements){
152
			return HtmlFormFields::radios($name,$elements,$caption,$value);
153
		}, $index,$attributes,"radios");
154
	}
155
156
	public function fieldAsInput($index,$attributes=NULL){
157
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes){
158
			$input= new HtmlFormInput($id,$caption,"text",$value);
159
			return $this->_prepareFormFields($input, $name, $attributes);
160
		}, $index,$attributes,"input");
161
	}
162
163
	public function fieldAsTextarea($index,$attributes=NULL){
164
		return $this->_fieldAs(function($id,$name,$value){
165
			$textarea=new HtmlFormTextarea($id,null,$value);
166
			$textarea->setName($name);
167
			return $textarea;
168
		}, $index,$attributes,"textarea");
169
	}
170
171
	public function fieldAsHidden($index,$attributes=NULL){
172
		if(!\is_array($attributes)){
173
			$attributes=[];
174
		}
175
		$attributes["imputType"]="hidden";
176
		return $this->fieldAsInput($index,$attributes);
177
	}
178
179
	public function fieldAsCheckbox($index,$attributes=NULL){
180
		return $this->_fieldAs(function($id,$name,$value){
181
			$input=new HtmlFormCheckbox($id,NULL,$this->_instanceViewer->getIdentifier());
182
			$input->setChecked(JString::isBooleanTrue($value));
183
			$input->setName($name);
184
			return $input;
185
		}, $index,$attributes,"ck");
186
	}
187
188
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
189
		return $this->_fieldAs(function($id,$name,$value) use($elements,$multiple){
190
			$dd=new HtmlFormDropdown($id,$elements,NULL,$value);
191
			$dd->asSelect($name,$multiple);
192
			return $dd;
193
		}, $index,$attributes,"dd");
194
	}
195
196
	public function fieldAsMessage($index,$attributes=NULL){
197
		return $this->_fieldAs(function($id,$name,$value,$caption){
198
			$mess= new HtmlMessage("message-".$id,$value);
199
			$mess->addHeader($caption);
200
			return $mess;
201
		}, $index,$attributes,"message");
202
	}
203
204
	public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
205
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle){
0 ignored issues
show
Unused Code introduced by
The parameter $caption is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
206
			$button=new HtmlButton($id,$value,$cssStyle);
207
			$this->_buttonAsSubmit($button,"click",$url,$responseElement);
208
			return $button;
209
		}, $index,$attributes);
210
	}
211
}