1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xetaravel\Http\Livewire\Traits; |
4
|
|
|
|
5
|
|
|
trait WithBulkActions |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Whatever the current page of rows are all selected or not. |
9
|
|
|
* |
10
|
|
|
* @var false |
11
|
|
|
*/ |
12
|
|
|
public $selectPage = false; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Whatever the user has selected all rows or not. |
16
|
|
|
* |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
public $selectAll = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The id array of selected rows. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
public $selected = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* If the selectAll is true, we need to select (and check the checkbox) of all rows |
30
|
|
|
* rendering in the current page. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function renderingWithBulkActions(): void |
35
|
|
|
{ |
36
|
|
|
if ($this->selectAll) { |
37
|
|
|
$this->selectPageRows(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Whenever the user unselect a checkbox, we need to disable the selectAll option and selectPage. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function updatedSelected(): void |
47
|
|
|
{ |
48
|
|
|
$this->selectAll = false; |
49
|
|
|
$this->selectPage = false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Whatever we have selected all rows in the current page. |
54
|
|
|
* |
55
|
|
|
* @param mixed $value The current page where all rows get selected. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function updatedSelectPage($value) |
60
|
|
|
{ |
61
|
|
|
if ($value) { |
62
|
|
|
return $this->selectPageRows(); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->selectAll = false; |
66
|
|
|
$this->selected = []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Convert the selected rows id into string type. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function selectPageRows(): void |
75
|
|
|
{ |
76
|
|
|
$this->selected = $this->rows->pluck('id')->map(fn($id) => (string) $id); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set selectAll to true. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function selectAll(): void |
85
|
|
|
{ |
86
|
|
|
$this->selectAll = true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get all select rows by their id, preparing for deleting them. |
91
|
|
|
* |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public function getSelectedRowsQueryProperty() |
95
|
|
|
{ |
96
|
|
|
return (clone $this->rowsQuery) |
97
|
|
|
->unless($this->selectAll, fn($query) => $query->whereKey($this->selected)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Delete all selected rows and display a flash message. |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function deleteSelected(): void |
106
|
|
|
{ |
107
|
|
|
$deleteCount = $this->selectedRowsQuery->count(); |
|
|
|
|
108
|
|
|
|
109
|
|
|
if ($deleteCount <= 0) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($this->selectedRowsQuery->delete()) { |
114
|
|
|
$this->fireFlash('delete', 'success', $deleteCount); |
|
|
|
|
115
|
|
|
} else { |
116
|
|
|
$this->fireFlash('delete', 'danger'); |
117
|
|
|
} |
118
|
|
|
$this->showDeleteModal = false; |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.