Passed
Push — master ( 87ef5c...f1a551 )
by Iman
07:54 queued 03:46
created

CBController::findFirst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Request;
19
use Illuminate\Support\Facades\DB;
20
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...
21
use CB;
22
use Schema;
23
24
class CBController extends Controller
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...
introduced by
The trait crocodicstudio\crudboost...ontroller\CbIndexLoader requires some properties which are not provided by crocodicstudio\crudboost...ontrollers\CBController: $buttonExport, $buttonImport
Loading history...
28
29
    public $data_inputan;
30
31
    public $module_name;
32
33
    public $table;
34
35
    public $title_field;
36
37
    public $primary_key = 'id';
38
39
    public $arr = [];
40
41
    public $col = [];
42
43
    public $form = [];
44
45
    public $data = [];
46
47
    //public $global_privilege = false;
48
49
    public $deleteBtn = true;
50
51
    public $button_action_style = 'button_icon';
52
53
    public $return_url = null;
54
55
    public $parent_field = null;
56
57
    public $parent_id = null;
58
59
    public $hide_form = [];
60
61
    public $index_return = false; //for export
62
63
    public function cbView($template, $data)
64
    {
65
        $this->cbLayoutLoader();
66
        view()->share($this->data);
67
        return view($template, $data);
68
    }
69
70
    public function cbLoader()
71
    {
72
        $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

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

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