Completed
Push — master ( 20f8c7...40c849 )
by Eliurkis
01:38
created

src/CrudDataTable.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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)
21
            ->with('columns', $this->columns)
22
            ->with('searchable', $this->searchable)
23
            ->with('buttons', $this->buttons)
24
            ->with('paginate', $this->paginate)
25
            ->with('t', $this->texts)
26
            ->with('htmlFilters', $this->htmlFilters)
27
            ->with('listDisplay', $this->listDisplay)
28
            ->with('links', $this->prepareLinks())
0 ignored issues
show
It seems like prepareLinks() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
            ->with('request', $request)
30
            ->with('orderBy', $this->orderBy)
31
            ->with('route', $this->route);
32
    }
33
34
    public function indexDataTableResults(Request $request)
35
    {
36
        list($colSortBy, $colOrderBy) = $this->getSortInformation($this->columns, $request);
37
        list($totalRows, $totalRowsFiltered) = $this->getRowsTotals($request->get('search')['value'] ?? null);
38
39
        $query = $this->entity->orderBy($colSortBy, $colOrderBy);
40
        $query = $this->applySearchScope($query, $request->get('search')['value'] ?? null);
41
42
        $rows = $query->offset($request->get('start') ?? 0)
43
            ->limit($request->get('length') ?? $totalRows)
44
            ->get();
45
46
        return response()->json([
47
            'data'            => $rows,
48
            'draw'            => (int) ($request->get('draw') ?? 0),
49
            'recordsFiltered' => $totalRowsFiltered,
50
            'recordsTotal'    => $totalRows,
51
            'colSortBy'       => $colSortBy,
52
            'colOrderBy'      => $colOrderBy,
53
        ]);
54
    }
55
56
    protected function getSortInformation($cols, $request)
57
    {
58
        return [
59
            $cols[$request->get('order')[0]['column'] ?? 0],
60
            $request->get('order')[0]['dir'] ?? 'asc',
61
        ];
62
    }
63
64
    protected function getRowsTotals($searchValue = null)
65
    {
66
        $totalRows = $totalRowsFiltered = $this->getRowsTotal();
67
68
        if ($searchValue != '' && $this->searchable) {
69
            $totalRowsFiltered = $this->getRowsTotal($searchValue);
70
        }
71
72
        return [$totalRows, $totalRowsFiltered];
73
    }
74
75
    protected function getRowsTotal($searchValue = null)
76
    {
77
        $query = $this->entity->select(DB::raw('count(*) as total'));
78
        $query = $this->applySearchScope($query, $searchValue);
79
80
        return $query->first()
81
            ->total;
82
    }
83
84
    protected function applySearchScope($query, $searchValue = null)
85
    {
86
        if ($searchValue == '' || !$this->searchable) {
87
            return $query;
88
        }
89
90
        $searchable = $this->searchable;
91
92
        return $query->where(function ($query) use ($searchValue, $searchable) {
93
            foreach ($searchable as $key => $field) {
94
                $query = $key === 0
95
                    ? $query->where($field, 'like', '%'.$searchValue.'%')
96
                    : $query->orWhere($field, 'like', '%'.$searchValue.'%');
97
98
            }
99
            return $query;
100
        });
101
    }
102
}
103