Passed
Pull Request — master (#1131)
by Iman
04:36
created

AdminModulesController::postStep3()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 1
dl 0
loc 4
c 0
b 0
f 0
cc 1
rs 10
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\controllers\Helpers\FontAwesome;
8
use Illuminate\Support\Facades\Session;
9
use Illuminate\Support\Facades\Request;
10
use Illuminate\Support\Facades\DB;
11
use CRUDBooster;
0 ignored issues
show
Bug introduced by
The type CRUDBooster was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class AdminModulesController extends CBController
14
{
15
    public function cbInit()
16
    {
17
        $this->table = 'cms_moduls';
18
        $this->primaryKey = 'id';
19
        $this->title_field = 'name' ;
20
        $this->limit = 100;
21
        $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...
22
        $this->buttonExport = false;
23
        $this->button_import = false;
24
        $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...
25
        $this->buttonDetail = false;
26
        $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...
27
        $this->button_action_style = 'button_icon';
28
        $this->orderby = ['is_protected' => 'asc', 'name' => 'asc'];
29
30
        $this->col = [];
31
        $this->col[] = ['label' => 'name', 'name' => 'name' ];
32
        $this->col[] = ['label' => "Table", 'name' => "table_name"];
33
        $this->col[] = ['label' => "Path", 'name' => "path"];
34
        $this->col[] = ['label' => "Controller", 'name' => "controller"];
35
        $this->col[] = ['label' => "Protected", 'name' => "is_protected", "visible" => false];
36
37
        $this->makeForm();
38
39
        $this->script_js = "
40
 			$(function() {
41
 				$('#table_name').change(function() {
42
					var v = $(this).val();
43
					$('#path').val(v);
44
				})	
45
 			}) ";
46
47
        $this->addaction[] = [
48
            'label' => 'Module Wizard',
49
            'icon' => 'fa fa-wrench',
50
            'url' => CRUDBooster::mainpath('step1').'/[id]',
51
            "showIf" => "[is_protected] == 0",
52
        ];
53
54
        $this->index_button[] = ['label' => 'Generate New Module', 'icon' => 'fa fa-plus', 'url' => CRUDBooster::mainpath('step1'), 'color' => 'success'];
55
    }
56
    // public function getIndex() {
57
    // 	$data['page_title'] = 'Module Generator';
58
    // 	$data['result'] = DB::table('cms_moduls')->where('is_protected',0)->orderby('name','asc')->get();
59
    // 	$this->cbView('CbModulesGen::index',$data);
60
    // }	
61
62
    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...
63
    {
64
        $controller = ModulesRepo::getControllerName($id);
65
        DB::table('cms_menus')->where('path', 'like', '%'.$controller.'%')->delete();
66
        @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

66
        /** @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...
67
    }
68
69
    public function getTableColumns($table)
70
    {
71
        $columns = DbInspector::getTableCols($table);
0 ignored issues
show
Bug introduced by
The type crocodicstudio\crudboost...leGenerator\DbInspector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
73
        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

73
        return response()->/** @scrutinizer ignore-call */ json($columns);
Loading history...
74
    }
75
76
    public function getCheckSlug($slug)
77
    {
78
        $check = DB::table('cms_moduls')->where('path', $slug)->count();
79
        $lastId = DB::table('cms_moduls')->max('id') + 1;
80
81
        return response()->json(['total' => $check, 'lastid' => $lastId]);
82
    }
83
84
    public function getAdd()
85
    {
86
        $this->cbLoader();
87
88
        return redirect()->route("ModulesControllerGetStep1");
89
    }
90
91
    public function getStep1($id = 0, Step1Handler $handler)
92
    {
93
        $this->cbLoader();
94
        return $handler->showForm($id);
95
    }
96
97
    public function getStep2($id, Step2Handler $handler)
98
    {
99
        $this->cbLoader();
100
        return $handler->showForm($id);
101
    }
102
103
    public function postStep2(Step1Handler $handler)
104
    {
105
        $this->cbLoader();
106
107
        return $handler->handleFormSubmit();
108
    }
109
110
    public function postStep3(Step2Handler $handler)
111
    {
112
        $this->cbLoader();
113
        return $handler->handleFormSubmit();
114
    }
115
116
    public function getStep3($id, Step3Handler $step3)
117
    {
118
        $this->cbLoader();
119
        return $step3->showForm($id);
120
    }
121
122
    public function getTypeInfo($type = 'text')
123
    {
124
        header("Content-Type: application/json");
125
        echo file_get_contents(CRUDBooster::componentsPath($type).'/info.json');
126
    }
127
128
    public function postStep4(Step3Handler $handler)
129
    {
130
        $this->cbLoader();
131
        return $handler->handleFormSubmit();
132
    }
133
134
    public function getStep4($id, Step4Handler $handler)
135
    {
136
        $this->cbLoader();
137
        return $handler->showForm($id);
138
    }
139
140
    public function postStepFinish(Step4Handler $handler)
141
    {
142
        $this->cbLoader();
143
        return $handler->handleFormSubmit();
144
    }
145
146
    public function postAddSave()
147
    {
148
        $this->cbLoader();
149
        app(FormValidator::class)->validate(null, $this->form, $this->table);
150
        $this->inputAssignment();
151
152
        //Generate Controller 
153
        $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

153
        $route_basename = basename(/** @scrutinizer ignore-type */ request('path'));
Loading history...
154
        if ($this->arr['controller'] == '') {
155
            $this->arr['controller'] = ControllerGenerator::generateController(request('table_name'), $route_basename);
156
        }
157
158
        $this->arr['created_at'] = date('Y-m-d H:i:s');
159
        $this->arr['id'] = $this->table()->max('id') + 1;
160
        $this->table()->insert($this->arr);
161
162
        //Insert Menu
163
        if ($this->arr['controller']) {
164
            $this->createMenuForModule();
165
        }
166
167
        $id_modul = $this->arr['id'];
168
169
        $user_id_privileges = CRUDBooster::myPrivilegeId();
170
        DB::table('cms_privileges_roles')->insert([
171
            'id_cms_moduls' => $id_modul,
172
            'id_cms_privileges' => $user_id_privileges,
173
            'is_visible' => 1,
174
            'is_create' => 1,
175
            'is_read' => 1,
176
            'is_edit' => 1,
177
            'is_delete' => 1,
178
        ]);
179
180
        //Refresh Session Roles
181
        $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();
182
        Session::put('admin_privileges_roles', $roles);
183
184
        $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...
185
        if (request('return_url')) {
186
            CRUDBooster::redirect(request('return_url'), cbTrans("alert_add_data_success"), 'success');
187
        } 
188
        if (request('submit') == cbTrans('button_save_more')) {
189
            CRUDBooster::redirect(CRUDBooster::mainpath('add'), cbTrans('alert_add_data_success'), 'success');
190
        }
191
        CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_add_data_success'), 'success');
192
    }
193
194
    public function postEditSave($id)
195
    {
196
        $this->cbLoader();
197
198
        $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...
199
200
201
        app(FormValidator::class)->validate($id, $this->form, $this->table);
202
203
        $this->inputAssignment();
204
205
        //Generate Controller 
206
        $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

206
        $route_basename = basename(/** @scrutinizer ignore-type */ request('path'));
Loading history...
207
        if ($this->arr['controller'] == '') {
208
            $this->arr['controller'] = CRUDBooster::generateController(request('table_name'), $route_basename);
209
        }
210
211
        $this->findRow($id)->update($this->arr);
212
213
        //Refresh Session Roles
214
        $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();
215
        Session::put('admin_privileges_roles', $roles);
216
217
        CRUDBooster::redirect(Request::server('HTTP_REFERER'), cbTrans('alert_update_data_success'), 'success');
218
    }
219
220
    public function getTest()
221
    {
222
        $code = FileManipulator::readCtrlContent('AdminCustomersController.php');
223
224
        $forms = parseFormToArray($code);
0 ignored issues
show
Bug introduced by
The function parseFormToArray was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

224
        $forms = /** @scrutinizer ignore-call */ parseFormToArray($code);
Loading history...
225
        echo '<pre>';
226
        print_r($forms);
227
    }
228
229
    private function makeForm()
230
    {
231
        $this->form = [];
232
        $this->form[] = ['label' => 'Name', 'name' => 'name', 'placeholder' => 'Module name here', 'required' => true];
233
234
        $tables_list = \CB::listCbTables();
235
236
        $this->form[] = [
237
            'label' => "Table Name",
238
            'name' => "table_name",
239
            'type' => "select2_dataenum",
240
            "options" => ['enum' => $tables_list],
241
            'required' => true,
242
        ];
243
244
        $fontawesome = FontAwesome::cssClass();
245
        $row = CRUDBooster::first($this->table, CRUDBooster::getCurrentId());
246
        $custom = view('crudbooster::components.list_icon', compact('fontawesome', 'row'))->render();
247
        $this->form[] = ['label' => 'Icon', 'name' => 'icon', 'type' => 'custom_html', 'options' => ['html' => $custom], 'required' => true];
248
249
        $this->form[] = ['label' => 'Path', 'name' => 'path', 'required' => true, 'placeholder' => 'Optional'];
250
        $this->form[] = ['label' => 'Controller', 'name' => 'controller', 'type' => 'text', 'placeholder' => '(Optional) Auto Generated'];
251
252
        if (in_array(CRUDBooster::getCurrentMethod(), ['getAdd', 'postAddSave'])) {
253
            return ;
254
        }
255
256
        $this->form[] = [
257
            'label' => "Global Privilege",
258
            'name' => "global_privilege",
259
            'type' => "radio",
260
            'dataenum' => ['0|No', '1|Yes'],
261
            'value' => 0,
262
            'help' => 'Global Privilege allows you to make the module to be accessible by all privileges',
263
            'exception' => true,
264
        ];
265
266
        $this->form[] = [
267
            'label' => 'Button Action Style',
268
            'name' => 'button_action_style',
269
            'type' => 'radio',
270
            'dataenum' => ['button_icon', 'button_icon_text', 'button_text', 'dropdown'],
271
            'value' => 'button_icon',
272
            'exception' => true,
273
        ];
274
        $this->form[] = [
275
            'label' => "Button Table Action",
276
            'name' => "button_table_action",
277
            'type' => "radio",
278
            'dataenum' => ['Yes', 'No'],
279
            'value' => 'Yes',
280
            'exception' => true,
281
        ];
282
        $this->form[] = [
283
            'label' => "Button Add",
284
            'name' => "button_add",
285
            'type' => "radio",
286
            'dataenum' => ['Yes', 'No'],
287
            'value' => 'Yes',
288
            'exception' => true,
289
        ];
290
        $this->form[] = [
291
            'label' => "Button Delete",
292
            'name' => "button_delete",
293
            'type' => "radio",
294
            'dataenum' => ['Yes', 'No'],
295
            'value' => 'Yes',
296
            'exception' => true,
297
        ];
298
        $this->form[] = [
299
            'label' => "Button Edit",
300
            'name' => "button_edit",
301
            'type' => "radio",
302
            'dataenum' => ['Yes', 'No'],
303
            'value' => 'Yes',
304
            'exception' => true,
305
        ];
306
        $this->form[] = [
307
            'label' => "Button Detail",
308
            'name' => "button_detail",
309
            'type' => "radio",
310
            'dataenum' => ['Yes', 'No'],
311
            'value' => 'Yes',
312
            'exception' => true,
313
        ];
314
        $this->form[] = [
315
            'label' => "Button Show",
316
            'name' => "button_show",
317
            'type' => "radio",
318
            'dataenum' => ['Yes', 'No'],
319
            'value' => 'Yes',
320
            'exception' => true,
321
        ];
322
        $this->form[] = [
323
            'label' => "Button Filter",
324
            'name' => "button_filter",
325
            'type' => "radio",
326
            'dataenum' => ['Yes', 'No'],
327
            'value' => 'Yes',
328
            'exception' => true,
329
        ];
330
        $this->form[] = [
331
            'label' => 'Button Export',
332
            'name' => 'button_export',
333
            'type' => 'radio',
334
            'dataenum' => ['Yes', 'No'],
335
            'value' => 'No',
336
            'exception' => true,
337
        ];
338
        $this->form[] = [
339
            'label' => 'Button Import',
340
            'name' => 'button_import',
341
            'type' => 'radio',
342
            'dataenum' => ['Yes', 'No'],
343
            'value' => 'No',
344
            'exception' => true,
345
        ];
346
347
    }
348
349
    private function createMenuForModule()
350
    {
351
        $parent_menu_sort = DB::table('cms_menus')->where('parent_id', 0)->max('sorting') + 1;
352
        $parent_menu_id = DB::table('cms_menus')->insertGetId([
353
            'created_at' => date('Y-m-d H:i:s'),
354
            'name' => $this->arr['name'],
355
            'icon' => $this->arr['icon'],
356
            'path' => '#',
357
            'type' => 'URL External',
358
            'is_active' => 1,
359
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
360
            'sorting' => $parent_menu_sort,
361
            'parent_id' => 0,
362
        ]);
363
364
        $arr = [
365
            'created_at' => date('Y-m-d H:i:s'),
366
            'type' => 'Route',
367
            'is_active' => 1,
368
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
369
            'parent_id' => $parent_menu_id,
370
        ];
371
372
        DB::table('cms_menus')->insert([
373
            'name' => cbTrans('text_default_add_new_module', ['module' => $this->arr['name']]),
374
            'icon' => 'fa fa-plus',
375
            'path' => $this->arr['controller'].'GetAdd',
376
            'sorting' => 1,
377
        ] + $arr);
378
379
        DB::table('cms_menus')->insert([
380
            'name' => cbTrans('text_default_list_module', ['module' => $this->arr['name']]),
381
            'icon' => 'fa fa-bars',
382
            'path' => $this->arr['controller'].'GetIndex',
383
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
384
            'sorting' => 2,
385
        ] + $arr);
386
387
    }
388
}
389