|
1
|
|
|
<?php namespace crocodicstudio\crudbooster\controllers; |
|
2
|
|
|
|
|
3
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\Join; |
|
4
|
|
|
use crocodicstudio\crudbooster\exceptions\CBValidationException; |
|
5
|
|
|
use crocodicstudio\crudbooster\models\ColumnModel; |
|
6
|
|
|
use Illuminate\Database\Query\Builder; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
use Illuminate\Support\Facades\Log; |
|
9
|
|
|
use Illuminate\Support\Facades\Validator; |
|
10
|
|
|
use Illuminate\Support\Facades\Schema; |
|
11
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\ColumnsRegister; |
|
12
|
|
|
use crocodicstudio\crudbooster\controllers\traits\ControllerSetting; |
|
13
|
|
|
use Illuminate\Support\Str; |
|
14
|
|
|
|
|
15
|
|
|
class CBController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
use ColumnsRegister, Join, ControllerSetting; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
private $assignmentData; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
columnSingleton()->newColumns(); |
|
24
|
|
|
$this->defaultData(); |
|
25
|
|
|
$this->cbInit(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function __call($method, $parameters) |
|
29
|
|
|
{ |
|
30
|
|
|
if($method == "getData") { |
|
31
|
|
|
$key = $parameters[0]; |
|
32
|
|
|
if(isset($this->data[$key])) { |
|
33
|
|
|
return $this->data[$key]; |
|
34
|
|
|
}else{ |
|
35
|
|
|
return null; |
|
36
|
|
|
} |
|
37
|
|
|
}else{ |
|
38
|
|
|
return null; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function repository($callback = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$joins = columnSingleton()->getJoin(); |
|
45
|
|
|
$columns = columnSingleton()->getColumns(); |
|
46
|
|
|
|
|
47
|
|
|
$query = DB::table($this->data['table']); |
|
48
|
|
|
|
|
49
|
|
|
$query->addSelect($this->data['table'].'.'.cb()->pk($this->data['table']).' as primary_key'); |
|
50
|
|
|
|
|
51
|
|
|
$softDelete = isset($this->data['disable_soft_delete'])?$this->data['disable_soft_delete']:true; |
|
52
|
|
|
if($softDelete === true && Schema::hasColumn($this->data['table'],'deleted_at')) { |
|
53
|
|
|
$query->whereNull($this->data['table'].'.deleted_at'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if(isset($joins)) { |
|
57
|
|
|
foreach($joins as $join) |
|
58
|
|
|
{ |
|
59
|
|
|
$query->join($join['target_table'], |
|
60
|
|
|
$join['target_table_primary'], |
|
61
|
|
|
$join['operator'], |
|
62
|
|
|
$join['source_table_foreign'], |
|
63
|
|
|
$join['type']); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
foreach($columns as $column) { |
|
68
|
|
|
/** @var ColumnModel $column */ |
|
69
|
|
|
if($column->getType() != "custom") { |
|
70
|
|
|
if(strpos($column->getField(),".") === false) { |
|
71
|
|
|
if(Schema::hasColumn($this->data['table'], $column->getField())) { |
|
72
|
|
|
$query->addSelect($this->data['table'].'.'.$column->getField()); |
|
73
|
|
|
} |
|
74
|
|
|
}else{ |
|
75
|
|
|
$query->addSelect($column->getField()); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$query = getTypeHook($column->getType())->query($query, $column); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if(request()->has('q')) |
|
83
|
|
|
{ |
|
84
|
|
|
if(isset($this->data['hook_search_query'])) { |
|
85
|
|
|
$query = call_user_func($this->data['hook_search_query'], $query); |
|
86
|
|
|
}else{ |
|
87
|
|
|
$query->where(function ($where) use ($columns) { |
|
88
|
|
|
/** |
|
89
|
|
|
* @var $where Builder |
|
90
|
|
|
*/ |
|
91
|
|
|
foreach($columns as $column) |
|
92
|
|
|
{ |
|
93
|
|
|
if(strpos($column->getField(),".") === false) { |
|
94
|
|
|
$field = $this->data['table'].'.'.$column->getField(); |
|
95
|
|
|
}else{ |
|
96
|
|
|
$field = $column->getField(); |
|
97
|
|
|
} |
|
98
|
|
|
$where->orWhere($field, 'like', '%'.request('q').'%'); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
}); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
// Callback From this Method |
|
106
|
|
|
if(isset($callback) && is_callable($callback)) { |
|
107
|
|
|
$query = call_user_func($callback, $query); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if(isset($this->data['hook_index_query']) && is_callable($this->data['hook_index_query'])) { |
|
111
|
|
|
$query = call_user_func($this->data['hook_index_query'], $query); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
if(request()->has(['order_by','order_sort'])) |
|
116
|
|
|
{ |
|
117
|
|
|
if(in_array(request('order_by'),columnSingleton()->getColumnNameOnly())) { |
|
118
|
|
|
$query->orderBy(request('order_by'), request('order_sort')); |
|
119
|
|
|
} |
|
120
|
|
|
}else{ |
|
121
|
|
|
$query->orderBy($this->data['table'].'.'.cb()->findPrimaryKey($this->data['table']), "desc"); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $query; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function getIndex() |
|
128
|
|
|
{ |
|
129
|
|
|
if(!module()->canBrowse()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
130
|
|
|
|
|
131
|
|
|
$query = $this->repository(); |
|
132
|
|
|
$result = $query->paginate( request("limit")?:cbConfig("LIMIT_TABLE_DATA") ); |
|
133
|
|
|
$data['result'] = $result; |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
return view("crudbooster::module.index.index", array_merge($data, $this->data)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function getFilterBy($field, $value, $parentPath) { |
|
139
|
|
|
if(!module()->canBrowse()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
140
|
|
|
|
|
141
|
|
|
if(!verifyReferalUrl()) return cb()->redirect(cb()->getAdminUrl($parentPath),"The url you are trying visit is incorrect"); |
|
142
|
|
|
|
|
143
|
|
|
$query = $this->repository(); |
|
144
|
|
|
|
|
145
|
|
|
$query->where($field, $value); |
|
146
|
|
|
|
|
147
|
|
|
$result = $query->paginate( request("limit")?:cbConfig("LIMIT_TABLE_DATA") ); |
|
148
|
|
|
$data['result'] = $result; |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
$additionalView = getReferalUrl("additional"); |
|
151
|
|
|
if($additionalView) { |
|
152
|
|
|
$data['additionalView'] = $additionalView; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return view("crudbooster::module.index.index", array_merge($data, $this->data)); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @throws CBValidationException |
|
161
|
|
|
*/ |
|
162
|
|
|
private function validation() |
|
163
|
|
|
{ |
|
164
|
|
|
if(isset($this->data['validation'])) { |
|
165
|
|
|
$validator = Validator::make(request()->all(), @$this->data['validation'], @$this->data['validation_messages']); |
|
166
|
|
|
if ($validator->fails()) { |
|
167
|
|
|
$message = $validator->messages(); |
|
168
|
|
|
$message_all = $message->all(); |
|
169
|
|
|
throw new CBValidationException(implode(', ',$message_all)); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function getAdd() |
|
175
|
|
|
{ |
|
176
|
|
|
if(!module()->canCreate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
177
|
|
|
|
|
178
|
|
|
$data = []; |
|
179
|
|
|
$data['page_title'] = $this->data['page_title'].' : '.cbLang('add'); |
|
|
|
|
|
|
180
|
|
|
$data['action_url'] = module()->addSaveURL(); |
|
181
|
|
|
return view('crudbooster::module.form.form',array_merge($data, $this->data)); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function postAddSave() |
|
185
|
|
|
{ |
|
186
|
|
|
if(!module()->canCreate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
187
|
|
|
|
|
188
|
|
|
try { |
|
189
|
|
|
$this->validation(); |
|
190
|
|
|
columnSingleton()->valueAssignment(); |
|
191
|
|
|
$data = columnSingleton()->getAssignmentData(); |
|
192
|
|
|
|
|
193
|
|
|
//Clear data from Primary Key |
|
194
|
|
|
unset($data[ cb()->pk($this->data['table']) ]); |
|
195
|
|
|
|
|
196
|
|
|
if(Schema::hasColumn($this->data['table'], 'created_at')) { |
|
197
|
|
|
$data['created_at'] = date('Y-m-d H:i:s'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if(Schema::hasColumn($this->data['table'], 'updated_at')) { |
|
201
|
|
|
$data['updated_at'] = date('Y-m-d H:i:s'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
if(isset($this->data['hook_before_insert']) && is_callable($this->data['hook_before_insert'])) { |
|
205
|
|
|
$data = call_user_func($this->data['hook_before_insert'], $data); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$id = DB::table($this->data['table'])->insertGetId($data); |
|
209
|
|
|
|
|
210
|
|
|
if(isset($this->data['hook_after_insert']) && is_callable($this->data['hook_after_insert'])) { |
|
211
|
|
|
call_user_func($this->data['hook_after_insert'], $id); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
} catch (CBValidationException $e) { |
|
215
|
|
|
Log::debug($e); |
|
216
|
|
|
return cb()->redirectBack($e->getMessage(),'info'); |
|
217
|
|
|
} catch (\Exception $e) { |
|
218
|
|
|
Log::error($e); |
|
219
|
|
|
return cb()->redirectBack(cbLang("something_went_wrong"),'warning'); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
if (Str::contains(request("submit"),cbLang("more"))) { |
|
223
|
|
|
return cb()->redirect(module()->addURL(), cbLang("the_data_has_been_added"), 'success'); |
|
224
|
|
|
} else { |
|
225
|
|
|
if(verifyReferalUrl()) { |
|
226
|
|
|
return cb()->redirect(getReferalUrl("url"), cbLang("the_data_has_been_added"), 'success'); |
|
227
|
|
|
} else { |
|
228
|
|
|
return cb()->redirect(module()->url(), cbLang("the_data_has_been_added"), 'success'); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function getEdit($id) |
|
234
|
|
|
{ |
|
235
|
|
|
if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
236
|
|
|
|
|
237
|
|
|
$data = []; |
|
238
|
|
|
$data['row'] = $this->repository()->where($this->data['table'].'.'.getPrimaryKey($this->data['table']), $id)->first(); |
|
239
|
|
|
$data['page_title'] = $this->data['page_title'].' : '.cbLang('edit'); |
|
240
|
|
|
$data['action_url'] = module()->editSaveURL($id); |
|
241
|
|
|
return view('crudbooster::module.form.form', array_merge($data, $this->data)); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
public function postEditSave($id) |
|
245
|
|
|
{ |
|
246
|
|
|
if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
247
|
|
|
|
|
248
|
|
|
try { |
|
249
|
|
|
$this->validation(); |
|
250
|
|
|
columnSingleton()->valueAssignment(); |
|
251
|
|
|
$data = columnSingleton()->getAssignmentData(); |
|
252
|
|
|
|
|
253
|
|
|
// Make sure the Primary Key is not included |
|
254
|
|
|
unset($data[ cb()->pk($this->data['table']) ]); |
|
255
|
|
|
|
|
256
|
|
|
if(Schema::hasColumn($this->data['table'], 'updated_at')) { |
|
257
|
|
|
$data['updated_at'] = date('Y-m-d H:i:s'); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
unset($data['created_at']); |
|
261
|
|
|
|
|
262
|
|
|
if(isset($this->data['hook_before_update']) && is_callable($this->data['hook_before_update'])) { |
|
263
|
|
|
$data = call_user_func($this->data['hook_before_update'], $id, $data); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
cb()->update($this->data['table'], $id, $data); |
|
267
|
|
|
|
|
268
|
|
|
if(isset($this->data['hook_after_update']) && is_callable($this->data['hook_after_update'])) { |
|
269
|
|
|
call_user_func($this->data['hook_after_update'], $id); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
} catch (CBValidationException $e) { |
|
273
|
|
|
Log::debug($e); |
|
274
|
|
|
return cb()->redirectBack($e->getMessage(),'info'); |
|
275
|
|
|
} catch (\Exception $e) { |
|
276
|
|
|
Log::error($e); |
|
277
|
|
|
return cb()->redirectBack(cbLang("something_went_wrong"),'warning'); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
if (Str::contains(request("submit"), cbLang("more"))) { |
|
282
|
|
|
return cb()->redirectBack( cbLang("the_data_has_been_updated"), 'success'); |
|
283
|
|
|
} else { |
|
284
|
|
|
if(verifyReferalUrl()) { |
|
285
|
|
|
return cb()->redirect(getReferalUrl("url"), cbLang("the_data_has_been_updated"), 'success'); |
|
286
|
|
|
} else { |
|
287
|
|
|
return cb()->redirect(module()->url(), cbLang("the_data_has_been_updated"), 'success'); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
public function getDelete($id) |
|
294
|
|
|
{ |
|
295
|
|
|
if(!module()->canDelete()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
296
|
|
|
|
|
297
|
|
|
if(isset($this->data['hook_before_delete']) && is_callable($this->data['hook_before_delete'])) { |
|
298
|
|
|
call_user_func($this->data['hook_before_delete'], $id); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
$softDelete = isset($this->data['disable_soft_delete'])?$this->data['disable_soft_delete']:true; |
|
302
|
|
|
|
|
303
|
|
|
if ($softDelete === true && Schema::hasColumn($this->data['table'],'deleted_at')) { |
|
304
|
|
|
DB::table($this->data['table']) |
|
305
|
|
|
->where(getPrimaryKey($this->data['table']), $id) |
|
306
|
|
|
->update(['deleted_at' => date('Y-m-d H:i:s')]); |
|
307
|
|
|
} else { |
|
308
|
|
|
DB::table($this->data['table']) |
|
309
|
|
|
->where(getPrimaryKey($this->data['table']), $id) |
|
310
|
|
|
->delete(); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
if(isset($this->data['hook_after_delete']) && is_callable($this->data['hook_after_delete'])) { |
|
314
|
|
|
call_user_func($this->data['hook_after_delete'], $id); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
return cb()->redirectBack( cbLang("the_data_has_been_deleted"), 'success'); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
public function getDetail($id) |
|
321
|
|
|
{ |
|
322
|
|
|
if(!module()->canRead()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
323
|
|
|
|
|
324
|
|
|
$data = []; |
|
325
|
|
|
$data['row'] = $this->repository()->where($this->data['table'].'.'.cb()->findPrimaryKey($this->data['table']), $id)->first(); |
|
326
|
|
|
$data['page_title'] = $this->data['page_title'].' : '.cbLang('detail'); |
|
327
|
|
|
return view('crudbooster::module.form.form_detail', array_merge($data, $this->data)); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
} |
|
331
|
|
|
|