Skip to content

Commit 8b82c3b

Browse files
committed
feat: Add quick sort options to VOD Groups
1 parent c61a453 commit 8b82c3b

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

app/Filament/Resources/VodGroups/Pages/ViewVodGroup.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Filament\Actions\ActionGroup;
1010
use Filament\Actions\DeleteAction;
1111
use Filament\Forms\Components\Select;
12+
use Filament\Forms\Components\TextInput;
1213
use Filament\Notifications\Notification;
1314
use Filament\Resources\Pages\ViewRecord;
1415
use Filament\Schemas\Components\Utilities\Get;
@@ -110,6 +111,68 @@ protected function getHeaderActions(): array
110111
->modalDescription('Move the group channels to the another group.')
111112
->modalSubmitActionLabel('Move now'),
112113

114+
Action::make('recount')
115+
->label('Recount Channels')
116+
->icon('heroicon-o-hashtag')
117+
->schema([
118+
TextInput::make('start')
119+
->label('Start Number')
120+
->numeric()
121+
->default(1)
122+
->required(),
123+
])
124+
->action(function (Group $record, array $data): void {
125+
$start = (int) $data['start'];
126+
$channels = $record->channels()->orderBy('sort')->cursor();
127+
foreach ($channels as $channel) {
128+
$channel->update(['channel' => $start++]);
129+
}
130+
})
131+
->after(function () {
132+
Notification::make()
133+
->success()
134+
->title('Channels Recounted')
135+
->body('The channels in this group have been recounted.')
136+
->send();
137+
})
138+
->requiresConfirmation()
139+
->modalIcon('heroicon-o-hashtag')
140+
->modalDescription('Recount all channels in this group sequentially?'),
141+
Action::make('sort_alpha')
142+
->label('Sort Alpha')
143+
->icon('heroicon-o-bars-arrow-down')
144+
->schema([
145+
Select::make('sort')
146+
->label('Sort Order')
147+
->options([
148+
'ASC' => 'A to Z',
149+
'DESC' => 'Z to A',
150+
])
151+
->default('ASC')
152+
->required(),
153+
])
154+
->action(function (Group $record, array $data): void {
155+
// Sort by title_custom (if present) then title, matching the UI column sort
156+
$order = $data['sort'] ?? 'ASC';
157+
$channels = $record->channels()
158+
->orderByRaw("COALESCE(title_custom, title) $order")
159+
->get();
160+
$sort = 1;
161+
foreach ($channels as $channel) {
162+
$channel->update(['sort' => $sort++]);
163+
}
164+
})
165+
->after(function () {
166+
Notification::make()
167+
->success()
168+
->title('Channels Sorted')
169+
->body('The channels in this group have been sorted alphabetically.')
170+
->send();
171+
})
172+
->requiresConfirmation()
173+
->modalIcon('heroicon-o-bars-arrow-down')
174+
->modalDescription('Sort all channels in this group alphabetically? This will update the sort order.'),
175+
113176
Action::make('enable')
114177
->label('Enable group channels')
115178
->action(function ($record): void {

app/Filament/Resources/VodGroups/VodGroupResource.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,68 @@ public static function table(Table $table): Table
237237
->modalDescription('Move the group channels to the another group.')
238238
->modalSubmitActionLabel('Move now'),
239239

240+
Action::make('recount')
241+
->label('Recount Channels')
242+
->icon('heroicon-o-hashtag')
243+
->schema([
244+
TextInput::make('start')
245+
->label('Start Number')
246+
->numeric()
247+
->default(1)
248+
->required(),
249+
])
250+
->action(function (Group $record, array $data): void {
251+
$start = (int) $data['start'];
252+
$channels = $record->channels()->orderBy('sort')->cursor();
253+
foreach ($channels as $channel) {
254+
$channel->update(['channel' => $start++]);
255+
}
256+
})
257+
->after(function () {
258+
Notification::make()
259+
->success()
260+
->title('Channels Recounted')
261+
->body('The channels in this group have been recounted.')
262+
->send();
263+
})
264+
->requiresConfirmation()
265+
->modalIcon('heroicon-o-hashtag')
266+
->modalDescription('Recount all channels in this group sequentially?'),
267+
Action::make('sort_alpha')
268+
->label('Sort Alpha')
269+
->icon('heroicon-o-bars-arrow-down')
270+
->schema([
271+
Select::make('sort')
272+
->label('Sort Order')
273+
->options([
274+
'ASC' => 'A to Z',
275+
'DESC' => 'Z to A',
276+
])
277+
->default('ASC')
278+
->required(),
279+
])
280+
->action(function (Group $record, array $data): void {
281+
// Sort by title_custom (if present) then title, matching the UI column sort
282+
$order = $data['sort'] ?? 'ASC';
283+
$channels = $record->channels()
284+
->orderByRaw("COALESCE(title_custom, title) $order")
285+
->cursor();
286+
$sort = 1;
287+
foreach ($channels as $channel) {
288+
$channel->update(['sort' => $sort++]);
289+
}
290+
})
291+
->after(function () {
292+
Notification::make()
293+
->success()
294+
->title('Channels Sorted')
295+
->body('The channels in this group have been sorted alphabetically.')
296+
->send();
297+
})
298+
->requiresConfirmation()
299+
->modalIcon('heroicon-o-bars-arrow-down')
300+
->modalDescription('Sort all channels in this group alphabetically? This will update the sort order.'),
301+
240302
Action::make('enable')
241303
->label('Enable group channels')
242304
->action(function ($record): void {

0 commit comments

Comments
 (0)