| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
-
- namespace App\Filament\Resources;
-
- use App\Filament\Resources\HistoryResource\Pages;
- use App\Models\History;
- use App\Service\DeepLService;
- use Filament\Forms\Components\FileUpload;
- use Filament\Forms\Components\Group;
- use Filament\Forms\Components\Radio;
- use Filament\Forms\Components\Section;
- use Filament\Forms\Components\Select;
- use Filament\Forms\Components\TextInput;
- use Filament\Forms\Form;
- use Filament\Resources\Resource;
- use Filament\Tables;
- use Filament\Tables\Columns\ImageColumn;
- use Filament\Tables\Columns\TextColumn;
- use Filament\Tables\Filters\SelectFilter;
- use Filament\Tables\Table;
- use Illuminate\Database\Eloquent\Builder;
- use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
-
- class HistoryResource extends Resource
- {
- protected static ?string $model = History::class;
-
- protected static ?string $navigationIcon = 'heroicon-o-list-bullet';
-
- protected static ?string $navigationLabel = '宏匯里程管理';
-
- protected static ?string $navigationGroup = '關於宏匯';
-
- protected static ?string $modelLabel = '宏匯里程管理';
-
- public ?string $tableSortColumn = null;
-
- public ?string $tableSortDirection = null;
-
- public static function form(Form $form): Form
- {
- return $form
- ->schema([
- Section::make('')->schema([
- Select::make('selected_year')->label('年份')->options(function () {
- $currentYear = now()->year;
- $years = [];
- for ($i = $currentYear - 15; $i <= $currentYear + 5; $i++) {
- $years[$i] = strval($i).'年';
- }
-
- return $years;
- })->columnSpan(1),
- Select::make('selected_month')->label('月份')->options(function () {
- $months = [];
- for ($i = 1; $i <= 12; $i++) {
- $months[$i] = strval($i).'月';
- }
-
- return $months;
- })->default(now()->month)->columnSpan(1),
- Translate::make()->schema(fn (string $locale) => [
- TextInput::make('title')->required($locale == 'zh_TW')->label('標題'),
- ])
- ->locales(['zh_TW', 'en'])
- ->actions([
- app(DeepLService::class)->createTranslationAction('Main', ['title']),
- ])->columnSpanFull(),
- Group::make()->schema([
- FileUpload::make('img_url')->label('圖片')->directory('histories')
- ->image()
- ->optimize('webp')
- ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->imageEditor()
- ->columnSpanFull(),
- ])->columnSpanFull(),
- Radio::make('visible')->label('顯示/不顯示')->options([1 => '顯示', 0 => '不顯示'])->inline()->default(1)->columnSpan(2),
- ])->columns(3),
- ]);
- }
-
- public static function table(Table $table): Table
- {
- return $table
- ->columns([
- TextColumn::make('selected_year')->label('年份')->alignCenter(),
- TextColumn::make('selected_month')->label('月份')->alignCenter(),
- TextColumn::make('title')->label('標題'),
- ImageColumn::make('img_url')->label('圖片')->disk(config('filesystem.default'))->alignCenter(),
- ])
- ->filters([
- SelectFilter::make('selected_year')->label('年份')
- ->options(function () {
- $currentYear = now()->year;
- $years = [];
- // 從 10 年前到 5 年後
- for ($i = $currentYear - 10; $i <= $currentYear + 10; $i++) {
- $years[$i] = strval($i).'年';
- }
-
- return $years;
- })
- ->attribute('selected_year'),
- SelectFilter::make('selected_month')->label('月份')
- ->options(function () {
- $months = [];
- for ($i = 1; $i <= 12; $i++) {
- $months[$i] = strval($i).'月';
- }
-
- return $months;
- })
- ->attribute('selected_month'),
- ])
- ->actions([
- Tables\Actions\EditAction::make(),
- ])
- ->bulkActions([
- Tables\Actions\BulkActionGroup::make([
- Tables\Actions\DeleteBulkAction::make(),
- ]),
- ])
- ->modifyQueryUsing(function (Builder $query) {
- // ✅ 正確:多欄位排序
- return $query
- ->orderBy('selected_year', 'desc')
- ->orderBy('selected_month', 'desc');
- });
- }
-
- public static function getRelations(): array
- {
- return [
- //
- ];
- }
-
- public static function getPages(): array
- {
- return [
- 'index' => Pages\ListHistories::route('/'),
- 'create' => Pages\CreateHistory::route('/create'),
- 'edit' => Pages\EditHistory::route('/{record}/edit'),
- ];
- }
- }
|