|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\crud; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\http\URequest; |
|
6
|
|
|
use Ubiquity\orm\DAO; |
|
7
|
|
|
use Ubiquity\controllers\admin\viewers\ModelViewer; |
|
8
|
|
|
use Ajax\semantic\widgets\datatable\Pagination; |
|
9
|
|
|
use Ajax\common\html\HtmlContentOnly; |
|
10
|
|
|
use Ubiquity\orm\OrmUtils; |
|
11
|
|
|
use Ajax\semantic\html\collections\HtmlMessage; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author jc |
|
15
|
|
|
* @property int $activePage |
|
16
|
|
|
* @property string $model |
|
17
|
|
|
* @property \Ajax\php\ubiquity\JsUtils $jquery |
|
18
|
|
|
* @property \Ubiquity\views\View $view |
|
19
|
|
|
*/ |
|
20
|
|
|
trait CRUDControllerUtilitiesTrait { |
|
21
|
|
|
abstract protected function _showSimpleMessage(CRUDMessage $message,$staticName=null):HtmlMessage; |
|
22
|
|
|
abstract public function loadView($viewName, $pData = NULL, $asString = false); |
|
23
|
|
|
abstract public function index(); |
|
24
|
|
|
abstract public function _getBaseRoute(); |
|
25
|
|
|
abstract protected function _showConfMessage(CRUDMessage $message,$url, $responseElement, $data, $attributes = NULL):HtmlMessage; |
|
26
|
|
|
|
|
27
|
|
|
protected $modelViewer; |
|
28
|
|
|
protected $adminDatas; |
|
29
|
|
|
protected $events; |
|
30
|
|
|
protected $crudFiles; |
|
31
|
|
|
|
|
32
|
6 |
|
protected function getInstances(&$totalCount,$page=1,$id=null){ |
|
33
|
6 |
|
$this->activePage=$page; |
|
34
|
6 |
|
$model=$this->model; |
|
35
|
6 |
|
$condition=$this->_getAdminData()->_getInstancesFilter($model); |
|
36
|
6 |
|
$totalCount=DAO::count($model,$condition); |
|
37
|
6 |
|
if($totalCount){ |
|
38
|
6 |
|
$recordsPerPage=$this->_getModelViewer()->recordsPerPage($model,$totalCount); |
|
39
|
6 |
|
if(is_numeric($recordsPerPage)){ |
|
40
|
1 |
|
if(isset($id)){ |
|
41
|
|
|
$rownum=DAO::getRownum($model, $id); |
|
42
|
|
|
$this->activePage=Pagination::getPageOfRow($rownum,$recordsPerPage); |
|
43
|
|
|
} |
|
44
|
1 |
|
return DAO::paginate($model,$this->activePage,$recordsPerPage,$condition); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
5 |
|
return DAO::getAll($model,$condition); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
protected function search($model,$search){ |
|
51
|
1 |
|
$fields=$this->_getAdminData()->getSearchFieldNames($model); |
|
52
|
1 |
|
$condition=$this->_getAdminData()->_getInstancesFilter($model); |
|
53
|
1 |
|
return CRUDHelper::search($model, $search, $fields,$condition); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param mixed $ids |
|
58
|
|
|
* @param boolean $included |
|
59
|
|
|
* @return object |
|
60
|
|
|
*/ |
|
61
|
4 |
|
private function getModelInstance($ids,$included=true) { |
|
62
|
4 |
|
$ids=\explode("_", $ids); |
|
63
|
4 |
|
if(!is_bool($included)){ |
|
64
|
2 |
|
if(!is_array($included)){ |
|
65
|
2 |
|
$included=[$included]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
4 |
|
$instance=DAO::getOne($this->model, $ids,$included); |
|
69
|
4 |
|
if(isset($instance)){ |
|
70
|
4 |
|
return $instance; |
|
71
|
|
|
} |
|
72
|
|
|
$message=new CRUDMessage("This object does not exist!","Get object","warning","warning circle"); |
|
73
|
|
|
$message=$this->_getEvents()->onNotFoundMessage($message,$ids); |
|
74
|
|
|
echo $this->_showSimpleMessage($message); |
|
75
|
|
|
echo $this->jquery->compile($this->view); |
|
76
|
|
|
exit(1); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
protected function updateMemberDataElement($member,$instance){ |
|
80
|
1 |
|
$dt=$this->_getModelViewer()->getModelDataElement($instance, $this->model, false); |
|
81
|
1 |
|
$dt->compile(); |
|
82
|
1 |
|
echo new HtmlContentOnly($dt->getFieldValue($member)); |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
private function _renderDataTableForRefresh($instances,$model,$totalCount){ |
|
86
|
1 |
|
$compo= $this->_getModelViewer()->getModelDataTable($instances, $model,$totalCount)->refresh(["tbody"]); |
|
87
|
1 |
|
$this->_getEvents()->onDisplayElements($compo,$instances,true); |
|
88
|
1 |
|
$compo->setLibraryId("_compo_"); |
|
89
|
1 |
|
$this->jquery->renderView("@framework/main/component.html"); |
|
90
|
1 |
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
protected function _edit($instance, $modal="no") { |
|
93
|
2 |
|
$_SESSION["instance"]=$instance; |
|
94
|
2 |
|
$modal=($modal == "modal"); |
|
95
|
2 |
|
$form=$this->_getModelViewer()->getForm("frmEdit", $instance); |
|
96
|
2 |
|
$this->jquery->click("#action-modal-frmEdit-0", "$('#frmEdit').form('submit');", false); |
|
97
|
2 |
|
if (!$modal) { |
|
98
|
2 |
|
$this->jquery->click("#bt-cancel", "$('#form-container').transition('drop');"); |
|
99
|
2 |
|
$this->jquery->compile($this->view); |
|
100
|
2 |
|
$this->loadView($this->_getFiles()->getViewForm(), [ "modal" => $modal,"instance"=>$instance,"isNew"=>$instance->_new ]); |
|
101
|
|
|
} else { |
|
102
|
|
|
$this->jquery->exec("$('#modal-frmEdit').modal('show');", true); |
|
103
|
|
|
$form=$form->asModal(\get_class($instance)); |
|
104
|
|
|
$form->setActions([ "Okay","Cancel" ]); |
|
105
|
|
|
$btOkay=$form->getAction(0); |
|
106
|
|
|
$btOkay->addClass("green")->setValue("Validate modifications"); |
|
107
|
|
|
$form->onHidden("$('#modal-frmEdit').remove();"); |
|
108
|
|
|
echo $form->compile($this->jquery, $this->view); |
|
109
|
|
|
echo $this->jquery->compile($this->view); |
|
110
|
|
|
} |
|
111
|
2 |
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
protected function _showModel($id=null) { |
|
114
|
1 |
|
$model=$this->model; |
|
115
|
1 |
|
$datas=$this->getInstances($totalCount,1,$id); |
|
116
|
1 |
|
return $this->_getModelViewer()->getModelDataTable($datas, $model,$totalCount,$this->activePage); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Helper to delete multiple objects |
|
121
|
|
|
* @param mixed $data |
|
122
|
|
|
* @param string $action |
|
123
|
|
|
* @param string $target the css selector for refreshing |
|
124
|
|
|
* @param callable|string $condition the callback for generating the SQL where (for deletion) with the parameter data, or a simple string |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function _deleteMultiple($data,$action,$target,$condition){ |
|
127
|
|
|
if(URequest::isPost()){ |
|
128
|
|
|
if(is_callable($condition)){ |
|
129
|
|
|
$condition=$condition($data); |
|
130
|
|
|
} |
|
131
|
|
|
$rep=DAO::deleteAll($this->model, $condition); |
|
132
|
|
|
if($rep){ |
|
133
|
|
|
$message=new CRUDMessage("Deleting {count} objects","Deletion","info","info circle",4000); |
|
134
|
|
|
$message=$this->_getEvents()->onSuccessDeleteMultipleMessage($message,$rep); |
|
135
|
|
|
$message->parseContent(["count"=>$rep]); |
|
136
|
|
|
} |
|
137
|
|
|
if(isset($message)){ |
|
138
|
|
|
$this->_showSimpleMessage($message,"delete-all"); |
|
139
|
|
|
} |
|
140
|
|
|
$this->index(); |
|
141
|
|
|
}else{ |
|
142
|
|
|
$message=new CRUDMessage("Do you confirm the deletion of this objects?", "Remove confirmation","error"); |
|
143
|
|
|
$this->_getEvents()->onConfDeleteMultipleMessage($message,$data); |
|
144
|
|
|
$message=$this->_showConfMessage($message, $this->_getBaseRoute() . "/{$action}/{$data}",$target, $data,["jqueryDone"=>"replaceWith"]); |
|
145
|
|
|
echo $message; |
|
146
|
|
|
echo $this->jquery->compile($this->view); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
2 |
|
protected function refreshInstance($instance,$isNew){ |
|
152
|
2 |
|
if($this->_getAdminData()->refreshPartialInstance() && !$isNew){ |
|
153
|
1 |
|
$this->jquery->setJsonToElement(OrmUtils::objectAsJSON($instance)); |
|
154
|
|
|
}else{ |
|
155
|
1 |
|
$pk=OrmUtils::getFirstKeyValue($instance); |
|
156
|
1 |
|
$this->jquery->get($this->_getBaseRoute() . "/refreshTable/".$pk, "#lv", [ "jqueryDone" => "replaceWith" ]); |
|
157
|
|
|
} |
|
158
|
2 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* To override for defining a new adminData |
|
162
|
|
|
* @return CRUDDatas |
|
163
|
|
|
*/ |
|
164
|
1 |
|
protected function getAdminData ():CRUDDatas{ |
|
165
|
1 |
|
return new CRUDDatas(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
6 |
|
public function _getAdminData ():CRUDDatas{ |
|
169
|
6 |
|
return $this->getSingleton($this->adminDatas,"getAdminData"); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* To override for defining a new ModelViewer |
|
174
|
|
|
* @return ModelViewer |
|
175
|
|
|
*/ |
|
176
|
1 |
|
protected function getModelViewer ():ModelViewer{ |
|
177
|
1 |
|
return new ModelViewer($this); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
6 |
|
private function _getModelViewer():ModelViewer{ |
|
181
|
6 |
|
return $this->getSingleton($this->modelViewer,"getModelViewer"); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* To override for changing view files |
|
186
|
|
|
* @return CRUDFiles |
|
187
|
|
|
*/ |
|
188
|
1 |
|
protected function getFiles ():CRUDFiles{ |
|
189
|
1 |
|
return new CRUDFiles(); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @return CRUDFiles |
|
194
|
|
|
*/ |
|
195
|
6 |
|
public function _getFiles(){ |
|
196
|
6 |
|
return $this->getSingleton($this->crudFiles,"getFiles"); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* To override for changing events |
|
201
|
|
|
* @return CRUDEvents |
|
202
|
|
|
*/ |
|
203
|
1 |
|
protected function getEvents ():CRUDEvents{ |
|
204
|
1 |
|
return new CRUDEvents($this); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
6 |
|
private function _getEvents():CRUDEvents{ |
|
208
|
6 |
|
return $this->getSingleton($this->events,"getEvents"); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
6 |
|
private function getSingleton($value, $method) { |
|
212
|
6 |
|
if (! isset ( $value )) { |
|
213
|
6 |
|
$value = $this->$method (); |
|
214
|
|
|
} |
|
215
|
6 |
|
return $value; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
6 |
|
private function crudLoadView($viewName,$vars=[]){ |
|
219
|
6 |
|
$this->_getEvents()->beforeLoadView($viewName,$vars); |
|
220
|
6 |
|
if(!URequest::isAjax()){ |
|
221
|
6 |
|
$files=$this->_getFiles(); |
|
222
|
6 |
|
$mainTemplate=$files->getBaseTemplate(); |
|
223
|
6 |
|
if(isset($mainTemplate)){ |
|
224
|
|
|
$vars["_viewname"]=$viewName; |
|
225
|
|
|
$vars["_base"]=$mainTemplate; |
|
226
|
|
|
$this->jquery->renderView($files->getViewBaseTemplate(),$vars); |
|
227
|
|
|
}else{ |
|
228
|
6 |
|
$vars["hasScript"]=true; |
|
229
|
6 |
|
$this->jquery->renderView($viewName,$vars); |
|
230
|
|
|
} |
|
231
|
|
|
}else{ |
|
232
|
|
|
$vars["hasScript"]=true; |
|
233
|
|
|
$this->jquery->renderView($viewName,$vars); |
|
234
|
|
|
} |
|
235
|
6 |
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param object $instance |
|
239
|
|
|
* @return string |
|
240
|
|
|
*/ |
|
241
|
2 |
|
protected function getInstanceToString($instance){ |
|
242
|
2 |
|
if (method_exists($instance, "__toString")){ |
|
243
|
2 |
|
return $instance . ""; |
|
244
|
|
|
} |
|
245
|
|
|
else{ |
|
246
|
|
|
return get_class($instance); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
|