sistem-akademik/app/Filament/Resources/AcademicYearResource.php
2025-05-24 21:08:04 +07:00

127 lines
4.5 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\AcademicYearResource\Pages;
use App\Filament\Resources\AcademicYearResource\RelationManagers;
use App\Models\AcademicYear;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class AcademicYearResource extends Resource
{
protected static ?string $model = AcademicYear::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationGroup = 'Academic Setup';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->placeholder("ex: 2024/2025")
->maxLength(255),
Forms\Components\Toggle::make('is_active')
->required(),
Forms\Components\DatePicker::make('start_date'),
Forms\Components\DatePicker::make('end_date'),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable()
->weight(fn (AcademicYear $record) => $record->is_active ? 'bold' : 'normal')
->color(fn (AcademicYear $record) => $record->is_active ? 'success' : null),
Tables\Columns\IconColumn::make('is_active')
->boolean(),
Tables\Columns\TextColumn::make('start_date')
->date()
->sortable(),
Tables\Columns\TextColumn::make('end_date')
->date()
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('deleted_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Tables\Filters\TrashedFilter::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\Action::make('toggleActive')
->label('Toggle Active')
->icon('heroicon-o-power')
->action(function (AcademicYear $record) {
// If setting to active, deactivate all others
if ($record->is_active) {
Notification::make()
->title('Info')
->body('The school year is active')
->info()
->send();
} else {
AcademicYear::where('is_active', true)->update(['is_active' => false]);
$record->update(['is_active' => true]);
}
}),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
]),
])
->emptyStateActions([
Tables\Actions\CreateAction::make(),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListAcademicYears::route('/'),
'create' => Pages\CreateAcademicYear::route('/create'),
'edit' => Pages\EditAcademicYear::route('/{record}/edit'),
];
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}