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

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