|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Xetaravel\Livewire\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
use Illuminate\Support\Facades\DB; |
|
11
|
|
|
use Masmerise\Toaster\Toastable; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
|
|
14
|
|
|
trait WithBulkActions |
|
15
|
|
|
{ |
|
16
|
|
|
use Toastable; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Whatever the current page of rows are all selected or not. |
|
20
|
|
|
* |
|
21
|
|
|
* @var bool |
|
22
|
|
|
*/ |
|
23
|
|
|
public bool $selectPage = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Whatever the user has selected all rows or not. |
|
27
|
|
|
* |
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
public bool $selectAll = false; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The id array of selected rows. |
|
34
|
|
|
* |
|
35
|
|
|
* @var Collection |
|
36
|
|
|
*/ |
|
37
|
|
|
public Collection $selected; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Filter the field on component mount regarding the allowed fields. |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function mountWithBulkActions(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->selected = collect(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* If the selectAll is true, we need to select (and check the checkbox) of all rows |
|
51
|
|
|
* rendering in the current page. |
|
52
|
|
|
* |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function renderingWithBulkActions(): void |
|
56
|
|
|
{ |
|
57
|
|
|
if ($this->selectAll) { |
|
58
|
|
|
$this->selectPageRows(); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Whenever the user unselect a checkbox, we need to disable the selectAll option and selectPage. |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
public function updatedSelected(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->selectAll = false; |
|
70
|
|
|
$this->selectPage = false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Whatever we have selected all rows in the current page. |
|
75
|
|
|
* |
|
76
|
|
|
* @param mixed $value The current page where all rows get selected. |
|
77
|
|
|
* |
|
78
|
|
|
* @return void|null |
|
79
|
|
|
*/ |
|
80
|
|
|
public function updatedSelectPage($value) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($value) { |
|
83
|
|
|
$this->selectPageRows(); |
|
84
|
|
|
|
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->selectAll = false; |
|
89
|
|
|
$this->selected = collect(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Convert the selected rows id into string type. |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
public function selectPageRows(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->selected = $this->rows->pluck('id')->map(fn ($id) => (string) $id); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set selectAll to true. |
|
104
|
|
|
* |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setSelectAll(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->selectAll = true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get all select rows by their id, preparing for deleting them. |
|
114
|
|
|
* |
|
115
|
|
|
* @eturn Builder |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getSelectedRowsQueryProperty(): Builder |
|
118
|
|
|
{ |
|
119
|
|
|
|
|
120
|
|
|
return app($this->model) |
|
121
|
|
|
->unless($this->selectAll, function ($query) { |
|
|
|
|
|
|
122
|
|
|
return $query->whereKey($this->selected->values()); |
|
123
|
|
|
}); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Delete all selected rows and display a flash message. |
|
128
|
|
|
* |
|
129
|
|
|
* @return bool |
|
130
|
|
|
* |
|
131
|
|
|
* @throws Throwable |
|
132
|
|
|
*/ |
|
133
|
|
|
public function deleteSelected(): bool |
|
134
|
|
|
{ |
|
135
|
|
|
$models = new EloquentCollection( |
|
136
|
|
|
$this->selectedRowsQuery->get()->map(function ($model) { |
|
|
|
|
|
|
137
|
|
|
$this->authorize('delete', $model); |
|
|
|
|
|
|
138
|
|
|
return $model; |
|
139
|
|
|
})->all() |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
if ($models->isEmpty()) { |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
DB::transaction(function () use ($models) { |
|
147
|
|
|
foreach ($models as $model) { |
|
148
|
|
|
$model->delete(); |
|
149
|
|
|
} |
|
150
|
|
|
}); |
|
151
|
|
|
|
|
152
|
|
|
$this->selected = collect(); |
|
153
|
|
|
|
|
154
|
|
|
return true; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|