Passed
Push — master ( d1c6ec...66cfc7 )
by Iman
06:37
created

AdminStatisticBuilderController::makeColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 0
dl 0
loc 4
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\StatisticModule;
4
5
use crocodicstudio\crudbooster\controllers\CBController;
6
use crocodicstudio\crudbooster\Modules\MenuModule\MenuRepo;
7
use Illuminate\Support\Facades\DB;
8
use crocodicstudio\crudbooster\helpers\CRUDBooster;
9
10
class AdminStatisticBuilderController extends CBController
11
{
12
    public function cbInit()
13
    {
14
        $this->table = "cms_statistics";
15
        $this->primaryKey = "id";
16
        $this->titleField = "name";
17
        $this->limit = 20;
18
        $this->orderby = "id,desc";
19
20
        $this->setButtons();
21
22
        $this->makeColumns();
23
24
        $this->form = StatisticForm::makeForm();
25
26
        $this->addAction = [];
27
        $this->addAction[] = ['label' => 'Builder', 'url' => CRUDBooster::mainpath('builder').'/[id]', 'icon' => 'fa fa-wrench'];
28
    }
29
30
    public function getShowDashboard()
31
    {
32
        $this->cbLoader();
33
        $m = MenuRepo::sidebarDashboard();
34
        $m->path = str_replace("statistic_builder/show/", "", $m->path);
35
        if ($m->type != 'Statistic') {
36
            redirect('/');
37
        }
38
        $row = CRUDBooster::first($this->table, ['slug' => $m->path]);
39
40
        $id_cms_statistics = $row->id;
41
        $page_title = $row->name;
42
43
        return view('CbStatistics::show', compact('page_title', 'id_cms_statistics'));
44
    }
45
46
    public function getShow($slug)
47
    {
48
        $this->cbLoader();
49
        $row = CRUDBooster::first($this->table, ['slug' => $slug]);
50
        $id_cms_statistics = $row->id;
51
        $page_title = $row->name;
52
53
        return view('CbStatistics::show', compact('page_title', 'id_cms_statistics'));
54
    }
55
56
    public function getDashboard()
57
    {
58
        $this->cbLoader();
59
60
        $menus = DB::table('cms_menus')->where('is_dashboard', 1)->where('type', 'Statistic')->first();
61
        $slug = str_replace(['statistic-builder/show/', 'statistic_builder/show/'], "", $menus->path);
62
63
        $row = CRUDBooster::first($this->table, ['slug' => $slug]);
64
        $id_cms_statistics = $row->id;
65
        $page_title = $row->name;
66
67
        return view('CbStatistics::show', compact('page_title', 'id_cms_statistics'));
68
    }
69
70
    public function getBuilder($id_cms_statistics)
71
    {
72
       CRUDBooster::allowOnlySuperAdmin();
73
        $this->cbLoader();
74
75
        $page_title = 'Statistic Builder';
76
77
        return view('CbStatistics::builder', compact('page_title', 'id_cms_statistics'));
78
    }
79
80
    public function getListComponent($id_cms_statistics, $area_name)
81
    {
82
        $rows = DB::table('cms_statistic_components')->where('id_cms_statistics', $id_cms_statistics)->where('area_name', $area_name)->orderby('sorting', 'asc')->get();
83
84
        return response()->json(['components' => $rows]);
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

84
        return response()->/** @scrutinizer ignore-call */ json(['components' => $rows]);
Loading history...
85
    }
86
87
    public function getViewComponent($componentID)
88
    {
89
        $component = CRUDBooster::first('cms_statistic_components', ['componentID' => $componentID]);
90
91
        $command = 'layout';
92
        $layout = view('CbStatistics::components.'.$component->component_name, compact('command', 'componentID'))->render();
93
94
        $component_name = $component->component_name;
95
        $area_name = $component->area_name;
0 ignored issues
show
Unused Code introduced by
The assignment to $area_name is dead and can be removed.
Loading history...
96
        $config = json_decode($component->config);
97
        if (! $config) {
98
            return response()->json(compact('componentID', 'layout'));
99
        }
100
        foreach ($config as $key => $value) {
101
            if (! $value) {
102
                continue;
103
            }
104
            $command = 'showFunction';
105
            $value = view('CbStatistics::components.'.$component_name, compact('command', 'value', 'key', 'config', 'componentID'))->render();
106
            $layout = str_replace('['.$key.']', $value, $layout);
107
        }
108
109
        return response()->json(compact('componentID', 'layout'));
110
    }
111
112
    public function postAddComponent()
113
    {
114
        $this->cbLoader();
115
        $component_name = request('component_name');
116
        $componentID = md5(time());
117
118
        $command = 'layout';
0 ignored issues
show
Unused Code introduced by
The assignment to $command is dead and can be removed.
Loading history...
119
120
        $data = [
121
            'id_cms_statistics' => request('id_cms_statistics'),
122
            'componentID' => $componentID,
123
            'component_name' => $component_name,
124
            'area_name' => request('area'),
125
            'sorting' => request('sorting'),
126
            'name' => 'Untitled',
127
        ];
128
129
        if (! $data['created_at'] && Schema::hasColumn('cms_statistic_components', 'created_at')) {
0 ignored issues
show
Bug introduced by
The type crocodicstudio\crudboost...\StatisticModule\Schema was not found. Did you mean Schema? If so, make sure to prefix the type with \.
Loading history...
130
            $data['created_at'] = YmdHis();
131
        }
132
133
        return DB::table('cms_statistic_components')->insertGetId($data);
134
135
        $layout = view('CbStatistics::components.'.$component_name, compact('command', 'componentID'))->render();
0 ignored issues
show
Unused Code introduced by
$layout = view('CbStatis...omponentID'))->render() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
136
137
        return response()->json(compact('layout', 'componentID'));
138
    }
