Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 97 additions & 8 deletions app/Filament/Resources/Categories/CategoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Filament\Actions\ViewAction;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Infolists\Components\TextEntry;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
Expand Down Expand Up @@ -146,12 +147,27 @@ public static function table(Table $table): Table
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$set('category', null);
$set('new_category', null);
$set('create_new_category', false);
}
})
->searchable(),
Toggle::make('create_new_category')
->label('Create new category')
->helperText('Enable to create a new category instead of selecting an existing one.')
->live()
->disabled(fn (Get $get) => ! $get('playlist'))
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$set('category', null);
} else {
$set('new_category', null);
}
}),
Select::make('category')
->label('Custom Category')
->disabled(fn (Get $get) => ! $get('playlist'))
->hidden(fn (Get $get) => $get('create_new_category'))
->helperText(fn (Get $get) => ! $get('playlist') ? 'Select a custom playlist first.' : 'Select the category you would like to assign to the selected series to.')
->options(function ($get) {
$customList = CustomPlaylist::find($get('playlist'));
Expand All @@ -161,16 +177,46 @@ public static function table(Table $table): Table
->toArray() : [];
})
->searchable(),
TextInput::make('new_category')
->label('New Category Name')
->helperText('Enter a name for the new category to create.')
->hidden(fn (Get $get) => ! $get('create_new_category'))
->disabled(fn (Get $get) => ! $get('playlist'))
->required(fn (Get $get) => $get('create_new_category'))
->maxLength(255),
])
->action(function ($record, array $data): void {
$playlist = CustomPlaylist::findOrFail($data['playlist']);
$playlist->series()->syncWithoutDetaching($record->series()->pluck('id'));
$tags = $playlist->categoryTags()->get();
$tag = $playlist->categoryTags()->where('name->en', $data['category'])->first();
foreach ($record->series()->cursor() as $series) {
// Need to detach any existing tags from this playlist first
$series->detachTags($tags);
$series->attachTag($tag);

// Determine which tag to use (existing or new)
$tag = null;
if ($data['create_new_category'] && $data['new_category']) {
// Create new category tag
$tagType = $playlist->uuid.'-category';
$existingTag = \Spatie\Tags\Tag::where('type', $tagType)
->where('name->en', $data['new_category'])
->first();
if ($existingTag) {
$tag = $existingTag;
} else {
$tag = \Spatie\Tags\Tag::create([
'name' => ['en' => $data['new_category']],
'type' => $tagType,
]);
$playlist->attachTag($tag);
}
} elseif ($data['category']) {
$tag = $playlist->categoryTags()->where('name->en', $data['category'])->first();
}

if ($tag) {
$tags = $playlist->categoryTags()->get();
foreach ($record->series()->cursor() as $series) {
// Need to detach any existing tags from this playlist first
$series->detachTags($tags);
$series->attachTag($tag);
}
}
})->after(function () {
Notification::make()
Expand Down Expand Up @@ -309,12 +355,27 @@ public static function table(Table $table): Table
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$set('category', null);
$set('new_category', null);
$set('create_new_category', false);
}
})
->searchable(),
Toggle::make('create_new_category')
->label('Create new category')
->helperText('Enable to create a new category instead of selecting an existing one.')
->live()
->disabled(fn (Get $get) => ! $get('playlist'))
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$set('category', null);
} else {
$set('new_category', null);
}
}),
Select::make('category')
->label('Custom Category')
->disabled(fn (Get $get) => ! $get('playlist'))
->hidden(fn (Get $get) => $get('create_new_category'))
->helperText(fn (Get $get) => ! $get('playlist') ? 'Select a custom playlist first.' : 'Select the category you would like to assign to the selected series to.')
->options(function ($get) {
$customList = CustomPlaylist::find($get('playlist'));
Expand All @@ -324,17 +385,45 @@ public static function table(Table $table): Table
->toArray() : [];
})
->searchable(),
TextInput::make('new_category')
->label('New Category Name')
->helperText('Enter a name for the new category to create.')
->hidden(fn (Get $get) => ! $get('create_new_category'))
->disabled(fn (Get $get) => ! $get('playlist'))
->required(fn (Get $get) => $get('create_new_category'))
->maxLength(255),
])
->action(function (Collection $records, array $data): void {
$playlist = CustomPlaylist::findOrFail($data['playlist']);

// Determine which tag to use (existing or new)
$tag = null;
if ($data['create_new_category'] && $data['new_category']) {
// Create new category tag
$tagType = $playlist->uuid.'-category';
$existingTag = \Spatie\Tags\Tag::where('type', $tagType)
->where('name->en', $data['new_category'])
->first();
if ($existingTag) {
$tag = $existingTag;
} else {
$tag = \Spatie\Tags\Tag::create([
'name' => ['en' => $data['new_category']],
'type' => $tagType,
]);
$playlist->attachTag($tag);
}
} elseif ($data['category']) {
$tag = $playlist->categoryTags()->where('name->en', $data['category'])->first();
}

$tags = $playlist->categoryTags()->get();
$tag = $data['category'] ? $playlist->categoryTags()->where('name->en', $data['category'])->first() : null;
foreach ($records as $record) {
// Sync the series to the custom playlist
// This will add the series to the playlist without detaching existing ones
// Prevents duplicates in the playlist
$playlist->series()->syncWithoutDetaching($record->series()->pluck('id'));
if ($data['category']) {
if ($tag) {
foreach ($record->series()->cursor() as $series) {
// Need to detach any existing tags from this playlist first
$series->detachTags($tags);
Expand Down
49 changes: 47 additions & 2 deletions app/Filament/Resources/Categories/Pages/ViewCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Filament\Actions\Action;
use Filament\Actions\ActionGroup;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ViewRecord;
use Filament\Schemas\Components\Utilities\Get;
Expand All @@ -35,12 +37,27 @@ protected function getHeaderActions(): array
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$set('category', null);
$set('new_category', null);
$set('create_new_category', false);
}
})
->searchable(),
Toggle::make('create_new_category')
->label('Create new category')
->helperText('Enable to create a new category instead of selecting an existing one.')
->live()
->disabled(fn (Get $get) => ! $get('playlist'))
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$set('category', null);
} else {
$set('new_category', null);
}
}),
Select::make('category')
->label('Custom Category')
->disabled(fn (Get $get) => ! $get('playlist'))
->hidden(fn (Get $get) => $get('create_new_category'))
->helperText(fn (Get $get) => ! $get('playlist') ? 'Select a custom playlist first.' : 'Select the category you would like to assign to the series to.')
->options(function ($get) {
$customList = CustomPlaylist::find($get('playlist'));
Expand All @@ -50,13 +67,41 @@ protected function getHeaderActions(): array
->toArray() : [];
})
->searchable(),
TextInput::make('new_category')
->label('New Category Name')
->helperText('Enter a name for the new category to create.')
->hidden(fn (Get $get) => ! $get('create_new_category'))
->disabled(fn (Get $get) => ! $get('playlist'))
->required(fn (Get $get) => $get('create_new_category'))
->maxLength(255),
])
->action(function ($record, array $data): void {
$playlist = CustomPlaylist::findOrFail($data['playlist']);
$playlist->series()->syncWithoutDetaching($record->series()->pluck('id'));
if ($data['category']) {
$tags = $playlist->categoryTags()->get();

// Determine which tag to use (existing or new)
$tag = null;
if ($data['create_new_category'] && $data['new_category']) {
// Create new category tag
$tagType = $playlist->uuid.'-category';
$existingTag = \Spatie\Tags\Tag::where('type', $tagType)
->where('name->en', $data['new_category'])
->first();
if ($existingTag) {
$tag = $existingTag;
} else {
$tag = \Spatie\Tags\Tag::create([
'name' => ['en' => $data['new_category']],
'type' => $tagType,
]);
$playlist->attachTag($tag);
}
} elseif ($data['category']) {
$tag = $playlist->categoryTags()->where('name->en', $data['category'])->first();
}

if ($tag) {
$tags = $playlist->categoryTags()->get();
foreach ($record->series()->cursor() as $series) {
// Need to detach any existing tags from this playlist first
$series->detachTags($tags);
Expand Down
46 changes: 44 additions & 2 deletions app/Filament/Resources/Channels/ChannelResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,27 @@ public static function getTableBulkActions($addToCustom = true): array
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$set('category', null);
$set('new_group', null);
$set('create_new_group', false);
}
})
->searchable(),
Toggle::make('create_new_group')
->label('Create new group')
->helperText('Enable to create a new group instead of selecting an existing one.')
->live()
->disabled(fn (Get $get) => ! $get('playlist'))
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$set('category', null);
} else {
$set('new_group', null);
}
}),
Select::make('category')
->label('Custom Group')
->disabled(fn (Get $get) => ! $get('playlist'))
->hidden(fn (Get $get) => $get('create_new_group'))
->helperText(fn (Get $get) => ! $get('playlist') ? 'Select a custom playlist first.' : 'Select the group you would like to assign to the selected channel(s) to.')
->options(function ($get) {
$customList = CustomPlaylist::find($get('playlist'));
Expand All @@ -473,13 +488,40 @@ public static function getTableBulkActions($addToCustom = true): array
->toArray() : [];
})
->searchable(),
TextInput::make('new_group')
->label('New Group Name')
->helperText('Enter a name for the new group to create.')
->hidden(fn (Get $get) => ! $get('create_new_group'))
->disabled(fn (Get $get) => ! $get('playlist'))
->required(fn (Get $get) => $get('create_new_group'))
->maxLength(255),
])
->action(function (Collection $records, array $data): void {
$playlist = CustomPlaylist::findOrFail($data['playlist']);
$playlist->channels()->syncWithoutDetaching($records->pluck('id'));
if ($data['category']) {
$tags = $playlist->groupTags()->get();

// Determine which tag to use (existing or new)
$tag = null;
if ($data['create_new_group'] && $data['new_group']) {
// Create new group tag
$existingTag = \Spatie\Tags\Tag::where('type', $playlist->uuid)
->where('name->en', $data['new_group'])
->first();
if ($existingTag) {
$tag = $existingTag;
} else {
$tag = \Spatie\Tags\Tag::create([
'name' => ['en' => $data['new_group']],
'type' => $playlist->uuid,
]);
$playlist->attachTag($tag);
}
} elseif ($data['category']) {
$tag = $playlist->groupTags()->where('name->en', $data['category'])->first();
}

if ($tag) {
$tags = $playlist->groupTags()->get();
foreach ($records as $record) {
// Need to detach any existing tags from this playlist first
$record->detachTags($tags);
Expand Down
Loading