1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\crud; |
4
|
|
|
|
5
|
|
|
use Ubiquity\orm\DAO; |
6
|
|
|
use Ubiquity\controllers\ControllerBase; |
7
|
|
|
use Ubiquity\controllers\admin\interfaces\HasModelViewerInterface; |
8
|
|
|
use Ubiquity\controllers\semantic\MessagesTrait; |
9
|
|
|
use Ubiquity\utils\http\URequest; |
10
|
|
|
use Ubiquity\utils\http\UResponse; |
11
|
|
|
use Ubiquity\controllers\rest\ResponseFormatter; |
12
|
|
|
use Ubiquity\orm\OrmUtils; |
13
|
|
|
use Ubiquity\utils\base\UString; |
14
|
|
|
use Ajax\semantic\html\collections\HtmlMessage; |
15
|
|
|
use Ajax\common\html\HtmlContentOnly; |
16
|
|
|
|
17
|
|
|
abstract class CRUDController extends ControllerBase implements HasModelViewerInterface{ |
18
|
|
|
use MessagesTrait,CRUDControllerUtilitiesTrait; |
19
|
|
|
|
20
|
|
|
protected $model; |
21
|
|
|
protected $activePage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Default page : list all objects |
25
|
|
|
* Uses modelViewer.isModal, modelViewer.getModelDataTable |
26
|
|
|
* Uses CRUDFiles.getViewIndex template (default : @framework/crud/index.html) |
27
|
|
|
* Triggers the events onDisplayElements,beforeLoadView |
28
|
|
|
*/ |
29
|
5 |
|
public function index() { |
30
|
5 |
|
$objects=$this->getInstances($totalCount); |
31
|
5 |
|
$modal=($this->_getModelViewer()->isModal($objects,$this->model))?"modal":"no"; |
32
|
5 |
|
$dt=$this->_getModelViewer()->getModelDataTable($objects, $this->model,$totalCount); |
33
|
5 |
|
$this->jquery->getOnClick ( "#btAddNew", $this->_getBaseRoute() . "/newModel/" . $modal, "#frm-add-update",["hasLoader"=>"internal"] ); |
34
|
5 |
|
$this->_getEvents()->onDisplayElements($dt,$objects,false); |
35
|
|
|
$this->crudLoadView($this->_getFiles()->getViewIndex(), [ "classname" => $this->model ,"messages"=>$this->jquery->semantic()->matchHtmlComponents(function($compo){return $compo instanceof HtmlMessage;})]); |
36
|
5 |
|
} |
37
|
|
|
|
38
|
2 |
|
public function updateMember($member,$callback=false){ |
39
|
2 |
|
$instance=$_SESSION["instance"]??null; |
40
|
2 |
|
if(isset($instance)){ |
41
|
2 |
|
$updated=CRUDHelper::update($instance, $_POST); |
42
|
2 |
|
if($updated){ |
43
|
2 |
|
if($callback===false){ |
44
|
1 |
|
$dt=$this->_getModelViewer()->getModelDataTable([$instance], $this->model, 1); |
45
|
1 |
|
$dt->compile(); |
46
|
1 |
|
echo new HtmlContentOnly($dt->getFieldValue($member)); |
47
|
|
|
}else{ |
48
|
1 |
|
if(method_exists($this, $callback)){ |
49
|
1 |
|
$this->$callback($member,$instance); |
50
|
|
|
}else{ |
51
|
|
|
throw new \Exception("The method `".$callback."` does not exists in ".get_class()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
}else{ |
55
|
|
|
UResponse::setResponseCode(404); |
56
|
|
|
} |
57
|
|
|
}else{ |
58
|
|
|
throw new \Exception('$_SESSION["instance"] is null'); |
59
|
|
|
} |
60
|
2 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Refreshes the area corresponding to the DataTable |
64
|
|
|
*/ |
65
|
1 |
|
public function refresh_(){ |
66
|
1 |
|
$model=$this->model; |
67
|
1 |
|
if(isset($_POST["s"])){ |
68
|
1 |
|
$instances=$this->search($model, $_POST["s"]); |
69
|
|
|
}else{ |
70
|
|
|
$page=URequest::post("p",1); |
71
|
|
|
$instances=$this->getInstances($totalCount,$page); |
72
|
|
|
} |
73
|
1 |
|
if(!isset($totalCount)){ |
74
|
1 |
|
$totalCount=DAO::count($model,$this->_getAdminData()->_getInstancesFilter($model)); |
75
|
|
|
} |
76
|
1 |
|
$recordsPerPage=$this->_getModelViewer()->recordsPerPage($model,$totalCount); |
77
|
1 |
|
$grpByFields=$this->_getModelViewer()->getGroupByFields(); |
78
|
1 |
|
if(isset($recordsPerPage)){ |
79
|
|
|
if(!is_array($grpByFields)){ |
80
|
|
|
UResponse::asJSON(); |
81
|
|
|
$responseFormatter=new ResponseFormatter(); |
82
|
|
|
print_r($responseFormatter->getJSONDatas($instances)); |
83
|
|
|
}else{ |
84
|
|
|
$this->_renderDataTableForRefresh($instances, $model,$totalCount); |
85
|
|
|
} |
86
|
|
|
}else{ |
87
|
1 |
|
$this->jquery->execAtLast('$("#search-query-content").html("'.$_POST["s"].'");$("#search-query").show();$("#table-details").html("");'); |
88
|
1 |
|
$this->_renderDataTableForRefresh($instances, $model,$totalCount); |
89
|
|
|
} |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Edits an instance |
94
|
|
|
* @param string $modal Accept "no" or "modal" for a modal dialog |
95
|
|
|
* @param string $ids the primary value(s) |
96
|
|
|
*/ |
97
|
1 |
|
public function edit($modal="no", $ids="") { |
98
|
1 |
|
if(URequest::isAjax()){ |
99
|
1 |
|
$instance=$this->getModelInstance($ids); |
100
|
1 |
|
$instance->_new=false; |
101
|
1 |
|
$this->_edit($instance, $modal); |
102
|
|
|
}else{ |
103
|
1 |
|
$this->jquery->execAtLast("$('._edit[data-ajax={$ids}]').trigger('click');"); |
104
|
1 |
|
$this->index(); |
105
|
|
|
} |
106
|
1 |
|
} |
107
|
|
|
/** |
108
|
|
|
* Adds a new instance and edits it |
109
|
|
|
* @param string $modal Accept "no" or "modal" for a modal dialog |
110
|
|
|
*/ |
111
|
1 |
|
public function newModel($modal="no") { |
112
|
1 |
|
if(URequest::isAjax()){ |
113
|
1 |
|
$model=$this->model; |
114
|
1 |
|
$instance=new $model(); |
115
|
1 |
|
$instance->_new=true; |
116
|
1 |
|
$this->_edit($instance, $modal); |
117
|
|
|
}else{ |
118
|
|
|
$this->jquery->execAtLast("$('.ui.button._new').trigger('click');"); |
119
|
|
|
$this->index(); |
120
|
|
|
} |
121
|
1 |
|
} |
122
|
|
|
|
123
|
2 |
|
public function editMember($member){ |
124
|
2 |
|
$ids=URequest::post("id"); |
125
|
2 |
|
$td=URequest::post("td"); |
126
|
2 |
|
$part=URequest::post("part"); |
127
|
2 |
|
$instance=$this->getModelInstance($ids,$member); |
128
|
2 |
|
$_SESSION["instance"]=$instance; |
129
|
2 |
|
$instance->_new=false; |
130
|
2 |
|
$form=$this->_getModelViewer()->getMemberForm("frm-member-".$member, $instance, $member,$td,$part); |
131
|
2 |
|
$form->setLibraryId("_compo_"); |
132
|
2 |
|
$this->jquery->renderView("@framework/main/component.html"); |
133
|
2 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Displays an instance |
137
|
|
|
* @param string $modal |
138
|
|
|
* @param string $ids |
139
|
|
|
*/ |
140
|
2 |
|
public function display($modal="no",$ids=""){ |
141
|
2 |
|
if(URequest::isAjax()){ |
142
|
2 |
|
$instance=$this->getModelInstance($ids); |
143
|
2 |
|
$key=OrmUtils::getFirstKeyValue($instance); |
144
|
2 |
|
$this->jquery->execOn("click","._close",'$("#table-details").html("");$("#dataTable").show();'); |
145
|
2 |
|
$this->jquery->getOnClick("._edit", $this->_getBaseRoute()."/edit/".$modal."/".$key,"#frm-add-update"); |
146
|
2 |
|
$this->jquery->getOnClick("._delete", $this->_getBaseRoute()."/delete/".$key,"#table-messages"); |
147
|
|
|
|
148
|
2 |
|
$this->_getModelViewer()->getModelDataElement($instance, $this->model,$modal); |
149
|
2 |
|
$this->jquery->renderView($this->_getFiles()->getViewDisplay(), [ "classname" => $this->model,"instance"=>$instance,"pk"=>$key ]); |
150
|
|
|
}else{ |
151
|
2 |
|
$this->jquery->execAtLast("$('._display[data-ajax={$ids}]').trigger('click');"); |
152
|
2 |
|
$this->index(); |
153
|
|
|
} |
154
|
2 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Deletes an instance |
158
|
|
|
* @param mixed $ids |
159
|
|
|
*/ |
160
|
2 |
|
public function delete($ids) { |
161
|
2 |
|
if(URequest::isAjax()){ |
162
|
2 |
|
$instance=$this->getModelInstance($ids); |
163
|
2 |
|
$instanceString=$this->getInstanceToString($instance); |
164
|
2 |
|
if (sizeof($_POST) > 0) { |
165
|
|
|
try{ |
166
|
2 |
|
if (DAO::remove($instance)) { |
167
|
1 |
|
$message=new CRUDMessage("Deletion of `<b>" . $instanceString . "</b>`","Deletion","info","info circle",4000); |
168
|
1 |
|
$message=$this->_getEvents()->onSuccessDeleteMessage($message,$instance); |
169
|
1 |
|
$this->jquery->exec("$('._element[data-ajax={$ids}]').remove();", true); |
170
|
|
|
} else { |
171
|
1 |
|
$message=new CRUDMessage("Can not delete `" . $instanceString . "`","Deletion","warning","warning circle"); |
172
|
1 |
|
$message=$this->_getEvents()->onErrorDeleteMessage($message,$instance); |
173
|
|
|
} |
174
|
|
|
}catch (\Exception $e){ |
175
|
|
|
$message=new CRUDMessage("Exception : can not delete `" . $instanceString . "`","Exception", "warning", "warning"); |
176
|
|
|
$message=$this->_getEvents()->onErrorDeleteMessage($message,$instance); |
177
|
|
|
} |
178
|
2 |
|
$message=$this->_showSimpleMessage($message); |
179
|
|
|
} else { |
180
|
2 |
|
$message=new CRUDMessage("Do you confirm the deletion of `<b>" . $instanceString . "</b>`?", "Remove confirmation","error","question circle"); |
181
|
2 |
|
$message=$this->_getEvents()->onConfDeleteMessage($message,$instance); |
182
|
2 |
|
$message=$this->_showConfMessage($message, $this->_getBaseRoute() . "/delete/{$ids}", "#table-messages", $ids); |
183
|
|
|
} |
184
|
2 |
|
echo $message; |
185
|
2 |
|
echo $this->jquery->compile($this->view); |
186
|
|
|
}else{ |
187
|
|
|
$this->jquery->execAtLast("$('._delete[data-ajax={$ids}]').trigger('click');"); |
188
|
|
|
$this->index(); |
189
|
|
|
} |
190
|
2 |
|
} |
191
|
|
|
|
192
|
1 |
|
public function refreshTable($id=null) { |
193
|
1 |
|
$compo= $this->_showModel($id); |
194
|
1 |
|
$this->jquery->execAtLast('$("#table-details").html("");'); |
195
|
1 |
|
$compo->setLibraryId("_compo_"); |
196
|
1 |
|
$this->jquery->renderView("@framework/main/component.html"); |
197
|
1 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Updates an instance from the data posted in a form |
201
|
|
|
* @return object The updated instance |
202
|
|
|
*/ |
203
|
2 |
|
public function update() { |
204
|
2 |
|
$message=new CRUDMessage("Modifications were successfully saved", "Updating"); |
205
|
2 |
|
$instance=$_SESSION["instance"]??null; |
206
|
2 |
|
if(isset($instance)){ |
207
|
2 |
|
$isNew=$instance->_new; |
208
|
|
|
try{ |
209
|
2 |
|
$updated=CRUDHelper::update($instance, $_POST); |
210
|
2 |
|
if($updated){ |
211
|
2 |
|
$message->setType("success")->setIcon("check circle outline"); |
212
|
2 |
|
$message=$this->_getEvents()->onSuccessUpdateMessage($message,$instance); |
213
|
2 |
|
$this->refreshInstance($instance,$isNew); |
214
|
|
|
} else { |
215
|
|
|
$message->setMessage("An error has occurred. Can not save changes.")->setType("error")->setIcon("warning circle"); |
216
|
|
|
$message=$this->_getEvents()->onErrorUpdateMessage($message,$instance); |
217
|
|
|
} |
218
|
|
|
}catch(\Exception $e){ |
219
|
|
|
$instanceString=$this->getInstanceToString($instance); |
220
|
|
|
$message=new CRUDMessage("Exception : can not update `" . $instanceString . "`","Exception", "warning", "warning"); |
221
|
|
|
$message=$this->_getEvents()->onErrorUpdateMessage($message,$instance); |
222
|
|
|
} |
223
|
2 |
|
echo $this->_showSimpleMessage($message,"updateMsg"); |
224
|
2 |
|
echo $this->jquery->compile($this->view); |
225
|
2 |
|
return $instance; |
226
|
|
|
}else{ |
227
|
|
|
throw new \Exception('$_SESSION["instance"] is null'); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Shows associated members with foreign keys |
233
|
|
|
* @param mixed $ids |
234
|
|
|
*/ |
235
|
1 |
|
public function showDetail($ids) { |
236
|
1 |
|
if(URequest::isAjax()){ |
237
|
1 |
|
$instance=$this->getModelInstance($ids); |
238
|
1 |
|
$viewer=$this->_getModelViewer(); |
239
|
1 |
|
$hasElements=false; |
240
|
1 |
|
$model=$this->model; |
241
|
1 |
|
$fkInstances=CRUDHelper::getFKIntances($instance, $model); |
242
|
1 |
|
$semantic=$this->jquery->semantic(); |
243
|
1 |
|
$grid=$semantic->htmlGrid("detail"); |
244
|
1 |
|
if (sizeof($fkInstances) > 0) { |
245
|
1 |
|
$wide=intval(16 / sizeof($fkInstances)); |
246
|
1 |
|
if ($wide < 4) |
247
|
|
|
$wide=4; |
248
|
1 |
|
foreach ( $fkInstances as $member=>$fkInstanceArray ) { |
249
|
1 |
|
$element=$viewer->getFkMemberElementDetails($member,$fkInstanceArray["objectFK"],$fkInstanceArray["fkClass"],$fkInstanceArray["fkTable"]); |
250
|
1 |
|
if (isset($element)) { |
251
|
1 |
|
$grid->addCol($wide)->setContent($element); |
252
|
1 |
|
$hasElements=true; |
253
|
|
|
} |
254
|
|
|
} |
255
|
1 |
|
if ($hasElements){ |
256
|
1 |
|
echo $grid; |
257
|
1 |
|
$url=$this->_getFiles()->getDetailClickURL($this->model); |
258
|
1 |
|
if(UString::isNotNull($url)){ |
259
|
|
|
$this->detailClick($url); |
260
|
|
|
} |
261
|
|
|
} |
262
|
1 |
|
echo $this->jquery->compile($this->view); |
263
|
|
|
} |
264
|
|
|
}else{ |
265
|
1 |
|
$this->jquery->execAtLast("$('tr[data-ajax={$ids}]').trigger('click');"); |
266
|
1 |
|
$this->index(); |
267
|
|
|
} |
268
|
|
|
|
269
|
1 |
|
} |
270
|
|
|
|
271
|
|
|
public function detailClick($url) { |
272
|
|
|
$this->jquery->postOnClick(".showTable", $this->_getBaseRoute() . "/".$url,"{}", "#divTable", [ "attr" => "data-ajax","ajaxTransition" => "random" ]); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
} |
276
|
|
|
|