Completed
Push — master ( 667888...aa0ebb )
by Jean-Christophe
03:27
created

FieldAsTrait::_getLabelField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Ajax\semantic\widgets\base;
3
use Ajax\semantic\html\elements\HtmlInput;
4
use Ajax\semantic\html\modules\checkbox\HtmlCheckbox;
5
use Ajax\service\JString;
6
use Ajax\semantic\html\modules\HtmlDropdown;
7
use Ajax\semantic\html\elements\HtmlImage;
8
use Ajax\semantic\html\modules\checkbox\HtmlRadio;
9
use Ajax\semantic\html\base\constants\Size;
10
use Ajax\semantic\html\elements\HtmlLabel;
11
use Ajax\semantic\html\modules\HtmlProgress;
12
use Ajax\semantic\html\modules\HtmlRating;
13
/**
14
 * @author jc
15
 * @property InstanceViewer $_instanceViewer
16
 */
17
18
trait FieldAsTrait{
19
20
	abstract protected function _getFieldIdentifier($prefix);
21
	abstract public function setValueFunction($index,$callback);
22
23
	private function _getLabelField($caption,$icon=NULL){
24
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
25
		return $label;
26
	}
27
28
	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...
29
30
	protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
31
		$this->setValueFunction($index,function($value)use ($index,&$attributes,$elementCallback,$prefix){
32
			$name=$this->_instanceViewer->getCaption($index)."[]";
33
			if(isset($attributes["name"])===true){
34
				$name=$attributes["name"];
35
			}
36
			$element=$elementCallback($this->_getFieldIdentifier($prefix),$value,$name);
37
			if(\is_array($attributes))
38
				$this->_applyAttributes($element, $attributes,$index);
0 ignored issues
show
Bug introduced by
It seems like _applyAttributes() 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...
39
			return $element;
40
		});
41
			return $this;
42
	}
43
44
45 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...
46
		$this->setValueFunction($index,function($value) use($label,$attributes){
47
			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
48
			return $pb;
49
		});
50
			return $this;
51
	}
52
53 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...
54
		$this->setValueFunction($index,function($value) use($max,$icon){
55
			$rating=new HtmlRating($this->_getFieldIdentifier("rat"),$value,$max,$icon);
56
			return $rating;
57
		});
58
			return $this;
59
	}
60
61
	public function fieldAsLabel($index,$icon=NULL){
62
		$this->setValueFunction($index,function($caption) use($icon){
63
			$lbl=$this->_getLabelField($caption,$icon);
64
			return $lbl;
65
		});
66
			return $this;
67
	}
68
69
	public function fieldAsImage($index,$size=Size::SMALL,$circular=false){
70
		$this->setValueFunction($index,function($img) use($size,$circular){
71
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
72
			return $image;
73
		});
74
			return $this;
75
	}
76
77
	public function fieldAsAvatar($index,$attributes=NULL){
78
		return $this->_fieldAs(function($id,$value,$name){
0 ignored issues
show
Unused Code introduced by
The parameter $name 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...
79
			$img= (new HtmlImage($id,$value))->asAvatar();
80
			return $img;
81
		}, $index,$attributes,"avatar");
82
	}
83
84
85
	public function fieldAsRadio($index,$attributes=NULL){
86
		return $this->_fieldAs(function($id,$value,$name){
87
			$input= new HtmlRadio($id,$name,$value,$value);
88
			return $input;
89
		}, $index,$attributes,"radio");
90
	}
91
92
	public function fieldAsInput($index,$attributes=NULL){
93
		return $this->_fieldAs(function($id,$value,$name){
94
			$input= new HtmlInput($id,"text",$value);
95
			$input->getField()->setProperty("name", $name);
96
			return $input;
97
		}, $index,$attributes,"input");
98
	}
99
100
	public function fieldAsCheckbox($index,$attributes=NULL){
101
		return $this->_fieldAs(function($id,$value,$name){
102
			$input=new HtmlCheckbox($id,"",$this->_instanceViewer->getIdentifier());
103
			$input->setChecked(JString::isBooleanTrue($value));
104
			$input->getField()->setProperty("name", $name);
105
			return $input;
106
		}, $index,$attributes,"ck");
107
	}
108
109
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
110
		return $this->_fieldAs(function($id,$value,$name) use($elements,$multiple){
111
			$dd=new HtmlDropdown($id,$value,$elements);
112
			$dd->asSelect($name,$multiple);
113
			return $dd;
114
		}, $index,$attributes,"dd");
115
	}
116
}