Passed
Pull Request — master (#1093)
by Iman
06:16
created

CBController::postDoImportChunk()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
nc 2
nop 0
dl 0
loc 13
c 0
b 0
f 0
cc 3
rs 9.4285
1
<?php
2
3
namespace crocodicstudio\crudbooster\controllers;
4
5
error_reporting(E_ALL ^ E_NOTICE);
6
7
use crocodicstudio\crudbooster\CBCoreModule\RelationHandler;
8
use crocodicstudio\crudbooster\CBCoreModule\Hooks;
9
use crocodicstudio\crudbooster\CBCoreModule\Index;
10
use crocodicstudio\crudbooster\controllers\CBController\CbFormLoader;
11
use crocodicstudio\crudbooster\controllers\CBController\CbIndexLoader;
12
use crocodicstudio\crudbooster\controllers\CBController\CbLayoutLoader;
13
use crocodicstudio\crudbooster\controllers\CBController\Deleter;
14
use crocodicstudio\crudbooster\controllers\CBController\ExportData;
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 Illuminate\Support\Facades\PDF;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\PDF 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 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;
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 $addaction = [];
48
49
    //public $global_privilege = false;
50
51
    public $button_delete = true;
52
53
    public $button_action_style = 'button_icon';
54
55
    public $return_url = null;
56
57
    public $parent_field = null;
58
59
    public $parent_id = null;
60
61
    public $hide_form = [];
62
63
    public $index_return = false; //for export
64
65
    public function cbView($template, $data)
66
    {
67
        $this->cbLayoutLoader();
68
        view()->share($this->data);
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

68
        /** @scrutinizer ignore-call */ 
69
        view()->share($this->data);
Loading history...
69
        return view($template, $data);
70
    }
71
72
    public function cbLoader()
73
    {
74
        $this->cbInit();
75
76
        $this->cbLayoutLoader();
77
        $this->cbFormLoader();
78
        $this->cbIndexLoader();
79
        $this->checkHideForm();
80
        $this->genericLoader();
81
82
        view()->share($this->data);
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

82
        /** @scrutinizer ignore-call */ 
83
        view()->share($this->data);
Loading history...
83
    }
84
85
    private function checkHideForm()
86
    {
87
        if (! count($this->hide_form)) {
88
            return null;
89
        }
90
        foreach ($this->form as $i => $f) {
91
            if (in_array($f['name'], $this->hide_form)) {
92
                unset($this->form[$i]);
93
            }
94
        }
95
    }
96
97
    public function getIndex()
98
    {
99
        $index = app(Index::class);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

99
        $index = /** @scrutinizer ignore-call */ app(Index::class);
Loading history...
100
        $this->cbIndexLoader();
101
        $this->genericLoader();
102
        $data = $index->index($this);
103
104
        return $this->cbView('crudbooster::default.index', $data);
105
    }
106
107
    public function getUpdateSingle()
108
    {
109
        $table = request('table');
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

109
        $table = /** @scrutinizer ignore-call */ request('table');
Loading history...
110
        $column = request('column');
111
        $value = request('value');
112
        $id = request('id');
113
        DB::table($table)->where(CB::pk($table), $id)->update([$column => $value]);
114
115
        backWithMsg(cbTrans('alert_update_data_success'));
116
    }
117
118
    public function getAdd()
119
    {
120
        $page_title = cbTrans('add_data_page_title', ['module' => CB::getCurrentModule()->name]);
121
        $command = 'add';
122
        return $this->cbForm(compact('page_title', 'command'));
123
    }
124
125
    public function postAddSave()
126
    {
127
        $this->genericLoader();
128
129
        app(FormValidator::class)->validate(null, $this->form, $this->table);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

129
        /** @scrutinizer ignore-call */ 
130
        app(FormValidator::class)->validate(null, $this->form, $this->table);
Loading history...
130
        $this->inputAssignment();
131
132
        $this->setTimeStamps('created_at');
133
134
        $this->hookBeforeAdd($this->arr);
135
136
        $this->arr[$this->primary_key] = $id = $this->table()->insertGetId($this->arr);
137
        app(RelationHandler::class)->save($this->table, $id, $this->data_inputan);
138
139
        $this->hookAfterAdd($this->arr[$this->primary_key]);
140
141
        $this->insertLog('log_add', $this->arr[$this->title_field]);
142
143
        $this->sendResponseForSave('alert_add_data_success');
144
    }
145
146
    public function inputAssignment($id = null)
147
    {
148
        $hide_form = (request('hide_form')) ? unserialize(request('hide_form')) : [];
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

148
        $hide_form = (/** @scrutinizer ignore-call */ request('hide_form')) ? unserialize(request('hide_form')) : [];
Loading history...
149
150
        foreach ($this->form as $ro) {
151
            $name = $ro['name'];
152
            $type = $ro['type'] ?: 'text';
153
            $inputdata = request($name);
154
155
            if (! $name || $ro['exception']) {
156
                continue;
157
            }
158
159
            if (count($hide_form) && in_array($name, $hide_form)) {
160
                continue;
161
            }
162
163
            $hookPath = \CB::componentsPath($type).DIRECTORY_SEPARATOR.'hookInputAssignment.php';
164
            if (file_exists($hookPath)) {
165
                require_once($hookPath);
166
            }
167
            unset($hookPath);
168
169
            if (Request::hasFile($name)) {
170
                continue;
171
            }
172
173
            if ($inputdata == '' && CB::isColumnNULL($this->table, $name)) {
174
                continue;
175
            }
176
177
            $this->arr[$name] = '';
178
179
            if ($inputdata != '') {
180
                $this->arr[$name] = $inputdata;
181
            }
182
        }
183
    }
184
185
    /**
186
     * @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...
187
     * @return mixed
188
     */
189
    public function table($tableName = null)
190
    {
191
        $table = $tableName ?: $this->table;
192
        return \DB::table($table);
193
    }
194
195
    public function getEdit($id)
196
    {
197
        $row = $this->findRow($id)->first();
198
199
        $page_title = cbTrans("edit_data_page_title", ['module' => CB::getCurrentModule()->name, 'name' => $row->{$this->title_field}]);
200
        $command = 'edit';
201
        session()->put('current_row_id', $id);
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

201
        /** @scrutinizer ignore-call */ 
202
        session()->put('current_row_id', $id);
Loading history...
202
203
        return $this->cbForm(compact('id', 'row', 'page_title', 'command'));
204
    }
205
206
    /**
207
     * @param $id
208
     * @return mixed
209
     */
210
    public function findRow($id)
211
    {
212
        return $this->table()->where($this->primary_key, $id);
213
    }
214
215
    public function postEditSave($id)
216
    {
217
        $this->genericLoader();
218
219
        app(FormValidator::class)->validate($id, $this->form, $this->table);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

219
        /** @scrutinizer ignore-call */ 
220
        app(FormValidator::class)->validate($id, $this->form, $this->table);
Loading history...
220
        $this->inputAssignment($id);
221
222
        $this->setTimeStamps('updated_at');
223
224
        $this->hookBeforeEdit($this->arr, $id);
225
        $this->findRow($id)->update($this->arr);
226
        app(RelationHandler::class)->save($this->table, $id, $this->data_inputan);
227
228
        $this->hookAfterEdit($id);
229
230
        $this->insertLog('log_update', $this->arr[$this->title_field]);
231
232
        $this->sendResponseForSave('alert_update_data_success');
233
    }
234
235
    public function getDetail($id)
236
    {
237
        $row = $this->findRow($id)->first();
238
239
240
        $page_title = cbTrans('detail_data_page_title', ['module' => CB::getCurrentModule()->name, 'name' => $row->{$this->title_field}]);
241
        $command = 'detail';
242
243
        session()->put('current_row_id', $id);
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

243
        /** @scrutinizer ignore-call */ 
244
        session()->put('current_row_id', $id);
Loading history...
244
245
        return $this->cbForm(compact('row', 'page_title', 'command', 'id'));
246
    }
247
248
    public function actionButtonSelected($id_selected, $button_name)
249
    {
250
    }
251
252
    private function sendResponseForSave($msg)
253
    {
254
        $this->return_url = $this->return_url ?: request('return_url');
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

254
        $this->return_url = $this->return_url ?: /** @scrutinizer ignore-call */ request('return_url');
Loading history...
255
        if ($this->return_url) {
256
            if (request('submit') == cbTrans('button_save_more')) {
257
                CB::redirect(Request::server('HTTP_REFERER'), cbTrans($msg), 'success');
258
            }
259
            CB::redirect($this->return_url, cbTrans($msg), 'success');
260
        }
261
        if (request('submit') == cbTrans('button_save_more')) {
262
            CB::redirect(CB::mainpath('add'), cbTrans($msg), 'success');
263
        }
264
        CB::redirect(CB::mainpath(), cbTrans($msg), 'success');
265
    }
266
267
    private function insertLog($msg, $name)
268
    {
269
        CB::insertLog(trans('logging.'.$msg, ['module' => CB::getCurrentModule()->name, 'name' => $name]));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

269
        CB::insertLog(/** @scrutinizer ignore-call */ trans('logging.'.$msg, ['module' => CB::getCurrentModule()->name, 'name' => $name]));
Loading history...
270
    }
271
272
    private function setTimeStamps($col)
273
    {
274
        if (Schema::hasColumn($this->table, $col)) {
275
            $this->arr[$col] = date('Y-m-d H:i:s');
276
        }
277
    }
278
279
    /**
280
     * @param $data
281
     * @return mixed
282
     */
283
    private function cbForm($data)
284
    {
285
        $this->genericLoader();
286
        $this->cbFormLoader();
287
        return $this->cbView('crudbooster::default.form', $data);
288
    }
289
290
    private function genericLoader()
291
    {
292
        $this->primary_key = CB::pk($this->table);
293
        $this->columns_table = $this->col;
294
        $this->data_inputan = $this->form;
295
        $this->data['pk'] = $this->primary_key;
296
        $this->data['forms'] = $this->data_inputan;
297
        $this->data['hide_form'] = $this->hide_form;
298
        $this->data['table'] = $this->table;
299
        $this->data['title_field'] = $this->title_field;
300
        $this->data['appname'] = cbGetsetting('appname');
301
        $this->data['index_button'] = $this->index_button;
302
        $this->data['button_delete'] = $this->button_delete;
303
        $this->data['sub_module'] = $this->sub_module;
304
        $this->data['parent_field'] = (request('parent_field')) ?: $this->parent_field;
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

304
        $this->data['parent_field'] = (/** @scrutinizer ignore-call */ request('parent_field')) ?: $this->parent_field;
Loading history...
305
        $this->data['parent_id'] = (request('parent_id')) ?: $this->parent_id;
306
307
        if (CB::getCurrentMethod() == 'getProfile') {
308
            session()->put('current_row_id', CB::myId());
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

308
            /** @scrutinizer ignore-call */ 
309
            session()->put('current_row_id', CB::myId());
Loading history...
309
            $this->data['return_url'] = Request::fullUrl();
310
        }
311
    }
312
}