PopupTable::filters()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 21
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 27
rs 9.2728
1
<?php
2
3
namespace Adminetic\Website\Http\Livewire\Admin\Popup;
4
5
use Adminetic\Website\Models\Admin\Popup;
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 PopupTable extends DataTableComponent
14
{
15
    public function builder(): Builder
16
    {
17
        return Popup::query()
0 ignored issues
show
Bug Best Practice introduced by
The expression return Adminetic\Website...')->orderBy('position') could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
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
        Popup::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 popup 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
            Popup::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 ?? '-'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => $row->category->name ?? '-'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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>'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

100
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'danger').' ">'.($row->getRawOriginal('active') ? 'Active' : 'Inactive').'</span>'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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>'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

106
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="badge badge-'.($row->getRawOriginal('popup') ? 'success' : 'primary').' ">'.($row->getRawOriginal('popup') ? 'Popup' : 'No Popup').'</span>'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
                )
108
                ->html()
109
                ->collapseOnTablet(),
110
            Column::make('Icon', 'icon')
111
                ->format(
112
                    fn ($value, $row, Column $column) => '<span class="'.$row->icon.' "></span>'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

112
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="'.$row->icon.' "></span>'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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>'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span style="height:30px;width:50px;background-color:'.$row->color.'"></span>'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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() : ''
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

124
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => ! is_null($row->expire) ? (Carbon::create($row->expire))->toFormattedDayDateString() : ''

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
                )
126
                ->html()
127
                ->collapseOnTablet(),
128
            Column::make('Action')
129
                ->label(
130
                    fn ($row, Column $column) => Blade::render('<x-adminetic-action :model="$model" route="popup" />', ['model' => $row])
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

130
                    fn ($row, /** @scrutinizer ignore-unused */ Column $column) => Blade::render('<x-adminetic-action :model="$model" route="popup" />', ['model' => $row])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
                )
132
                ->html()
133
                ->collapseOnTablet(),
134
        ];
135
    }
136
}
137