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