Completed
Push — master ( 22ba15...552305 )
by Jean-Christophe
03:10
created

FieldAsTrait::fieldAsHidden()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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
use Ajax\semantic\html\collections\HtmlMessage;
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 fieldAsImage($index,$size=Size::SMALL,$circular=false){
71
		$this->setValueFunction($index,function($img) use($size,$circular){
72
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
73
			return $image;
74
		});
75
			return $this;
76
	}
77
78
	public function fieldAsAvatar($index,$attributes=NULL){
79
		return $this->_fieldAs(function($id,$name,$value){
80
			$img=new HtmlImage($id,$value);
81
			$img->asAvatar();
82
			return $img;
83
		}, $index,$attributes,"avatar");
84
	}
85
86
	public function fieldAsRadio($index,$attributes=NULL){
87
		return $this->_fieldAs(function($id,$name,$value){
88
			$input= new HtmlRadio($id,$name,$value,$value);
89
			return $input;
90
		}, $index,$attributes,"radio");
91
	}
92
93 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...
94
		return $this->_fieldAs(function($id,$name,$value){
95
			$input= new HtmlInput($id,"text",$value);
96
			//TODO check getField
97
			$input->setName($name);
98
			return $input;
99
		}, $index,$attributes,"input");
100
	}
101
102
	public function fieldAsHidden($index,$attributes=NULL){
103
		if(\is_array($attributes)===false){
104
			$attributes=[];
105
		}
106
		$attributes["imputType"]="hidden";
107
		return $this->fieldAsInput($index,$attributes);
108
	}
109
110
	public function fieldAsCheckbox($index,$attributes=NULL){
111
		return $this->_fieldAs(function($id,$name,$value){
112
			$input=new HtmlCheckbox($id,"",$this->_instanceViewer->getIdentifier());
113
			$input->setChecked(JString::isBooleanTrue($value));
114
			$input->getField()->setProperty("name", $name);
115
			return $input;
116
		}, $index,$attributes,"ck");
117
	}
118
119 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...
120
		return $this->_fieldAs(function($id,$name,$value) use($elements,$multiple){
121
			$dd=new HtmlDropdown($id,$value,$elements);
122
			$dd->asSelect($name,$multiple);
123
			return $dd;
124
		}, $index,$attributes,"dd");
125
	}
126
}