Completed
Push — master ( 0222da...59a7ac )
by Eliurkis
02:18
created

CrudDataTable::indexDataTableResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 15
nc 1
nop 1
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 indexDataTable(Request $request)
13
    {
14
        if ($request->ajax() || $request->wantsJson()) {
15
            return $this->indexDataTableResults($request);
16
        }
17
18
        return view('crud::list-datatable')
19
            ->with('rows', [])
20
            ->with('fields', $this->fields)
0 ignored issues
show
Bug introduced by Eliurkis Diaz
The property fields 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...
21
            ->with('columns', $this->columns)
0 ignored issues
show
Bug introduced by Eliurkis Diaz
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...
22
            ->with('searchable', $this->searchable)
0 ignored issues
show
Bug introduced by Eliurkis Diaz
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...
23
            ->with('buttons', $this->buttons)
0 ignored issues
show
Bug introduced by Eliurkis Diaz
The property buttons 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...
24
            ->with('paginate', $this->paginate)
0 ignored issues
show
Bug introduced by Eliurkis Diaz
The property paginate 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...
25
            ->with('t', $this->texts)
0 ignored issues
show
Bug introduced by Eliurkis Diaz
The property texts 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...
26
            ->with('htmlFilters', $this->htmlFilters)
0 ignored issues
show
Bug introduced by Eliurkis Diaz
The property htmlFilters 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...
27
            ->with('listDisplay', $this->listDisplay)
0 ignored issues
show
Bug introduced by Eliurkis Diaz
The property listDisplay 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...
28
            ->with('request', $request)
29
            ->with('orderBy', $this->orderBy)
0 ignored issues
show
Bug introduced by Eliurkis Diaz
The property orderBy 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...
30
            ->with('route', $this->route);
0 ignored issues
show
Bug introduced by Eliurkis Diaz
The property route 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...
31
    }
32
33
    public function indexDataTableResults(Request $request)
34
    {
35
        list($colSortBy, $colOrderBy) = $this->getSortInformation($this->columns, $request);
36
        list($totalRows, $totalRowsFiltered) = $this->getRowsTotals($request->get('search')['value'] ?? null);
37
38
        $query = $this->entity->orderBy($colSortBy, $colOrderBy);
0 ignored issues
show
Bug introduced by Eliurkis Diaz
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...
39
        $query = $this->applySearchScope($query, $request->get('search')['value'] ?? null);
40
41
        $rows = $query->offset($request->get('start') ?? 0)
42
            ->limit($request->get('length') ?? $totalRows)
43
            ->get();
44
45
        return response()->json([
46
            'data'            => $rows,
47
            'draw'            => (int) ($request->get('draw') ?? 0),
48
            'recordsFiltered' => $totalRowsFiltered,
49
            'recordsTotal'    => $totalRows,
50
            'colSortBy'       => $colSortBy,
51
            'colOrderBy'      => $colOrderBy,
52
        ]);
53
    }
54
55
    protected function getSortInformation($cols, $request)
56
    {
57
        return [
58
            $cols[$request->get('order')[0]['column'] ?? 0],
59
            $request->get('order')[0]['dir'] ?? 'asc',
60
        ];
61
    }
62
63
    protected function getRowsTotals($searchValue = null)
64
    {
65
        $totalRows = $totalRowsFiltered = $this->getRowsTotal();
66
67
        if ($searchValue != '' && $this->searchable) {
68
            $totalRowsFiltered = $this->getRowsTotal($searchValue);
69
        }
70
71
        return [$totalRows, $totalRowsFiltered];
72
    }
73
74
    protected function getRowsTotal($searchValue = null)
75
    {
76
        $query = $this->entity->select(DB::raw('count(*) as total'));
77
        $query = $this->applySearchScope($query, $searchValue);
78
79
        return $query->first()
80
            ->total;
81
    }
82
83
    protected function applySearchScope($query, $searchValue = null)
84
    {
85
        if ($searchValue == '' || !$this->searchable) {
86
            return $query;
87
        }
88
89
        $searchable = $this->searchable;
90
91
        return $query->where(function ($query) use ($searchValue, $searchable) {
92
            foreach ($searchable as $key => $field) {
93
                $query = $key === 0
94
                    ? $query->where($field, 'like', '%'.$searchValue.'%')
95
                    : $query->orWhere($field, 'like', '%'.$searchValue.'%');
96
97
            }
98
            return $query;
99
        });
100
    }
101
}
102