Completed
Push — master ( 914f3a...0222da )
by Eliurkis
01:29
created

CrudDataTable::getSortInformation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Eliurkis\Crud;
4
5
use DB;
6
use Illuminate\Http\Request;
7
8
trait CrudDataTable
9
{
10
    protected $dataTableActivated = true;
11
12
    public function index_source(Request $request)
13
    {
14
        list($colSortBy, $colOrderBy) = $this->getSortInformation($this->columns, $request);
0 ignored issues
show
Bug introduced by
The property columns does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
        list($totalRows, $totalRowsFiltered) = $this->getRowsTotals($request->get('search')['value'] ?? null);
16
17
        $query = $this->entity->orderBy($colSortBy, $colOrderBy);
0 ignored issues
show
Bug introduced by
The property entity does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
        $query = $this->applySearchScope($query, $request->get('search')['value'] ?? null);
19
20
        $rows = $query->offset($request->get('start') ?? 0)
21
            ->limit($request->get('length') ?? $totalRows)
22
            ->get();
23
24
        return response()->json([
25
            'data'            => $rows,
26
            'draw'            => (int) ($request->get('draw') ?? 0),
27
            'recordsFiltered' => $totalRowsFiltered,
28
            'recordsTotal'    => $totalRows,
29
            'colSortBy'       => $colSortBy,
30
            'colOrderBy'      => $colOrderBy,
31
        ]);
32
    }
33
34
    protected function getSortInformation($cols, $request)
35
    {
36
        return [
37
            $cols[$request->get('order')[0]['column'] ?? 0],
38
            $request->get('order')[0]['dir'] ?? 'asc',
39
        ];
40
    }
41
42
    protected function getRowsTotals($searchValue = null)
43
    {
44
        $totalRows = $totalRowsFiltered = $this->getRowsTotal();
45
46
        if ($searchValue != '' && $this->searchable) {
0 ignored issues
show
Bug introduced by
The property searchable does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
            $totalRowsFiltered = $this->getRowsTotal($searchValue);
48
        }
49
50
        return [$totalRows, $totalRowsFiltered];
51
    }
52
53
    protected function getRowsTotal($searchValue = null)
54
    {
55
        $query = $this->entity->select(DB::raw('count(*) as total'));
56
        $query = $this->applySearchScope($query, $searchValue);
57
58
        return $query->first()
59
            ->total;
60
    }
61
62
    protected function applySearchScope($query, $searchValue = null)
63
    {
64
        if ($searchValue == '' || !$this->searchable) {
65
            return $query;
66
        }
67
68
        $searchable = $this->searchable;
69
70
        return $query->where(function ($query) use ($searchValue, $searchable) {
71
            foreach ($searchable as $key => $field) {
72
                $query = $key === 0
73
                    ? $query->where($field, 'like', '%'.$searchValue.'%')
74
                    : $query->orWhere($field, 'like', '%'.$searchValue.'%');
75
76
            }
77
            return $query;
78
        });
79
    }
80
}
81