|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+<?php
|
|
|
2
|
+
|
|
|
3
|
+namespace App\Filament\Resources;
|
|
|
4
|
+
|
|
|
5
|
+use App\Filament\Resources\AlbumResource\Pages;
|
|
|
6
|
+use App\Filament\Resources\AlbumResource\RelationManagers;
|
|
|
7
|
+use App\Service\DeepLService;
|
|
|
8
|
+use App\Models\Album;
|
|
|
9
|
+use App\Models\AlbumCategory;
|
|
|
10
|
+use App\Models\NewsCategory;
|
|
|
11
|
+use Filament\Forms;
|
|
|
12
|
+use Filament\Forms\Components\Actions\Action;
|
|
|
13
|
+use Filament\Forms\Components\DatePicker;
|
|
|
14
|
+use Filament\Forms\Components\DateTimePicker;
|
|
|
15
|
+use Filament\Forms\Components\FileUpload;
|
|
|
16
|
+use Filament\Forms\Components\Group;
|
|
|
17
|
+use Filament\Forms\Components\Radio;
|
|
|
18
|
+use Filament\Forms\Components\Section;
|
|
|
19
|
+use Filament\Forms\Components\Select;
|
|
|
20
|
+use Filament\Forms\Components\Textarea;
|
|
|
21
|
+use Filament\Forms\Components\TextInput;
|
|
|
22
|
+use Filament\Forms\Components\Toggle;
|
|
|
23
|
+use Filament\Forms\Form;
|
|
|
24
|
+use Filament\Forms\Get;
|
|
|
25
|
+use Filament\Resources\Resource;
|
|
|
26
|
+use Filament\Tables;
|
|
|
27
|
+use Filament\Tables\Columns\ImageColumn;
|
|
|
28
|
+use Filament\Tables\Columns\TextColumn;
|
|
|
29
|
+use Filament\Tables\Filters\QueryBuilder;
|
|
|
30
|
+use Filament\Tables\Filters\QueryBuilder\Constraints\SelectConstraint;
|
|
|
31
|
+use Filament\Tables\Filters\QueryBuilder\Constraints\TextConstraint;
|
|
|
32
|
+use Filament\Tables\Filters\SelectFilter;
|
|
|
33
|
+use Filament\Tables\Table;
|
|
|
34
|
+use Illuminate\Database\Eloquent\Builder;
|
|
|
35
|
+use Illuminate\Database\Eloquent\Collection;
|
|
|
36
|
+use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
|
37
|
+use Illuminate\Support\Facades\DB;
|
|
|
38
|
+use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
|
|
|
39
|
+
|
|
|
40
|
+class AlbumResource extends Resource
|
|
|
41
|
+{
|
|
|
42
|
+ protected static ?string $model = Album::class;
|
|
|
43
|
+ protected static ?string $modelLabel = "影音管理";
|
|
|
44
|
+ protected static ?string $navigationIcon = 'heroicon-o-photo';
|
|
|
45
|
+ protected static ?string $navigationGroup = '上稿內容管理';
|
|
|
46
|
+ protected static ?string $navigationLabel = "影音管理";
|
|
|
47
|
+
|
|
|
48
|
+ public static function form(Form $form): Form
|
|
|
49
|
+ {
|
|
|
50
|
+ return $form
|
|
|
51
|
+ ->schema([
|
|
|
52
|
+ Section::make("新增 影音")->schema([
|
|
|
53
|
+ Group::make()->schema([
|
|
|
54
|
+ Select::make("album_category_id")
|
|
|
55
|
+ ->options(AlbumCategory::orderBy("order")->get()->pluck("name","id"))
|
|
|
56
|
+ ->label("影音分類")
|
|
|
57
|
+ ->required()
|
|
|
58
|
+ ->columnSpan(1)
|
|
|
59
|
+ ->native(false)
|
|
|
60
|
+ ->Live(),
|
|
|
61
|
+ ])->columnSpanFull()->columns(2),
|
|
|
62
|
+ Group::make()->schema([
|
|
|
63
|
+ DatePicker::make('post_date')
|
|
|
64
|
+ ->label("發布日期")
|
|
|
65
|
+ ->closeOnDateSelection(),
|
|
|
66
|
+ ])->columnSpanFull()->columns(2),
|
|
|
67
|
+ FileUpload::make('news_banner')->label("列表大圖")
|
|
|
68
|
+ ->disk("s3")
|
|
|
69
|
+ ->directory("album/img")
|
|
|
70
|
+ ->helperText('建議寬高限制為:2000*720px,出血寬度720px,主要圖像範圍為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
|
|
|
71
|
+ FileUpload::make('news_img_pc')->label("列表圖(desktop)")
|
|
|
72
|
+ ->disk("s3")
|
|
|
73
|
+ ->directory("album/img")
|
|
|
74
|
+ ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
|
|
|
75
|
+ FileUpload::make('news_img_mobile')->label("列表圖(mobile)")
|
|
|
76
|
+ ->disk("s3")
|
|
|
77
|
+ ->directory("album/img")
|
|
|
78
|
+ ->helperText('建議寬高限制為:600x896px,檔案大小限制為1M以下')->maxSize('1024'),
|
|
|
79
|
+ Translate::make()->schema(fn (string $locale) => [
|
|
|
80
|
+ TextInput::make('title')
|
|
|
81
|
+ ->label("標題")
|
|
|
82
|
+ ->columnSpan(1),
|
|
|
83
|
+ ])
|
|
|
84
|
+ ->locales(["zh_TW", "en", "jp"])
|
|
|
85
|
+ ->actions([
|
|
|
86
|
+ app(DeepLService::class)->createTranslationAction("Main", ["title"])
|
|
|
87
|
+ ])
|
|
|
88
|
+ ->id("main")->columnSpanFull()->columns(3),
|
|
|
89
|
+
|
|
|
90
|
+ Section::make("")->schema([
|
|
|
91
|
+ Radio::make("upload_type")->label("")->options([
|
|
|
92
|
+ 1 => "網址",
|
|
|
93
|
+ 2 => "檔案"
|
|
|
94
|
+ ])->columnSpanFull()->default(1)->Live(),
|
|
|
95
|
+ Group::make()->schema([
|
|
|
96
|
+ TextInput::make('link_video')->label("網址")->nullable(),
|
|
|
97
|
+ ])->visible(fn (Get $get):bool => $get("upload_type") == 1)->columnSpanFull(),
|
|
|
98
|
+ Group::make()->schema([
|
|
|
99
|
+ FileUpload::make('link_upload')->label("")->disk("s3")->directory("album/video")
|
|
|
100
|
+ ->helperText('建議影片寬高限制為:1920*1080px,出血寬度720px,大小限制為:100M以下')
|
|
|
101
|
+ ->maxSize(102400)->nullable(),
|
|
|
102
|
+ ])->visible(fn (Get $get):bool => $get("upload_type") == 2)->columnSpanFull(),
|
|
|
103
|
+ ])->columnSpanFull(),
|
|
|
104
|
+ Toggle::make("on_top")->inline()->label("置頂輪播")->columnSpanFull(),
|
|
|
105
|
+ Toggle::make("homepage_top")->inline()->label("在首頁輪播")->columnSpanFull(),
|
|
|
106
|
+ TextInput::make('order')->label("排序")->integer()->default(0),
|
|
|
107
|
+ ])->columns(3),
|
|
|
108
|
+ ]);
|
|
|
109
|
+ }
|
|
|
110
|
+
|
|
|
111
|
+ public static function table(Table $table): Table
|
|
|
112
|
+ {
|
|
|
113
|
+ return $table
|
|
|
114
|
+ ->columns([
|
|
|
115
|
+ //
|
|
|
116
|
+ TextColumn::make("albumCategory.name")->label("分類")->alignCenter(),
|
|
|
117
|
+ TextColumn::make("title")->label("標題")->alignCenter(),
|
|
|
118
|
+ TextColumn::make("post_date")->date()->alignCenter(),
|
|
|
119
|
+ ImageColumn::make("news_img_pc")->disk('s3')->alignCenter(),
|
|
|
120
|
+ TextColumn::make("list_audit_state")->label("狀態")->badge()
|
|
|
121
|
+ ->color(fn (string $state): string => match ($state) {
|
|
|
122
|
+ '暫存' => 'warning',
|
|
|
123
|
+ '已發佈' => 'success',
|
|
|
124
|
+ }),
|
|
|
125
|
+ TextColumn::make("created_at")->label("建立時間")->dateTime()->alignCenter(),
|
|
|
126
|
+ TextColumn::make("updated_at")->label("更新時間")->dateTime()->alignCenter(),
|
|
|
127
|
+ ])
|
|
|
128
|
+ ->filters([
|
|
|
129
|
+ SelectFilter::make('post_date')->label("年份")
|
|
|
130
|
+ ->options(Album::select(DB::raw("DATE_FORMAT(post_date, '%Y') as year"))->whereNotNull("post_date")->distinct()->pluck("year","year")->toArray())
|
|
|
131
|
+ ->query(
|
|
|
132
|
+ fn (array $data, Builder $query): Builder =>
|
|
|
133
|
+ $query->when(
|
|
|
134
|
+ $data['value'],
|
|
|
135
|
+ fn (Builder $query, $value): Builder => $query->where('post_date', 'like', $data['value']. "%")
|
|
|
136
|
+ )
|
|
|
137
|
+ ),
|
|
|
138
|
+ SelectFilter::make('album_category_id')->label("分類")
|
|
|
139
|
+ ->relationship('albumCategory', 'name')
|
|
|
140
|
+ ->getOptionLabelFromRecordUsing(fn($record, $livewire) => $record->getTranslation('name', "zh_TW")),
|
|
|
141
|
+ SelectFilter::make('visible')->label("狀態")
|
|
|
142
|
+ ->options([
|
|
|
143
|
+ 0 => "暫存",
|
|
|
144
|
+ 1 => "已發佈",
|
|
|
145
|
+ ])
|
|
|
146
|
+ ->query(
|
|
|
147
|
+ fn (array $data, Builder $query): Builder =>
|
|
|
148
|
+ $query->when(
|
|
|
149
|
+ $data['value'],
|
|
|
150
|
+ fn (Builder $query, $value): Builder => $query->where('visible', $data['value'])
|
|
|
151
|
+ )
|
|
|
152
|
+ ),
|
|
|
153
|
+ ])
|
|
|
154
|
+ ->actions([
|
|
|
155
|
+ Tables\Actions\EditAction::make(),
|
|
|
156
|
+ Tables\Actions\DeleteAction::make(),
|
|
|
157
|
+ \Filament\Tables\Actions\Action::make("audit")
|
|
|
158
|
+ ->label(fn ($record) => match ($record->visible) {
|
|
|
159
|
+ 0 => '發佈',
|
|
|
160
|
+ 1 => '下架',
|
|
|
161
|
+ })
|
|
|
162
|
+ ->color(fn ($record) => match ($record->visible) {
|
|
|
163
|
+ 0 => 'warning',
|
|
|
164
|
+ 1 => 'gray',
|
|
|
165
|
+ })
|
|
|
166
|
+ ->icon(fn ($record) => match ($record->visible) {
|
|
|
167
|
+ 0 => 'heroicon-m-chevron-double-up',
|
|
|
168
|
+ 1 => 'heroicon-m-chevron-double-down',
|
|
|
169
|
+ })
|
|
|
170
|
+ ->action(function ($record) {
|
|
|
171
|
+ $record->visible = !$record->visible;
|
|
|
172
|
+ $record->save();
|
|
|
173
|
+ })
|
|
|
174
|
+ ->outlined()
|
|
|
175
|
+ ->requiresConfirmation(),
|
|
|
176
|
+ ])
|
|
|
177
|
+ ->bulkActions([
|
|
|
178
|
+ Tables\Actions\BulkActionGroup::make([
|
|
|
179
|
+ Tables\Actions\DeleteBulkAction::make(),
|
|
|
180
|
+ ]),
|
|
|
181
|
+ ])
|
|
|
182
|
+ ->defaultSort('order', 'desc')
|
|
|
183
|
+ ->defaultSort('created_at', 'desc');
|
|
|
184
|
+ }
|
|
|
185
|
+
|
|
|
186
|
+ public static function getRelations(): array
|
|
|
187
|
+ {
|
|
|
188
|
+ return [
|
|
|
189
|
+ //
|
|
|
190
|
+ ];
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+ public static function getPages(): array
|
|
|
194
|
+ {
|
|
|
195
|
+ return [
|
|
|
196
|
+ 'index' => Pages\ListAlbums::route('/'),
|
|
|
197
|
+ 'create' => Pages\CreateAlbum::route('/create'),
|
|
|
198
|
+ 'edit' => Pages\EditAlbum::route('/{record}/edit'),
|
|
|
199
|
+ 'view' => Pages\ViewAlbum::route('/{record}/view'),
|
|
|
200
|
+ ];
|
|
|
201
|
+ }
|
|
|
202
|
+}
|