<commit_before><?php namespace CosmicRadioTV\Podcast\Components;

use Request;
use Cms\Classes\ComponentBase;
use CosmicRadioTV\Podcast\Models\Episode;
use CosmicRadioTV\Podcast\Models\Release;
use CosmicRadioTV\Podcast\Models\ReleaseType;
use CosmicRadioTV\Podcast\Classes\VideoUrlParser;

class EpisodePlayer extends ComponentBase
{

    public function componentDetails()
    {
        return [
            'name'        => 'EpisodePlayer Component',
            'description' => 'No description provided yet...'
        ];
    }

    public function defineProperties()
    {
        return [
            'episodeSlug' => [
                'title' =>          'Episode Slug',
                'type' =>           'string',
                'default' =>        '',
                'required' =>       true,
            ],
            'releaseId' => [
                'title' =>          'Release ID',
                'description' =>    'Choose a specific release (otherwise defaults to first one).',
                'type' =>           'dropdown',
                'depends' =>        ['episode_slug'],
            ],
            'releaseType' => [
                'title' =>          'Release Type',
                'description' =>    'If no release id is set, you can use this to choose the first release of a release type.',
                'type' =>           'dropdown',
            ],
            'width' => [
                'title' =>          'Width',
                'type' =>           'string',
                'default' =>        '640',
            ],
            'height' => [
                'title' =>          'Height',
                'type' =>           'string',
                'default' =>        '360',
            ]
        ];
    }

    public function getReleaseIdOptions()
    {
        $episodeSlug = Request::input('episodeSlug');
        $episode = Episode::where('slug', '=', $episodeSlug)->firstOrFail();

        return Release::where('episode_id', '=', $episode->id)->lists('url','id');
    }

    public function release()
    {
        $release = null;
        if (trim($this->property('releaseId')) != false) {
            $release = Release::where('id', '=', trim($this->property('releaseId')))->firstOrFail();
        } else {
            $episode = Episode::where('slug','=',trim($this->property('episodeSlug')))->firstOrFail();
            $release_query = Release::where('episode_id','=',$episode->id);
            if (trim($this->property('releaseType')) != false) {
                $release_query = $release_query->where('release_type_id','=',trim($this->property('releaseType')));
            }
            $release = $release_query->firstOrFail();
        }
        return $release;
    }

    public function releaseType()
    {
        $release = $this->release();
        return ReleaseType::where('id','=',$release->release_type_id)->firstOrFail();
    }

    public function onRun()
    {
        $this->addJs('/plugins/cosmicradiotv/podcast/assets/vendor/jquery/js/jquery.min.js');
        $this->addJs('/plugins/cosmicradiotv/podcast/assets/vendor/mediaelement/js/mediaelement-and-player.min.js');
        $this->addJs('/plugins/cosmicradiotv/podcast/assets/js/podcast.js');

        $this->addCss('/plugins/cosmicradiotv/podcast/assets/vendor/mediaelement/css/mediaelementplayer.css');
    }

    /**
     * Gets the embed url from a youtube url.<commit_msg>Fixed the release type dropdown for the podcast player not getting filled (I forgot to even do it in the first place). Made the player component much less prone to exceptions.<commit_after><?php namespace CosmicRadioTV\Podcast\Components;

use Request;
use Cms\Classes\ComponentBase;
use CosmicRadioTV\Podcast\Models\Episode;
use CosmicRadioTV\Podcast\Models\Release;
use CosmicRadioTV\Podcast\Models\ReleaseType;
use CosmicRadioTV\Podcast\Classes\VideoUrlParser;

class EpisodePlayer extends ComponentBase
{

    public function componentDetails()
    {
        return [
            'name'        => 'EpisodePlayer Component',
            'description' => 'No description provided yet...'
        ];
    }

    public function defineProperties()
    {
        return [
            'episodeSlug' => [
                'title' =>          'Episode Slug',
                'type' =>           'string',
                'default' =>        '',
                'required' =>       true,
            ],
            'releaseId' => [
                'title' =>          'Release ID',
                'description' =>    'Choose a specific release (otherwise defaults to first one).',
                'type' =>           'dropdown',
                'depends' =>        ['episode_slug'],
                'placeholder'       => 'Select Release',
            ],
            'releaseType' => [
                'title' =>          'Release Type',
                'description' =>    'If no release id is set, you can use this to choose the first release of a release type.',
                'type' =>           'dropdown',
                'depends' =>        ['episode_slug'],
                'placeholder'       => 'Select Release Type',
            ],
            'width' => [
                'title' =>          'Width',
                'type' =>           'string',
                'default' =>        '640',
            ],
            'height' => [
                'title' =>          'Height',
                'type' =>           'string',
                'default' =>        '360',
            ]
        ];
    }

    public function getReleaseIdOptions()
    {
        $episodeSlug = Request::input('episodeSlug');
        $episode = Episode::where('slug', '=', $episodeSlug)->take(1)->get()->first();
        if (!empty($episode)) {
            return Release::where('episode_id', '=', $episode->id)->lists('url','id');
        } else {
            return [];
        }
    }

    public function getReleaseTypeOptions()
    {
        return ReleaseType::all()->lists('name','slug');
    }

    public function release()
    {
        $release = null;
        if (trim($this->property('releaseId')) != false) {
            $release_query = Release::where('id', '=', trim($this->property('releaseId')));
        } else {
            $episode = Episode::where('slug','=',trim($this->property('episodeSlug')))->take(1)->get()->first();
            if (!empty($episode)) {
                $release_query = Release::where('episode_id','=',$episode->id);
                if (trim($this->property('releaseType')) != false) {
                    $release_type = ReleaseType::where('slug','=',trim($this->property('releaseType')))->take(1)->get()->first();

                    if (!empty($release_type)) {
                        $release_query = $release_query->where('release_type_id','=',$release_type->id);
                    }
                }
            }
        }
        return $release_query->take(1)->get()->first();;
    }

    public function releaseType()
    {
        $release = $this->release();
        return ReleaseType::where('id','=',$release->release_type_id)->take(1)->get()->first();
    }

    public function onRun()
    {
        $this->addJs('/plugins/cosmicradiotv/podcast/assets/vendor/jquery/js/jquery.min.js');
        $this->addJs('/plugins/cosmicradiotv/podcast/assets/vendor/mediaelement/js/mediaelement-and-player.min.js');
        $this->addJs('/plugins/cosmicradiotv/podcast/assets/js/podcast.js');

        $this->addCss('/plugins/cosmicradiotv/podcast/assets/vendor/mediaelement/css/mediaelementplayer.css');
    }

    /**
     * Gets the embed url from a youtube url.
{'fraction_non_alphanumeric': 0.15528113504992117, 'fraction_numerical': 0.002233315817130846, 'mean_word_length': 1.8269587820274786, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 106, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3922874', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2069, 'n_words': 371}