|
1
|
|
|
<?php namespace crocodicstudio\crudbooster\controllers; |
|
2
|
|
|
|
|
3
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\Join; |
|
4
|
|
|
use crocodicstudio\crudbooster\controllers\traits\ColumnIntervention; |
|
5
|
|
|
use crocodicstudio\crudbooster\controllers\traits\Query; |
|
6
|
|
|
use crocodicstudio\crudbooster\controllers\traits\Validation; |
|
7
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\ColumnsRegister; |
|
8
|
|
|
use crocodicstudio\crudbooster\controllers\traits\ControllerSetting; |
|
9
|
|
|
use crocodicstudio\crudbooster\exceptions\CBValidationException; |
|
10
|
|
|
use crocodicstudio\crudbooster\models\ColumnModel; |
|
11
|
|
|
use Illuminate\Database\Query\Builder; |
|
12
|
|
|
use Illuminate\Support\Facades\Cache; |
|
13
|
|
|
use Illuminate\Support\Facades\DB; |
|
14
|
|
|
use Illuminate\Support\Facades\Log; |
|
15
|
|
|
use Illuminate\Support\Facades\Validator; |
|
16
|
|
|
use Illuminate\Support\Facades\Schema; |
|
17
|
|
|
use Illuminate\Support\Str; |
|
18
|
|
|
|
|
19
|
|
|
class CBController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
use ColumnsRegister, Join, ControllerSetting, Validation, Query, SubModuleController, ColumnIntervention; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
private $assignmentData; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
columnSingleton()->newColumns(); |
|
28
|
|
|
$this->defaultData(); |
|
29
|
|
|
$this->cbInit(); |
|
30
|
|
|
$this->columnIntervention(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getIndex() |
|
34
|
|
|
{ |
|
35
|
|
|
if(!module()->canBrowse()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
36
|
|
|
|
|
37
|
|
|
$query = $this->repository(); |
|
38
|
|
|
$result = $query->paginate( request("limit")?:cbConfig("LIMIT_TABLE_DATA") ); |
|
39
|
|
|
$data['result'] = $result; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
return view("crudbooster::module.index.index", array_merge($data, $this->data)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getAdd() |
|
45
|
|
|
{ |
|
46
|
|
|
if(!module()->canCreate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
47
|
|
|
|
|
48
|
|
|
$data = []; |
|
49
|
|
|
$data['page_title'] = $this->data['page_title'].' : '.cbLang('add'); |
|
|
|
|
|
|
50
|
|
|
$data['action_url'] = module()->addSaveURL(); |
|
51
|
|
|
return view('crudbooster::module.form.form',array_merge($data, $this->data)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function postAddSave() |
|
55
|
|
|
{ |
|
56
|
|
|
if(!module()->canCreate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
|
|
$this->validation(); |
|
60
|
|
|
columnSingleton()->valueAssignment(); |
|
61
|
|
|
$data = columnSingleton()->getAssignmentData(); |
|
62
|
|
|
|
|
63
|
|
|
//Clear data from Primary Key |
|
64
|
|
|
unset($data[ cb()->pk($this->data['table']) ]); |
|
65
|
|
|
|
|
66
|
|
|
if(Schema::hasColumn($this->data['table'], 'created_at')) { |
|
67
|
|
|
$data['created_at'] = date('Y-m-d H:i:s'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if(Schema::hasColumn($this->data['table'], 'updated_at')) { |
|
71
|
|
|
$data['updated_at'] = date('Y-m-d H:i:s'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if(isset($this->data['hook_before_insert']) && is_callable($this->data['hook_before_insert'])) { |
|
75
|
|
|
$data = call_user_func($this->data['hook_before_insert'], $data); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$id = DB::table($this->data['table'])->insertGetId($data); |
|
79
|
|
|
|
|
80
|
|
|
if(isset($this->data['hook_after_insert']) && is_callable($this->data['hook_after_insert'])) { |
|
81
|
|
|
call_user_func($this->data['hook_after_insert'], $id); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} catch (CBValidationException $e) { |
|
85
|
|
|
Log::debug($e); |
|
86
|
|
|
return cb()->redirectBack($e->getMessage(),'info'); |
|
87
|
|
|
} catch (\Exception $e) { |
|
88
|
|
|
Log::error($e); |
|
89
|
|
|
return cb()->redirectBack(cbLang("something_went_wrong"),'warning'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (Str::contains(request("submit"),cbLang("more"))) { |
|
93
|
|
|
return cb()->redirectBack(cbLang("the_data_has_been_added"), 'success'); |
|
94
|
|
|
} else { |
|
95
|
|
|
if(verifyReferalUrl()) { |
|
96
|
|
|
return cb()->redirect(getReferalUrl("url"), cbLang("the_data_has_been_added"), 'success'); |
|
97
|
|
|
} else { |
|
98
|
|
|
return cb()->redirect(module()->url(), cbLang("the_data_has_been_added"), 'success'); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getEdit($id) |
|
104
|
|
|
{ |
|
105
|
|
|
if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
106
|
|
|
|
|
107
|
|
|
$data = []; |
|
108
|
|
|
$data['row'] = $this->repository()->where($this->data['table'].'.'.getPrimaryKey($this->data['table']), $id)->first(); |
|
109
|
|
|
$data['page_title'] = $this->data['page_title'].' : '.cbLang('edit'); |
|
110
|
|
|
$data['action_url'] = module()->editSaveURL($id); |
|
111
|
|
|
return view('crudbooster::module.form.form', array_merge($data, $this->data)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function postEditSave($id) |
|
115
|
|
|
{ |
|
116
|
|
|
if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
117
|
|
|
|
|
118
|
|
|
try { |
|
119
|
|
|
$this->validation(); |
|
120
|
|
|
columnSingleton()->valueAssignment(); |
|
121
|
|
|
$data = columnSingleton()->getAssignmentData(); |
|
122
|
|
|
|
|
123
|
|
|
// Make sure the Primary Key is not included |
|
124
|
|
|
unset($data[ cb()->pk($this->data['table']) ]); |
|
125
|
|
|
|
|
126
|
|
|
if(Schema::hasColumn($this->data['table'], 'updated_at')) { |
|
127
|
|
|
$data['updated_at'] = date('Y-m-d H:i:s'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
unset($data['created_at']); |
|
131
|
|
|
|
|
132
|
|
|
if(isset($this->data['hook_before_update']) && is_callable($this->data['hook_before_update'])) { |
|
133
|
|
|
$data = call_user_func($this->data['hook_before_update'], $id, $data); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
cb()->update($this->data['table'], $id, $data); |
|
137
|
|
|
|
|
138
|
|
|
if(isset($this->data['hook_after_update']) && is_callable($this->data['hook_after_update'])) { |
|
139
|
|
|
call_user_func($this->data['hook_after_update'], $id); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
} catch (CBValidationException $e) { |
|
143
|
|
|
Log::debug($e); |
|
144
|
|
|
return cb()->redirectBack($e->getMessage(),'info'); |
|
145
|
|
|
} catch (\Exception $e) { |
|
146
|
|
|
Log::error($e); |
|
147
|
|
|
return cb()->redirectBack(cbLang("something_went_wrong"),'warning'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
if (Str::contains(request("submit"), cbLang("more"))) { |
|
152
|
|
|
return cb()->redirectBack( cbLang("the_data_has_been_updated"), 'success'); |
|
153
|
|
|
} else { |
|
154
|
|
|
if(verifyReferalUrl()) { |
|
155
|
|
|
return cb()->redirect(getReferalUrl("url"), cbLang("the_data_has_been_updated"), 'success'); |
|
156
|
|
|
} else { |
|
157
|
|
|
return cb()->redirect(module()->url(), cbLang("the_data_has_been_updated"), 'success'); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function getDelete($id) |
|
164
|
|
|
{ |
|
165
|
|
|
if(!module()->canDelete()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
166
|
|
|
|
|
167
|
|
|
if(isset($this->data['hook_before_delete']) && is_callable($this->data['hook_before_delete'])) { |
|
168
|
|
|
call_user_func($this->data['hook_before_delete'], $id); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$softDelete = isset($this->data['disable_soft_delete'])?$this->data['disable_soft_delete']:true; |
|
172
|
|
|
|
|
173
|
|
|
if ($softDelete === true && Schema::hasColumn($this->data['table'],'deleted_at')) { |
|
174
|
|
|
DB::table($this->data['table']) |
|
175
|
|
|
->where(getPrimaryKey($this->data['table']), $id) |
|
176
|
|
|
->update(['deleted_at' => date('Y-m-d H:i:s')]); |
|
177
|
|
|
} else { |
|
178
|
|
|
DB::table($this->data['table']) |
|
179
|
|
|
->where(getPrimaryKey($this->data['table']), $id) |
|
180
|
|
|
->delete(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if(isset($this->data['hook_after_delete']) && is_callable($this->data['hook_after_delete'])) { |
|
184
|
|
|
call_user_func($this->data['hook_after_delete'], $id); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return cb()->redirectBack( cbLang("the_data_has_been_deleted"), 'success'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function getDetail($id) |
|
191
|
|
|
{ |
|
192
|
|
|
if(!module()->canRead()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
|
193
|
|
|
|
|
194
|
|
|
$data = []; |
|
195
|
|
|
$data['row'] = $this->repository()->where($this->data['table'].'.'.cb()->findPrimaryKey($this->data['table']), $id)->first(); |
|
196
|
|
|
$data['page_title'] = $this->data['page_title'].' : '.cbLang('detail'); |
|
197
|
|
|
return view('crudbooster::module.form.form_detail', array_merge($data, $this->data)); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param string $method |
|
204
|
|
|
* @param array $parameters |
|
205
|
|
|
* @return mixed|null |
|
206
|
|
|
* |
|
207
|
|
|
* This method is to get the data columns and retrieve the value from this class |
|
208
|
|
|
*/ |
|
209
|
|
|
public function __call($method, $parameters) |
|
210
|
|
|
{ |
|
211
|
|
|
if($method == "getData") { |
|
212
|
|
|
$key = $parameters[0]; |
|
213
|
|
|
if(isset($this->data[$key])) { |
|
214
|
|
|
return $this->data[$key]; |
|
215
|
|
|
}else{ |
|
216
|
|
|
return null; |
|
217
|
|
|
} |
|
218
|
|
|
}else{ |
|
219
|
|
|
return null; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|