Completed
Push — master ( bda9b8...aa1d0c )
by Jean-Christophe
03:12
created

FieldAsTrait::fieldAsHeader()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 1
nop 4
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
use Ajax\semantic\html\elements\HtmlHeader;
14
/**
15
 * @author jc
16
 * @property InstanceViewer $_instanceViewer
17
 */
18
19
trait FieldAsTrait{
20
21
	abstract protected function _getFieldIdentifier($prefix);
22
	abstract public function setValueFunction($index,$callback);
23
24
	private function _getLabelField($caption,$icon=NULL){
25
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
26
		return $label;
27
	}
28
29
	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...
30
31
	protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
32
		$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){
33
			$name=$this->_instanceViewer->getCaption($index)."[]";
34
			if(isset($attributes["name"])===true){
35
				$name=$attributes["name"];
36
			}
37
			$element=$elementCallback($this->_getFieldIdentifier($prefix),$name,$value,"");
38
			if(\is_array($attributes))
39
				$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...
40
			return $element;
41
		});
42
			return $this;
43
	}
44
45
46 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...
47
		$this->setValueFunction($index,function($value) use($label,$attributes){
48
			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
49
			return $pb;
50
		});
51
			return $this;
52
	}
53
54 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...
55
		$this->setValueFunction($index,function($value) use($max,$icon){
56
			$rating=new HtmlRating($this->_getFieldIdentifier("rat"),$value,$max,$icon);
57
			return $rating;
58
		});
59
			return $this;
60
	}
61
62
	public function fieldAsLabel($index,$icon=NULL){
63
		$this->setValueFunction($index,function($caption) use($icon){
64
			$lbl=$this->_getLabelField($caption,$icon);
65
			return $lbl;
66
		});
67
			return $this;
68
	}
69
70
	public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){
71
		return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){
72
			$header=new HtmlHeader($id,$niveau,$value);
73
			if(isset($icon))
74
				$header->asIcon($icon, $value);
75
			return $header;
76
		}, $index,$attributes,"header");
77
	}
78
79
80
	public function fieldAsImage($index,$size=Size::MINI,$circular=false){
81
		$this->setValueFunction($index,function($img) use($size,$circular){
82
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
83
			return $image;
84
		});
85
			return $this;
86
	}
87
88
	public function fieldAsAvatar($index,$attributes=NULL){
89
		return $this->_fieldAs(function($id,$name,$value){
90
			$img=new HtmlImage($id,$value);
91
			$img->asAvatar();
92
			return $img;
93
		}, $index,$attributes,"avatar");
94
	}
95
96
	public function fieldAsRadio($index,$attributes=NULL){
97
		return $this->_fieldAs(function($id,$name,$value){
98
			$input= new HtmlRadio($id,$name,$value,$value);
99
			return $input;
100
		}, $index,$attributes,"radio");
101
	}
102
103 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...
104
		return $this->_fieldAs(function($id,$name,$value){
105
			$input= new HtmlInput($id,"text",$value);
106
			//TODO check getField
107
			$input->setName($name);
108
			return $input;
109
		}, $index,$attributes,"input");
110
	}
111
112
	public function fieldAsHidden($index,$attributes=NULL){
113
		if(\is_array($attributes)===false){
114
			$attributes=[];
115
		}
116
		$attributes["imputType"]="hidden";
117
		return $this->fieldAsInput($index,$attributes);
118
	}
119
120
	public function fieldAsCheckbox($index,$attributes=NULL){
121
		return $this->_fieldAs(function($id,$name,$value){
122
			$input=new HtmlCheckbox($id,"",$this->_instanceViewer->getIdentifier());
123
			$input->setChecked(JString::isBooleanTrue($value));
124
			$input->getField()->setProperty("name", $name);
125
			return $input;
126
		}, $index,$attributes,"ck");
127
	}
128
129 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...
130
		return $this->_fieldAs(function($id,$name,$value) use($elements,$multiple){
131
			$dd=new HtmlDropdown($id,$value,$elements);
132
			$dd->asSelect($name,$multiple);
133
			return $dd;
134
		}, $index,$attributes,"dd");
135
	}
136
}