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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 getEloquentQuery(): Builder
  23. {
  24. return parent::getEloquentQuery()
  25. ->where('company_id', Auth::user()->currentCompany->id);
  26. }
  27. public static function form(Form $form): Form
  28. {
  29. return $form
  30. ->schema([
  31. Forms\Components\Section::make('General')
  32. ->schema([
  33. Forms\Components\TextInput::make('name')
  34. ->label('Name')
  35. ->required(),
  36. Forms\Components\ColorPicker::make('color')
  37. ->label('Color')
  38. ->default('#4f46e5')
  39. ->required(),
  40. Forms\Components\Select::make('type')
  41. ->label('Type')
  42. ->options(Category::getCategoryTypes())
  43. ->searchable()
  44. ->required(),
  45. ToggleButton::make('enabled')
  46. ->label('Default')
  47. ->offColor('danger')
  48. ->onColor('primary'),
  49. ])->columns(),
  50. ]);
  51. }
  52. /**
  53. * @throws Exception
  54. */
  55. public static function table(Table $table): Table
  56. {
  57. return $table
  58. ->columns([
  59. Tables\Columns\TextColumn::make('name')
  60. ->label('Name')
  61. ->weight('semibold')
  62. ->icon(static fn (Category $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
  63. ->tooltip(static fn (Category $record) => $record->enabled ? "Default " .ucwords($record->type) . " Category" : null)
  64. ->iconPosition('after')
  65. ->searchable()
  66. ->sortable(),
  67. Tables\Columns\TextColumn::make('type')
  68. ->label('Type')
  69. ->formatStateUsing(static fn (Category $record): string => ucwords($record->type))
  70. ->searchable()
  71. ->sortable(),
  72. Tables\Columns\ColorColumn::make('color')
  73. ->label('Color')
  74. ->copyable()
  75. ->copyMessage('Color copied to clipboard.'),
  76. ])
  77. ->filters([
  78. //
  79. ])
  80. ->actions([
  81. Tables\Actions\EditAction::make(),
  82. Tables\Actions\DeleteAction::make()
  83. ->before(static function (Category $record, Tables\Actions\DeleteAction $action) {
  84. if ($record->enabled) {
  85. Notification::make()
  86. ->danger()
  87. ->title('Action Denied')
  88. ->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)]))
  89. ->persistent()
  90. ->send();
  91. $action->cancel();
  92. }
  93. }),
  94. ])
  95. ->bulkActions([
  96. Tables\Actions\DeleteBulkAction::make()
  97. ->before(static function (Collection $records, Tables\Actions\DeleteBulkAction $action) {
  98. $defaultCategories = $records->filter(static function (Category $record) {
  99. return $record->enabled;
  100. });
  101. if ($defaultCategories->isNotEmpty()) {
  102. $defaultCategoryNames = $defaultCategories->pluck('name')->toArray();
  103. Notification::make()
  104. ->danger()
  105. ->title('Action Denied')
  106. ->body(static function () use ($defaultCategoryNames) {
  107. $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>";
  108. $message .= implode("<br>", array_map(static function ($name) {
  109. return "&bull; " . $name;
  110. }, $defaultCategoryNames));
  111. return $message;
  112. })
  113. ->persistent()
  114. ->send();
  115. $action->cancel();
  116. }
  117. }),
  118. ]);
  119. }
  120. public static function getRelations(): array
  121. {
  122. return [
  123. //
  124. ];
  125. }
  126. public static function getPages(): array
  127. {
  128. return [
  129. 'index' => Pages\ListCategories::route('/'),
  130. 'create' => Pages\CreateCategory::route('/create'),
  131. 'edit' => Pages\EditCategory::route('/{record}/edit'),
  132. ];
  133. }
  134. }