|
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\admin\UbiquityMyAdminData; |
|
9
|
|
|
use Ubiquity\controllers\admin\viewers\ModelViewer; |
|
10
|
|
|
use Ubiquity\controllers\semantic\MessagesTrait; |
|
11
|
|
|
|
|
12
|
|
|
abstract class CRUDController extends ControllerBase implements HasModelViewerInterface{ |
|
13
|
|
|
use MessagesTrait; |
|
14
|
|
|
protected $model; |
|
15
|
|
|
protected $modelViewer; |
|
16
|
|
|
protected $crudFiles; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Default page : list all objects |
|
20
|
|
|
*/ |
|
21
|
|
|
public function index() { |
|
22
|
|
|
$objects=DAO::getAll($this->model); |
|
23
|
|
|
$modal=($this->_getModelViewer()->isModal($objects,$this->model))?"modal":"no"; |
|
24
|
|
|
$this->_getModelViewer()->getModelDataTable($objects, $this->model); |
|
25
|
|
|
$this->jquery->getOnClick ( "#btAddNew", $this->_getBaseRoute() . "/newModel/" . $modal, "#frm-add-update",["hasLoader"=>"internal"] ); |
|
26
|
|
|
$this->jquery->compile($this->view); |
|
27
|
|
|
$this->loadView($this->_getFiles()->getViewShowTable(), [ "classname" => $this->model ]); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Edits an instance |
|
32
|
|
|
* @param string $modal Accept "no" or "modal" for a modal dialog |
|
33
|
|
|
* @param string $ids the primary value(s) |
|
34
|
|
|
*/ |
|
35
|
|
|
public function edit($modal="no", $ids="") { |
|
36
|
|
|
$instance=$this->getModelInstance($ids); |
|
37
|
|
|
$instance->_new=false; |
|
38
|
|
|
$this->_edit($instance, $modal); |
|
39
|
|
|
} |
|
40
|
|
|
/** |
|
41
|
|
|
* Adds a new instance and edits it |
|
42
|
|
|
* @param string $modal Accept "no" or "modal" for a modal dialog |
|
43
|
|
|
*/ |
|
44
|
|
|
public function newModel($modal="no") { |
|
45
|
|
|
$model=$this->model; |
|
46
|
|
|
$instance=new $model(); |
|
47
|
|
|
$instance->_new=true; |
|
48
|
|
|
$this->_edit($instance, $modal); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function _edit($instance, $modal="no") { |
|
52
|
|
|
$_SESSION["instance"]=$instance; |
|
53
|
|
|
$modal=($modal == "modal"); |
|
54
|
|
|
$form=$this->_getModelViewer()->getForm("frmEdit", $instance); |
|
55
|
|
|
$this->jquery->click("#action-modal-frmEdit-0", "$('#frmEdit').form('submit');", false); |
|
56
|
|
|
if (!$modal) { |
|
57
|
|
|
$this->jquery->click("#bt-cancel", "$('#form-container').transition('drop');"); |
|
58
|
|
|
$this->jquery->compile($this->view); |
|
59
|
|
|
$this->loadView($this->_getFiles()->getViewEditTable(), [ "modal" => $modal ]); |
|
60
|
|
|
} else { |
|
61
|
|
|
$this->jquery->exec("$('#modal-frmEdit').modal('show');", true); |
|
62
|
|
|
$form=$form->asModal(\get_class($instance)); |
|
63
|
|
|
$form->setActions([ "Okay","Cancel" ]); |
|
64
|
|
|
$btOkay=$form->getAction(0); |
|
65
|
|
|
$btOkay->addClass("green")->setValue("Validate modifications"); |
|
66
|
|
|
$form->onHidden("$('#modal-frmEdit').remove();"); |
|
67
|
|
|
echo $form->compile($this->jquery, $this->view); |
|
68
|
|
|
echo $this->jquery->compile($this->view); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function _showModel() { |
|
73
|
|
|
$model=$this->model; |
|
74
|
|
|
$datas=DAO::getAll($model); |
|
75
|
|
|
return $this->_getModelViewer()->getModelDataTable($datas, $model); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Deletes an instance |
|
80
|
|
|
* @param mixed $ids |
|
81
|
|
|
*/ |
|
82
|
|
|
public function delete($ids) { |
|
83
|
|
|
$instance=$this->getModelInstance($ids); |
|
84
|
|
|
if (method_exists($instance, "__toString")) |
|
85
|
|
|
$instanceString=$instance . ""; |
|
86
|
|
|
else |
|
87
|
|
|
$instanceString=get_class($instance); |
|
88
|
|
|
if (sizeof($_POST) > 0) { |
|
89
|
|
|
try{ |
|
90
|
|
|
if (DAO::remove($instance)) { |
|
|
|
|
|
|
91
|
|
|
$message=$this->showSimpleMessage("Deletion of `<b>" . $instanceString . "</b>`", "info", "info", 4000); |
|
92
|
|
|
$this->jquery->exec("$('tr[data-ajax={$ids}]').remove();", true); |
|
93
|
|
|
} else { |
|
94
|
|
|
$message=$this->showSimpleMessage("Can not delete `" . $instanceString . "`", "warning", "warning"); |
|
95
|
|
|
} |
|
96
|
|
|
}catch (\Exception $e){ |
|
97
|
|
|
$message=$this->showSimpleMessage("Exception : can not delete `" . $instanceString . "`", "warning", "warning"); |
|
98
|
|
|
} |
|
99
|
|
|
} else { |
|
100
|
|
|
$message=$this->showConfMessage("Do you confirm the deletion of `<b>" . $instanceString . "</b>`?", "error", $this->_getBaseRoute() . "/delete/{$ids}", "#table-messages", $ids); |
|
101
|
|
|
} |
|
102
|
|
|
echo $message; |
|
103
|
|
|
echo $this->jquery->compile($this->view); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function refreshTable() { |
|
107
|
|
|
echo $this->_showModel(); |
|
108
|
|
|
echo $this->jquery->compile($this->view); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Updates an instance from the data posted in a form |
|
113
|
|
|
*/ |
|
114
|
|
|
public function update() { |
|
115
|
|
|
$message=$this->jquery->semantic()->htmlMessage("msgUpdate", "Modifications were successfully saved", "info"); |
|
116
|
|
|
$instance=@$_SESSION["instance"]; |
|
117
|
|
|
$updated=CRUDHelper::update($instance, $_POST,$this->_getAdminData()->getUpdateManyToOneInForm(),$this->_getAdminData()->getUpdateManyToManyInForm()); |
|
118
|
|
|
if($updated){ |
|
119
|
|
|
$message->setStyle("success")->setIcon("checkmark"); |
|
120
|
|
|
$this->jquery->get($this->_getBaseRoute() . "/refreshTable", "#lv", [ "jqueryDone" => "replaceWith" ]); |
|
121
|
|
|
} else { |
|
122
|
|
|
$message->setContent("An error has occurred. Can not save changes.")->setStyle("error")->setIcon("warning circle"); |
|
123
|
|
|
} |
|
124
|
|
|
echo $message; |
|
125
|
|
|
echo $this->jquery->compile($this->view); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Shows associated members with foreign keys |
|
130
|
|
|
* @param mixed $ids |
|
131
|
|
|
*/ |
|
132
|
|
|
public function showDetail($ids) { |
|
133
|
|
|
$instance=$this->getModelInstance($ids); |
|
134
|
|
|
$viewer=$this->_getModelViewer(); |
|
135
|
|
|
$hasElements=false; |
|
136
|
|
|
$model=$this->model; |
|
137
|
|
|
$fkInstances=CRUDHelper::getFKIntances($instance, $model); |
|
138
|
|
|
$semantic=$this->jquery->semantic(); |
|
139
|
|
|
$grid=$semantic->htmlGrid("detail"); |
|
140
|
|
|
if (sizeof($fkInstances) > 0) { |
|
141
|
|
|
$wide=intval(16 / sizeof($fkInstances)); |
|
142
|
|
|
if ($wide < 4) |
|
143
|
|
|
$wide=4; |
|
144
|
|
|
foreach ( $fkInstances as $member=>$fkInstanceArray ) { |
|
145
|
|
|
$element=$viewer->getFkMemberElement($member,$fkInstanceArray["objectFK"],$fkInstanceArray["fkClass"],$fkInstanceArray["fkTable"]); |
|
146
|
|
|
if (isset($element)) { |
|
147
|
|
|
$grid->addCol($wide)->setContent($element); |
|
148
|
|
|
$hasElements=true; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
if ($hasElements) |
|
152
|
|
|
echo $grid; |
|
153
|
|
|
$this->jquery->getOnClick(".showTable", $this->_getBaseRoute() . "/showTableClick", "#divTable", [ "attr" => "data-ajax","ajaxTransition" => "random" ]); |
|
154
|
|
|
echo $this->jquery->compile($this->view); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
private function getModelInstance($ids) { |
|
160
|
|
|
$ids=\explode("_", $ids); |
|
161
|
|
|
$instance=DAO::getOne($this->model, $ids); |
|
162
|
|
|
if(isset($instance)){ |
|
163
|
|
|
return $instance; |
|
164
|
|
|
} |
|
165
|
|
|
echo $this->showSimpleMessage("This object does not exist!", "warning","warning circle"); |
|
166
|
|
|
echo $this->jquery->compile($this->view); |
|
167
|
|
|
exit(1); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function _getAdminData ():UbiquityMyAdminData{ |
|
171
|
|
|
return new UbiquityMyAdminData(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* To override for defining a new ModelViewer |
|
176
|
|
|
* @return ModelViewer |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function getModelViewer ():ModelViewer{ |
|
179
|
|
|
return new ModelViewer($this); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
private function _getModelViewer():modelViewer{ |
|
183
|
|
|
return $this->getSingleton($this->modelViewer,"getModelViewer"); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* To override for changing view files |
|
188
|
|
|
* @return CRUDFiles |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function getFiles ():CRUDFiles{ |
|
191
|
|
|
return new CRUDFiles(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
private function _getFiles():CRUDFiles{ |
|
195
|
|
|
return $this->getSingleton($this->crudFiles,"getFiles"); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
private function getSingleton($value, $method) { |
|
199
|
|
|
if (! isset ( $value )) { |
|
200
|
|
|
$value = $this->$method (); |
|
201
|
|
|
} |
|
202
|
|
|
return $value; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.