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