Passed
Push — master ( 062ac3...61cc62 )
by Iman
04:02
created

AdminModulesController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 224
rs 10
c 0
b 0
f 0
wmc 24

18 Methods

Rating   Name   Duplication   Size   Complexity  
A postStep4() 0 5 1
A getStep4() 0 5 1
A getStep3() 0 5 1
A getStep2() 0 5 1
A postStepFinish() 0 5 1
B cbInit() 0 35 1
A getStep1() 0 5 1
A getTableColumns() 0 5 1
A getAdd() 0 5 1
A makeColumns() 0 8 1
A postStep2() 0 5 1
A postStep3() 0 5 1
A hookBeforeDelete() 0 9 2
A getTypeInfo() 0 4 1
A getCheckSlug() 0 6 1
A grantFullAccess() 0 10 1
B postAddSave() 0 35 5
A postEditSave() 0 22 2
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)
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...
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));
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

70
            /** @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...
71
        }
72
73
        return $ids;
74
    }
75
76
    public function getTableColumns($table)
77
    {
78
        $columns = \Schema::getColumnListing($table);
79
80
        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

80
        return response()->/** @scrutinizer ignore-call */ json($columns);
Loading history...
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,
0 ignored issues
show
Bug introduced by
Accessing id_cms_privileges on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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