PrivilegesController::getDelete()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php namespace crocodicstudio\crudbooster\controllers;
2
3
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...
4
use Illuminate\Support\Facades\DB;
5
use Illuminate\Support\Facades\Excel;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Excel 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...
6
use Illuminate\Support\Facades\PDF;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\PDF 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...
7
use Illuminate\Support\Facades\Request;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Support\Facades\Session;
10
11
class PrivilegesController extends CBController
12
{
13
    public function cbInit()
14
    {
15
        $this->module_name = "Privilege";
16
        $this->table = 'cms_privileges';
17
        $this->primary_key = 'id';
18
        $this->title_field = "name";
19
        $this->button_import = false;
20
        $this->button_export = false;
21
        $this->button_action_style = 'button_icon';
22
        $this->button_detail = false;
23
        $this->button_bulk_action = false;
24
25
        $this->col = [];
26
        $this->col[] = ["label" => "ID", "name" => "id"];
27
        $this->col[] = ["label" => "Name", "name" => "name"];
28
        $this->col[] = [
29
            "label" => "Superadmin",
30
            "name" => "is_superadmin",
31
            'callback_php' => '($row->is_superadmin)?"<span class=\"label label-success\">Superadmin</span>":"<span class=\"label label-default\">Standard</span>"',
32
        ];
33
34
        $this->form = [];
35
        $this->form[] = ["label" => "Name", "name" => "name", 'required' => true];
36
        $this->form[] = ["label" => "Is Superadmin", "name" => "is_superadmin", 'required' => true];
37
        $this->form[] = ["label" => "Theme Color", "name" => "theme_color", 'required' => true];
38
39
        $this->alert[] = [
40
            'message' => "You can use the helper <code>CRUDBooster::getMyPrivilegeId()</code> to get current user login privilege id, or <code>CRUDBooster::getMyPrivilegeName()</code> to get current user login privilege name",
41
            'type' => 'info',
42
        ];
43
    }
44
45
    public function getAdd()
46
    {
47
        $this->cbLoader();
48
49
        if (! CRUDBooster::isCreate() && $this->global_privilege == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
50
            CRUDBooster::insertLog(trans('crudbooster.log_try_add', ['module' => CRUDBooster::getCurrentModule()->name]));
51
            CRUDBooster::redirect(CRUDBooster::adminPath(), trans("crudbooster.denied_access"));
52
        }
53
54
        $id = 0;
55
        $data['page_title'] = "Add Data";
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
56
        $data['moduls'] = DB::table("cms_moduls")->where('is_protected', 0)->whereNull('deleted_at')->select("cms_moduls.*", DB::raw("(select is_visible from cms_privileges_roles where id_cms_moduls = cms_moduls.id and id_cms_privileges = '$id') as is_visible"), DB::raw("(select is_create from cms_privileges_roles where id_cms_moduls  = cms_moduls.id and id_cms_privileges = '$id') as is_create"), DB::raw("(select is_read from cms_privileges_roles where id_cms_moduls    = cms_moduls.id and id_cms_privileges = '$id') as is_read"), DB::raw("(select is_edit from cms_privileges_roles where id_cms_moduls    = cms_moduls.id and id_cms_privileges = '$id') as is_edit"), DB::raw("(select is_delete from cms_privileges_roles where id_cms_moduls  = cms_moduls.id and id_cms_privileges = '$id') as is_delete"))->orderby("name", "asc")->get();
57
        $data['page_menu'] = Route::getCurrentRoute()->getActionName();
58
59
        return view('crudbooster::privileges', $data);
60
    }
61
62
    public function postAddSave()
63
    {
64
        $this->cbLoader();
65
66
        if (! CRUDBooster::isCreate() && $this->global_privilege == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
67
            CRUDBooster::insertLog(trans('crudbooster.log_try_add_save', [
68
                'name' => Request::input($this->title_field),
69
                'module' => CRUDBooster::getCurrentModule()->name,
70
            ]));
71
            CRUDBooster::redirect(CRUDBooster::adminPath(), trans("crudbooster.denied_access"));
72
        }
73
74
        $this->validation($request);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $request seems to be never defined.
Loading history...
75
        $this->input_assignment($request);
76
77
        $this->arr[$this->primary_key] = DB::table($this->table)->max($this->primary_key) + 1;
78
79
        DB::table($this->table)->insert($this->arr);
80
        $id = $this->arr[$this->primary_key];
81
82
        //set theme
83
        Session::put('theme_color', $this->arr['theme_color']);
84
85
        $priv = Request::input("privileges");
86
        if ($priv) {
87
            foreach ($priv as $id_modul => $data) {
88
                $arrs = [];
89
                $arrs['id'] = DB::table('cms_privileges_roles')->max('id') + 1;
90
                $arrs['is_visible'] = @$data['is_visible'] ?: 0;
91
                $arrs['is_create'] = @$data['is_create'] ?: 0;
92
                $arrs['is_read'] = @$data['is_read'] ?: 0;
93
                $arrs['is_edit'] = @$data['is_edit'] ?: 0;
94
                $arrs['is_delete'] = @$data['is_delete'] ?: 0;
95
                $arrs['id_cms_privileges'] = $id;
96
                $arrs['id_cms_moduls'] = $id_modul;
97
                DB::table("cms_privileges_roles")->insert($arrs);
98
99
                $module = DB::table('cms_moduls')->where('id', $id_modul)->first();
0 ignored issues
show
Unused Code introduced by
The assignment to $module is dead and can be removed.
Loading history...
100
            }
101
        }
102
103
        //Refresh Session Roles
104
        $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();
105
        Session::put('admin_privileges_roles', $roles);
106
107
        CRUDBooster::redirect(CRUDBooster::mainpath(), trans("crudbooster.alert_add_data_success"), 'success');
108
    }
109
110
    public function getEdit($id)
111
    {
112
        $this->cbLoader();
113
114
        $row = DB::table($this->table)->where("id", $id)->first();
115
116
        if (! CRUDBooster::isRead() && $this->global_privilege == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
117
            CRUDBooster::insertLog(trans("crudbooster.log_try_edit", [
118
                'name' => $row->{$this->title_field},
119
                'module' => CRUDBooster::getCurrentModule()->name,
120
            ]));
121
            CRUDBooster::redirect(CRUDBooster::adminPath(), trans('crudbooster.denied_access'));
122
        }
123
124
        $page_title = trans('crudbooster.edit_data_page_title', ['module' => 'Privilege', 'name' => $row->name]);
125
126
        $moduls = DB::table("cms_moduls")->where('is_protected', 0)->where('deleted_at', null)->select("cms_moduls.*")->orderby("name", "asc")->get();
127
        $page_menu = Route::getCurrentRoute()->getActionName();
128
129
        return view('crudbooster::privileges', compact('row', 'page_title', 'moduls', 'page_menu'));
130
    }
131
132
    public function postEditSave($id)
133
    {
134
        $this->cbLoader();
135
136
        $row = CRUDBooster::first($this->table, $id);
137
138
        if (! CRUDBooster::isUpdate() && $this->global_privilege == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
139
            CRUDBooster::insertLog(trans("crudbooster.log_try_add", ['name' => $row->{$this->title_field}, 'module' => CRUDBooster::getCurrentModule()->name]));
140
            CRUDBooster::redirect(CRUDBooster::adminPath(), trans('crudbooster.denied_access'));
141
        }
142
143
        $this->validation($id);
144
        $this->input_assignment($id);
145
146
        DB::table($this->table)->where($this->primary_key, $id)->update($this->arr);
147
148
        $priv = Request::input("privileges");
149
150
        // This solves issue #1074
151
        DB::table("cms_privileges_roles")->where("id_cms_privileges", $id)->delete();
152
153
        if ($priv) {
154
155
            foreach ($priv as $id_modul => $data) {
156
                //Check Menu
157
                $module = DB::table('cms_moduls')->where('id', $id_modul)->first();
0 ignored issues
show
Unused Code introduced by
The assignment to $module is dead and can be removed.
Loading history...
158
                $currentPermission = DB::table('cms_privileges_roles')->where('id_cms_moduls', $id_modul)->where('id_cms_privileges', $id)->first();
159
160
                if ($currentPermission) {
161
                    $arrs = [];
162
                    $arrs['is_visible'] = @$data['is_visible'] ?: 0;
163
                    $arrs['is_create'] = @$data['is_create'] ?: 0;
164
                    $arrs['is_read'] = @$data['is_read'] ?: 0;
165
                    $arrs['is_edit'] = @$data['is_edit'] ?: 0;
166
                    $arrs['is_delete'] = @$data['is_delete'] ?: 0;
167
                    DB::table('cms_privileges_roles')->where('id', $currentPermission->id)->update($arrs);
168
                } else {
169
                    $arrs = [];
170
                    $arrs['id'] = DB::table('cms_privileges_roles')->max('id') + 1;
171
                    $arrs['is_visible'] = @$data['is_visible'] ?: 0;
172
                    $arrs['is_create'] = @$data['is_create'] ?: 0;
173
                    $arrs['is_read'] = @$data['is_read'] ?: 0;
174
                    $arrs['is_edit'] = @$data['is_edit'] ?: 0;
175
                    $arrs['is_delete'] = @$data['is_delete'] ?: 0;
176
                    $arrs['id_cms_privileges'] = $id;
177
                    $arrs['id_cms_moduls'] = $id_modul;
178
                    DB::table("cms_privileges_roles")->insert($arrs);
179
                }
180
            }
181
        }
182
183
        //Refresh Session Roles
184
        if ($id == CRUDBooster::myPrivilegeId()) {
185
            $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();
186
            Session::put('admin_privileges_roles', $roles);
187
188
            Session::put('theme_color', $this->arr['theme_color']);
189
        }
190
191
        CRUDBooster::redirect(CRUDBooster::mainpath(), trans("crudbooster.alert_update_data_success", [
192
            'module' => "Privilege",
193
            'title' => $row->name,
194
        ]), 'success');
195
    }
196
197
    public function getDelete($id)
198
    {
199
        $this->cbLoader();
200
201
        $row = DB::table($this->table)->where($this->primary_key, $id)->first();
202
203
        if (! CRUDBooster::isDelete() && $this->global_privilege == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
204
            CRUDBooster::insertLog(trans("crudbooster.log_try_delete", [
205
                'name' => $row->{$this->title_field},
206
                'module' => CRUDBooster::getCurrentModule()->name,
207
            ]));
208
            CRUDBooster::redirect(CRUDBooster::adminPath(), trans('crudbooster.denied_access'));
209
        }
210
211
        DB::table($this->table)->where($this->primary_key, $id)->delete();
212
        DB::table("cms_privileges_roles")->where("id_cms_privileges", $row->id)->delete();
213
214
        CRUDBooster::redirect(CRUDBooster::mainpath(), trans("crudbooster.alert_delete_data_success"), 'success');
215
    }
216
}
217