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){} |
|
|
|
|
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); |
|
|
|
|
39
|
|
|
return $element; |
40
|
|
|
}); |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
View Code Duplication |
public function fieldAsProgress($index,$label=NULL, $attributes=array()){ |
|
|
|
|
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=""){ |
|
|
|
|
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){ |
|
|
|
|
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
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.