Passed
Push — master ( d63a45...8016ac )
by Iman
09:10
created

CBController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

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

13 Methods

Rating   Name   Duplication   Size   Complexity  
A cbLoader() 0 9 1
A getAdd() 0 6 1
A table() 0 4 2
A getDetail() 0 10 1
A cbView() 0 5 1
A actionButtonSelected() 0 2 1
A getEdit() 0 10 1
A findFirst() 0 3 1
A getUpdateSingle() 0 9 1
A getIndex() 0 7 1
A findRow() 0 3 1
A cbForm() 0 4 1
A genericLoader() 0 14 3
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\Controllers;
4
5
use Crocodicstudio\Crudbooster\CBCoreModule\Hooks;
6
use Crocodicstudio\Crudbooster\CBCoreModule\Index;
7
use Crocodicstudio\Crudbooster\Controllers\CBController\CbFormLoader;
8
use Crocodicstudio\Crudbooster\Controllers\CBController\CbIndexLoader;
9
use Crocodicstudio\Crudbooster\Controllers\CBController\CbLayoutLoader;
10
use Crocodicstudio\Crudbooster\Controllers\CBController\Deleter;
11
use Crocodicstudio\Crudbooster\Controllers\CBController\ExportData;
12
use Crocodicstudio\Crudbooster\Controllers\CBController\FormSubmitHandlers;
13
use Crocodicstudio\Crudbooster\Controllers\CBController\ImportData;
14
use Crocodicstudio\Crudbooster\Controllers\CBController\IndexAjax;
15
use Crocodicstudio\Crudbooster\Helpers\DbInspector;
16
use Illuminate\Support\Facades\DB;
17
use Crocodicstudio\Crudbooster\Helpers\CRUDBooster;
18
use Schema;
19
20
abstract class CBController extends Controller
21
{
22
    abstract public function cbInit();
23
24
    use Hooks;
25
    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...
26
27
    public $table = '';
28
29
    public $titleField = '';
30
31
    public $primaryKey = 'id';
32
33
    public $form = [];
34
35
    public $data = [];
36
37
    public $parent_field = null;
38
39
    public function cbView($template, $data)
40
    {
41
        $this->cbLayoutLoader();
42
        view()->share($this->data);
43
        return view($template, $data);
44
    }
45
46
    public function cbLoader()
47
    {
48
        $this->genericLoader();
49
        $this->cbLayoutLoader();
50
        $this->cbFormLoader();
51
        $this->cbIndexLoader();
52
        $this->checkHideForm();
53
54
        view()->share($this->data);
55
    }
56
57
    public function getIndex()
58
    {
59
        $this->genericLoader();
60
        $this->cbIndexLoader();
61
        $data = app(Index::class)->index($this);
62
63
        return $this->cbView('crudbooster::index.index', $data);
64
    }
65
66
    public function getUpdateSingle()
67
    {
68
        $table = request('table');
69
        $column = request('column');
70
        $value = request('value');
71
        $id = request('id');
72
        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

72
        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

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