1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\controllers; |
4
|
|
|
|
5
|
|
|
error_reporting(E_ALL ^ E_NOTICE); |
6
|
|
|
|
7
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\RelationHandler; |
8
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\Hooks; |
9
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\Index; |
10
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\CbFormLoader; |
11
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\CbIndexLoader; |
12
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\CbLayoutLoader; |
13
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\Deleter; |
14
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\ExportData; |
15
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\ImportData; |
16
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\IndexAjax; |
17
|
|
|
use Illuminate\Support\Facades\Request; |
18
|
|
|
use Illuminate\Support\Facades\DB; |
19
|
|
|
use Illuminate\Support\Facades\PDF; |
|
|
|
|
20
|
|
|
use CRUDBooster; |
|
|
|
|
21
|
|
|
use CB; |
22
|
|
|
use Schema; |
23
|
|
|
|
24
|
|
|
class CBController extends Controller |
25
|
|
|
{ |
26
|
|
|
use Hooks; |
27
|
|
|
use Deleter, CbFormLoader, CbIndexLoader, CbLayoutLoader, IndexAjax, ImportData, ExportData; |
28
|
|
|
|
29
|
|
|
public $data_inputan; |
30
|
|
|
|
31
|
|
|
public $module_name; |
32
|
|
|
|
33
|
|
|
public $table; |
34
|
|
|
|
35
|
|
|
public $title_field; |
36
|
|
|
|
37
|
|
|
public $primary_key = 'id'; |
38
|
|
|
|
39
|
|
|
public $arr = []; |
40
|
|
|
|
41
|
|
|
public $col = []; |
42
|
|
|
|
43
|
|
|
public $form = []; |
44
|
|
|
|
45
|
|
|
public $data = []; |
46
|
|
|
|
47
|
|
|
public $addaction = []; |
48
|
|
|
|
49
|
|
|
//public $global_privilege = false; |
50
|
|
|
|
51
|
|
|
public $button_delete = true; |
52
|
|
|
|
53
|
|
|
public $button_action_style = 'button_icon'; |
54
|
|
|
|
55
|
|
|
public $return_url = null; |
56
|
|
|
|
57
|
|
|
public $parent_field = null; |
58
|
|
|
|
59
|
|
|
public $parent_id = null; |
60
|
|
|
|
61
|
|
|
public $hide_form = []; |
62
|
|
|
|
63
|
|
|
public $index_return = false; //for export |
64
|
|
|
|
65
|
|
|
public function cbView($template, $data) |
66
|
|
|
{ |
67
|
|
|
$this->cbLayoutLoader(); |
68
|
|
|
view()->share($this->data); |
|
|
|
|
69
|
|
|
return view($template, $data); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function cbLoader() |
73
|
|
|
{ |
74
|
|
|
$this->cbInit(); |
75
|
|
|
|
76
|
|
|
$this->cbLayoutLoader(); |
77
|
|
|
$this->cbFormLoader(); |
78
|
|
|
$this->cbIndexLoader(); |
79
|
|
|
$this->checkHideForm(); |
80
|
|
|
$this->genericLoader(); |
81
|
|
|
|
82
|
|
|
view()->share($this->data); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function checkHideForm() |
86
|
|
|
{ |
87
|
|
|
if (! count($this->hide_form)) { |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
foreach ($this->form as $i => $f) { |
91
|
|
|
if (in_array($f['name'], $this->hide_form)) { |
92
|
|
|
unset($this->form[$i]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getIndex() |
98
|
|
|
{ |
99
|
|
|
$index = app(Index::class); |
|
|
|
|
100
|
|
|
$this->cbIndexLoader(); |
101
|
|
|
$this->genericLoader(); |
102
|
|
|
$data = $index->index($this); |
103
|
|
|
|
104
|
|
|
return $this->cbView('crudbooster::default.index', $data); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getUpdateSingle() |
108
|
|
|
{ |
109
|
|
|
$table = request('table'); |
|
|
|
|
110
|
|
|
$column = request('column'); |
111
|
|
|
$value = request('value'); |
112
|
|
|
$id = request('id'); |
113
|
|
|
DB::table($table)->where(CB::pk($table), $id)->update([$column => $value]); |
114
|
|
|
|
115
|
|
|
backWithMsg(cbTrans('alert_update_data_success')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getAdd() |
119
|
|
|
{ |
120
|
|
|
$page_title = cbTrans('add_data_page_title', ['module' => CB::getCurrentModule()->name]); |
121
|
|
|
$command = 'add'; |
122
|
|
|
return $this->cbForm(compact('page_title', 'command')); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function postAddSave() |
126
|
|
|
{ |
127
|
|
|
$this->genericLoader(); |
128
|
|
|
|
129
|
|
|
app(FormValidator::class)->validate(null, $this->form, $this->table); |
|
|
|
|
130
|
|
|
$this->inputAssignment(); |
131
|
|
|
|
132
|
|
|
$this->setTimeStamps('created_at'); |
133
|
|
|
|
134
|
|
|
$this->hookBeforeAdd($this->arr); |
135
|
|
|
|
136
|
|
|
$this->arr[$this->primary_key] = $id = $this->table()->insertGetId($this->arr); |
137
|
|
|
app(RelationHandler::class)->save($this->table, $id, $this->data_inputan); |
138
|
|
|
|
139
|
|
|
$this->hookAfterAdd($this->arr[$this->primary_key]); |
140
|
|
|
|
141
|
|
|
$this->insertLog('log_add', $this->arr[$this->title_field]); |
142
|
|
|
|
143
|
|
|
$this->sendResponseForSave('alert_add_data_success'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function inputAssignment($id = null) |
147
|
|
|
{ |
148
|
|
|
$hide_form = (request('hide_form')) ? unserialize(request('hide_form')) : []; |
|
|
|
|
149
|
|
|
|
150
|
|
|
foreach ($this->form as $ro) { |
151
|
|
|
$name = $ro['name']; |
152
|
|
|
$type = $ro['type'] ?: 'text'; |
153
|
|
|
$inputdata = request($name); |
154
|
|
|
|
155
|
|
|
if (! $name || $ro['exception']) { |
156
|
|
|
continue; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (count($hide_form) && in_array($name, $hide_form)) { |
160
|
|
|
continue; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$hookPath = \CB::componentsPath($type).DIRECTORY_SEPARATOR.'hookInputAssignment.php'; |
164
|
|
|
if (file_exists($hookPath)) { |
165
|
|
|
require_once($hookPath); |
166
|
|
|
} |
167
|
|
|
unset($hookPath); |
168
|
|
|
|
169
|
|
|
if (Request::hasFile($name)) { |
170
|
|
|
continue; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ($inputdata == '' && CB::isColumnNULL($this->table, $name)) { |
174
|
|
|
continue; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->arr[$name] = ''; |
178
|
|
|
|
179
|
|
|
if ($inputdata != '') { |
180
|
|
|
$this->arr[$name] = $inputdata; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param null $tableName |
|
|
|
|
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
|
|
public function table($tableName = null) |
190
|
|
|
{ |
191
|
|
|
$table = $tableName ?: $this->table; |
192
|
|
|
return \DB::table($table); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function getEdit($id) |
196
|
|
|
{ |
197
|
|
|
$row = $this->findRow($id)->first(); |
198
|
|
|
|
199
|
|
|
$page_title = cbTrans("edit_data_page_title", ['module' => CB::getCurrentModule()->name, 'name' => $row->{$this->title_field}]); |
200
|
|
|
$command = 'edit'; |
201
|
|
|
session()->put('current_row_id', $id); |
|
|
|
|
202
|
|
|
|
203
|
|
|
return $this->cbForm(compact('id', 'row', 'page_title', 'command')); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param $id |
208
|
|
|
* @return mixed |
209
|
|
|
*/ |
210
|
|
|
public function findRow($id) |
211
|
|
|
{ |
212
|
|
|
return $this->table()->where($this->primary_key, $id); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function postEditSave($id) |
216
|
|
|
{ |
217
|
|
|
$this->genericLoader(); |
218
|
|
|
|
219
|
|
|
app(FormValidator::class)->validate($id, $this->form, $this->table); |
|
|
|
|
220
|
|
|
$this->inputAssignment($id); |
221
|
|
|
|
222
|
|
|
$this->setTimeStamps('updated_at'); |
223
|
|
|
|
224
|
|
|
$this->hookBeforeEdit($this->arr, $id); |
225
|
|
|
$this->findRow($id)->update($this->arr); |
226
|
|
|
app(RelationHandler::class)->save($this->table, $id, $this->data_inputan); |
227
|
|
|
|
228
|
|
|
$this->hookAfterEdit($id); |
229
|
|
|
|
230
|
|
|
$this->insertLog('log_update', $this->arr[$this->title_field]); |
231
|
|
|
|
232
|
|
|
$this->sendResponseForSave('alert_update_data_success'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function getDetail($id) |
236
|
|
|
{ |
237
|
|
|
$row = $this->findRow($id)->first(); |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
$page_title = cbTrans('detail_data_page_title', ['module' => CB::getCurrentModule()->name, 'name' => $row->{$this->title_field}]); |
241
|
|
|
$command = 'detail'; |
242
|
|
|
|
243
|
|
|
session()->put('current_row_id', $id); |
|
|
|
|
244
|
|
|
|
245
|
|
|
return $this->cbForm(compact('row', 'page_title', 'command', 'id')); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function actionButtonSelected($id_selected, $button_name) |
249
|
|
|
{ |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
private function sendResponseForSave($msg) |
253
|
|
|
{ |
254
|
|
|
$this->return_url = $this->return_url ?: request('return_url'); |
|
|
|
|
255
|
|
|
if ($this->return_url) { |
256
|
|
|
if (request('submit') == cbTrans('button_save_more')) { |
257
|
|
|
CB::redirect(Request::server('HTTP_REFERER'), cbTrans($msg), 'success'); |
258
|
|
|
} |
259
|
|
|
CB::redirect($this->return_url, cbTrans($msg), 'success'); |
260
|
|
|
} |
261
|
|
|
if (request('submit') == cbTrans('button_save_more')) { |
262
|
|
|
CB::redirect(CB::mainpath('add'), cbTrans($msg), 'success'); |
263
|
|
|
} |
264
|
|
|
CB::redirect(CB::mainpath(), cbTrans($msg), 'success'); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
private function insertLog($msg, $name) |
268
|
|
|
{ |
269
|
|
|
CB::insertLog(trans('logging.'.$msg, ['module' => CB::getCurrentModule()->name, 'name' => $name])); |
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
private function setTimeStamps($col) |
273
|
|
|
{ |
274
|
|
|
if (Schema::hasColumn($this->table, $col)) { |
275
|
|
|
$this->arr[$col] = date('Y-m-d H:i:s'); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param $data |
281
|
|
|
* @return mixed |
282
|
|
|
*/ |
283
|
|
|
private function cbForm($data) |
284
|
|
|
{ |
285
|
|
|
$this->genericLoader(); |
286
|
|
|
$this->cbFormLoader(); |
287
|
|
|
return $this->cbView('crudbooster::default.form', $data); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
private function genericLoader() |
291
|
|
|
{ |
292
|
|
|
$this->primary_key = CB::pk($this->table); |
293
|
|
|
$this->columns_table = $this->col; |
294
|
|
|
$this->data_inputan = $this->form; |
295
|
|
|
$this->data['pk'] = $this->primary_key; |
296
|
|
|
$this->data['forms'] = $this->data_inputan; |
297
|
|
|
$this->data['hide_form'] = $this->hide_form; |
298
|
|
|
$this->data['table'] = $this->table; |
299
|
|
|
$this->data['title_field'] = $this->title_field; |
300
|
|
|
$this->data['appname'] = cbGetsetting('appname'); |
301
|
|
|
$this->data['index_button'] = $this->index_button; |
302
|
|
|
$this->data['button_delete'] = $this->button_delete; |
303
|
|
|
$this->data['sub_module'] = $this->sub_module; |
304
|
|
|
$this->data['parent_field'] = (request('parent_field')) ?: $this->parent_field; |
|
|
|
|
305
|
|
|
$this->data['parent_id'] = (request('parent_id')) ?: $this->parent_id; |
306
|
|
|
|
307
|
|
|
if (CB::getCurrentMethod() == 'getProfile') { |
308
|
|
|
session()->put('current_row_id', CB::myId()); |
|
|
|
|
309
|
|
|
$this->data['return_url'] = Request::fullUrl(); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths