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