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

AdminMenusController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 145
c 0
b 0
f 0
rs 10
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setCols() 0 5 1
A getMenuId() 0 14 3
A postSaveMenu() 0 18 4
A getMenuPath() 0 9 3
A getIndex() 0 9 1
A setButtons() 0 12 1
A cbInit() 0 20 2
A hookBeforeAdd() 0 13 2
A hookBeforeEdit() 0 12 2
A hookAfterDelete() 0 3 1
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\MenuModule;
4
5
use crocodicstudio\crudbooster\controllers\CBController;
6
use Illuminate\Support\Facades\Request;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Cache;
9
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...
10
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...
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 AdminMenusController extends CBController
14
{
15
    public function cbInit()
16
    {
17
        $this->table = "cms_menus";
18
        $this->primaryKey = "id";
19
        $this->title_field = "name";
20
        $this->limit = 20;
21
        $this->orderby = ["id" => "desc"];
22
23
        $this->setButtons();
24
25
        $id = CRUDBooster::getCurrentId();
26
        $row = CRUDBooster::first($this->table, $id);
27
        $row = (Request::segment(3) == 'edit') ? $row : null;
28
29
        $this->script_js = view('CbMenu::js', ['id' => $id, 'type' => $row->type])->render();
30
31
        $this->setCols();
32
33
        list($statisticId, $moduleId) = $this->getMenuId($row);
34
        $this->form = MenusForm::makeForm($statisticId, $moduleId, $row);
35
    }
36
37
    public function getIndex()
38
    {
39
        $this->cbLoader();
40
41
        $return_url = Request::fullUrl();
42
43
        $page_title = 'Menu Management';
44
45
        return view('CbMenu::menus_management', compact('return_url', 'page_title'));
46
    }
47
48
    public function hookBeforeAdd(&$postData)
49
    {
50
        $postData['parent_id'] = 0;
51
52
        $postData['path'] = $this->getMenuPath($postData);
53
54
        unset($postData['module_slug']);
55
        unset($postData['statistic_slug']);
56
57
        if ($postData['is_dashboard'] == 1) {
58
            //If set dashboard, so unset for first all dashboard
59
            $this->table()->where('is_dashboard', 1)->update(['is_dashboard' => 0]);
60
            Cache::forget('sidebarDashboard'.CRUDBooster::myPrivilegeId());
61
        }
62
    }
63
64
    public function hookBeforeEdit(&$postData, $id)
65
    {
66
        if ($postData['is_dashboard'] == 1) {
67
            //If set dashboard, so unset for first all dashboard
68
            $this->table()->where('is_dashboard', 1)->update(['is_dashboard' => 0]);
69
            Cache::forget('sidebarDashboard'.CRUDBooster::myPrivilegeId());
70
        }
71
72
        $postData['path'] = $this->getMenuPath($postData);
73
74
        unset($postData['module_slug']);
75
        unset($postData['statistic_slug']);
76
    }
77
78
    public function hookAfterDelete($id)
79
    {
80
        $this->table()->where('parent_id', $id)->delete();
81
    }
82
83
    public function postSaveMenu()
84
    {
85
        $this->cbInit();
86
        $isActive = request('isActive');
87
        $post = json_decode(request('menus'), true);
0 ignored issues
show
Bug introduced by
It seems like request('menus') can also be of type array; however, parameter $json of json_decode() 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

87
        $post = json_decode(/** @scrutinizer ignore-type */ request('menus'), true);
Loading history...
88
89
        foreach ($post[0] as $i => $menu) {
90
            $pid = $menu['id'];
91
            $children = $menu['children'][0] ?: [];
92
93
            foreach ($children as $index => $child) {
94
                $this->findRow($child['id'])->update(['sorting' => $index + 1, 'parent_id' => $pid, 'is_active' => $isActive]);
95
            }
96
97
            $this->findRow($pid)->update(['sorting' => $i + 1, 'parent_id' => 0, 'is_active' => $isActive]);
98
        }
99
100
        return response()->json(['success' => true]);
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

100
        return response()->/** @scrutinizer ignore-call */ json(['success' => true]);
Loading history...
101
    }
102
103
    /**
104
     * @param $postdata
105
     * @return string
106
     */
107
    private function getMenuPath($postdata)
108
    {
109
        if ($postdata['type'] == 'Statistic') {
110
            $stat = CRUDBooster::first('cms_statistics', ['id' => $postdata['statistic_slug']])->slug;
111
            return 'statistic-builder/show/'.$stat;
112
        }
113
114
        if ($postdata['type'] == 'Module') {
115
            return CRUDBooster::first('cms_moduls', ['id' => $postdata['module_slug']])->path;
116
        }
117
    }
118
119
    private function setButtons()
120
    {
121
        $this->buttonTableAction = true;
122
        $this->button_action_style = "FALSE";
123
        $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...
124
        $this->deleteBtn = true;
125
        $this->button_edit = true;
126
        $this->buttonDetail = true;
127
        $this->button_show = false;
0 ignored issues
show
Bug Best Practice introduced by
The property button_show does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
128
        $this->button_filter = true;
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...
129
        $this->buttonExport = false;
130
        $this->button_import = false;
131
    }
132
133
    /**
134
     * @param $row
135
     * @return array
136
     */
137
    private function getMenuId($row)
138
    {
139
        $idModule = $idStatistic = 0;
140
141
        if ($row->type == 'Module') {
142
            $idModule = DB::table('cms_moduls')->where('path', $row->path)->first()->id;
143
        }
144
145
        if ($row->type == 'Statistic') {
146
            $row->path = str_replace('statistic-builder/show/', '', $row->path);
147
            $idStatistic = DB::table('cms_statistics')->where('slug', $row->path)->first()->id;
148
        }
149
150
        return [$idStatistic, $idModule];
151
    }
152
153
    private function setCols()
154
    {
155
        $this->col = [];
156
        $this->col[] = ["label" => "Name", "name" => "name"];
157
        $this->col[] = ["label" => "Is Active", "name" => "is_active"];
158
    }
159
}