Completed
Pull Request — master (#82)
by Brandon
02:15
created

SensorDataTable::dataTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 14
cp 0
rs 9.4285
cc 1
eloc 11
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\DataTables;
4
5
use Yajra\DataTables\Services\DataTable;
6
use App\Sensor;
7
8
class SensorDataTable extends DataTable
9
{
10
    /**
11
     * Build DataTable class.
12
     *
13
     * @return \Yajra\DataTables\Engines\BaseEngine
14
     */
15
    public function dataTable($query)
16
    {
17
        return datatables($query)
18
            ->editColumn('device_id', function ($sensor) {
19
                return '<a href="/device/' . $sensor->device_id . '">'. ($sensor->device->name ?? '') . '</a>';
20
            })
21
            ->editColumn('name', function ($sensor) {
22
                return '<a href="/sensor/' . $sensor->id . '">'. $sensor->name . '</a>';
23
            })
24
            ->addColumn('value', function ($sensor) {
25
                return $sensor->latest_data->value;
26
            })
27
            ->addColumn('action', 'sensor.action')
28
            ->blacklist([ 'action', 'value'])
29
            ->rawColumns(['device_id', 'name', 'action']);
30
    }
31
32
    /**
33
     * Get the query object to be processed by dataTables.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
36
     */
37
    public function query()
38
    {
39
        $query = Sensor::query();
40
        return $this->applyScopes($query);
41
    }
42
43
    /**
44
     * Optional method if you want to use html builder.
45
     *
46
     * @return \Yajra\DataTables\Html\Builder
47
     */
48
    public function html()
49
    {
50
        return $this->builder()
51
            ->columns($this->getColumns())
52
            ->minifiedAjax()
53
            ->parameters($this->getBuilderParameters());
54
    }
55
56
    /**
57
     * Get columns.
58
     *
59
     * @return array
60
     */
61 View Code Duplication
    protected function getColumns()
62
    {
63
        return [
64
            'id',
65
            [ 'name' => 'device_id', 'data' => 'device_id', 'title' => 'Device', 'render' => null, 'searchable' => true, 'orderable' => false, 'exportable' => true, 'printable' => true, 'footer' => '' ],
66
            'name',
67
            'type',
68
            'value',
69
            'action'
70
        ];
71
    }
72
73
    /**
74
     * Get builder parameters.
75
     *
76
     * @return array
77
     */
78 View Code Duplication
    protected function getBuilderParameters()
79
    {
80
        return [
81
            'dom'     => 'Bfrtip',
82
            'order'   => [ [ 0, 'desc' ] ],
83
            'buttons' => [
84
                'create',
85
                'export',
86
                'print',
87
                'reset',
88
                'reload',
89
            ],
90
            'paging' => true,
91
            'searching' => true,
92
            'info' => true,
93
            'searchDelay' => 500,
94
        ];
95
    }
96
97
    /**
98
     * Get filename for export.
99
     *
100
     * @return string
101
     */
102
    protected function filename()
103
    {
104
        return 'sensor_'.time();
105
    }
106
}
107