Skip to content
Snippets Groups Projects
PodcastController.php 6.91 KiB
Newer Older
/**
 * @copyright  2020 Podlibre
 * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
 * @link       https://castopod.org/
 */
namespace App\Controllers;
use App\Models\PodcastModel;
use CodeIgniter\Exceptions\PageNotFoundException;
class PodcastController extends BaseController
    public function _remap(string $method, string ...$params): mixed
            throw PageNotFoundException::forPageNotFound();
            ($this->podcast = (new PodcastModel())->getPodcastByName($params[0],)) !== null
        throw PageNotFoundException::forPageNotFound();
    public function activity(): string
        // Prevent analytics hit when authenticated
            $this->registerPodcastWebpageHit($this->podcast->id);
        }

        $cacheName = implode(
            '_',
            array_filter([
                'page',
                "podcast#{$this->podcast->id}",
                'activity',
                can_user_interact() ? '_authenticated' : null,
            ]),
        );

        if (! ($cachedView = cache($cacheName))) {
                'notes' => (new NoteModel())->getActorPublishedNotes($this->podcast->actor_id,),
            ];

            // if user is logged in then send to the authenticated activity view
            if (can_user_interact()) {
                helper('form');
                return view('podcast/activity_authenticated', $data);
            }

            $secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
                $this->podcast->id,
            );

            return view('podcast/activity', $data, [
                'cache' => $secondsToNextUnpublishedEpisode
                    ? $secondsToNextUnpublishedEpisode
                    : DECADE,
                'cache_name' => $cacheName,
            ]);
    public function episodes(): string
        // Prevent analytics hit when authenticated
            $this->registerPodcastWebpageHit($this->podcast->id);
        }
        $yearQuery = $this->request->getGet('year');
        $seasonQuery = $this->request->getGet('season');

        if (! $yearQuery && ! $seasonQuery) {
            $defaultQuery = (new PodcastModel())->getDefaultQuery($this->podcast->id);
                if ($defaultQuery['type'] === 'season') {
                    $seasonQuery = $defaultQuery['data']['season_number'];
                } elseif ($defaultQuery['type'] === 'year') {
                    $yearQuery = $defaultQuery['data']['year'];
                }
                "podcast#{$this->podcast->id}",
                $yearQuery ? 'year' . $yearQuery : null,
                $seasonQuery ? 'season' . $seasonQuery : null,
                can_user_interact() ? '_authenticated' : null,
        if (! ($cachedView = cache($cacheName))) {
            $podcastModel = new PodcastModel();
            $years = $podcastModel->getYears($this->podcast->id);
            $seasons = $podcastModel->getSeasons($this->podcast->id);

            $episodesNavigation = [];
            $activeQuery = null;
            foreach ($years as $year) {
                $isActive = $yearQuery === $year['year'];
                    $activeQuery = [
                        'type' => 'year',
                        'value' => $year['year'],
                        'label' => $year['year'],
                        'number_of_episodes' => $year['number_of_episodes'],
                    ];
                    'label' => $year['year'],
                    'number_of_episodes' => $year['number_of_episodes'],
                    'route' =>
                        route_to('podcast-episodes', $this->podcast->name) .
                        '?year=' .
                        $year['year'],
                    'is_active' => $isActive,
                $isActive = $seasonQuery === $season['season_number'];
                if ($isActive) {
                    $activeQuery = [
                        'type' => 'season',
                        'value' => $season['season_number'],
                        'label' => lang('Podcast.season', [
                            'seasonNumber' => $season['season_number'],
                        ]),
                        'number_of_episodes' => $season['number_of_episodes'],
                    'label' => lang('Podcast.season', [
                        'seasonNumber' => $season['season_number'],
                    ]),
                    'number_of_episodes' => $season['number_of_episodes'],
                    'route' =>
                        route_to('podcast-episodes', $this->podcast->name) .
                        '?season=' .
                        $season['season_number'],
                    'is_active' => $isActive,
            }

            $data = [
                'podcast' => $this->podcast,
                'episodesNav' => $episodesNavigation,
                'activeQuery' => $activeQuery,
                'episodes' => (new EpisodeModel())->getPodcastEpisodes(
                    $this->podcast->id,
                    $this->podcast->type,
                    $yearQuery,
            $secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
            // if user is logged in then send to the authenticated episodes view
            if (can_user_interact()) {
                return view('podcast/episodes_authenticated', $data);
            return view('podcast/episodes', $data, [
                'cache' => $secondsToNextUnpublishedEpisode
                    ? $secondsToNextUnpublishedEpisode
                    : DECADE,
                'cache_name' => $cacheName,
            ]);