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