Completed
Push — master ( 7eba8f...c52d76 )
by Iman
18s queued 10s
created

CBController::postAddSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
nc 1
nop 0
dl 0
loc 19
c 0
b 0
f 0
cc 1
rs 9.4285

2 Methods

Rating   Name   Duplication   Size   Complexity  
A CBController::getEdit() 0 9 1
A CBController::table() 0 4 2
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 Illuminate\Support\Facades\Request;
18
use Illuminate\Support\Facades\DB;
19
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...
20
use CB;
21
use Schema;
22
23
class CBController extends Controller
24
{
25
    use Hooks;
26
    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...
27
28
    public $data_inputan;
29
30
    public $module_name;
31
32
    public $table;
33
34
    public $title_field;
35
36
    public $primary_key = 'id';
37
38
    public $arr = [];
39
40
    public $col = [];
41
42
    public $form = [];
43
44
    public $data = [];
45
46
    public $addaction = [];
47
48
    //public $global_privilege = false;
49
50
    public $button_delete = true;
51
52
    public $button_action_style = 'button_icon';
53
54
    public $return_url = null;
55
56
    public $parent_field = null;
57
58
    public $parent_id = null;
59
60
    public $hide_form = [];
61
62
    public $index_return = false; //for export
63
64
    public function cbView($template, $data)
65
    {
66
        $this->cbLayoutLoader();
67
        view()->share($this->data);
68
        return view($template, $data);
69
    }
70
71
    public function cbLoader()
72
    {
73
        $this->cbInit();
0 ignored issues
show
Bug introduced by
The method cbInit() does not exist on crocodicstudio\crudboost...ontrollers\CBController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

73
        $this->/** @scrutinizer ignore-call */ 
74
               cbInit();
Loading history...
74
75
        $this->cbLayoutLoader();
76
        $this->cbFormLoader();
77
        $this->cbIndexLoader();
78
        $this->checkHideForm();
79
        $this->genericLoader();
80
81
        view()->share($this->data);
82
    }
83
84
    private function checkHideForm()
85
    {
86
        if (! count($this->hide_form)) {
87
            return null;
88
        }
89
        foreach ($this->form as $i => $f) {
90
            if (in_array($f['name'], $this->hide_form)) {
91
                unset($this->form[$i]);
92
            }
93
        }
94
    }
95
96
    public function getIndex()
97
    {
98
        $index = app(Index::class);
99
        $this->cbIndexLoader();
100
        $this->genericLoader();
101
        $data = $index->index($this);
102
103
        return $this->cbView('crudbooster::default.index', $data);
104
    }
105
106
    public function getUpdateSingle()
107
    {
108
        $table = request('table');
109
        $column = request('column');
110
        $value = request('value');
111
        $id = request('id');
112
        DB::table($table)->where(CB::pk($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\Database\Connection::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

112
        DB::table(/** @scrutinizer ignore-type */ $table)->where(CB::pk($table), $id)->update([$column => $value]);
Loading history...
113
114
        backWithMsg(cbTrans('alert_update_data_success'));
115
    }
116
117
    public function getAdd()
118
    {
119
        $page_title = cbTrans('add_data_page_title', ['module' => CB::getCurrentModule()->name]);
120
        $command = 'add';
121
        return $this->cbForm(compact('page_title', 'command'));
122
    }
123
124
125
    /**
126
     * @param null $tableName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $tableName is correct as it would always require null to be passed?
Loading history...
127
     * @return mixed
128
     */
129
    public function table($tableName = null)
130
    {
131
        $table = $tableName ?: $this->table;
132
        return \DB::table($table);
133
    }
134
135
    public function getEdit($id)
136
    {
137
        $row = $this->findRow($id)->first();
138
139
        $page_title = cbTrans("edit_data_page_title", ['module' => CB::getCurrentModule()->name, 'name' => $row->{$this->title_field}]);
140
        $command = 'edit';
141
        session()->put('current_row_id', $id);
142
143
        return $this->cbForm(compact('id', 'row', 'page_title', 'command'));
144
    }
145
146
    /**
147
     * @param $id
148
     * @return mixed
149
     */
150
    public function findRow($id)
151
    {
152
        return $this->table()->where($this->primary_key, $id);
153
    }
154
155
    public function getDetail($id)
156
    {
157
        $row = $this->findRow($id)->first();
158
159
160
        $page_title = cbTrans('detail_data_page_title', ['module' => CB::getCurrentModule()->name, 'name' => $row->{$this->title_field}]);
161
        $command = 'detail';
162
163
        session()->put('current_row_id', $id);
164
165
        return $this->cbForm(compact('row', 'page_title', 'command', 'id'));
166
    }
167
168
    public function actionButtonSelected($id_selected, $button_name)
169
    {
170
    }
171
172
    private function insertLog($msg, $name)
173
    {
174
        CB::insertLog(trans('logging.'.$msg, ['module' => CB::getCurrentModule()->name, 'name' => $name]));
175
    }
176
177
    /**
178
     * @param $data
179
     * @return mixed
180
     */
181
    private function cbForm($data)
182
    {
183
        $this->genericLoader();
184
        $this->cbFormLoader();
185
        return $this->cbView('crudbooster::default.form', $data);
186
    }
187
188
    private function genericLoader()
189
    {
190
        $this->primary_key = CB::pk($this->table);
191
        $this->columns_table = $this->col;
192
        $this->data_inputan = $this->form;
193
        $this->data['pk'] = $this->primary_key;
194
        $this->data['forms'] = $this->data_inputan;
195
        $this->data['hide_form'] = $this->hide_form;
196
        $this->data['table'] = $this->table;
197
        $this->data['title_field'] = $this->title_field;
198
        $this->data['appname'] = cbGetsetting('appname');
199
        $this->data['index_button'] = $this->index_button;
200
        $this->data['button_delete'] = $this->button_delete;
201
        $this->data['sub_module'] = $this->sub_module;
202
        $this->data['parent_field'] = (request('parent_field')) ?: $this->parent_field;
203
        $this->data['parent_id'] = (request('parent_id')) ?: $this->parent_id;
204
205
        if (CB::getCurrentMethod() == 'getProfile') {
206
            session()->put('current_row_id', CB::myId());
207
            $this->data['return_url'] = Request::fullUrl();
208
        }
209
    }
210
}