Passed
Push — master ( 6bd04a...b9eac4 )
by Iman
04:02
created

AdminModulesController::makeColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
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 Illuminate\Support\Facades\Session;
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Support\Facades\DB;
10
use crocodicstudio\crudbooster\helpers\CRUDBooster;
11
12
class AdminModulesController extends CBController
13
{
14
    public function cbInit()
15
    {
16
        $this->table = 'cms_moduls';
17
        $this->primaryKey = 'id';
18
        $this->title_field = 'name' ;
19
        $this->limit = 100;
20
        $this->button_add = false;
0 ignored issues
show
Bug Best Practice introduced by
The property button_add does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
21
        $this->buttonExport = false;
22
        $this->button_import = false;
23
        $this->button_filter = false;
0 ignored issues
show
Bug Best Practice introduced by
The property button_filter does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
        $this->buttonDetail = false;
25
        $this->button_bulk_action = false;
0 ignored issues
show
Bug Best Practice introduced by
The property button_bulk_action does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26
        $this->button_action_style = 'button_icon';
27
        $this->orderby = ['is_protected' => 'asc', 'name' => 'asc'];
28
29
        $this->makeColumns();
30
31
        $this->form = Form::makeForm($this->table);
32
33
        $this->script_js = "
34
 			$(function() {
35
 				$('#table_name').change(function() {
36
					var v = $(this).val();
37
					$('#path').val(v);
38
				})	
39
 			}) ";
40
41
        $this->addaction[] = [
42
            'label' => 'Module Wizard',
43
            'icon' => 'fa fa-wrench',
44
            'url' => CRUDBooster::mainpath('step1').'/[id]',
45
            "showIf" => "[is_protected] == 0",
46
        ];
47
48
        $this->index_button[] = ['label' => 'Generate New Module', 'icon' => 'fa fa-plus', 'url' => CRUDBooster::mainpath('step1'), 'color' => 'success'];
49
    }
50
    // public function getIndex() {
51
    // 	$data['page_title'] = 'Module Generator';
52
    // 	$data['result'] = DB::table('cms_moduls')->where('is_protected',0)->orderby('name','asc')->get();
53
    // 	$this->cbView('CbModulesGen::index',$data);
54
    // }	
55
56
    function hookBeforeDelete($id)
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...
57
    {
58
        $controller = ModulesRepo::getControllerName($id);
59
        DB::table('cms_menus')->where('path', 'like', '%'.$controller.'%')->delete();
60
        @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

60
        /** @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...
61
    }
62
63
    public function getTableColumns($table)
64
    {
65
        $columns = \Schema::getColumnListing($table);
66
67
        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

67
        return response()->/** @scrutinizer ignore-call */ json($columns);
Loading history...
68
    }
69
70
    public function getCheckSlug($slug)
71
    {
72
        $check = DB::table('cms_moduls')->where('path', $slug)->count();
73
        $lastId = DB::table('cms_moduls')->max('id') + 1;
74
75
        return response()->json(['total' => $check, 'lastid' => $lastId]);
76
    }
77
78
    public function getAdd()
79
    {
80
        $this->cbLoader();
81
82
        return redirect()->route("ModulesControllerGetStep1");
83
    }
84
85
    public function getStep1($id = 0, Step1Handler $handler)
86
    {
87
        $this->cbLoader();
88
        return $handler->showForm($id);
89
    }
90
91
    public function getStep2($id, Step2Handler $handler)
92
    {
93
        $this->cbLoader();
94
        return $handler->showForm($id);
95
    }
96
97
    public function postStep2(Step1Handler $handler)
98
    {
99
        $this->cbLoader();
100
101
        return $handler->handleFormSubmit();
102
    }
103
104
    public function postStep3(Step2Handler $handler)
105
    {
106
        $this->cbLoader();
107
        return $handler->handleFormSubmit();
108
    }
109
110
    public function getStep3($id, Step3Handler $step3)
111
    {
112
        $this->cbLoader();
113
        return $step3->showForm($id);
114
    }
115
116
    public function getTypeInfo($type = 'text')
117
    {
118
        header("Content-Type: application/json");
119
        echo file_get_contents(CbComponentsPath($type).'/info.json');
120
    }
121
122
    public function postStep4(Step3Handler $handler)
123
    {
124
        $this->cbLoader();
125
        return $handler->handleFormSubmit();
126
    }
127
128
    public function getStep4($id, Step4Handler $handler)
129
    {
130
        $this->cbLoader();
131
        return $handler->showForm($id);
132
    }
133
134
    public function postStepFinish(Step4Handler $handler)
135
    {
136
        $this->cbLoader();
137
        return $handler->handleFormSubmit();
138
    }
139
140
    public function postAddSave()
141
    {
142
        $this->cbLoader();
143
        app(FormValidator::class)->validate(null, $this->form, $this->table);
144
        $this->inputAssignment();
145
146
        //Generate Controller 
147
        $route_basename = basename(request('path'));
0 ignored issues
show
Bug introduced by
It seems like request('path') can also be of type array; however, parameter $path of basename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

147
        $route_basename = basename(/** @scrutinizer ignore-type */ request('path'));
Loading history...
148
        if ($this->arr['controller'] == '') {
149
            $this->arr['controller'] = ControllerGenerator::generateController(request('table_name'), $route_basename);
150
        }
151
152
        $this->arr['created_at'] = date('Y-m-d H:i:s');
153
        $this->arr['id'] = $this->table()->max('id') + 1;
154
        $this->table()->insert($this->arr);
155
156
        //Insert Menu
157
        if ($this->arr['controller']) {
158
            $this->createMenuForModule();
159
        }
160
161
        $id_modul = $this->arr['id'];
162
163
        $user_id_privileges = CRUDBooster::myPrivilegeId();
164
        DB::table('cms_privileges_roles')->insert([
165
            'id_cms_moduls' => $id_modul,
166
            'id_cms_privileges' => $user_id_privileges,
167
            'is_visible' => 1,
168
            'is_create' => 1,
169
            'is_read' => 1,
170
            'is_edit' => 1,
171
            'is_delete' => 1,
172
        ]);
173
174
        //Refresh Session Roles
175
        $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();
176
        Session::put('admin_privileges_roles', $roles);
177
178
        $ref_parameter = Request::input('ref_parameter');
0 ignored issues
show
Unused Code introduced by
The assignment to $ref_parameter is dead and can be removed.
Loading history...
179
        if (request('return_url')) {
180
            CRUDBooster::redirect(request('return_url'), cbTrans("alert_add_data_success"), 'success');
181
        } 
182
        if (request('submit') == cbTrans('button_save_more')) {
183
            CRUDBooster::redirect(CRUDBooster::mainpath('add'), cbTrans('alert_add_data_success'), 'success');
184
        }
185
        CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_add_data_success'), 'success');
186
    }
187
188
    public function postEditSave($id)
189
    {
190
        $this->cbLoader();
191
192
        $row = $this->table()->where($this->primaryKey, $id)->first();
0 ignored issues
show
Unused Code introduced by
The assignment to $row is dead and can be removed.
Loading history...
193
194
195
        app(FormValidator::class)->validate($id, $this->form, $this->table);
196
197
        $this->inputAssignment();
198
199
        //Generate Controller 
200
        $route_basename = basename(request('path'));
0 ignored issues
show
Bug introduced by
It seems like request('path') can also be of type array; however, parameter $path of basename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

200
        $route_basename = basename(/** @scrutinizer ignore-type */ request('path'));
Loading history...
201
        if ($this->arr['controller'] == '') {
202
            $this->arr['controller'] = ControllerGenerator::generateController(request('table_name'), $route_basename);
203
        }
204
205
        $this->findRow($id)->update($this->arr);
206
207
        //Refresh Session Roles
208
        $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();
209
        Session::put('admin_privileges_roles', $roles);
210
211
        CRUDBooster::redirect(Request::server('HTTP_REFERER'), cbTrans('alert_update_data_success'), 'success');
212
    }
213
214
    private function createMenuForModule()
215
    {
216
        $parent_menu_sort = DB::table('cms_menus')->where('parent_id', 0)->max('sorting') + 1;
217
        $parent_menu_id = DB::table('cms_menus')->insertGetId([
218
            'created_at' => date('Y-m-d H:i:s'),
219
            'name' => $this->arr['name'],
220
            'icon' => $this->arr['icon'],
221
            'path' => '#',
222
            'type' => 'URL External',
223
            'is_active' => 1,
224
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
225
            'sorting' => $parent_menu_sort,
226
            'parent_id' => 0,
227
        ]);
228
229
        $arr = [
230
            'created_at' => date('Y-m-d H:i:s'),
231
            'type' => 'Route',
232
            'is_active' => 1,
233
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
234
            'parent_id' => $parent_menu_id,
235
        ];
236
237
        DB::table('cms_menus')->insert([
238
            'name' => cbTrans('text_default_add_new_module', ['module' => $this->arr['name']]),
239
            'icon' => 'fa fa-plus',
240
            'path' => $this->arr['controller'].'GetAdd',
241
            'sorting' => 1,
242
        ] + $arr);
243
244
        DB::table('cms_menus')->insert([
245
            'name' => cbTrans('text_default_list_module', ['module' => $this->arr['name']]),
246
            'icon' => 'fa fa-bars',
247
            'path' => $this->arr['controller'].'GetIndex',
248
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
249
            'sorting' => 2,
250
        ] + $arr);
251
252
    }
253
254
    private function makeColumns()
255
    {
256
        $this->col = [];
257
        $this->col[] = ['label' => 'name', 'name' => 'name'];
258
        $this->col[] = ['label' => "Table", 'name' => "table_name"];
259
        $this->col[] = ['label' => "Path", 'name' => "path"];
260
        $this->col[] = ['label' => "Controller", 'name' => "controller"];
261
        $this->col[] = ['label' => "Protected", 'name' => "is_protected", "visible" => false];
262
    }
263
}
264