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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 App\Traits\NotifiesOnDelete;
  7. use Closure;
  8. use Exception;
  9. use Filament\Forms;
  10. use Filament\Forms\Form;
  11. use Filament\Resources\Resource;
  12. use Filament\Support\Enums\FontWeight;
  13. use Filament\Tables;
  14. use Filament\Tables\Table;
  15. use Wallo\FilamentSelectify\Components\ToggleButton;
  16. class CategoryResource extends Resource
  17. {
  18. use NotifiesOnDelete;
  19. protected static ?string $model = Category::class;
  20. protected static ?string $modelLabel = 'Category';
  21. protected static ?string $navigationIcon = 'heroicon-o-folder';
  22. protected static ?string $navigationGroup = 'Settings';
  23. protected static ?string $slug = 'settings/categories';
  24. public static function getModelLabel(): string
  25. {
  26. $modelLabel = static::$modelLabel;
  27. return translate($modelLabel);
  28. }
  29. public static function form(Form $form): Form
  30. {
  31. return $form
  32. ->schema([
  33. Forms\Components\Section::make('General')
  34. ->schema([
  35. Forms\Components\TextInput::make('name')
  36. ->localizeLabel()
  37. ->autofocus()
  38. ->required()
  39. ->maxLength(255)
  40. ->rule(static function (Forms\Get $get, Forms\Components\Component $component): Closure {
  41. return static function (string $attribute, $value, Closure $fail) use ($get, $component) {
  42. $existingCategory = Category::where('company_id', auth()->user()->currentCompany->id)
  43. ->where('name', $value)
  44. ->where('type', $get('type'))
  45. ->first();
  46. if ($existingCategory && $existingCategory->getKey() !== $component->getRecord()?->getKey()) {
  47. $message = translate('The :Type :record ":name" already exists.', [
  48. 'Type' => $existingCategory->type->getLabel(),
  49. 'record' => strtolower(static::getModelLabel()),
  50. 'name' => $value,
  51. ]);
  52. $fail($message);
  53. }
  54. };
  55. }),
  56. Forms\Components\Select::make('type')
  57. ->localizeLabel()
  58. ->options(CategoryType::class)
  59. ->required(),
  60. Forms\Components\ColorPicker::make('color')
  61. ->localizeLabel()
  62. ->required(),
  63. ToggleButton::make('enabled')
  64. ->localizeLabel('Default')
  65. ->onLabel(Category::enabledLabel())
  66. ->offLabel(Category::disabledLabel()),
  67. ])->columns(),
  68. ]);
  69. }
  70. /**
  71. * @throws Exception
  72. */
  73. public static function table(Table $table): Table
  74. {
  75. return $table
  76. ->columns([
  77. Tables\Columns\TextColumn::make('name')
  78. ->localizeLabel()
  79. ->weight(FontWeight::Medium)
  80. ->icon(static fn (Category $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  81. ->tooltip(static function (Category $record) {
  82. $tooltipMessage = translate('Default :Type :Record', [
  83. 'Type' => $record->type->getLabel(),
  84. 'Record' => static::getModelLabel(),
  85. ]);
  86. return $record->isEnabled() ? $tooltipMessage : null;
  87. })
  88. ->iconPosition('after')
  89. ->searchable()
  90. ->sortable(),
  91. Tables\Columns\TextColumn::make('type')
  92. ->localizeLabel()
  93. ->sortable()
  94. ->searchable(),
  95. Tables\Columns\ColorColumn::make('color')
  96. ->localizeLabel()
  97. ->copyable(),
  98. ])
  99. ->filters([
  100. Tables\Filters\SelectFilter::make('type')
  101. ->label('Type')
  102. ->multiple()
  103. ->options(CategoryType::class),
  104. ])
  105. ->actions([
  106. Tables\Actions\EditAction::make(),
  107. Tables\Actions\DeleteAction::make(),
  108. ])
  109. ->bulkActions([
  110. Tables\Actions\BulkActionGroup::make([
  111. Tables\Actions\DeleteBulkAction::make(),
  112. ]),
  113. ])
  114. ->checkIfRecordIsSelectableUsing(static function (Category $record) {
  115. return $record->isDisabled();
  116. })
  117. ->emptyStateActions([
  118. Tables\Actions\CreateAction::make(),
  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. }