|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BaseDataTable.php |
|
4
|
|
|
* |
|
5
|
|
|
* -Description- |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
* @package LibreNMS |
|
21
|
|
|
* @link http://librenms.org |
|
22
|
|
|
* @copyright 2016 Tony Murray |
|
23
|
|
|
* @author Tony Murray <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace App\DataTables; |
|
27
|
|
|
|
|
28
|
|
|
use Yajra\Datatables\Services\DataTable; |
|
29
|
|
|
|
|
30
|
|
|
abstract class BaseDataTable extends DataTable |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Optional method if you want to use html builder. |
|
34
|
|
|
* |
|
35
|
|
|
* @return \Yajra\Datatables\Html\Builder |
|
36
|
|
|
*/ |
|
37
|
|
|
public function html() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->builder() |
|
40
|
|
|
->columns($this->getColumns()) |
|
41
|
|
|
->parameters($this->getBuilderParameters()) |
|
42
|
|
|
->ajax($this->getAjax()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get columns. |
|
47
|
|
|
* |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
abstract public function getColumns(); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the query object to be processed by datatables. |
|
54
|
|
|
* |
|
55
|
|
|
* @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
|
56
|
|
|
*/ |
|
57
|
|
|
abstract public function query(); |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get Builder Params |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function getBuilderParameters() |
|
65
|
|
|
{ |
|
66
|
|
|
return [ |
|
|
|
|
|
|
67
|
|
|
'dom' => "<'row'<'col-sm-3'l><'col-sm-6 text-center'B><'col-sm-3'f>>". |
|
68
|
|
|
"<'row'<'col-sm-12'tr>>". |
|
69
|
|
|
"<'row'<'col-sm-5'i><'col-sm-7'p>>", |
|
70
|
|
|
'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]], |
|
71
|
|
|
'buttons' => [ |
|
72
|
|
|
'csv', 'excel', 'pdf', 'print', 'reset', 'reload', |
|
73
|
|
|
], |
|
74
|
|
|
'autoWidth' => false, |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get ajax url. |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getAjax() |
|
84
|
|
|
{ |
|
85
|
|
|
return ''; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.