Passed
Push — master ( 6b1f98...aa64af )
by Iman
04:54
created

AdminModulesController::cbInit()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
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
use Illuminate\Support\Facades\Session;
10
11
class AdminModulesController extends CBController
12
{
13
    public function cbInit()
14
    {
15
        $this->table = 'cms_moduls';
16
        $this->primaryKey = 'id';
17
        $this->titleField = 'name';
18
        $this->limit = 100;
19
        $this->buttonAdd = false;
20
        $this->buttonExport = false;
21
        $this->buttonImport = false;
22
        $this->buttonFilter = false;
23
        $this->buttonDetail = false;
24
        $this->buttonBulkAction = false;
25
        $this->buttonActionStyle = 'button_icon';
26
        $this->orderby = ['is_protected' => 'asc', 'name' => 'asc'];
27
28
        $this->makeColumns();
29
30
        $this->form = Form::makeForm($this->table);
31
32
        $this->scriptJs = "
33
 			$(function() {
34
 				$('#table_name').change(function() {
35
					var v = $(this).val();
36
					$('#path').val(v);
37
				})	
38
 			}) ";
39
40
        $this->addAction[] = [
41
            'label' => 'Module Wizard',
42
            'icon' => 'fa fa-wrench',
43
            'url' => CRUDBooster::mainpath('step1').'/[id]',
44
            "showIf" => "[is_protected] == 0",
45
        ];
46
47
        $this->indexButton[] = ['label' => 'Generate New Module', 'icon' => 'fa fa-plus', 'url' => CRUDBooster::mainpath('step1'), 'color' => 'success'];
48
    }
49
    // public function getIndex() {
50
    // 	$data['page_title'] = 'Module Generator';
51
    // 	$data['result'] = DB::table('cms_moduls')->where('is_protected',0)->orderby('name','asc')->get();
52
    // 	$this->cbView('CbModulesGen::index',$data);
53
    // }	
54
55
    private function makeColumns()
56
    {
57
        $this->col = [
58
            ['label' => 'name', 'name' => 'name'],
59
            ['label' => "Table", 'name' => "table_name"],
60
            ['label' => "Path", 'name' => "path"],
61
            ['label' => "Controller", 'name' => "controller"],
62
            ['label' => "Protected", 'name' => "is_protected", "visible" => false],
63
        ];
64
    }
65
66
    function hookBeforeDelete($ids)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
    {
68
        foreach ($ids as $id) {
69
            $controller = ModulesRepo::getControllerName($id);
70
            DB::table('cms_menus')->where('path', 'like', '%'.$controller.'%')->delete();
71
            @unlink(controller_path($controller));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

71
            /** @scrutinizer ignore-unhandled */ @unlink(controller_path($controller));

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
72
        }
73
74
        return $ids;
75
    }
76
77
    public function getTableColumns($table)
78
    {
79
        $columns = \Schema::getColumnListing($table);
80
81
        return response()->json($columns);
0 ignored issues
show
Bug introduced by
The method json() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        return response()->/** @scrutinizer ignore-call */ json($columns);
Loading history...
82
    }
83
84
    public function getCheckSlug($slug)
85
    {
86
        $check = DB::table('cms_moduls')->where('path', $slug)->count();
87
        $lastId = DB::table('cms_moduls')->max('id') + 1;
88
89
        return response()->json(['total' => $check, 'lastid' => $lastId]);
90
    }
91
92
    public function getAdd()
93
    {
94
        $this->cbLoader();
95
96
        return redirect()->route("ModulesControllerGetStep1");
97
    }
98
99
    public function getStep1($id = 0, Step1Handler $handler)
100
    {
101
        $this->cbLoader();
102
103
        return $handler->showForm($id);
104
    }
105
106
    public function getStep2($id, Step2Handler $handler)
107
    {
108
        $this->cbLoader();
109
110
        return $handler->showForm($id);
111
    }
112
113
    public function postStep2(Step1Handler $handler)
114
    {
115
        $this->cbLoader();
116
117
        return $handler->handleFormSubmit();
118
    }
119
120
    public function postStep3(Step2Handler $handler)
121
    {
122
        $this->cbLoader();
123
124
        return $handler->handleFormSubmit();
125
    }
126
127
    public function getStep3($id, Step3Handler $step3)
128
    {
129
        $this->cbLoader();
130
131
        return $step3->showForm($id);
132
    }
133
134
    public function getTypeInfo($type = 'text')
135
    {
136
        header("Content-Type: application/json");
137
        echo file_get_contents(CbComponentsPath($type).'/info.json');
138
    }
139
140
    public function postStep4(Step3Handler $handler)
141
    {
142
        $this->cbLoader();
143
144
        return $handler->handleFormSubmit();
145
    }
146
147
    public function getStep4($id, Step4Handler $handler)
148
    {
149
        $this->cbLoader();
150
151
        return $handler->showForm($id);
152
    }
153
154
    public function postStepFinish(Step4Handler $handler)
155
    {
156
        $this->cbLoader();
157
158
        return $handler->handleFormSubmit();
159
    }
160
161
    public function postAddSave()
162
    {
163
        $this->cbLoader();
164
        app(FormValidator::class)->validate(null, $this->form, $this);
165
        $this->inputAssignment();
166
167
        //Generate Controller
168
        $route_basename = basename(request('path'));
169
        if ($this->arr['controller'] == '') {
170
            $this->arr['controller'] = ControllerGenerator::generateController(request('table_name'), $route_basename);
171
        }
172
173
        $this->arr['created_at'] = YmdHis();
174
        $this->arr['id'] = $this->table()->max('id') + 1;
175
        $this->table()->insert($this->arr);
176
177
        //Insert Menu
178
        if ($this->arr['controller']) {
179
            $this->createMenuForModule();
180
        }
181
182
        $id_modul = $this->arr['id'];
183
184
        $user_id_privileges = CRUDBooster::myPrivilegeId();
185
        DB::table('cms_privileges_roles')->insert([
186
            'id_cms_moduls' => $id_modul,
187
            'id_cms_privileges' => $user_id_privileges,
188
            'is_visible' => 1,
189
            'is_create' => 1,
190
            'is_read' => 1,
191
            'is_edit' => 1,
192
            'is_delete' => 1,
193
        ]);
194
195
        //Refresh Session Roles
196
        $roles = DB::table('cms_privileges_roles')->where('id_cms_privileges', CRUDBooster::myPrivilegeId())->join('cms_moduls', 'cms_moduls.id', '=', 'id_cms_moduls')->select('cms_moduls.name', 'cms_moduls.path', 'is_visible', 'is_create', 'is_read', 'is_edit', 'is_delete')->get();
197
        Session::put('admin_privileges_roles', $roles);
198
199
        //$ref_parameter = Request::input('ref_parameter');
200
        if (request('return_url')) {
201
            CRUDBooster::redirect(request('return_url'), cbTrans("alert_add_data_success"), 'success');
202
        }
203
        if (request('submit') == cbTrans('button_save_more')) {
204
            CRUDBooster::redirect(CRUDBooster::mainpath('add'), cbTrans('alert_add_data_success'), 'success');
205
        }
206
        CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_add_data_success'), 'success');
207
    }
208
209
    private function createMenuForModule()
210
    {
211
        $parent_menu_sort = DB::table('cms_menus')->where('parent_id', 0)->max('sorting') + 1;
212
        $parent_menu_id = DB::table('cms_menus')->insertGetId([
213
            'created_at' => YmdHis(),
214
            'name' => $this->arr['name'],
215
            'icon' => $this->arr['icon'],
216
            'path' => '#',
217
            'type' => 'URL External',
218
            'is_active' => 1,
219
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
220
            'sorting' => $parent_menu_sort,
221
            'parent_id' => 0,
222
        ]);
223
224
        $arr = [
225
            'created_at' => YmdHis(),
226
            'type' => 'Route',
227
            'is_active' => 1,
228
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
229
            'parent_id' => $parent_menu_id,
230
        ];
231
232
        DB::table('cms_menus')->insert([
233
                'name' => cbTrans('text_default_add_new_module', ['module' => $this->arr['name']]),
234
                'icon' => 'fa fa-plus',
235
                'path' => $this->arr['controller'].'GetAdd',
236
                'sorting' => 1,
237
            ] + $arr);
238
239
        DB::table('cms_menus')->insert([
240
                'name' => cbTrans('text_default_list_module', ['module' => $this->arr['name']]),
241
                'icon' => 'fa fa-bars',
242
                'path' => $this->arr['controller'].'GetIndex',
243
                'cms_privileges' => CRUDBooster::myPrivilegeId(),
244
                'sorting' => 2,
245
            ] + $arr);
246
    }
247
248
    public function postEditSave($id)
249
    {
250
        $this->cbLoader();
251
252
        $row = $this->table()->where($this->primaryKey, $id)->first();
253
254
        app(FormValidator::class)->validate($id, $this->form, $this);
255
256
        $this->inputAssignment();
257
258
        //Generate Controller
259
        $route_basename = basename(request('path'));
260
        if ($this->arr['controller'] == '') {
261
            $this->arr['controller'] = ControllerGenerator::generateController(request('table_name'), $route_basename);
262
        }
263
264
        $this->findRow($id)->update($this->arr);
265
266
        //Refresh Session Roles
267
        $roles = DB::table('cms_privileges_roles')->where('id_cms_privileges', CRUDBooster::myPrivilegeId())->join('cms_moduls', 'cms_moduls.id', '=', 'id_cms_moduls')->select('cms_moduls.name', 'cms_moduls.path', 'is_visible', 'is_create', 'is_read', 'is_edit', 'is_delete')->get();
268
        Session::put('admin_privileges_roles', $roles);
269
270
        backWithMsg(cbTrans('alert_update_data_success'));
271
    }
272
}
273