Passed
Push — master ( 284e72...816a06 )
by Iman
04:12
created

CBController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 150
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdd() 0 6 1
A getDetail() 0 10 1
A cbLoader() 0 9 1
A findRow() 0 3 1
A getIndex() 0 7 1
A getEdit() 0 10 1
A genericLoader() 0 14 2
A cbView() 0 5 1
A actionButtonSelected() 0 2 1
A insertLog() 0 3 1
A getUpdateSingle() 0 9 1
A findFirst() 0 3 1
A table() 0 4 2
A cbForm() 0 4 1
1
<?php
2
3
namespace crocodicstudio\crudbooster\controllers;
4
5
error_reporting(E_ALL ^ E_NOTICE);
6
7
use crocodicstudio\crudbooster\CBCoreModule\Hooks;
8
use crocodicstudio\crudbooster\CBCoreModule\Index;
9
use crocodicstudio\crudbooster\controllers\CBController\CbFormLoader;
10
use crocodicstudio\crudbooster\controllers\CBController\CbIndexLoader;
11
use crocodicstudio\crudbooster\controllers\CBController\CbLayoutLoader;
12
use crocodicstudio\crudbooster\controllers\CBController\Deleter;
13
use crocodicstudio\crudbooster\controllers\CBController\ExportData;
14
use crocodicstudio\crudbooster\controllers\CBController\FormSubmitHandlers;
15
use crocodicstudio\crudbooster\controllers\CBController\ImportData;
16
use crocodicstudio\crudbooster\controllers\CBController\IndexAjax;
17
use crocodicstudio\crudbooster\helpers\DbInspector;
18
use Illuminate\Support\Facades\DB;
19
use crocodicstudio\crudbooster\helpers\CRUDBooster;
20
use Schema;
21
22
abstract class CBController extends Controller
23
{
24
    abstract public function cbInit();
25
26
    use Hooks;
27
    use Deleter, CbFormLoader, CbIndexLoader, CbLayoutLoader, IndexAjax, ImportData, ExportData, FormSubmitHandlers;
0 ignored issues
show
Bug introduced by
The trait crocodicstudio\crudboost...CBController\ImportData requires the property $name which is not provided by crocodicstudio\crudboost...ontrollers\CBController.
Loading history...
28
29
    public $table;
30
31
    public $titleField;
32
33
    public $primaryKey = 'id';
34
35
    public $form = [];
36
37
    public $data = [];
38
39
    public $parent_field = null;
40
41
    public function cbView($template, $data)
42
    {
43
        $this->cbLayoutLoader();
44
        view()->share($this->data);
45
        return view($template, $data);
46
    }
47
48
    public function cbLoader()
49
    {
50
        $this->genericLoader();
51
        $this->cbLayoutLoader();
52
        $this->cbFormLoader();
53
        $this->cbIndexLoader();
54
        $this->checkHideForm();
55
56
        view()->share($this->data);
57
    }
58
59
    public function getIndex()
60
    {
61
        $this->genericLoader();
62
        $this->cbIndexLoader();
63
        $data = app(Index::class)->index($this);
64
65
        return $this->cbView('crudbooster::index.index', $data);
66
    }
67
68
    public function getUpdateSingle()
69
    {
70
        $table = request('table');
71
        $column = request('column');
72
        $value = request('value');
73
        $id = request('id');
74
        DB::table($table)->where(DbInspector::findPk($table), $id)->update([$column => $value]);
0 ignored issues
show
Bug introduced by
It seems like $table can also be of type array; however, parameter $table of Illuminate\Support\Facades\DB::table() 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

74
        DB::table(/** @scrutinizer ignore-type */ $table)->where(DbInspector::findPk($table), $id)->update([$column => $value]);
Loading history...
Bug introduced by
It seems like $table can also be of type array; however, parameter $table of crocodicstudio\crudboost...s\DbInspector::findPK() 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

74
        DB::table($table)->where(DbInspector::findPk(/** @scrutinizer ignore-type */ $table), $id)->update([$column => $value]);
Loading history...
75
76
        backWithMsg(cbTrans('alert_update_data_success'));
77
    }
78
79
    public function getAdd()
80
    {
81
        $this->genericLoader();
82
        $page_title = cbTrans('add_data_page_title', ['module' => CRUDBooster::getCurrentModule()->name]);
83
        $command = 'add';
84
        return $this->cbForm(compact('page_title', 'command'));
85
    }
86
87
    /**
88
     * @param string $tableName
89
     * @return mixed
90
     */
91
    public function table($tableName = null)
92
    {
93
        $tableName = $tableName ?: $this->table;
94
        return \DB::table($tableName);
95
    }
96
97
    public function getEdit($id)
98
    {
99
        $this->genericLoader();
100
        $row = $this->findFirst($id);
101
102
        $page_title = cbTrans("edit_data_page_title", ['module' => CRUDBooster::getCurrentModule()->name, 'name' => $row->{$this->titleField}]);
103
        $command = 'edit';
104
        session()->put('current_row_id', $id);
105
106
        return $this->cbForm(compact('id', 'row', 'page_title', 'command'));
107
    }
108
109
    /**
110
     * @param $id
111
     * @return mixed
112
     */
113
    public function findRow($id)
114
    {
115
        return $this->table()->where($this->primaryKey, $id);
116
    }
117
118
    public function getDetail($id)
119
    {
120
        $this->genericLoader();
121
        $this->cbFormLoader();
122
        $row = $this->findFirst($id);
123
124
        $page_title = cbTrans('detail_data_page_title', ['module' => CRUDBooster::getCurrentModule()->name, 'name' => $row->{$this->titleField}]);
125
126
        session()->put('current_row_id', $id);
127
        return $this->cbView('crudbooster::form.details', compact('row', 'page_title', 'id'));
128
    }
129
130
    public function actionButtonSelected($id_selected, $button_name)
131
    {
132
    }
133
134
    private function insertLog($msg, $name)
135
    {
136
        CRUDBooster::insertLog(trans('logging.'.$msg, ['module' => CRUDBooster::getCurrentModule()->name, 'name' => $name]));
137
    }
138
139
    /**
140
     * @param $data
141
     * @return mixed
142
     */
143
    private function cbForm($data)
144
    {
145
        $this->cbFormLoader();
146
        return $this->cbView('crudbooster::form.form', $data);
147
    }
148
149
    protected function genericLoader()
150
    {
151
        $this->primaryKey = DbInspector::findPk($this->table);
152
        $this->cbInit();
153
        $this->data_inputan = $this->form;
154
        $this->data['pk'] = $this->primaryKey;
155
        $this->data['hide_form'] = $this->hide_form;
156
        $this->data['table'] = $this->table;
157
        $this->data['titleField'] = $this->titleField;
158
        $this->data['appname'] = cbGetsetting('appname');
159
        $this->data['indexButton'] = $this->indexButton;
160
161
        $this->data['sub_module'] = $this->sub_module;
162
        $this->data['parent_field'] = (request('parent_field')) ?: $this->parent_field;
163
    }
164
165
    /**
166
     * @param $id
167
     * @return mixed
168
     */
169
    private function findFirst($id)
170
    {
171
        return $this->findRow($id)->first();
172
    }
173
}