Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
CRUDControllerUtilitiesTrait.php
Go to the documentation of this file.
1<?php
2
4
5use Ajax\common\html\HtmlContentOnly;
6use Ajax\semantic\html\collections\HtmlMessage;
7use Ajax\semantic\widgets\datatable\Pagination;
12
22
23 abstract protected function showSimpleMessage_(CRUDMessage $message, $staticName = null): HtmlMessage;
24
25 abstract public function loadView(string $viewName, $pData = null, bool $asString = false);
26
27 abstract public function index();
28
29 abstract public function _getBaseRoute(): string;
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 protected function getInstances(&$totalCount, $page = 1, $id = null) {
41 $this->activePage = $page;
42 $model = $this->model;
43 $condition = $this->_getAdminData()->_getInstancesFilter($model);
44 $totalCount = DAO::count($model, $condition);
45 if ($totalCount) {
46 $recordsPerPage = $this->_getModelViewer()->recordsPerPage($model, $totalCount);
47 if (is_numeric($recordsPerPage)) {
48 if (isset ($id)) {
49 $rownum = DAO::getRownum($model, $id);
50 $this->activePage = Pagination::getPageOfRow($rownum, $recordsPerPage);
51 }
52 return DAO::paginate($model, $this->activePage, $recordsPerPage, $condition);
53 }
54 }
55 return DAO::getAll($model, $condition);
56 }
57
58 protected function search($model, $search) {
59 $fields = $this->_getAdminData()->getSearchFieldNames($model);
60 $condition = $this->_getAdminData()->_getInstancesFilter($model);
61 return CRUDHelper::search($model, $search, $fields, $condition);
62 }
63
71 private function getModelInstance($ids, $transform = true, $included = true) {
72 $ids = \explode("_", $ids);
73 if (!is_bool($included)) {
74 if (!is_array($included)) {
75 $included = [$included];
76 }
77 }
78 DAO::$useTransformers = $transform;
79 $instance = DAO::getById($this->model, $ids, $included);
80 if (isset ($instance)) {
81 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 protected function updateMemberDataElement($member, $instance) {
91 $dt = $this->_getModelViewer()->getModelDataElement($instance, $this->model, false);
92 $dt->compile();
93 echo new HtmlContentOnly ($dt->getFieldValue($member));
94 }
95
96 private function _renderDataTableForRefresh($instances, $model, $totalCount) {
97 $compo = $this->_getModelViewer()->getModelDataTable($instances, $model, $totalCount)->refresh(["tbody"]);
98 $this->_getEvents()->onDisplayElements($compo, $instances, true);
99 $compo->setLibraryId("_compo_");
100 $this->jquery->renderView("@framework/main/component.html");
101 }
102
103 protected function _edit($instance, $modal = "no") {
104 $_SESSION ["instance"] = $instance;
105 $modal = ($modal == "modal");
106 $modelViewer = $this->_getModelViewer();
107 $form = $modelViewer->getForm("frmEdit", $instance);
108 $this->_setStyle($form);
109 $this->jquery->click("#action-modal-frmEdit-0", "$('#frmEdit').form('submit');", false);
110 if (!$modal) {
111 $this->jquery->click("#bt-cancel", "$('#form-container').transition('drop');");
112 $this->jquery->compile($this->view);
113 $this->loadView($this->_getFiles()->getViewForm(), ["modal" => $modal, "instance" => $instance, "isNew" => $instance->_new, 'inverted' => $this->style]);
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 }
127
128 protected function _showModel($id = null) {
129 $model = $this->model;
130 $datas = $this->getInstances($totalCount, 1, $id);
131 return $this->_getModelViewer()->getModelDataTable($datas, $model, $totalCount, $this->activePage);
132 }
133
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 protected function refreshInstance($instance, $isNew) {
168 if ($this->_getAdminData()->refreshPartialInstance() && !$isNew) {
169 $this->jquery->setJsonToElement(OrmUtils::objectAsJSON($instance));
170 } else {
171 $pk = OrmUtils::getFirstKeyValue($instance);
172 $this->jquery->get($this->_getBaseRoute() . "/refreshTable/" . $pk, "#lv", ["jqueryDone" => "replaceWith"]);
173 }
174 }
175
181 protected function getAdminData(): CRUDDatas {
182 return new CRUDDatas ($this);
183 }
184
185 public function _getAdminData(): CRUDDatas {
186 return $this->getSingleton($this->adminDatas, 'getAdminData');
187 }
188
194 protected function getModelViewer(): ModelViewer {
195 return new ModelViewer ($this, $this->style ?? null);
196 }
197
198 protected function _getModelViewer(): ModelViewer {
199 return $this->getSingleton($this->modelViewer, 'getModelViewer');
200 }
201
207 protected function getFiles(): CRUDFiles {
208 return new CRUDFiles ();
209 }
210
215 public function _getFiles() {
216 return $this->getSingleton($this->crudFiles, 'getFiles');
217 }
218
224 protected function getEvents(): CRUDEvents {
225 return new CRUDEvents ($this);
226 }
227
228 private function _getEvents(): CRUDEvents {
229 return $this->getSingleton($this->events, 'getEvents');
230 }
231
232 private function getSingleton(&$value, $method) {
233 if (!isset ($value)) {
234 $value = $this->$method ();
235 }
236 return $value;
237 }
238
239 private function crudLoadView($viewName, $vars = []) {
240 $vars['inverted'] = $this->style;
241 $this->_getEvents()->beforeLoadView($viewName, $vars);
242 if (!URequest::isAjax()) {
243 $files = $this->_getFiles();
244 $mainTemplate = $files->getBaseTemplate();
245 if (isset ($mainTemplate)) {
246 $vars ['_viewname'] = $viewName;
247 $vars ['_base'] = $mainTemplate;
248 $this->jquery->renderView($files->getViewBaseTemplate(), $vars);
249 } else {
250 $vars ['hasScript'] = true;
251 $this->jquery->renderView($viewName, $vars);
252 }
253 } else {
254 $vars ['hasScript'] = true;
255 $this->jquery->renderView($viewName, $vars);
256 }
257 }
258
264 protected function getInstanceToString($instance) {
265 if (\method_exists($instance, '__toString')) {
266 return $instance . '';
267 } else {
268 return \get_class($instance);
269 }
270 }
271}
272
showConfMessage_(CRUDMessage $message, $url, $responseElement, $data, $attributes=null)
getModelViewer()
To override for defining a new ModelViewer.
_deleteMultiple($data, $action, $target, $condition, $params=[])
Helper to delete multiple objects.
showSimpleMessage_(CRUDMessage $message, $staticName=null)
loadView(string $viewName, $pData=null, bool $asString=false)
The base class for displaying datas in CRUD controllers Ubiquity\controllers\crud$CRUDDatas This clas...
Definition CRUDDatas.php:17
static search($model, $search, $fields, $initialCondition='1=1')
Associated with a CRUDController class Responsible of the display.
Gateway class between database and object model.
Definition DAO.php:33
Object/relational mapping utilities.
Definition OrmUtils.php:17
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18