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