|
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
|
1 |
|
public function index() { |
|
30
|
1 |
|
$objects=$this->getInstances($totalCount); |
|
31
|
1 |
|
$modal=($this->_getModelViewer()->isModal($objects,$this->model))?"modal":"no"; |
|
32
|
1 |
|
$dt=$this->_getModelViewer()->getModelDataTable($objects, $this->model,$totalCount); |
|
33
|
1 |
|
$this->jquery->getOnClick ( "#btAddNew", $this->_getBaseRoute() . "/newModel/" . $modal, "#frm-add-update",["hasLoader"=>"internal"] ); |
|
34
|
1 |
|
$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
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function updateMember($member,$callback=false){ |
|
39
|
1 |
|
$instance=$_SESSION["instance"]??null; |
|
40
|
1 |
|
if(isset($instance)){ |
|
41
|
1 |
|
$updated=CRUDHelper::update($instance, $_POST); |
|
42
|
1 |
|
if($updated){ |
|
43
|
1 |
|
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
|
|
|
if(method_exists($this, $callback)){ |
|
49
|
|
|
$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
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Refreshes the area corresponding to the DataTable |
|
64
|
|
|
*/ |
|
65
|
|
|
public function refresh_(){ |
|
66
|
|
|
$model=$this->model; |
|
67
|
|
|
if(isset($_POST["s"])){ |
|
68
|
|
|
$instances=$this->search($model, $_POST["s"]); |
|
69
|
|
|
}else{ |
|
70
|
|
|
$page=URequest::post("p",1); |
|
71
|
|
|
$instances=$this->getInstances($totalCount,$page); |
|
72
|
|
|
} |
|
73
|
|
|
if(!isset($totalCount)){ |
|
74
|
|
|
$totalCount=DAO::count($model,$this->_getAdminData()->_getInstancesFilter($model)); |
|
75
|
|
|
} |
|
76
|
|
|
$recordsPerPage=$this->_getModelViewer()->recordsPerPage($model,$totalCount); |
|
77
|
|
|
$grpByFields=$this->_getModelViewer()->getGroupByFields(); |
|
78
|
|
|
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
|
|
|
$this->jquery->execAtLast('$("#search-query-content").html("'.$_POST["s"].'");$("#search-query").show();$("#table-details").html("");'); |
|
88
|
|
|
$this->_renderDataTableForRefresh($instances, $model,$totalCount); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
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
|
|
|
public function newModel($modal="no") { |
|
112
|
|
|
if(URequest::isAjax()){ |
|
113
|
|
|
$model=$this->model; |
|
114
|
|
|
$instance=new $model(); |
|
115
|
|
|
$instance->_new=true; |
|
116
|
|
|
$this->_edit($instance, $modal); |
|
117
|
|
|
}else{ |
|
118
|
|
|
$this->jquery->execAtLast("$('.ui.button._new').trigger('click');"); |
|
119
|
|
|
$this->index(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public function editMember($member){ |
|
124
|
1 |
|
$ids=URequest::post("id"); |
|
125
|
1 |
|
$td=URequest::post("td"); |
|
126
|
1 |
|
$part=URequest::post("part"); |
|
127
|
1 |
|
$instance=$this->getModelInstance($ids,$member); |
|
128
|
1 |
|
$_SESSION["instance"]=$instance; |
|
129
|
1 |
|
$instance->_new=false; |
|
130
|
1 |
|
$form=$this->_getModelViewer()->getMemberForm("frm-member-".$member, $instance, $member,$td,$part); |
|
131
|
1 |
|
$form->setLibraryId("_compo_"); |
|
132
|
1 |
|
$this->jquery->renderView("@framework/main/component.html"); |
|
133
|
1 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Displays an instance |
|
137
|
|
|
* @param string $modal |
|
138
|
|
|
* @param string $ids |
|
139
|
|
|
*/ |
|
140
|
|
|
public function display($modal="no",$ids=""){ |
|
141
|
|
|
if(URequest::isAjax()){ |
|
142
|
|
|
$instance=$this->getModelInstance($ids); |
|
143
|
|
|
$key=OrmUtils::getFirstKeyValue($instance); |
|
144
|
|
|
$this->jquery->execOn("click","._close",'$("#table-details").html("");$("#dataTable").show();'); |
|
145
|
|
|
$this->jquery->getOnClick("._edit", $this->_getBaseRoute()."/edit/".$modal."/".$key,"#frm-add-update"); |
|
146
|
|
|
$this->jquery->getOnClick("._delete", $this->_getBaseRoute()."/delete/".$key,"#table-messages"); |
|
147
|
|
|
|
|
148
|
|
|
$this->_getModelViewer()->getModelDataElement($instance, $this->model,$modal); |
|
149
|
|
|
$this->jquery->renderView($this->_getFiles()->getViewDisplay(), [ "classname" => $this->model,"instance"=>$instance,"pk"=>$key ]); |
|
150
|
|
|
}else{ |
|
151
|
|
|
$this->jquery->execAtLast("$('._display[data-ajax={$ids}]').trigger('click');"); |
|
152
|
|
|
$this->index(); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Deletes an instance |
|
158
|
|
|
* @param mixed $ids |
|
159
|
|
|
*/ |
|
160
|
|
|
public function delete($ids) { |
|
161
|
|
|
if(URequest::isAjax()){ |
|
162
|
|
|
$instance=$this->getModelInstance($ids); |
|
163
|
|
|
$instanceString=$this->getInstanceToString($instance); |
|
164
|
|
|
if (sizeof($_POST) > 0) { |
|
165
|
|
|
try{ |
|
166
|
|
|
if (DAO::remove($instance)) { |
|
167
|
|
|
$message=new CRUDMessage("Deletion of `<b>" . $instanceString . "</b>`","Deletion","info","info circle",4000); |
|
168
|
|
|
$message=$this->_getEvents()->onSuccessDeleteMessage($message,$instance); |
|
169
|
|
|
$this->jquery->exec("$('._element[data-ajax={$ids}]').remove();", true); |
|
170
|
|
|
} else { |
|
171
|
|
|
$message=new CRUDMessage("Can not delete `" . $instanceString . "`","Deletion","warning","warning circle"); |
|
172
|
|
|
$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
|
|
|
$message=$this->_showSimpleMessage($message); |
|
179
|
|
|
} else { |
|
180
|
|
|
$message=new CRUDMessage("Do you confirm the deletion of `<b>" . $instanceString . "</b>`?", "Remove confirmation","error","question circle"); |
|
181
|
|
|
$message=$this->_getEvents()->onConfDeleteMessage($message,$instance); |
|
182
|
|
|
$message=$this->_showConfMessage($message, $this->_getBaseRoute() . "/delete/{$ids}", "#table-messages", $ids); |
|
183
|
|
|
} |
|
184
|
|
|
echo $message; |
|
185
|
|
|
echo $this->jquery->compile($this->view); |
|
186
|
|
|
}else{ |
|
187
|
|
|
$this->jquery->execAtLast("$('._delete[data-ajax={$ids}]').trigger('click');"); |
|
188
|
|
|
$this->index(); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function refreshTable($id=null) { |
|
193
|
|
|
$compo= $this->_showModel($id); |
|
194
|
|
|
$this->jquery->execAtLast('$("#table-details").html("");'); |
|
195
|
|
|
$compo->setLibraryId("_compo_"); |
|
196
|
|
|
$this->jquery->renderView("@framework/main/component.html"); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Updates an instance from the data posted in a form |
|
201
|
|
|
* @return object The updated instance |
|
202
|
|
|
*/ |
|
203
|
1 |
|
public function update() { |
|
204
|
1 |
|
$message=new CRUDMessage("Modifications were successfully saved", "Updating"); |
|
205
|
1 |
|
$instance=$_SESSION["instance"]??null; |
|
206
|
1 |
|
if(isset($instance)){ |
|
207
|
1 |
|
$isNew=$instance->_new; |
|
208
|
|
|
try{ |
|
209
|
1 |
|
$updated=CRUDHelper::update($instance, $_POST); |
|
210
|
1 |
|
if($updated){ |
|
211
|
1 |
|
$message->setType("success")->setIcon("check circle outline"); |
|
212
|
1 |
|
$message=$this->_getEvents()->onSuccessUpdateMessage($message,$instance); |
|
213
|
1 |
|
$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
|
1 |
|
echo $this->_showSimpleMessage($message,"updateMsg"); |
|
224
|
1 |
|
echo $this->jquery->compile($this->view); |
|
225
|
1 |
|
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
|
|
|
|