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 6.0KB

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