Passed
Push — master ( aae8bb...37fc91 )
by Iman
03:40
created

AdminModulesController::postStep1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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 crocodicstudio\crudbooster\helpers\CRUDBooster;
8
use crocodicstudio\crudbooster\Modules\PrivilegeModule\PrivilegeRepo;
9
use Illuminate\Support\Facades\DB;
10
11
class AdminModulesController extends CBController
12
{
13
    public function cbInit()
14
    {
15
        $this->table = 'cms_modules';
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);
0 ignored issues
show
Unused Code introduced by
The call to crocodicstudio\crudboost...erator\Form::makeForm() has too many arguments starting with $this->table. ( Ignorable by Annotation )

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

30
        /** @scrutinizer ignore-call */ 
31
        $this->form = Form::makeForm($this->table);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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_modules')->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 = ModulesRepo::countByPath($slug);
87
        $lastId = DB::table('cms_modules')->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(Step1Handler $handler)
100
    {
101
        $this->cbLoader();
102
103
        return $handler->showForm();
104
    }
105
106
    public function getStep2($id, Step2Handler $handler)
107
    {
108
        $this->cbLoader();
109
110
        return $handler->showForm($id);
111
    }
112
113
    public function postStep1(Step1Handler $handler)
114
    {
115
        $this->cbLoader();
116
117
        return $handler->handleFormSubmit();
118
    }
119
120
    public function postStep2(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 postStep3(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
        if ($this->arr['controller'] == '') {
169
            $this->arr['controller'] = ControllerGenerator::generateController(request('table_name'), basename(request('path')));
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
        (new PrivilegeRepo())->grantAllPermissions($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