Completed
Push — master ( 235574...667888 )
by Jean-Christophe
06:30 queued 03:14
created

FieldAsTrait::_getFieldIdentifier()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
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\base\HtmlSemDoubleElement;
14
15
/**
16
 * @author jc
17
 * @property InstanceViewer $_instanceViewer
18
 */
19
20
trait FieldAsTrait{
21
22
	abstract protected function _getFieldIdentifier($prefix);
23
	abstract public function setValueFunction($index,$callback);
24
25
	private function _getLabelField($caption,$icon=NULL){
26
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
27
		return $label;
28
	}
29
30
	/**
31
	 * @param HtmlSemDoubleElement $element
32
	 * @param array $attributes
33
	 */
34
	protected function _applyAttributes($element,&$attributes,$index){
35 View Code Duplication
		if(isset($attributes["callback"])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
36
			$callback=$attributes["callback"];
37
			if(\is_callable($callback)){
38
				$callback($element,$this->_modelInstance,$index);
0 ignored issues
show
Bug introduced by
The property _modelInstance does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
				unset($attributes["callback"]);
40
			}
41
		}
42
		$element->fromArray($attributes);
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){
79
		$this->setValueFunction($index,function($img){return (new HtmlImage("",$img))->asAvatar();});
80
		return $this;
81
	}
82
83
84
	public function fieldAsRadio($index,$attributes=NULL){
85
		$this->setValueFunction($index,function($value)use ($index,$attributes){
86
			if(isset($attributes["name"])===false){
87
				$attributes["name"]=$this->_instanceViewer->getCaption($index)."[]";
88
			}
89
			$radio=new HtmlRadio($this->_getFieldIdentifier("radio"),$attributes["name"],$value,$value);
90
			$this->_applyAttributes($radio, $attributes, $index);
91
			return $radio;
92
		});
93
			return $this;
94
	}
95
96 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...
97
		$this->setValueFunction($index,function($value) use($index,$attributes){
98
			$input=new HtmlInput($this->_getFieldIdentifier("input"),"text",$value);
99
			if(isset($attributes["name"])===false){
100
				$attributes["name"]=$this->_instanceViewer->getCaption($index)."[]";
101
			}
102
			$input->getField()->setProperty("name", $attributes["name"]);
103
			$this->_applyAttributes($input, $attributes, $index);
104
			return $input;
105
		});
106
			return $this;
107
	}
108
109 View Code Duplication
	public function fieldAsCheckbox($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...
110
		$this->setValueFunction($index,function($value) use($index,$attributes){
111
			$checkbox=new HtmlCheckbox($this->_getFieldIdentifier("ck"),"",$value);
112
			$checkbox->setChecked(JString::isBooleanTrue($value));
113
			if(isset($attributes["name"])===false){
114
				$attributes["name"]=$this->_instanceViewer->getCaption($index)."[]";
115
			}
116
			$checkbox->getField()->setProperty("name", $attributes["name"]);
117
			$this->_applyAttributes($checkbox, $attributes, $index);
118
			return $checkbox;
119
		});
120
			return $this;
121
	}
122
123
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
124
		$this->setValueFunction($index,function($value) use($index,$elements,$multiple,$attributes){
125
			$dd=new HtmlDropdown($this->_getFieldIdentifier("dd"),$value,$elements);
126
			if(isset($attributes["name"])===false){
127
				$attributes["name"]=$this->_instanceViewer->getCaption($index)."[]";
128
			}
129
			$dd->asSelect($attributes["name"],$multiple);
130
			$this->_applyAttributes($dd, $attributes, $index);
131
			return $dd;
132
		});
133
			return $this;
134
	}
135
}