|
9 | 9 | use Filament\Actions\ActionGroup; |
10 | 10 | use Filament\Actions\DeleteAction; |
11 | 11 | use Filament\Forms\Components\Select; |
| 12 | +use Filament\Forms\Components\TextInput; |
12 | 13 | use Filament\Notifications\Notification; |
13 | 14 | use Filament\Resources\Pages\ViewRecord; |
14 | 15 | use Filament\Schemas\Components\Utilities\Get; |
@@ -110,6 +111,68 @@ protected function getHeaderActions(): array |
110 | 111 | ->modalDescription('Move the group channels to the another group.') |
111 | 112 | ->modalSubmitActionLabel('Move now'), |
112 | 113 |
|
| 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 | + |
113 | 176 | Action::make('enable') |
114 | 177 | ->label('Enable group channels') |
115 | 178 | ->action(function ($record): void { |
|
0 commit comments