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

CrudDataTable   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 94
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A indexDataTable() 0 20 3
A indexDataTableResults() 0 21 1
A getSortInformation() 0 7 1
A getRowsTotals() 0 10 3
A getRowsTotal() 0 8 1
B applySearchScope() 0 18 5
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
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
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
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
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
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
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
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
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
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
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
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