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

FormSubmitHandlers::postEditSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 17
rs 9.4285
1
<?php
2
3
namespace crocodicstudio\crudbooster\controllers\CBController;
4
5
use CB;
6
use crocodicstudio\crudbooster\controllers\FormValidator;
7
use Illuminate\Support\Facades\Request;
8
use crocodicstudio\crudbooster\CBCoreModule\RelationHandler;
9
use Illuminate\Support\Facades\Schema;
10
11
trait FormSubmitHandlers
12
{
13
    public function inputAssignment($id = null)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

13
    public function inputAssignment(/** @scrutinizer ignore-unused */ $id = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
    {
15
        $hide_form = (request('hide_form')) ? unserialize(request('hide_form')) : [];
0 ignored issues
show
Bug introduced by
It seems like request('hide_form') can also be of type array; however, parameter $str of unserialize() 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

15
        $hide_form = (request('hide_form')) ? unserialize(/** @scrutinizer ignore-type */ request('hide_form')) : [];
Loading history...
16
17
        foreach ($this->form as $form) {
18
            $name = $form['name'];
19
            $type = $form['type'] ?: 'text';
20
            $inputdata = request($name);
21
22
            if (! $name || in_array($name, $hide_form) || $form['exception']) {
23
                continue;
24
            }
25
26
            $hookPath = \CB::componentsPath($type).DIRECTORY_SEPARATOR.'hookInputAssignment.php';
27
            if (file_exists($hookPath)) {
28
                require_once($hookPath);
29
            }
30
            unset($hookPath);
31
32
            if (Request::hasFile($name)) {
33
                continue;
34
            }
35
36
            if ($inputdata == '' && CB::isColumnNULL($this->table, $name)) {
37
                continue;
38
            }
39
40
            $this->arr[$name] = '';
0 ignored issues
show
Bug Best Practice introduced by
The property arr does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
42
            if ($inputdata != '') {
43
                $this->arr[$name] = $inputdata;
44
            }
45
        }
46
    }
47
48
49
    public function postAddSave()
50
    {
51
        $this->genericLoader();
0 ignored issues
show
Bug introduced by
It seems like genericLoader() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

51
        $this->/** @scrutinizer ignore-call */ 
52
               genericLoader();
Loading history...
52
53
        app(FormValidator::class)->validate(null, $this->form, $this->table);
54
        $this->inputAssignment();
55
56
        $this->setTimeStamps('created_at');
57
58
        $this->hookBeforeAdd($this->arr);
0 ignored issues
show
Bug introduced by
It seems like hookBeforeAdd() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

58
        $this->/** @scrutinizer ignore-call */ 
59
               hookBeforeAdd($this->arr);
Loading history...
59
        $id = $this->table()->insertGetId($this->arr);
0 ignored issues
show
Bug introduced by
It seems like table() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

59
        $id = $this->/** @scrutinizer ignore-call */ table()->insertGetId($this->arr);
Loading history...
60
        app(RelationHandler::class)->save($this->table, $id, $this->data_inputan);
61
        $this->hookAfterAdd($id);
0 ignored issues
show
Bug introduced by
It seems like hookAfterAdd() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

61
        $this->/** @scrutinizer ignore-call */ 
62
               hookAfterAdd($id);
Loading history...
62
63
        $this->insertLog('log_add', $id. ' on ' . $this->table);
0 ignored issues
show
Bug introduced by
It seems like insertLog() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

63
        $this->/** @scrutinizer ignore-call */ 
64
               insertLog('log_add', $id. ' on ' . $this->table);
Loading history...
64
65
        $this->sendResponseForSave('alert_add_data_success');
66
    }
67
68
    public function postEditSave($id)
69
    {
70
        $this->genericLoader();
71
72
        app(FormValidator::class)->validate($id, $this->form, $this->table);
73
        $this->inputAssignment($id);
74
75
        $this->setTimeStamps('updated_at');
76
77
        $this->hookBeforeEdit($this->arr, $id);
0 ignored issues
show
Bug introduced by
It seems like hookBeforeEdit() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

77
        $this->/** @scrutinizer ignore-call */ 
78
               hookBeforeEdit($this->arr, $id);
Loading history...
78
        $this->findRow($id)->update($this->arr);
0 ignored issues
show
Bug introduced by
It seems like findRow() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

78
        $this->/** @scrutinizer ignore-call */ 
79
               findRow($id)->update($this->arr);
Loading history...
79
        app(RelationHandler::class)->save($this->table, $id, $this->data_inputan);
80
        $this->hookAfterEdit($id);
0 ignored issues
show
Bug introduced by
It seems like hookAfterEdit() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

80
        $this->/** @scrutinizer ignore-call */ 
81
               hookAfterEdit($id);
Loading history...
81
82
        $this->insertLog('log_update', $id. ' on ' . $this->table);
83
84
        $this->sendResponseForSave('alert_update_data_success');
85
    }
86
87
    private function sendResponseForSave($msg)
88
    {
89
        $this->return_url = $this->return_url ?: request('return_url');
0 ignored issues
show
Bug Best Practice introduced by
The property return_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
90
        if ($this->return_url) {
91
            if (request('submit') == cbTrans('button_save_more')) {
92
                CB::redirect(Request::server('HTTP_REFERER'), cbTrans($msg), 'success');
93
            }
94
            CB::redirect($this->return_url, cbTrans($msg), 'success');
95
        }
96
        if (request('submit') == cbTrans('button_save_more')) {
97
            CB::redirect(CB::mainpath('add'), cbTrans($msg), 'success');
98
        }
99
        CB::redirect(CB::mainpath(), cbTrans($msg), 'success');
100
    }
101
102
    private function setTimeStamps($col)
103
    {
104
        if (Schema::hasColumn($this->table, $col)) {
105
            $this->arr[$col] = date('Y-m-d H:i:s');
0 ignored issues
show
Bug Best Practice introduced by
The property arr does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
106
        }
107
    }
108
}