1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator; |
4
|
|
|
|
5
|
|
|
use crocodicstudio\crudbooster\controllers\CBController; |
6
|
|
|
use crocodicstudio\crudbooster\controllers\FormValidator; |
7
|
|
|
use crocodicstudio\crudbooster\helpers\CRUDBooster; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
|
10
|
|
|
class AdminModulesController extends CBController |
11
|
|
|
{ |
12
|
|
|
public function cbInit() |
13
|
|
|
{ |
14
|
|
|
$this->table = 'cms_modules'; |
15
|
|
|
$this->primaryKey = 'id'; |
16
|
|
|
$this->titleField = 'name'; |
17
|
|
|
$this->limit = 100; |
18
|
|
|
$this->buttonAdd = false; |
19
|
|
|
$this->buttonExport = false; |
20
|
|
|
$this->buttonImport = false; |
21
|
|
|
$this->buttonFilter = false; |
22
|
|
|
$this->buttonDetail = false; |
23
|
|
|
$this->buttonBulkAction = false; |
24
|
|
|
$this->buttonActionStyle = 'button_icon'; |
25
|
|
|
$this->orderby = ['is_protected' => 'asc', 'name' => 'asc']; |
26
|
|
|
|
27
|
|
|
$this->makeColumns(); |
28
|
|
|
|
29
|
|
|
$this->form = Form::makeForm($this->table); |
30
|
|
|
|
31
|
|
|
$this->scriptJs = " |
32
|
|
|
$(function() { |
33
|
|
|
$('#table_name').change(function() { |
34
|
|
|
var v = $(this).val(); |
35
|
|
|
$('#path').val(v); |
36
|
|
|
}) |
37
|
|
|
}) "; |
38
|
|
|
|
39
|
|
|
$this->addAction[] = [ |
40
|
|
|
'label' => 'Module Wizard', |
41
|
|
|
'icon' => 'fa fa-wrench', |
42
|
|
|
'url' => CRUDBooster::mainpath('step1').'/[id]', |
43
|
|
|
"showIf" => "[is_protected] == 0", |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
$this->indexButton[] = ['label' => 'Generate New Module', 'icon' => 'fa fa-plus', 'url' => CRUDBooster::mainpath('step1'), 'color' => 'success']; |
47
|
|
|
} |
48
|
|
|
// public function getIndex() { |
49
|
|
|
// $data['page_title'] = 'Module Generator'; |
50
|
|
|
// $data['result'] = DB::table('cms_modules')->where('is_protected',0)->orderby('name','asc')->get(); |
51
|
|
|
// $this->cbView('CbModulesGen::index',$data); |
52
|
|
|
// } |
53
|
|
|
|
54
|
|
|
private function makeColumns() |
55
|
|
|
{ |
56
|
|
|
$this->col = [ |
57
|
|
|
['label' => 'name', 'name' => 'name'], |
58
|
|
|
['label' => "Table", 'name' => "table_name"], |
59
|
|
|
['label' => "Path", 'name' => "path"], |
60
|
|
|
['label' => "Controller", 'name' => "controller"], |
61
|
|
|
['label' => "Protected", 'name' => "is_protected", "visible" => false], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function hookBeforeDelete($ids) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
foreach ($ids as $id) { |
68
|
|
|
$controller = ModulesRepo::getControllerName($id); |
69
|
|
|
DB::table('cms_menus')->where('path', 'like', '%'.$controller.'%')->delete(); |
70
|
|
|
@unlink(controller_path($controller)); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $ids; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getTableColumns($table) |
77
|
|
|
{ |
78
|
|
|
$columns = \Schema::getColumnListing($table); |
79
|
|
|
|
80
|
|
|
return response()->json($columns); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getCheckSlug($slug) |
84
|
|
|
{ |
85
|
|
|
$check = ModulesRepo::countByPath($slug); |
86
|
|
|
$lastId = DB::table('cms_modules')->max('id') + 1; |
87
|
|
|
|
88
|
|
|
return response()->json(['total' => $check, 'lastid' => $lastId]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getAdd() |
92
|
|
|
{ |
93
|
|
|
$this->cbLoader(); |
94
|
|
|
|
95
|
|
|
return redirect()->route("ModulesControllerGetStep1"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getStep1($id = 0, Step1Handler $handler) |
99
|
|
|
{ |
100
|
|
|
$this->cbLoader(); |
101
|
|
|
|
102
|
|
|
return $handler->showForm($id); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getStep2($id, Step2Handler $handler) |
106
|
|
|
{ |
107
|
|
|
$this->cbLoader(); |
108
|
|
|
|
109
|
|
|
return $handler->showForm($id); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function postStep2(Step1Handler $handler) |
113
|
|
|
{ |
114
|
|
|
$this->cbLoader(); |
115
|
|
|
|
116
|
|
|
return $handler->handleFormSubmit(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function postStep3(Step2Handler $handler) |
120
|
|
|
{ |
121
|
|
|
$this->cbLoader(); |
122
|
|
|
|
123
|
|
|
return $handler->handleFormSubmit(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getStep3($id, Step3Handler $step3) |
127
|
|
|
{ |
128
|
|
|
$this->cbLoader(); |
129
|
|
|
|
130
|
|
|
return $step3->showForm($id); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getTypeInfo($type = 'text') |
134
|
|
|
{ |
135
|
|
|
header("Content-Type: application/json"); |
136
|
|
|
echo file_get_contents(CbComponentsPath($type).'/info.json'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function postStep4(Step3Handler $handler) |
140
|
|
|
{ |
141
|
|
|
$this->cbLoader(); |
142
|
|
|
|
143
|
|
|
return $handler->handleFormSubmit(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getStep4($id, Step4Handler $handler) |
147
|
|
|
{ |
148
|
|
|
$this->cbLoader(); |
149
|
|
|
|
150
|
|
|
return $handler->showForm($id); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function postStepFinish(Step4Handler $handler) |
154
|
|
|
{ |
155
|
|
|
$this->cbLoader(); |
156
|
|
|
|
157
|
|
|
return $handler->handleFormSubmit(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function postAddSave() |
161
|
|
|
{ |
162
|
|
|
$this->cbLoader(); |
163
|
|
|
app(FormValidator::class)->validate(null, $this->form, $this); |
164
|
|
|
$this->inputAssignment(); |
165
|
|
|
|
166
|
|
|
//Generate Controller |
167
|
|
|
$route_basename = basename(request('path')); |
168
|
|
|
if ($this->arr['controller'] == '') { |
169
|
|
|
$this->arr['controller'] = ControllerGenerator::generateController(request('table_name'), $route_basename); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->arr['created_at'] = YmdHis(); |
173
|
|
|
$this->table()->insert($this->arr); |
174
|
|
|
|
175
|
|
|
//Insert Menu |
176
|
|
|
if ($this->arr['controller']) { |
177
|
|
|
(new CreateMenuForNewModule())->execute($this->arr['controller'], $this->arr['name'], $this->arr['icon']); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$moduleId = $this->arr['id']; |
181
|
|
|
|
182
|
|
|
$this->grantFullAccess($moduleId); |
183
|
|
|
|
184
|
|
|
//Refresh Session Roles |
185
|
|
|
CRUDBooster::refreshSessionRoles(); |
186
|
|
|
|
187
|
|
|
//$ref_parameter = Request::input('ref_parameter'); |
188
|
|
|
if (request('return_url')) { |
189
|
|
|
CRUDBooster::redirect(request('return_url'), cbTrans("alert_add_data_success"), 'success'); |
190
|
|
|
} |
191
|
|
|
if (request('submit') == cbTrans('button_save_more')) { |
192
|
|
|
CRUDBooster::redirect(CRUDBooster::mainpath('add'), cbTrans('alert_add_data_success'), 'success'); |
193
|
|
|
} |
194
|
|
|
CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_add_data_success'), 'success'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function postEditSave($id) |
198
|
|
|
{ |
199
|
|
|
$this->cbLoader(); |
200
|
|
|
|
201
|
|
|
//$row = $this->table()->where($this->primaryKey, $id)->first(); |
202
|
|
|
|
203
|
|
|
app(FormValidator::class)->validate($id, $this->form, $this); |
204
|
|
|
|
205
|
|
|
$this->inputAssignment(); |
206
|
|
|
|
207
|
|
|
//Generate Controller |
208
|
|
|
$route_basename = basename(request('path')); |
209
|
|
|
if ($this->arr['controller'] == '') { |
210
|
|
|
$this->arr['controller'] = ControllerGenerator::generateController(request('table_name'), $route_basename); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$this->findRow($id)->update($this->arr); |
214
|
|
|
|
215
|
|
|
//Refresh Session Roles |
216
|
|
|
CRUDBooster::refreshSessionRoles(); |
217
|
|
|
|
218
|
|
|
backWithMsg(cbTrans('alert_update_data_success')); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param $module_id |
223
|
|
|
*/ |
224
|
|
|
private function grantFullAccess($module_id) |
225
|
|
|
{ |
226
|
|
|
DB::table('cms_privileges_roles')->insert([ |
227
|
|
|
'id_cms_modules' => $module_id, |
228
|
|
|
'id_cms_privileges' => auth('cbAdmin')->user()->id_cms_privileges, |
|
|
|
|
229
|
|
|
'can_see_module' => 1, |
230
|
|
|
'can_create' => 1, |
231
|
|
|
'can_read' => 1, |
232
|
|
|
'can_edit' => 1, |
233
|
|
|
'can_delete' => 1, |
234
|
|
|
]); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.