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
|
|
|
//Clear data from Primary Key |
170
|
|
|
unset($data[ cb()->pk($this->data['table']) ]); |
171
|
|
|
|
172
|
|
|
if(Schema::hasColumn($this->data['table'], 'created_at')) { |
173
|
|
|
$data['created_at'] = date('Y-m-d H:i:s'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$id = DB::table($this->data['table'])->insertGetId($data); |
177
|
|
|
|
178
|
|
|
if(isset($this->data['hook_after_insert']) && is_callable($this->data['hook_after_insert'])) { |
179
|
|
|
call_user_func($this->data['hook_after_insert'], $id); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} catch (CBValidationException $e) { |
183
|
|
|
Log::debug($e); |
184
|
|
|
return cb()->redirectBack($e->getMessage(),'info'); |
185
|
|
|
} catch (\Exception $e) { |
186
|
|
|
Log::error($e); |
187
|
|
|
return cb()->redirectBack($e->getMessage(),'warning'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (request('submit') == trans('crudbooster.button_save_more')) { |
191
|
|
|
return cb()->redirect(module()->addURL(), trans("crudbooster.alert_add_data_success"), 'success'); |
192
|
|
|
} else { |
193
|
|
|
return cb()->redirect(module()->url(), trans("crudbooster.alert_add_data_success"), 'success'); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getEdit($id) |
198
|
|
|
{ |
199
|
|
|
if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),"You do not have a privilege access to this area"); |
200
|
|
|
|
201
|
|
|
$data = []; |
202
|
|
|
$data['row'] = $this->repository()->where($this->data['table'].'.'.getPrimaryKey($this->data['table']), $id)->first(); |
203
|
|
|
$data['page_title'] = $this->data['page_title'].' : Edit'; |
204
|
|
|
$data['action_url'] = module()->editSaveURL($id); |
205
|
|
|
return view('crudbooster::module.form.form', array_merge($data, $this->data)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function postEditSave($id) |
209
|
|
|
{ |
210
|
|
|
if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),"You do not have a privilege access to this area"); |
211
|
|
|
|
212
|
|
|
try { |
213
|
|
|
$this->validation(); |
214
|
|
|
columnSingleton()->valueAssignment(); |
215
|
|
|
$data = columnSingleton()->getAssignmentData(); |
216
|
|
|
|
217
|
|
|
//Clear data from Primary Key |
218
|
|
|
unset($data[ cb()->pk($this->data['table']) ]); |
219
|
|
|
|
220
|
|
|
if(Schema::hasColumn($this->data['table'], 'updated_at')) { |
221
|
|
|
$data['updated_at'] = date('Y-m-d H:i:s'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if(isset($this->data['hook_before_update']) && is_callable($this->data['hook_before_update'])) { |
225
|
|
|
call_user_func($this->data['hook_before_update'], $id); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
DB::table($this->data['table']) |
229
|
|
|
->where(cb()->pk($this->data['table']), $id) |
230
|
|
|
->update($data); |
231
|
|
|
|
232
|
|
|
if(isset($this->data['hook_after_update']) && is_callable($this->data['hook_after_update'])) { |
233
|
|
|
call_user_func($this->data['hook_after_update'], $id); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
} catch (CBValidationException $e) { |
237
|
|
|
Log::debug($e); |
238
|
|
|
return cb()->redirectBack($e->getMessage(),'info'); |
239
|
|
|
} catch (\Exception $e) { |
240
|
|
|
Log::error($e); |
241
|
|
|
return cb()->redirectBack($e->getMessage(),'warning'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
if (request('submit') == trans('crudbooster.button_save_more')) { |
246
|
|
|
return cb()->redirectBack(trans("crudbooster.alert_update_data_success"), 'success'); |
247
|
|
|
} else { |
248
|
|
|
return cb()->redirect(module()->url(), trans("crudbooster.alert_update_data_success"), 'success'); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function getDelete($id) |
253
|
|
|
{ |
254
|
|
|
if(!module()->canDelete()) return cb()->redirect(cb()->getAdminUrl(),"You do not have a privilege access to this area"); |
255
|
|
|
|
256
|
|
|
if(isset($this->data['hook_before_delete']) && is_callable($this->data['hook_before_delete'])) { |
257
|
|
|
call_user_func($this->data['hook_before_delete'], $id); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$softDelete = isset($this->data['disable_soft_delete'])?$this->data['disable_soft_delete']:true; |
261
|
|
|
|
262
|
|
|
if ($softDelete === true && Schema::hasColumn($this->data['table'],'deleted_at')) { |
263
|
|
|
DB::table($this->data['table']) |
264
|
|
|
->where(getPrimaryKey($this->data['table']), $id) |
265
|
|
|
->update(['deleted_at' => date('Y-m-d H:i:s')]); |
266
|
|
|
} else { |
267
|
|
|
DB::table($this->data['table']) |
268
|
|
|
->where(getPrimaryKey($this->data['table']), $id) |
269
|
|
|
->delete(); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if(isset($this->data['hook_after_delete']) && is_callable($this->data['hook_after_delete'])) { |
273
|
|
|
call_user_func($this->data['hook_after_delete'], $id); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return cb()->redirectBack(trans("crudbooster.alert_delete_data_success"), 'success'); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function getDetail($id) |
280
|
|
|
{ |
281
|
|
|
if(!module()->canRead()) return cb()->redirect(cb()->getAdminUrl(),"You do not have a privilege access to this area"); |
282
|
|
|
|
283
|
|
|
$data = []; |
284
|
|
|
$data['row'] = $this->repository()->where($this->data['table'].'.'.getPrimaryKey($this->data['table']), $id)->first(); |
285
|
|
|
$data['page_title'] = $this->data['page_title'].' : Detail'; |
286
|
|
|
return view('crudbooster::module.form.form_detail', array_merge($data, $this->data)); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
} |
290
|
|
|
|