Completed
Push — master ( d551f2...460f9e )
by Jean-Christophe
03:07
created

FieldAsTrait::_fieldAs()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 11
nc 1
nop 4
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
16
/**
17
 * @author jc
18
 * @property InstanceViewer $_instanceViewer
19
 * @property boolean $_edition
20
 * @property mixed _modelInstance
21
 */
22
trait FieldAsTrait{
23
24
	abstract protected function _getFieldIdentifier($prefix);
25
	abstract public function setValueFunction($index,$callback);
26
27
	/**
28
	 * @param HtmlFormField $element
29
	 * @param array $attributes
30
	 */
31
	protected function _applyAttributes($element,&$attributes,$index){
32
		$this->_addRules($element, $attributes);
33
		if(isset($attributes["callback"])){
34
			$callback=$attributes["callback"];
35
			if(\is_callable($callback)){
36
				$callback($element,$this->_modelInstance,$index);
37
				unset($attributes["callback"]);
38
			}
39
		}
40
		$element->fromArray($attributes);
41
	}
42
43
	private function _getLabelField($caption,$icon=NULL){
44
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
45
		return $label;
46
	}
47
48
	protected function _addRules($element,&$attributes){}
0 ignored issues
show
Unused Code introduced by
The parameter $element 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...
Unused Code introduced by
The parameter $attributes 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...
49
50
	protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
51
		$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){
52
			$name=$this->_instanceViewer->getFieldName($index)."[]";
53
			if(isset($attributes["name"])){
54
				$name=$attributes["name"];
55
			}
56
			$element=$elementCallback($this->_getFieldIdentifier($prefix),$name,$value,"");
57
			if(\is_array($attributes))
58
				$this->_applyAttributes($element, $attributes,$index);
59
			$element->setDisabled(!$this->_edition);
60
			return $element;
61
		});
62
			return $this;
63
	}
64
65
66 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...
67
		$this->setValueFunction($index,function($value) use($label,$attributes){
68
			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
69
			return $pb;
70
		});
71
			return $this;
72
	}
73
74 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...
75
		$this->setValueFunction($index,function($value) use($max,$icon){
76
			$rating=new HtmlRating($this->_getFieldIdentifier("rat"),$value,$max,$icon);
77
			return $rating;
78
		});
79
			return $this;
80
	}
81
82
	public function fieldAsLabel($index,$icon=NULL){
83
		$this->setValueFunction($index,function($caption) use($icon){
84
			$lbl=$this->_getLabelField($caption,$icon);
85
			return $lbl;
86
		});
87
			return $this;
88
	}
89
90
	public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){
91
		return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){
92
			$header=new HtmlHeader($id,$niveau,$value);
93
			if(isset($icon))
94
				$header->asIcon($icon, $value);
95
			return $header;
96
		}, $index,$attributes,"header");
97
	}
98
99
100
	public function fieldAsImage($index,$size=Size::MINI,$circular=false){
101
		$this->setValueFunction($index,function($img) use($size,$circular){
102
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
103
			return $image;
104
		});
105
			return $this;
106
	}
107
108
	public function fieldAsAvatar($index,$attributes=NULL){
109
		return $this->_fieldAs(function($id,$name,$value){
110
			$img=new HtmlImage($id,$value);
111
			$img->asAvatar();
112
			return $img;
113
		}, $index,$attributes,"avatar");
114
	}
115
116
	public function fieldAsRadio($index,$attributes=NULL){
117
		return $this->_fieldAs(function($id,$name,$value){
118
			$input= new HtmlRadio($id,$name,$value,$value);
119
			return $input;
120
		}, $index,$attributes,"radio");
121
	}
122
123 View Code Duplication
	public function fieldAsInput($index,$attributes=NULL){
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...
124
		return $this->_fieldAs(function($id,$name,$value){
125
			$input= new HtmlFormInput($id,"","text",$value);
126
			$input->setName($name);
127
			return $input;
128
		}, $index,$attributes,"input");
129
	}
130
131 View Code Duplication
	public function fieldAsTextarea($index,$attributes=NULL){
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...
132
		return $this->_fieldAs(function($id,$name,$value){
133
			$textarea=new HtmlFormTextarea($id,null,$value);
134
			$textarea->setName($name);
135
			return $textarea;
136
		}, $index,$attributes,"textarea");
137
	}
138
139
	public function fieldAsHidden($index,$attributes=NULL){
140
		if(!\is_array($attributes)){
141
			$attributes=[];
142
		}
143
		$attributes["imputType"]="hidden";
144
		return $this->fieldAsInput($index,$attributes);
145
	}
146
147
	public function fieldAsCheckbox($index,$attributes=NULL){
148
		return $this->_fieldAs(function($id,$name,$value){
149
			$input=new HtmlFormCheckbox($id,NULL,$this->_instanceViewer->getIdentifier());
150
			$input->setChecked(JString::isBooleanTrue($value));
151
			$input->getField()->setProperty("name", $name);
152
			return $input;
153
		}, $index,$attributes,"ck");
154
	}
155
156 View Code Duplication
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
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...
157
		return $this->_fieldAs(function($id,$name,$value) use($elements,$multiple){
158
			//$dd=new HtmlDropdown($id,$value,$elements);
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
159
			$dd=new HtmlFormDropdown($id,$elements,NULL,$value);
160
			$dd->asSelect($name,$multiple);
161
			return $dd;
162
		}, $index,$attributes,"dd");
163
	}
164
}