-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathCategoryResource.php
More file actions
193 lines (174 loc) · 6.88 KB
/
Copy pathCategoryResource.php
File metadata and controls
193 lines (174 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace LaraZeus\Bolt\Filament\Resources;
use BackedEnum;
use Filament\Actions\ActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Str;
use LaraZeus\Bolt\BoltPlugin;
use LaraZeus\Bolt\Filament\Resources\CategoryResource\Pages\CreateCategory;
use LaraZeus\Bolt\Filament\Resources\CategoryResource\Pages\EditCategory;
use LaraZeus\Bolt\Filament\Resources\CategoryResource\Pages\ListCategories;
use LaraZeus\Bolt\Models\Category;
class CategoryResource extends BoltResource
{
protected static string | BackedEnum | null $navigationIcon = 'tabler-tags-filled';
protected static ?int $navigationSort = 4;
protected static ?string $recordTitleAttribute = 'name';
public static function getModel(): string
{
return BoltPlugin::getModel('Category');
}
public static function getNavigationBadge(): ?string
{
if (! BoltPlugin::getNavigationBadgesVisibility(self::class)) {
return null;
}
return (string) BoltPlugin::getModel('Category')::query()->count();
}
public static function form(Schema $schema): Schema
{
return $schema
->components([
Section::make()
->columnSpanFull()
->columns()
->schema([
TextInput::make('name')
->required()
->maxLength(255)
->live(onBlur: true)
->label(__('zeus-bolt::category.name'))
->afterStateUpdated(function (Set $set, $state, $context) {
if ($context === 'edit') {
return;
}
$set('slug', Str::slug($state));
}),
TextInput::make('slug')
->required()
->maxLength(255)
->label(__('zeus-bolt::category.slug')),
TextInput::make('ordering')
->required()
->numeric()
->label(__('zeus-bolt::category.ordering')),
Toggle::make('is_active')
->label(__('zeus-bolt::category.is_active'))
->default(1),
Textarea::make('description')
->maxLength(65535)
->columnSpan(['sm' => 2])
->label(__('zeus-bolt::category.description')),
FileUpload::make('logo')
->disk(config('zeus-bolt.uploadDisk'))
->directory(config('zeus-bolt.uploadDirectory'))
->visibility(config('zeus-bolt.uploadVisibility'))
->image()
->columnSpan(['sm' => 2])
->label(__('zeus-bolt::category.logo')),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
ImageColumn::make('logo')
->disk(config('zeus-bolt.uploadDisk'))
->visibility(config('zeus-bolt.uploadVisibility'))
->toggleable()
->label(__('zeus-bolt::category.logo')),
TextColumn::make('name')
->label(__('zeus-bolt::category.name'))
->forceSearchCaseInsensitive()
->sortable()
->toggleable()
->searchable(),
TextColumn::make('forms_count')
->counts('forms')
->label(__('zeus-bolt::category.forms'))
->toggleable()
->searchable(),
IconColumn::make('is_active')
->boolean()
->sortable()
->toggleable()
->label(__('zeus-bolt::category.is_active')),
])
->reorderable('ordering')
->defaultSort('id', 'description')
->recordActions([
ActionGroup::make([
EditAction::make(),
DeleteAction::make(),
ForceDeleteAction::make(),
RestoreAction::make(),
]),
])
->filters([
TrashedFilter::make(),
Filter::make('is_active')
->label(__('zeus-bolt::category.is_active'))
->toggle()
->query(fn (Builder $query): Builder => $query->where('is_active', true)),
Filter::make('not_active')
->label(__('zeus-bolt::category.not_active'))
->toggle()
->query(fn (Builder $query): Builder => $query->where('is_active', false)),
])
->toolbarActions([
DeleteBulkAction::make(),
ForceDeleteBulkAction::make(),
RestoreBulkAction::make(),
]);
}
/** @phpstan-return Builder<Category> */
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
public static function getPages(): array
{
return [
'index' => ListCategories::route('/'),
'create' => CreateCategory::route('/create'),
'edit' => EditCategory::route('/{record}/edit'),
];
}
public static function getModelLabel(): string
{
return __('zeus-bolt::category.label');
}
public static function getPluralModelLabel(): string
{
return __('zeus-bolt::category.plural_label');
}
public static function getNavigationLabel(): string
{
return __('zeus-bolt::category.navigation_label');
}
}