Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DepartmentResource.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Filament\Company\Resources\Core;
  3. use App\Filament\Company\Resources\Core\DepartmentResource\Pages;
  4. use App\Filament\Company\Resources\Core\DepartmentResource\RelationManagers\ChildrenRelationManager;
  5. use App\Models\Core\Department;
  6. use Filament\Forms;
  7. use Filament\Forms\Form;
  8. use Filament\Resources\Resource;
  9. use Filament\Tables;
  10. use Filament\Tables\Table;
  11. use Illuminate\Database\Eloquent\Builder;
  12. class DepartmentResource extends Resource
  13. {
  14. protected static ?string $model = Department::class;
  15. protected static ?string $modelLabel = 'Department';
  16. protected static ?string $navigationIcon = 'heroicon-o-square-3-stack-3d';
  17. protected static ?string $navigationGroup = 'HR';
  18. protected static ?string $slug = 'hr/departments';
  19. public static function getModelLabel(): string
  20. {
  21. $modelLabel = static::$modelLabel;
  22. return translate($modelLabel);
  23. }
  24. public static function form(Form $form): Form
  25. {
  26. return $form
  27. ->schema([
  28. Forms\Components\Section::make('General')
  29. ->schema([
  30. Forms\Components\TextInput::make('name')
  31. ->autofocus()
  32. ->required()
  33. ->localizeLabel()
  34. ->maxLength(100),
  35. Forms\Components\Select::make('manager_id')
  36. ->relationship(
  37. name: 'manager',
  38. titleAttribute: 'name',
  39. modifyQueryUsing: static function (Builder $query) {
  40. $company = auth()->user()->currentCompany;
  41. $companyUsers = $company->allUsers()->pluck('id')->toArray();
  42. return $query->whereIn('id', $companyUsers);
  43. }
  44. )
  45. ->localizeLabel()
  46. ->searchable()
  47. ->preload()
  48. ->nullable(),
  49. Forms\Components\Group::make()
  50. ->schema([
  51. Forms\Components\Select::make('parent_id')
  52. ->localizeLabel('Parent Department')
  53. ->relationship('parent', 'name')
  54. ->preload()
  55. ->searchable()
  56. ->nullable(),
  57. Forms\Components\Textarea::make('description')
  58. ->autosize()
  59. ->nullable()
  60. ->localizeLabel(),
  61. ])->columns(1),
  62. ])->columns(),
  63. ]);
  64. }
  65. public static function table(Table $table): Table
  66. {
  67. return $table
  68. ->columns([
  69. Tables\Columns\TextColumn::make('name')
  70. ->localizeLabel()
  71. ->weight('semibold')
  72. ->searchable()
  73. ->sortable(),
  74. Tables\Columns\TextColumn::make('manager.name')
  75. ->localizeLabel()
  76. ->searchable()
  77. ->sortable(),
  78. Tables\Columns\TextColumn::make('children_count')
  79. ->localizeLabel('Children')
  80. ->badge()
  81. ->counts('children')
  82. ->searchable()
  83. ->sortable(),
  84. ])
  85. ->filters([
  86. //
  87. ])
  88. ->actions([
  89. Tables\Actions\EditAction::make(),
  90. ])
  91. ->bulkActions([
  92. Tables\Actions\BulkActionGroup::make([
  93. Tables\Actions\DeleteBulkAction::make(),
  94. ]),
  95. ]);
  96. }
  97. public static function getRelations(): array
  98. {
  99. return [
  100. ChildrenRelationManager::class,
  101. ];
  102. }
  103. public static function getPages(): array
  104. {
  105. return [
  106. 'index' => Pages\ListDepartments::route('/'),
  107. 'create' => Pages\CreateDepartment::route('/create'),
  108. 'edit' => Pages\EditDepartment::route('/{record}/edit'),
  109. ];
  110. }
  111. }