Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CategoryResource.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Filament\Company\Resources\Setting;
  3. use App\Enums\CategoryType;
  4. use App\Filament\Company\Resources\Setting\CategoryResource\Pages;
  5. use App\Models\Setting\Category;
  6. use Closure;
  7. use Exception;
  8. use Filament\Forms;
  9. use Filament\Forms\Form;
  10. use Filament\Notifications\Notification;
  11. use Filament\Resources\Resource;
  12. use Filament\Tables;
  13. use Filament\Tables\Table;
  14. use Illuminate\Database\Eloquent\Collection;
  15. use Wallo\FilamentSelectify\Components\ToggleButton;
  16. class CategoryResource extends Resource
  17. {
  18. protected static ?string $model = Category::class;
  19. protected static ?string $navigationIcon = 'heroicon-o-folder';
  20. protected static ?string $navigationGroup = 'Settings';
  21. protected static ?string $slug = 'settings/categories';
  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. ->autofocus()
  31. ->required()
  32. ->maxLength(255)
  33. ->rule(static function (Forms\Get $get, Forms\Components\Component $component): Closure {
  34. return static function (string $attribute, $value, Closure $fail) use ($get, $component) {
  35. $existingCategory = Category::where('company_id', auth()->user()->currentCompany->id)
  36. ->where('name', $value)
  37. ->where('type', $get('type'))
  38. ->first();
  39. if ($existingCategory && $existingCategory->getKey() !== $component->getRecord()?->getKey()) {
  40. $type = ucwords($get('type'));
  41. $fail("The {$type} category \"{$value}\" already exists.");
  42. }
  43. };
  44. }),
  45. Forms\Components\Select::make('type')
  46. ->options(CategoryType::class)
  47. ->required()
  48. ->native(false)
  49. ->label('Type'),
  50. Forms\Components\ColorPicker::make('color')
  51. ->required()
  52. ->label('Color'),
  53. ToggleButton::make('enabled')
  54. ->label('Default'),
  55. ])->columns(),
  56. ]);
  57. }
  58. /**
  59. * @throws Exception
  60. */
  61. public static function table(Table $table): Table
  62. {
  63. return $table
  64. ->columns([
  65. Tables\Columns\TextColumn::make('name')
  66. ->label('Name')
  67. ->weight('semibold')
  68. ->icon(static fn (Category $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
  69. ->tooltip(static fn (Category $record) => $record->enabled ? "Default {$record->type->getLabel()} Category" : null)
  70. ->iconPosition('after')
  71. ->searchable()
  72. ->sortable(),
  73. Tables\Columns\TextColumn::make('type')
  74. ->label('Type')
  75. ->sortable()
  76. ->searchable(),
  77. Tables\Columns\ColorColumn::make('color')
  78. ->label('Color')
  79. ->copyable()
  80. ->copyMessage('Color code copied'),
  81. ])
  82. ->filters([
  83. Tables\Filters\SelectFilter::make('type')
  84. ->label('Type')
  85. ->multiple()
  86. ->options(CategoryType::class),
  87. ])
  88. ->actions([
  89. Tables\Actions\EditAction::make(),
  90. Tables\Actions\DeleteAction::make()
  91. ->before(static function (Category $record, Tables\Actions\DeleteAction $action) {
  92. if ($record->enabled) {
  93. Notification::make()
  94. ->danger()
  95. ->title('Action Denied')
  96. ->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' => $record->type->getLabel()]))
  97. ->persistent()
  98. ->send();
  99. $action->cancel();
  100. }
  101. }),
  102. ])
  103. ->bulkActions([
  104. Tables\Actions\BulkActionGroup::make([
  105. Tables\Actions\DeleteBulkAction::make()
  106. ->before(static function (Collection $records, Tables\Actions\DeleteBulkAction $action) {
  107. $defaultCategories = $records->filter(static function (Category $record) {
  108. return $record->enabled;
  109. });
  110. if ($defaultCategories->isNotEmpty()) {
  111. $defaultCategoryNames = $defaultCategories->pluck('name')->toArray();
  112. Notification::make()
  113. ->danger()
  114. ->title('Action Denied')
  115. ->body(static function () use ($defaultCategoryNames) {
  116. $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>";
  117. $message .= implode("<br>", array_map(static function ($name) {
  118. return "&bull; " . $name;
  119. }, $defaultCategoryNames));
  120. return $message;
  121. })
  122. ->persistent()
  123. ->send();
  124. $action->cancel();
  125. }
  126. }),
  127. ]),
  128. ])
  129. ->emptyStateActions([
  130. Tables\Actions\CreateAction::make(),
  131. ]);
  132. }
  133. public static function getPages(): array
  134. {
  135. return [
  136. 'index' => Pages\ListCategories::route('/'),
  137. 'create' => Pages\CreateCategory::route('/create'),
  138. 'edit' => Pages\EditCategory::route('/{record}/edit'),
  139. ];
  140. }
  141. }