1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Adminetic\Website\Http\Livewire\Admin\Notice; |
4
|
|
|
|
5
|
|
|
use Adminetic\Website\Models\Admin\Notice; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Support\Facades\Blade; |
9
|
|
|
use Rappasoft\LaravelLivewireTables\DataTableComponent; |
10
|
|
|
use Rappasoft\LaravelLivewireTables\Views\Column; |
11
|
|
|
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter; |
12
|
|
|
|
13
|
|
|
class NoticeTable extends DataTableComponent |
14
|
|
|
{ |
15
|
|
|
public function builder(): Builder |
16
|
|
|
{ |
17
|
|
|
return Notice::query() |
|
|
|
|
18
|
|
|
->with('category')->orderBy('position'); // Eager load anything; // Select some things |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public array $bulkActions = [ |
22
|
|
|
'bulkDelete' => 'Bulk Delete', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public function bulkDelete() |
26
|
|
|
{ |
27
|
|
|
Notice::whereIn('id', $this->getSelected())->delete(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function filters(): array |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
SelectFilter::make('Active') |
34
|
|
|
->options([ |
35
|
|
|
'' => 'All', |
36
|
|
|
'1' => 'Active', |
37
|
|
|
'0' => 'Inactive', |
38
|
|
|
]) |
39
|
|
|
->filter(function (Builder $builder, string $value) { |
40
|
|
|
if ($value === '1') { |
41
|
|
|
$builder->where('active', true); |
42
|
|
|
} elseif ($value === '0') { |
43
|
|
|
$builder->where('active', false); |
44
|
|
|
} |
45
|
|
|
}), |
46
|
|
|
SelectFilter::make('Popup') |
47
|
|
|
->options([ |
48
|
|
|
'' => 'All', |
49
|
|
|
'1' => 'Popup', |
50
|
|
|
'0' => 'No Popup', |
51
|
|
|
]) |
52
|
|
|
->filter(function (Builder $builder, string $value) { |
53
|
|
|
if ($value === '1') { |
54
|
|
|
$builder->where('popup', true); |
55
|
|
|
} elseif ($value === '0') { |
56
|
|
|
$builder->where('popup', false); |
57
|
|
|
} |
58
|
|
|
}), |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function configure(): void |
63
|
|
|
{ |
64
|
|
|
$this->setPrimaryKey('id'); |
65
|
|
|
|
66
|
|
|
$this->setEagerLoadAllRelationsEnabled(); |
67
|
|
|
|
68
|
|
|
$this->setEmptyMessage('No notice found'); |
69
|
|
|
|
70
|
|
|
$this->setReorderStatus(true); |
71
|
|
|
|
72
|
|
|
$this->setDefaultReorderSort('position', 'asc'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function reorder($items): void |
76
|
|
|
{ |
77
|
|
|
foreach ($items as $item) { |
78
|
|
|
Notice::find((int) $item['value'])->update(['position' => (int) $item['order']]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function columns(): array |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
Column::make('ID', 'id') |
86
|
|
|
->sortable() |
87
|
|
|
->searchable(), |
88
|
|
|
Column::make('Name', 'name') |
89
|
|
|
->sortable() |
90
|
|
|
->searchable(), |
91
|
|
|
Column::make('Category', 'category_id') |
92
|
|
|
->format( |
93
|
|
|
fn ($value, $row, Column $column) => $row->category->name ?? '-' |
|
|
|
|
94
|
|
|
) |
95
|
|
|
->sortable() |
96
|
|
|
->searchable() |
97
|
|
|
->collapseOnTablet(), |
98
|
|
|
Column::make('Active', 'active') |
99
|
|
|
->format( |
100
|
|
|
fn ($value, $row, Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'danger').' ">'.($row->getRawOriginal('active') ? 'Active' : 'Inactive').'</span>' |
|
|
|
|
101
|
|
|
) |
102
|
|
|
->html() |
103
|
|
|
->collapseOnTablet(), |
104
|
|
|
Column::make('Popup', 'popup') |
105
|
|
|
->format( |
106
|
|
|
fn ($value, $row, Column $column) => '<span class="badge badge-'.($row->getRawOriginal('popup') ? 'success' : 'primary').' ">'.($row->getRawOriginal('popup') ? 'Popup' : 'No Popup').'</span>' |
|
|
|
|
107
|
|
|
) |
108
|
|
|
->html() |
109
|
|
|
->collapseOnTablet(), |
110
|
|
|
Column::make('Icon', 'icon') |
111
|
|
|
->format( |
112
|
|
|
fn ($value, $row, Column $column) => '<span class="'.$row->icon.' "></span>' |
|
|
|
|
113
|
|
|
) |
114
|
|
|
->html() |
115
|
|
|
->collapseOnTablet(), |
116
|
|
|
Column::make('Color', 'color') |
117
|
|
|
->format( |
118
|
|
|
fn ($value, $row, Column $column) => '<span style="height:30px;width:50px;background-color:'.$row->color.'"></span>' |
|
|
|
|
119
|
|
|
) |
120
|
|
|
->html() |
121
|
|
|
->collapseOnTablet(), |
122
|
|
|
Column::make('Expiry', 'expire') |
123
|
|
|
->format( |
124
|
|
|
fn ($value, $row, Column $column) => ! is_null($row->expire) ? (Carbon::create($row->expire))->toFormattedDayDateString() : '' |
|
|
|
|
125
|
|
|
) |
126
|
|
|
->html() |
127
|
|
|
->collapseOnTablet(), |
128
|
|
|
Column::make('Action') |
129
|
|
|
->label( |
130
|
|
|
fn ($row, Column $column) => Blade::render('<x-adminetic-action :model="$model" route="notice" />', ['model' => $row]) |
|
|
|
|
131
|
|
|
) |
132
|
|
|
->html() |
133
|
|
|
->collapseOnTablet(), |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|