Skip to content
Snippets Groups Projects
PodcastController.php 11.1 KiB
Newer Older
/**
 * @copyright  2020 Podlibre
 * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
 * @link       https://castopod.org/
 */
use CodeIgniter\Exceptions\PageNotFoundException;
use Config\Database;
use App\Models\CategoryModel;
use App\Models\LanguageModel;
use App\Models\PodcastModel;
use App\Models\EpisodeModel;
use CodeIgniter\HTTP\RedirectResponse;
class PodcastController extends BaseController
    public function _remap(string $method, string ...$params): mixed
        if (
            ($this->podcast = (new PodcastModel())->getPodcastById(
                (int) $params[0],
            )) !== null
        ) {
            return $this->$method();
        }

        throw PageNotFoundException::forPageNotFound();
    public function list(): string
                'podcasts' => (new PodcastModel())->getUserPodcasts(
                    (int) user_id(),
                ),
            ];
        } else {
            $data = ['podcasts' => (new PodcastModel())->findAll()];
        return view('admin/podcast/list', $data);
    }

    public function view(): string
        $data = ['podcast' => $this->podcast];
        replace_breadcrumb_params([0 => $this->podcast->title]);
        return view('admin/podcast/view', $data);
    public function viewAnalytics(): string
    {
        $data = ['podcast' => $this->podcast];

        replace_breadcrumb_params([0 => $this->podcast->title]);
    public function viewAnalyticsWebpages(): string
    {
        $data = ['podcast' => $this->podcast];

        replace_breadcrumb_params([0 => $this->podcast->title]);
        return view('admin/podcast/analytics/webpages', $data);
    }

    public function viewAnalyticsLocations(): string
    {
        $data = ['podcast' => $this->podcast];

        replace_breadcrumb_params([0 => $this->podcast->title]);
        return view('admin/podcast/analytics/locations', $data);
    }

    public function viewAnalyticsUniqueListeners(): string
    {
        $data = ['podcast' => $this->podcast];

        replace_breadcrumb_params([0 => $this->podcast->title]);
        return view('admin/podcast/analytics/unique_listeners', $data);
    }

    public function viewAnalyticsListeningTime(): string
    {
        $data = ['podcast' => $this->podcast];

        replace_breadcrumb_params([0 => $this->podcast->title]);
        return view('admin/podcast/analytics/listening_time', $data);
    public function viewAnalyticsTimePeriods(): string
    {
        $data = ['podcast' => $this->podcast];

        replace_breadcrumb_params([0 => $this->podcast->title]);
        return view('admin/podcast/analytics/time_periods', $data);
    }

    public function viewAnalyticsPlayers(): string
    {
        $data = ['podcast' => $this->podcast];

        replace_breadcrumb_params([0 => $this->podcast->title]);
        return view('admin/podcast/analytics/players', $data);
    public function create(): string
        $languageOptions = (new LanguageModel())->getLanguageOptions();
        $categoryOptions = (new CategoryModel())->getCategoryOptions();
            'languageOptions' => $languageOptions,
            'categoryOptions' => $categoryOptions,
                $this->request->getServer('HTTP_ACCEPT_LANGUAGE'),
        return view('admin/podcast/create', $data);
    public function attemptCreate(): RedirectResponse
            'image' =>
                'uploaded[image]|is_image[image]|ext_in[image,jpg,png]|min_dims[image,1400,1400]|is_image_squared[image]',
        ];

        if (!$this->validate($rules)) {
            return redirect()
                ->back()
                ->withInput()
                ->with('errors', $this->validator->getErrors());
        }

            'title' => $this->request->getPost('title'),
            'name' => $this->request->getPost('name'),
            'description_markdown' => $this->request->getPost('description'),
            'image' => new Image($this->request->getFile('image')),
            'language_code' => $this->request->getPost('language'),
            'category_id' => $this->request->getPost('category'),
            'parental_advisory' =>
                $this->request->getPost('parental_advisory') !== 'undefined'
                    ? $this->request->getPost('parental_advisory')
                    : null,
            'owner_name' => $this->request->getPost('owner_name'),
            'owner_email' => $this->request->getPost('owner_email'),
            'publisher' => $this->request->getPost('publisher'),
            'type' => $this->request->getPost('type'),
            'copyright' => $this->request->getPost('copyright'),
            'location' => new Location(
                $this->request->getPost('location_name'),
            ),
            'payment_pointer' => $this->request->getPost('payment_pointer'),
            'custom_rss_string' => $this->request->getPost('custom_rss'),
            'partner_id' => $this->request->getPost('partner_id'),
            'partner_link_url' => $this->request->getPost('partner_link_url'),
            'partner_image_url' => $this->request->getPost('partner_image_url'),
            'is_blocked' => $this->request->getPost('block') === 'yes',
            'is_completed' => $this->request->getPost('complete') === 'yes',
            'is_locked' => $this->request->getPost('lock') === 'yes',
            'created_by' => user_id(),
            'updated_by' => user_id(),
        if (!($newPodcastId = $podcastModel->insert($podcast, true))) {
                ->with('errors', $podcastModel->errors());
        $podcastAdminGroup = $authorize->group('podcast_admin');
        // set Podcast categories
        (new CategoryModel())->setPodcastCategories(
            $this->request->getPost('other_categories'),
        // set interact as the newly created podcast actor
        $createdPodcast = (new PodcastModel())->getPodcastById($newPodcastId);
        set_interact_as_actor($createdPodcast->actor_id);

        return redirect()->route('podcast-view', [$newPodcastId]);
    public function edit(): string
        $languageOptions = (new LanguageModel())->getLanguageOptions();
        $categoryOptions = (new CategoryModel())->getCategoryOptions();
        $data = [
            'podcast' => $this->podcast,
            'languageOptions' => $languageOptions,
            'categoryOptions' => $categoryOptions,
        replace_breadcrumb_params([0 => $this->podcast->title]);
        return view('admin/podcast/edit', $data);
    public function attemptEdit(): RedirectResponse
                'is_image[image]|ext_in[image,jpg,png]|min_dims[image,1400,1400]|is_image_squared[image]',
        ];

        if (!$this->validate($rules)) {
            return redirect()
                ->back()
                ->withInput()
                ->with('errors', $this->validator->getErrors());
        }

        $this->podcast->title = $this->request->getPost('title');
        $this->podcast->description_markdown = $this->request->getPost(

        $image = $this->request->getFile('image');
        if ($image !== null && $image->isValid()) {
            $this->podcast->image = new Image($image);
        $this->podcast->language_code = $this->request->getPost('language');
        $this->podcast->category_id = $this->request->getPost('category');
        $this->podcast->parental_advisory =
            $this->request->getPost('parental_advisory') !== 'undefined'
                ? $this->request->getPost('parental_advisory')
                : null;
        $this->podcast->publisher = $this->request->getPost('publisher');
        $this->podcast->owner_name = $this->request->getPost('owner_name');
        $this->podcast->owner_email = $this->request->getPost('owner_email');
        $this->podcast->type = $this->request->getPost('type');
        $this->podcast->copyright = $this->request->getPost('copyright');
        $this->podcast->location = new Location(
            $this->request->getPost('location_name'),
        );
        $this->podcast->payment_pointer = $this->request->getPost(
        $this->podcast->custom_rss_string = $this->request->getPost(
        $this->podcast->partner_id = $this->request->getPost('partner_id');
        $this->podcast->partner_link_url = $this->request->getPost(
        );
        $this->podcast->partner_image_url = $this->request->getPost(
        $this->podcast->is_blocked = $this->request->getPost('block') === 'yes';
        $this->podcast->is_completed =
            $this->request->getPost('complete') === 'yes';
        $this->podcast->is_locked = $this->request->getPost('lock') === 'yes';
        $this->podcast->updated_by = (int) user_id();
        $podcastModel = new PodcastModel();
        if (!$podcastModel->update($this->podcast->id, $this->podcast)) {
            $db->transRollback();
                ->with('errors', $podcastModel->errors());
        // set Podcast categories
        (new CategoryModel())->setPodcastCategories(
            $this->podcast->id,
            $this->request->getPost('other_categories'),
        );

        $db->transComplete();

        return redirect()->route('podcast-view', [$this->podcast->id]);
    }

    public function latestEpisodes(int $limit, int $podcast_id): string
            ->orderBy('created_at', 'desc')
            ->findAll($limit);

        return view('admin/podcast/latest_episodes', ['episodes' => $episodes]);
    public function delete(): RedirectResponse
        (new PodcastModel())->delete($this->podcast->id);
        return redirect()->route('podcast-list');