Passed
Pull Request — master (#1275)
by
unknown
02:44
created

IndexAjax::applyQ()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 2
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\Controllers\CBController;
4
5
use Crocodicstudio\Crudbooster\CBCoreModule\Search;
6
use Illuminate\Support\Facades\Cache;
7
use Illuminate\Support\Facades\DB;
8
9
trait IndexAjax
10
{
11
    public function getDataTable()
12
    {
13
        $table = request('table');
14
        $label = request('label');
15
        $datatableWhere = urldecode(request('datatable_where'));
0 ignored issues
show
Bug introduced by
It seems like request('datatable_where') can also be of type array; however, parameter $str of urldecode() 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
        $datatableWhere = urldecode(/** @scrutinizer ignore-type */ request('datatable_where'));
Loading history...
16
		$datatableFormat = urldecode(Request::get('datatable_format'));
0 ignored issues
show
Bug introduced by
The type Crocodicstudio\Crudboost...rs\CBController\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
17
        $foreignKeyName = request('fk_name');
18
        $foreignKeyValue = request('fk_value');
19
        if (! $table || ! $label || ! $foreignKeyName || ! $foreignKeyValue) {
20
            return response()->json([]);
21
        }
22
        $query = DB::table($table);
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

22
        $query = DB::table(/** @scrutinizer ignore-type */ $table);
Loading history...
23
        if ($datatableWhere) {
24
            $query->whereRaw($datatableWhere);
25
        }
26
		if($datatableFormat) {
27
			$label = $datatableFormat;
28
			$query->select(DB::raw('id as select_value, CONCAT('.$label.') as select_label'));
29
		} else {
30
			$query->select('id as select_value',$label.' as select_label');
0 ignored issues
show
Bug introduced by
Are you sure $label of type Illuminate\Http\Request|string|array can be used in concatenation? ( Ignorable by Annotation )

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

30
			$query->select('id as select_value',/** @scrutinizer ignore-type */ $label.' as select_label');
Loading history...
31
		}
32
        $query->where($foreignKeyName, $foreignKeyValue);
33
        $query->orderby('select_label', 'asc');
34
35
        return response()->json($query->get());
36
    }
37
38
    public function getDataModalDatatable()
39
    {
40
        $data = base64_decode(json_decode(request('data'), true));
0 ignored issues
show
Bug introduced by
It seems like request('data') can also be of type array; however, parameter $json of json_decode() 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

40
        $data = base64_decode(json_decode(/** @scrutinizer ignore-type */ request('data'), true));
Loading history...
41
42
        $columns = explode(',', $data['columns']);
43
44
        $result = DB::table($data['table']);
45
        $this->applyQ($result, $columns);
46
47
        if ($data['sql_where']) {
48
            $result->whereraw($data['sql_where']);
49
        }
50
51
        $this->applyOrderBy($data, $result);
52
        $limit = ($data['limit']) ?: 6;
53
54
        return view('crudbooster::form.type_components.datamodal.browser', ['result' => $result->paginate($limit), 'data' => $data]);
55
    }
56
57
    public function getDataQuery()
58
    {
59
        $key = request('query');
60
        if (! Cache::has($key)) {
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type array; however, parameter $key of Illuminate\Support\Facades\Cache::has() 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

60
        if (! Cache::has(/** @scrutinizer ignore-type */ $key)) {
Loading history...
61
            return response()->json(['items' => []]);
62
        }
63
        $query = Cache::get($key);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type array; however, parameter $key of Illuminate\Support\Facades\Cache::get() 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

63
        $query = Cache::get(/** @scrutinizer ignore-type */ $key);
Loading history...
64
65
        $fk_name = request('fk_name');
66
        $fk_value = request('fk_value');
67
68
        $condition = ' where ';
69
        if (strpos(strtolower($query), 'where') !== false) {
70
            $condition = ' and ';
71
        }
72
73
        if (strpos(strtolower($query), 'order by')) {
74
            $query = str_replace('ORDER BY', 'order by', $query);
75
            $qraw = explode('order by', $query);
76
            $query = $qraw[0].$condition.$fk_name." = '$fk_value' $qraw[1]";
0 ignored issues
show
Bug introduced by
Are you sure $fk_name of type Illuminate\Http\Request|string|array can be used in concatenation? ( Ignorable by Annotation )

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

76
            $query = $qraw[0].$condition./** @scrutinizer ignore-type */ $fk_name." = '$fk_value' $qraw[1]";
Loading history...
77
        } else {
78
            $query .= $condition.$fk_name." = '$fk_value'";
79
        }
80
81
        $query = DB::select(DB::raw($query));
82
83
        return response()->json(['items' => $query]);
84
    }
85
86
    public function postFindData()
87
    {
88
        $items = app(Search::class)->searchData(request('data'), request('q'), request('id'));
89
90
        return response()->json(['items' => $items]);
91
    }
92
93
    /**
94
     * @param $result
95
     * @param $columns
96
     */
97
    private function applyQ($result, $columns)
98
    {
99
        if (request('q')) {
100
            return;
101
        }
102
        $result->where(function ($where) use ($columns) {
103
            foreach ($columns as $c => $col) {
104
                if ($c == 0) {
105
                    $where->where($col, 'like', '%'.request('q').'%');
0 ignored issues
show
Bug introduced by
Are you sure request('q') of type Illuminate\Http\Request|string|array can be used in concatenation? ( Ignorable by Annotation )

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

105
                    $where->where($col, 'like', '%'./** @scrutinizer ignore-type */ request('q').'%');
Loading history...
106
                } else {
107
                    $where->orWhere($col, 'like', '%'.request('q').'%');
108
                }
109
            }
110
        });
111
    }
112
113
    /**
114
     * @param $data
115
     * @param $result
116
     */
117
    private function applyOrderBy($data, $result)
118
    {
119
        if ($data['sql_orderby']) {
120
            $result->orderByRaw($data['sql_orderby']);
121
        } else {
122
            $result->orderBy($data['column_value'], 'desc');
123
        }
124
    }
125
}