Passed
Push — master ( 5b852a...062ac3 )
by Iman
05:12
created

AdminModulesController::createMenuForModule()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
nc 1
nop 0
dl 0
loc 37
c 0
b 0
f 0
cc 1
rs 8.8571
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->arr['id'] = $this->table()->max('id') + 1;
174
        $this->table()->insert($this->arr);
175
176
        //Insert Menu
177
        if ($this->arr['controller']) {
178
            (new CreateMenuForNewModule())->execute($this->arr['controller'], $this->arr['name'], $this->arr['icon']);
179
        }
180
181
        $id_modul = $this->arr['id'];
182
183
        $user_id_privileges = auth('cbAdmin')->user()->id_cms_privileges;
184
        DB::table('cms_privileges_roles')->insert([
185
            'id_cms_modules' => $id_modul,
186
            'id_cms_privileges' => $user_id_privileges,
187
            'can_see_module' => 1,
188
            'can_create' => 1,
189
            'can_read' => 1,
190
            'can_edit' => 1,
191
            'can_delete' => 1,
192
        ]);
193
194
        //Refresh Session Roles
195
        CRUDBooster::refreshSessionRoles();
196
197
        //$ref_parameter = Request::input('ref_parameter');
198
        if (request('return_url')) {
199
            CRUDBooster::redirect(request('return_url'), cbTrans("alert_add_data_success"), 'success');
200
        }
201
        if (request('submit') == cbTrans('button_save_more')) {
202
            CRUDBooster::redirect(CRUDBooster::mainpath('add'), cbTrans('alert_add_data_success'), 'success');
203
        }
204
        CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_add_data_success'), 'success');
205
    }
206
207
    public function postEditSave($id)
208
    {
209
        $this->cbLoader();
210
211
        //$row = $this->table()->where($this->primaryKey, $id)->first();
212
213
        app(FormValidator::class)->validate($id, $this->form, $this);
214
215
        $this->inputAssignment();
216
217
        //Generate Controller
218
        $route_basename = basename(request('path'));
219
        if ($this->arr['controller'] == '') {
220
            $this->arr['controller'] = ControllerGenerator::generateController(request('table_name'), $route_basename);
221
        }
222
223
        $this->findRow($id)->update($this->arr);
224
225
        //Refresh Session Roles
226
        CRUDBooster::refreshSessionRoles();
227
228
        backWithMsg(cbTrans('alert_update_data_success'));
229
    }
230
}
231