You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CategoryResource.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\CategoryResource\Pages;
  4. use App\Filament\Resources\CategoryResource\RelationManagers;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Support\Facades\Auth;
  7. use Wallo\FilamentSelectify\Components\ToggleButton;
  8. use App\Models\Setting\Category;
  9. use Exception;
  10. use Filament\Forms;
  11. use Filament\Notifications\Notification;
  12. use Filament\Resources\Form;
  13. use Filament\Resources\Resource;
  14. use Filament\Resources\Table;
  15. use Filament\Tables;
  16. use Illuminate\Support\Collection;
  17. class CategoryResource extends Resource
  18. {
  19. protected static ?string $model = Category::class;
  20. protected static ?string $navigationIcon = 'heroicon-o-folder';
  21. protected static ?string $navigationGroup = 'Settings';
  22. public static function form(Form $form): Form
  23. {
  24. return $form
  25. ->schema([
  26. Forms\Components\Section::make('General')
  27. ->schema([
  28. Forms\Components\TextInput::make('name')
  29. ->label('Name')
  30. ->required(),
  31. Forms\Components\ColorPicker::make('color')
  32. ->label('Color')
  33. ->default('#4f46e5')
  34. ->required(),
  35. Forms\Components\Select::make('type')
  36. ->label('Type')
  37. ->options(Category::getCategoryTypes())
  38. ->searchable()
  39. ->required(),
  40. ToggleButton::make('enabled')
  41. ->label('Default')
  42. ->offColor('danger')
  43. ->onColor('primary'),
  44. ])->columns(),
  45. ]);
  46. }
  47. /**
  48. * @throws Exception
  49. */
  50. public static function table(Table $table): Table
  51. {
  52. return $table
  53. ->columns([
  54. Tables\Columns\TextColumn::make('name')
  55. ->label('Name')
  56. ->weight('semibold')
  57. ->icon(static fn (Category $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
  58. ->tooltip(static fn (Category $record) => $record->enabled ? "Default " .ucwords($record->type) . " Category" : null)
  59. ->iconPosition('after')
  60. ->searchable()
  61. ->sortable(),
  62. Tables\Columns\TextColumn::make('type')
  63. ->label('Type')
  64. ->formatStateUsing(static fn (Category $record): string => ucwords($record->type))
  65. ->searchable()
  66. ->sortable(),
  67. Tables\Columns\ColorColumn::make('color')
  68. ->label('Color')
  69. ->copyable()
  70. ->copyMessage('Color copied to clipboard.'),
  71. ])
  72. ->filters([
  73. //
  74. ])
  75. ->actions([
  76. Tables\Actions\EditAction::make(),
  77. Tables\Actions\DeleteAction::make()
  78. ->before(static function (Category $record, Tables\Actions\DeleteAction $action) {
  79. if ($record->enabled) {
  80. Notification::make()
  81. ->danger()
  82. ->title('Action Denied')
  83. ->body(__('The :name category is currently set as your default :Type category and cannot be deleted. Please set a different category as your default before attempting to delete this one.', ['name' => $record->name, 'Type' => ucwords($record->type)]))
  84. ->persistent()
  85. ->send();
  86. $action->cancel();
  87. }
  88. }),
  89. ])
  90. ->bulkActions([
  91. Tables\Actions\DeleteBulkAction::make()
  92. ->before(static function (Collection $records, Tables\Actions\DeleteBulkAction $action) {
  93. $defaultCategories = $records->filter(static function (Category $record) {
  94. return $record->enabled;
  95. });
  96. if ($defaultCategories->isNotEmpty()) {
  97. $defaultCategoryNames = $defaultCategories->pluck('name')->toArray();
  98. Notification::make()
  99. ->danger()
  100. ->title('Action Denied')
  101. ->body(static function () use ($defaultCategoryNames) {
  102. $message = __('The following categories are currently set as your default and cannot be deleted. Please set a different category as your default before attempting to delete these ones.') . "<br><br>";
  103. $message .= implode("<br>", array_map(static function ($name) {
  104. return "&bull; " . $name;
  105. }, $defaultCategoryNames));
  106. return $message;
  107. })
  108. ->persistent()
  109. ->send();
  110. $action->cancel();
  111. }
  112. }),
  113. ]);
  114. }
  115. public static function getRelations(): array
  116. {
  117. return [
  118. //
  119. ];
  120. }
  121. public static function getPages(): array
  122. {
  123. return [
  124. 'index' => Pages\ListCategories::route('/'),
  125. 'create' => Pages\CreateCategory::route('/create'),
  126. 'edit' => Pages\EditCategory::route('/{record}/edit'),
  127. ];
  128. }
  129. }