Passed
Push — master ( 1cde08...5efba4 )
by Iman
07:34 queued 03:12
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
cc 4
eloc 8
nc 2
nop 2
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
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
        $foreignKeyName = request('fk_name');
17
        $foreignKeyValue = request('fk_value');
18
        if (! $table || ! $label || ! $foreignKeyName || ! $foreignKeyValue) {
19
            return response()->json([]);
0 ignored issues
show
Bug introduced by
The method json() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

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

19
            return response()->/** @scrutinizer ignore-call */ json([]);
Loading history...
20
        }
21
        $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\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

21
        $query = DB::table(/** @scrutinizer ignore-type */ $table);
Loading history...
22
        if ($datatableWhere) {
23
            $query->whereRaw($datatableWhere);
24
        }
25
        $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

25
        $query->select('id as select_value', /** @scrutinizer ignore-type */ $label.' as select_label');
Loading history...
26
        $query->where($foreignKeyName, $foreignKeyValue);
27
        $query->orderby($label, 'asc');
0 ignored issues
show
Bug introduced by
It seems like $label can also be of type array; however, parameter $column of Illuminate\Database\Query\Builder::orderBy() 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

27
        $query->orderby(/** @scrutinizer ignore-type */ $label, 'asc');
Loading history...
28
29
        return response()->json($query->get());
30
    }
31
32
    public function getDataModalDatatable()
33
    {
34
        $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

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

57
        $query = Cache::get(/** @scrutinizer ignore-type */ $key);
Loading history...
58
59
        $fk_name = request('fk_name');
60
        $fk_value = request('fk_value');
61
62
        $condition = ' where ';
63
        if (strpos(strtolower($query), 'where') !== false) {
0 ignored issues
show
Bug introduced by
$query of type Illuminate\Contracts\Cache\Repository is incompatible with the type string expected by parameter $str of strtolower(). ( Ignorable by Annotation )

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

63
        if (strpos(strtolower(/** @scrutinizer ignore-type */ $query), 'where') !== false) {
Loading history...
64
            $condition = ' and ';
65
        }
66
67
        if (strpos(strtolower($query), 'order by')) {
68
            $query = str_replace('ORDER BY', 'order by', $query);
69
            $qraw = explode('order by', $query);
70
            $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

70
            $query = $qraw[0].$condition./** @scrutinizer ignore-type */ $fk_name." = '$fk_value' $qraw[1]";
Loading history...
71
        } else {
72
            $query .= $condition.$fk_name." = '$fk_value'";
73
        }
74
75
        $query = DB::select(DB::raw($query));
76
77
        return response()->json(['items' => $query]);
78
    }
79
80
    public function postFindData()
81
    {
82
        $items = app(Search::class)->searchData(request('data'), request('q'), request('id'));
83
84
        return response()->json(['items' => $items]);
85
    }
86
87
    /**
88
     * @param $result
89
     * @param $columns
90
     */
91
    private function applyQ($result, $columns)
92
    {
93
        if (request('q')) {
94
            return;
95
        }
96
        $result->where(function ($where) use ($columns) {
97
            foreach ($columns as $c => $col) {
98
                if ($c == 0) {
99
                    $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

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