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