1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Microboard\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Microboard\Foundations\Traits\ViewResolverTrait; |
7
|
|
|
use Yajra\DataTables\DataTableAbstract; |
8
|
|
|
use Yajra\DataTables\Html\Builder; |
9
|
|
|
use Yajra\DataTables\Html\Button; |
10
|
|
|
|
11
|
|
|
trait DataTable |
12
|
|
|
{ |
13
|
|
|
use ViewResolverTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected string $baseName; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* DataTable constructor. |
22
|
|
|
* |
23
|
|
|
* @param array $attributes |
24
|
|
|
*/ |
25
|
|
|
public function __construct($attributes = []) |
26
|
|
|
{ |
27
|
|
|
$this->baseName = str_replace('DataTable', '', class_basename($this)); |
28
|
|
|
$this->attributes = $attributes; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Build DataTable class. |
33
|
|
|
* |
34
|
|
|
* @param $query |
35
|
|
|
* @return DataTableAbstract |
36
|
|
|
*/ |
37
|
|
|
public function build($query) |
38
|
|
|
{ |
39
|
|
|
return datatables() |
40
|
|
|
->eloquent($query) |
41
|
|
|
->addColumn('action', function ($model) { |
42
|
|
|
$html = ''; |
43
|
|
|
|
44
|
|
|
if (auth()->user()->can('view', $model)) { |
45
|
|
|
$html .= '<a href="' . route($this->route('show'), $model) . '" ' . |
46
|
|
|
'class="table-action" data-toggle="tooltip" ' . |
47
|
|
|
'data-original-title="' . trans($this->trans('view.action-button')) . '">' . |
48
|
|
|
'<i class="fas fa-eye"></i></a>'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (auth()->user()->can('update', $model)) { |
52
|
|
|
$html .= '<a href="' . route($this->route('edit'), $model) . '" ' . |
53
|
|
|
'class="table-action" data-toggle="tooltip" ' . |
54
|
|
|
'data-original-title="' . trans($this->trans('edit.action-button')) . '">' . |
55
|
|
|
'<i class="fas fa-edit"></i></a>'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (auth()->user()->can('delete', $model)) { |
59
|
|
|
$html .= '<form action="' . route($this->route('destroy'), $model) . '" method="post" class="d-inline-block">' . |
60
|
|
|
csrf_field() . method_field('DELETE') . |
61
|
|
|
'<button type="submit" data-toggle="tooltip" ' . |
62
|
|
|
'class="bg-transparent border-0 p-0 table-action table-action-delete" ' . |
63
|
|
|
'data-original-title="' . trans($this->trans('delete.action-button')) . '" ' . |
64
|
|
|
'data-modal-title="' . trans($this->trans('delete.title')) . '" ' . |
65
|
|
|
'data-modal-text="' . trans($this->trans('delete.text')) . '" ' . |
66
|
|
|
'data-confirm="' . trans($this->trans('delete.confirm')) . '" ' . |
67
|
|
|
'data-cancel="' . trans($this->trans('delete.cancel')) . '">' . |
68
|
|
|
'<i class="fas fa-trash"></i></button></form>'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $html; |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $name |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function route($name): string |
80
|
|
|
{ |
81
|
|
|
return $this->buildVariables()['routePrefix'] . '.' . $name; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $key |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
protected function trans($key): string |
89
|
|
|
{ |
90
|
|
|
return $this->buildVariables()['translationsPrefix'] . '.' . $key; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Optional method if you want to use html builder. |
95
|
|
|
* |
96
|
|
|
* @return Builder |
97
|
|
|
*/ |
98
|
|
|
public function html() |
99
|
|
|
{ |
100
|
|
|
return $this->builder() |
101
|
|
|
->language(trans('microboard::datatable', [])) |
102
|
|
|
->addTableClass('table table-striped table-hover align-items-center') |
103
|
|
|
->columns($this->getColumns()) |
104
|
|
|
->setTableId("{$this->baseName}-table") |
105
|
|
|
->autoWidth(false) |
106
|
|
|
->orderBy(0, 'asc') |
107
|
|
|
->minifiedAjax() |
108
|
|
|
->dom("<'card'" . |
109
|
|
|
"<'card-header border-0'<'row align-items-center'<'col-12 col-sm-8 col-md-6'<'row no-gutters'<'col-4'l><'col-8'f>>><'col-12 col-sm-4 col-md-6 text-right'B>>>" . |
110
|
|
|
"<'table-responsive't>" . |
111
|
|
|
"<'card-footer'<'row'<'col-12 col-sm-6'i><'col-12 col-sm-6'p>>>" . |
112
|
|
|
"r>") |
113
|
|
|
->buttons( |
114
|
|
|
Button::make('print')->text('<span>' . trans('microboard::datatable.print') . '</span><i class="fa fa-print"></i>'), |
115
|
|
|
Button::make('excel')->text('<span>' . trans('microboard::datatable.excel') . '</span><i class="fa fa-file-excel"></i>'), |
116
|
|
|
Button::make('reload')->text('<span>' . trans('microboard::datatable.reload') . '</span><i class="fa fa-sync"></i>') |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get filename for export. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
protected function filename() |
126
|
|
|
{ |
127
|
|
|
return "{$this->baseName}_" . date('YmdHis'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|