|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ajax\semantic\widgets\datatable; |
|
3
|
|
|
use Ajax\semantic\html\elements\HtmlButton; |
|
4
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
|
5
|
|
|
use Ajax\common\html\BaseHtml; |
|
6
|
|
|
use Ajax\semantic\html\elements\HtmlButtonGroups; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* trait used in DataTable |
|
10
|
|
|
* @author jc |
|
11
|
|
|
* @property array $_deleteBehavior |
|
12
|
|
|
* @property array $_editBehavior |
|
13
|
|
|
* @property array _displayBehavior |
|
14
|
|
|
* @property boolean $_visibleHover |
|
15
|
|
|
* @property InstanceViewer $_instanceViewer |
|
16
|
|
|
*/ |
|
17
|
|
|
trait DataTableFieldAsTrait{ |
|
18
|
|
|
protected $_buttons=["display","edit","delete"]; |
|
19
|
|
|
protected $_buttonsColumn; |
|
20
|
|
|
abstract public function addField($field,$key=null); |
|
21
|
|
|
abstract public function insertField($index,$field,$key=null); |
|
22
|
|
|
abstract public function insertInField($index,$field,$key=null); |
|
23
|
|
|
abstract public function fieldAs($index,$type,$attributes=NULL); |
|
24
|
|
|
abstract protected function cleanIdentifier($id); |
|
25
|
|
|
abstract protected function _fieldAs($elementCallback,&$index,$attributes=NULL,$prefix=null); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $caption |
|
29
|
|
|
* @param callable $callback |
|
30
|
|
|
* @param boolean $visibleHover |
|
31
|
|
|
* @return callable |
|
32
|
|
|
*/ |
|
33
|
|
|
private function getFieldButtonCallable($caption,$visibleHover=true,$callback=null){ |
|
34
|
|
|
return $this->getCallable("getFieldButton",[$caption,$visibleHover],$callback); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param callable $thisCallback |
|
39
|
|
|
* @param array $parameters |
|
40
|
|
|
* @param callable $callback |
|
41
|
|
|
* @return callable |
|
42
|
|
|
*/ |
|
43
|
|
|
private function getCallable($thisCallback,$parameters,$callback=null){ |
|
44
|
|
|
$result=function($instance) use($thisCallback,$parameters,$callback){ |
|
45
|
|
|
$object=call_user_func_array(array($this,$thisCallback), $parameters); |
|
46
|
|
|
if(isset($callback)){ |
|
47
|
|
|
if(\is_callable($callback)){ |
|
48
|
|
|
$callback($object,$instance,$this->_instanceViewer->count()+1); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
if($object instanceof HtmlSemDoubleElement){ |
|
52
|
|
|
$id=$this->_instanceViewer->getIdentifier(); |
|
53
|
|
|
$object->setProperty("data-ajax",$id); |
|
54
|
|
|
if($object->propertyContains("class","visibleover")){ |
|
55
|
|
|
$this->_visibleHover=true; |
|
56
|
|
|
$object->setProperty("style","visibility:hidden;"); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
return $object; |
|
60
|
|
|
}; |
|
61
|
|
|
return $result; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $caption |
|
66
|
|
|
* @return HtmlButton |
|
67
|
|
|
*/ |
|
68
|
|
|
private function getFieldButton($caption,$visibleHover=true){ |
|
69
|
|
|
$bt= new HtmlButton($this->cleanIdentifier($caption),$caption); |
|
70
|
|
|
if($visibleHover) |
|
71
|
|
|
$this->_visibleOver($bt); |
|
72
|
|
|
return $bt; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function getFieldButtons($buttons,$visibleHover=true){ |
|
76
|
|
|
$bts=new HtmlButtonGroups("",$buttons); |
|
77
|
|
|
if($visibleHover) |
|
78
|
|
|
$this->_visibleOver($bts); |
|
79
|
|
|
return $bts; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Creates a submit button at $index position |
|
84
|
|
|
* @param int $index |
|
85
|
|
|
* @param string $cssStyle |
|
86
|
|
|
* @param string $url |
|
87
|
|
|
* @param string $responseElement |
|
88
|
|
|
* @param array $attributes associative array (<b>ajax</b> key is for ajax post) |
|
89
|
|
|
* @return DataTable |
|
90
|
|
|
*/ |
|
91
|
|
|
public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){ |
|
92
|
|
|
return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle,$attributes){ |
|
93
|
|
|
$button=new HtmlButton($id,$value,$cssStyle); |
|
94
|
|
|
$button->postOnClick($url,"$(event.target).closest('tr').find(':input').serialize()",$responseElement,$attributes["ajax"]); |
|
95
|
|
|
if(!isset($attributes["visibleHover"]) || $attributes["visibleHover"]) |
|
96
|
|
|
$this->_visibleOver($button); |
|
97
|
|
|
return $button; |
|
98
|
|
|
}, $index,$attributes); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
protected function _visibleOver(BaseHtml $element){ |
|
102
|
|
|
$this->_visibleHover=true; |
|
103
|
|
|
return $element->addToProperty("class", "visibleover")->setProperty("style","visibility:hidden;"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Inserts a new Button for each row |
|
108
|
|
|
* @param string $caption |
|
109
|
|
|
* @param callable $callback |
|
110
|
|
|
* @param boolean $visibleHover |
|
111
|
|
|
* @return DataTable |
|
112
|
|
|
*/ |
|
113
|
|
|
public function addFieldButton($caption,$visibleHover=true,$callback=null){ |
|
114
|
|
|
$this->addField($this->getCallable("getFieldButton",[$caption,$visibleHover],$callback)); |
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Inserts a new ButtonGroups for each row |
|
120
|
|
|
* @param array $buttons |
|
121
|
|
|
* @param callable $callback |
|
122
|
|
|
* @param boolean $visibleHover |
|
123
|
|
|
* @return DataTable |
|
124
|
|
|
*/ |
|
125
|
|
|
public function addFieldButtons($buttons,$visibleHover=true,$callback=null){ |
|
126
|
|
|
$this->addField($this->getCallable("getFieldButtons",[$buttons,$visibleHover],$callback)); |
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Inserts a new Button for each row at col $index |
|
132
|
|
|
* @param int $index |
|
133
|
|
|
* @param string $caption |
|
134
|
|
|
* @param callable $callback |
|
135
|
|
|
* @return DataTable |
|
136
|
|
|
*/ |
|
137
|
|
|
public function insertFieldButton($index,$caption,$visibleHover=true,$callback=null){ |
|
138
|
|
|
$this->insertField($index, $this->getFieldButtonCallable($caption,$visibleHover,$callback)); |
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Inserts a new Button for each row in col at $index |
|
144
|
|
|
* @param int $index |
|
145
|
|
|
* @param string $caption |
|
146
|
|
|
* @param callable $callback |
|
147
|
|
|
* @return DataTable |
|
148
|
|
|
*/ |
|
149
|
|
|
public function insertInFieldButton($index,$caption,$visibleHover=true,$callback=null,$key=null){ |
|
150
|
|
|
$this->insertInField($index, $this->getFieldButtonCallable($caption,$visibleHover,$callback),$key); |
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private function addDefaultButton($icon,$class=null,$visibleHover=true,$callback=null,$key=null){ |
|
155
|
|
|
$this->addField($this->getCallable("getDefaultButton",[$icon,$class,$visibleHover],$callback),$key); |
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function insertDefaultButtonIn($index,$icon,$class=null,$visibleHover=true,$callback=null,$key=null){ |
|
160
|
|
|
$this->insertInField($index,$this->getCallable("getDefaultButton",[$icon,$class,$visibleHover],$callback),$key); |
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
private function getDefaultButton($icon,$class=null,$visibleHover=true){ |
|
165
|
|
|
$bt=$this->getFieldButton("",$visibleHover); |
|
166
|
|
|
$bt->asIcon($icon); |
|
167
|
|
|
if(isset($class)) |
|
168
|
|
|
$bt->addClass($class); |
|
169
|
|
|
return $bt; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Adds a delete button |
|
174
|
|
|
* @param boolean $visibleHover |
|
175
|
|
|
* @param array $deleteBehavior default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"data-ajax","params"=>"{}","method"=>"get") |
|
176
|
|
|
* @param callable $callback this function takes the following arguments : $object=>the delete button, $instance : the active instance of the object |
|
177
|
|
|
* @return DataTable |
|
178
|
|
|
*/ |
|
179
|
|
|
public function addDeleteButton($visibleHover=true,$deleteBehavior=[],$callback=null){ |
|
180
|
|
|
$this->_deleteBehavior=$deleteBehavior; |
|
181
|
|
|
return $this->addDefaultButton("remove","_delete red basic",$visibleHover,$callback,"delete"); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Adds an edit button |
|
186
|
|
|
* @param boolean $visibleHover |
|
187
|
|
|
* @param array $editBehavior default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"data-ajax","params"=>"{}","method"=>"get") |
|
188
|
|
|
* @param callable $callback this function takes the following arguments : $object=>the delete button, $instance : the active instance of the object |
|
189
|
|
|
* @return DataTable |
|
190
|
|
|
*/ |
|
191
|
|
|
public function addEditButton($visibleHover=true,$editBehavior=[],$callback=null){ |
|
192
|
|
|
$this->_editBehavior=$editBehavior; |
|
193
|
|
|
return $this->addDefaultButton("edit","_edit basic",$visibleHover,$callback,"edit"); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Adds a button for displaying an object |
|
198
|
|
|
* @param boolean $visibleHover |
|
199
|
|
|
* @param array $displayBehavior default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"data-ajax","params"=>"{}","method"=>"get") |
|
200
|
|
|
* @param callable $callback this function takes the following arguments : $object=>the delete button, $instance : the active instance of the object |
|
201
|
|
|
* @return DataTable |
|
202
|
|
|
*/ |
|
203
|
|
|
public function addDisplayButton($visibleHover=true,$displayBehavior=[],$callback=null){ |
|
204
|
|
|
$this->_displayBehavior=$displayBehavior; |
|
205
|
|
|
return $this->addDefaultButton("eye","_display basic",$visibleHover,$callback,"display"); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Adds an edit and a delete button |
|
210
|
|
|
* @param boolean $visibleHover |
|
211
|
|
|
* @param array $behavior default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"data-ajax","params"=>"{}","method"=>"get") |
|
212
|
|
|
* @param callable $callbackEdit this function takes the following arguments : $object=>the delete button, $instance : the active instance of the object |
|
213
|
|
|
* @param callable $callbackDelete this function takes the following arguments : $object=>the delete button, $instance : the active instance of the object |
|
214
|
|
|
* @return DataTable |
|
215
|
|
|
*/ |
|
216
|
|
|
public function addEditDeleteButtons($visibleHover=true,$behavior=[],$callbackEdit=null,$callbackDelete=null){ |
|
217
|
|
|
$this->addEditButton($visibleHover,$behavior,$callbackEdit); |
|
218
|
|
|
$index=$this->_instanceViewer->visiblePropertiesCount()-1; |
|
219
|
|
|
$this->insertDeleteButtonIn($index,$visibleHover,$behavior,$callbackDelete); |
|
220
|
|
|
return $this; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Adds an edit and a delete button |
|
225
|
|
|
* @param boolean $visibleHover |
|
226
|
|
|
* @param array $behavior default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"data-ajax","params"=>"{}","method"=>"get") |
|
227
|
|
|
* @param callable $callbackDisplay this function takes the following arguments : $object=>the delete button, $instance : the active instance of the object |
|
228
|
|
|
* @param callable $callbackEdit this function takes the following arguments : $object=>the delete button, $instance : the active instance of the object |
|
229
|
|
|
* @param callable $callbackDelete this function takes the following arguments : $object=>the delete button, $instance : the active instance of the object |
|
230
|
|
|
* @return DataTable |
|
231
|
|
|
*/ |
|
232
|
|
|
public function addAllButtons($visibleHover=true,$behavior=[],$callbackDisplay=null,$callbackEdit=null,$callbackDelete=null){ |
|
233
|
|
|
$this->addDisplayButton($visibleHover,$behavior,$callbackDisplay); |
|
234
|
|
|
$index=$this->_instanceViewer->visiblePropertiesCount()-1; |
|
235
|
|
|
$this->_buttonsColumn=$index; |
|
236
|
|
|
$this->insertEditButtonIn($index,$visibleHover,$behavior,$callbackEdit); |
|
237
|
|
|
$this->insertDeleteButtonIn($index,$visibleHover,$behavior,$callbackDelete); |
|
238
|
|
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function insertDeleteButtonIn($index,$visibleHover=true,$deleteBehavior=[],$callback=null){ |
|
242
|
|
|
$this->_deleteBehavior=$deleteBehavior; |
|
243
|
|
|
return $this->insertDefaultButtonIn($index,"remove","_delete red basic",$visibleHover,$callback,"delete"); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function insertEditButtonIn($index,$visibleHover=true,$editBehavior=[],$callback=null){ |
|
247
|
|
|
$this->_editBehavior=$editBehavior; |
|
248
|
|
|
return $this->insertDefaultButtonIn($index,"edit","_edit basic",$visibleHover,$callback,"edit"); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
public function insertDisplayButtonIn($index,$visibleHover=true,$displayBehavior=[],$callback=null){ |
|
252
|
|
|
$this->_displayBehavior=$displayBehavior; |
|
253
|
|
|
return $this->insertDefaultButtonIn($index,"eye","_display basic",$visibleHover,$callback,"display"); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @param multitype:string $_buttons |
|
|
|
|
|
|
258
|
|
|
*/ |
|
259
|
|
|
public function setButtons($_buttons) { |
|
260
|
|
|
$this->_buttons = $_buttons; |
|
261
|
|
|
return $this; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|