139
140
    public function postUpdateAreaComponent()
141
    {
142
        DB::table('cms_statistic_components')->where('componentID', request('componentid'))->update([
143
                'sorting' => request('sorting'),
144
                'area_name' => request('areaname'),
145
            ]);
146
147
        return response()->json(['status' => true]);
148
    }
149
150
    public function getEditComponent($componentID)
151
    {
152
       CRUDBooster::allowOnlySuperAdmin();
153
        $this->cbLoader();
154
155
        $component_row = CRUDBooster::first('cms_statistic_components', ['componentID' => $componentID]);
156
157
        $config = json_decode($component_row->config);
158
159
        $command = 'configuration';
160
161
        return view('CbStatistics::components.'.$component_row->component_name, compact('command', 'componentID', 'config'));
162
    }
163
164
    public function postSaveComponent()
165
    {
166
        DB::table('cms_statistic_components')->where('componentID', request('componentid'))->update([
167
                'name' => request('name'),
168
                'config' => json_encode(request('config')),
169
            ]);
170
171
        return response()->json(['status' => true]);
172
    }
173
174
    public function getDeleteComponent($id)
175
    {
176
       CRUDBooster::allowOnlySuperAdmin();
177
178
        DB::table('cms_statistic_components')->where('componentID', $id)->delete();
179
180
        return response()->json(['status' => true]);
181
    }
182
183
    public function hookBeforeAdd($arr)
184
    {
185
        //Your code here
186
        $arr['slug'] = str_slug($arr['name']);
187
        return $arr;
188
    }
189
190
    public function hookBeforeEdit($postData, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

190
    public function hookBeforeEdit($postData, /** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
191
    {
192
        $postData['slug'] = str_slug($postData['name']);
193
        return $postData;
194
    }
195
196
    private function makeColumns()
197
    {
198
        $this->col = [];
199
        $this->col[] = ['label' => 'Name', 'name' => 'name'];
200
    }
201
202
    private function setButtons()
203
    {
204
        $this->buttonTableAction = true;
205
        $this->buttonActionStyle = 'button_icon_text';
206
        $this->buttonAdd = true;
207
        $this->deleteBtn = true;
208
        $this->buttonEdit = true;
209
        $this->buttonDetail = false;
210
        $this->buttonShow = true;
211
        $this->buttonFilter = false;
212
        $this->buttonExport = false;
213
        $this->buttonImport = false;
214
    }
215
}
216