Skip to content

Commit 586bf39

Browse files
committed
chore: Fix playlist view output; use unified query for consistent ordering and data
Resolves #652
1 parent bceffde commit 586bf39

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

app/Http/Controllers/Api/EpgApiController.php

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Facades\PlaylistFacade;
88
use App\Http\Controllers\Controller;
99
use App\Http\Controllers\LogoProxyController;
10+
use App\Http\Controllers\PlaylistGenerateController;
1011
use App\Models\Epg;
1112
use App\Models\Playlist;
1213
use App\Models\StreamProfile;
@@ -191,27 +192,7 @@ public function getDataForPlaylist(string $uuid, Request $request)
191192
]);
192193
try {
193194
// Get enabled channels from the playlist
194-
$playlistChannels = $playlist->channels()
195-
->leftJoin('groups', 'channels.group_id', '=', 'groups.id')
196-
->where('channels.enabled', true)
197-
->when(! $vod, function ($query) {
198-
return $query->where('channels.is_vod', false);
199-
})
200-
->with(['epgChannel', 'tags', 'group'])
201-
->orderBy('groups.sort_order') // Primary sort
202-
->orderBy('channels.sort') // Secondary sort
203-
->orderBy('channels.channel')
204-
->orderBy('channels.title')
205-
->when($search, function ($queryBuilder) use ($search) {
206-
$search = Str::lower($search);
207-
208-
return $queryBuilder->where(function ($query) use ($search) {
209-
$query->whereRaw('LOWER(channels.name) LIKE ?', ['%'.$search.'%'])
210-
->orWhereRaw('LOWER(channels.name_custom) LIKE ?', ['%'.$search.'%'])
211-
->orWhereRaw('LOWER(channels.title) LIKE ?', ['%'.$search.'%'])
212-
->orWhereRaw('LOWER(channels.title_custom) LIKE ?', ['%'.$search.'%']);
213-
});
214-
})
195+
$playlistChannels = PlaylistGenerateController::getChannelQuery($playlist)
215196
->limit($perPage)
216197
->offset($skip)
217198
->select('channels.*')
@@ -249,7 +230,7 @@ public function getDataForPlaylist(string $uuid, Request $request)
249230
$playlistChannelData = [];
250231
$channelSortIndex = $skip;
251232
foreach ($playlistChannels as $channel) {
252-
$epgData = $channel->epgChannel ?? null;
233+
$epgId = $channel->epg_id ?? null;
253234
$channelNo = $channel->channel;
254235
if (! $channelNo && ($playlist->auto_channel_increment || $idChannelBy === PlaylistChannelId::Number)) {
255236
$channelNo = ++$channelNumber;
@@ -258,25 +239,24 @@ public function getDataForPlaylist(string $uuid, Request $request)
258239
// Ensure we always have a unique identifier for the channel
259240
// Use database ID as fallback if channel number is not set
260241
$channelKey = $channelNo ?: $channel->id;
261-
if ($epgData) {
262-
$epgId = $epgData->epg_id;
242+
if ($epgId) {
263243
$epgIds[] = $epgId;
264244
if (! isset($epgChannelMap[$epgId])) {
265245
$epgChannelMap[$epgId] = [];
266246
}
267247

268248
// Map EPG channel ID to playlist channel info
269249
// Store array of playlist channels for each EPG channel (one-to-many mapping)
270-
if (! isset($epgChannelMap[$epgId][$epgData->channel_id])) {
271-
$epgChannelMap[$epgId][$epgData->channel_id] = [];
250+
if (! isset($epgChannelMap[$epgId][$channel->epg_channel_id])) {
251+
$epgChannelMap[$epgId][$channel->epg_channel_id] = [];
272252
}
273253

274254
$logo = url('/placeholder.png');
275255
if ($channel->logo) {
276256
// Logo override takes precedence
277257
$logo = $channel->logo;
278-
} elseif ($channel->logo_type === ChannelLogoType::Epg && $channel->epgChannel && $channel->epgChannel->icon) {
279-
$logo = $channel->epgChannel->icon;
258+
} elseif ($channel->logo_type === ChannelLogoType::Epg && ($channel->epg_icon || $channel->epg_icon_custom)) {
259+
$logo = $channel->epg_icon_custom ?? $channel->epg_icon ?? '';
280260
} elseif ($channel->logo_type === ChannelLogoType::Channel && ($channel->logo || $channel->logo_internal)) {
281261
$logo = $channel->logo ?? $channel->logo_internal ?? '';
282262
$logo = filter_var($logo, FILTER_VALIDATE_URL) ? $logo : url('/placeholder.png');
@@ -286,7 +266,7 @@ public function getDataForPlaylist(string $uuid, Request $request)
286266
}
287267

288268
// Add the playlist channel info to the EPG channel map
289-
$epgChannelMap[$epgId][$epgData->channel_id][] = [
269+
$epgChannelMap[$epgId][$channel->epg_channel_id][] = [
290270
'playlist_channel_id' => $channelKey,
291271
'display_name' => $channel->title_custom ?? $channel->title,
292272
'title' => $channel->name_custom ?? $channel->name,
@@ -381,8 +361,8 @@ public function getDataForPlaylist(string $uuid, Request $request)
381361
'channel_number' => $channelNo,
382362
'group' => $channel->group ?? $channel->group_internal,
383363
'icon' => $icon,
384-
'has_epg' => $epgData !== null,
385-
'epg_channel_id' => $epgData->channel_id ?? null,
364+
'has_epg' => $epgId !== null,
365+
'epg_channel_id' => $channel->epg_channel_id ?? null,
386366
'tvg_shift' => (int) ($channel->tvg_shift ?? 0), // EPG time shift in hours
387367
'sort_index' => $channelSortIndex++,
388368
];

app/Http/Controllers/PlaylistGenerateController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,6 @@ private function getDeviceInfo(Request $request, $playlist, ?string $username =
598598

599599
/**
600600
* Build the base query for channels for a playlist.
601-
*
602-
* @param Playlist $playlist
603601
*/
604602
public static function getChannelQuery($playlist): mixed
605603
{
@@ -620,6 +618,8 @@ public static function getChannelQuery($playlist): mixed
620618

621619
// Join EPG channel data to avoid N+1 queries and select common fields
622620
$query->leftJoin('epg_channels', 'channels.epg_channel_id', '=', 'epg_channels.id')
621+
->selectRaw('epg_channels.epg_id as epg_id')
622+
->selectRaw('epg_channels.channel_id as epg_channel_id')
623623
->selectRaw('epg_channels.icon as epg_icon')
624624
->selectRaw('epg_channels.icon_custom as epg_icon_custom')
625625
// Alias the external EPG channel identifier to avoid clobbering the FK attribute

0 commit comments

Comments
 (0)