Skip to content
Snippets Groups Projects
Podcast.php 13.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * @copyright  2020 Podlibre
     * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
     * @link       https://castopod.org/
     */
    
    
    namespace App\Entities;
    
    
    use App\Libraries\Image;
    use App\Libraries\SimpleRSSElement;
    
    use App\Models\EpisodeModel;
    
    use App\Models\PodcastPersonModel;
    
    use CodeIgniter\Files\File;
    use CodeIgniter\HTTP\Files\UploadedFile;
    
    use League\CommonMark\CommonMarkConverter;
    
    
    class Podcast extends Entity
    {
    
        /**
         * @var string
         */
        protected $link;
    
        /**
    
        protected $episodes;
    
         */
        protected $other_categories;
    
        /**
         * @var integer[]
         */
        protected $other_categories_ids;
    
    
         * Holds text only description, striped of any markdown or html special characters
         *
         * @var string
    
        /**
         * Return custom rss as string
         *
         * @var string
         */
        protected $custom_rss_string;
    
    
        protected $casts = [
    
            'name' => 'string',
    
            'description_markdown' => 'string',
            'description_html' => 'string',
    
            'language_code' => 'string',
    
            'category_id' => 'integer',
    
            'parental_advisory' => '?string',
            'publisher' => '?string',
    
            'owner_name' => 'string',
            'owner_email' => 'string',
    
            'copyright' => '?string',
    
            'episode_description_footer_markdown' => '?string',
            'episode_description_footer_html' => '?string',
            'is_blocked' => 'boolean',
            'is_completed' => 'boolean',
            'is_locked' => 'boolean',
    
            'imported_feed_url' => '?string',
    
            'new_feed_url' => '?string',
    
            'location_name' => '?string',
            'location_geo' => '?string',
            'location_osmid' => '?string',
    
            'payment_pointer' => '?string',
    
            'partner_id' => '?string',
            'partner_link_url' => '?string',
            'partner_image_url' => '?string',
    
            'created_by' => 'integer',
            'updated_by' => 'integer',
    
                    'Podcast must have an actor_id before getting actor.',
                );
            }
    
            if (empty($this->actor)) {
    
                $this->actor = model('ActorModel')->getActorById($this->actor_id);
    
        /**
         * Saves a cover image to the corresponding podcast folder in `public/media/podcast_name/`
         *
    
        {
            if ($image) {
                helper('media');
    
    
                $this->attributes['image_mimetype'] = $image->getMimeType();
    
                $this->attributes['image_path'] = save_media(
    
                    'podcasts/' . $this->attributes['name'],
                    'cover',
    
                    $this->attributes['image_path'],
    
                    $this->attributes['image_mimetype'],
    
                );
                $this->image->saveSizes();
    
                $this->attributes['image_path'],
    
            return url_to('podcast-activity', $this->attributes['name']);
    
            return url_to('podcast_feed', $this->attributes['name']);
    
                    'Podcast must be created before getting episodes.',
    
                $this->episodes = (new EpisodeModel())->getPodcastEpisodes(
    
        /**
         * Returns the podcast's persons
         *
    
                    'Podcast must be created before getting persons.',
    
                $this->persons = (new PodcastPersonModel())->getPodcastPersons(
    
        /**
         * Returns the podcast category entity
         *
    
                    'Podcast must be created before getting category.',
    
                $this->category = (new CategoryModel())->getCategoryById(
                    $this->category_id,
                );
    
                    'Podcasts must be created before getting contributors.',
    
                );
            }
    
            if (empty($this->contributors)) {
                $this->contributors = (new UserModel())->getPodcastContributors(
    
        public function setDescriptionMarkdown(string $descriptionMarkdown): self
    
            $converter = new CommonMarkConverter([
                'html_input' => 'strip',
                'allow_unsafe_links' => false,
            ]);
    
            $this->attributes['description_markdown'] = $descriptionMarkdown;
            $this->attributes['description_html'] = $converter->convertToHtml(
    
            );
    
            return $this;
        }
    
        public function setEpisodeDescriptionFooterMarkdown(
    
            ?string $episodeDescriptionFooterMarkdown = null
        ): self {
    
            if ($episodeDescriptionFooterMarkdown) {
                $converter = new CommonMarkConverter([
                    'html_input' => 'strip',
                    'allow_unsafe_links' => false,
                ]);
    
                $this->attributes[
                    'episode_description_footer_markdown'
                ] = $episodeDescriptionFooterMarkdown;
                $this->attributes[
                    'episode_description_footer_html'
                ] = $converter->convertToHtml($episodeDescriptionFooterMarkdown);
            }
    
            return $this;
        }
    
    
        {
            if ($this->description) {
                return $this->description;
            }
    
            return trim(
                preg_replace(
                    '/\s+/',
                    ' ',
    
                    strip_tags($this->attributes['description_html']),
                ),
    
         * Returns the podcast's podcasting platform links
    
                    'Podcast must be created before getting podcasting platform links.',
    
            if (empty($this->podcastingPlatforms)) {
                $this->podcastingPlatforms = (new PlatformModel())->getPodcastPlatforms(
                    $this->id,
    
                );
            }
    
            return $this->podcastingPlatforms;
        }
    
        /**
         * Returns the podcast's social platform links
         *
    
                    'Podcast must be created before getting social platform links.',
    
                );
            }
    
            if (empty($this->socialPlatforms)) {
                $this->socialPlatforms = (new PlatformModel())->getPodcastPlatforms(
                    $this->id,
    
                );
            }
    
            return $this->socialPlatforms;
        }
    
        /**
         * Returns the podcast's funding platform links
         *
    
                    'Podcast must be created before getting funding platform links.',
    
                );
            }
    
            if (empty($this->fundingPlatforms)) {
                $this->fundingPlatforms = (new PlatformModel())->getPodcastPlatforms(
                    $this->id,
    
                    'Podcast must be created before getting other categories.',
    
                );
            }
    
            if (empty($this->other_categories)) {
                $this->other_categories = (new CategoryModel())->getPodcastCategories(
    
        /**
         * @return array<int>
         */
        public function getOtherCategoriesIds(): array
    
        {
            if (empty($this->other_categories_ids)) {
                $this->other_categories_ids = array_column(
                    $this->getOtherCategories(),
    
    
        /**
         * Saves the location name and fetches OpenStreetMap info
         */
    
        public function setLocation(?string $locationName = null): self
    
        {
            helper('location');
    
            if (
                $locationName &&
                (empty($this->attributes['location_name']) ||
                    $this->attributes['location_name'] != $locationName)
            ) {
                $this->attributes['location_name'] = $locationName;
                if ($location = fetch_osm_location($locationName)) {
                    $this->attributes['location_geo'] = $location['geo'];
                    $this->attributes['location_osmid'] = $location['osmid'];
                }
            } elseif (empty($locationName)) {
                $this->attributes['location_name'] = null;
                $this->attributes['location_geo'] = null;
                $this->attributes['location_osmid'] = null;
            }
    
    
            helper('rss');
    
            $xmlNode = (new SimpleRSSElement(
                '<?xml version="1.0" encoding="utf-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:podcast="https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"/>',
            ))->addChild('channel');
            array_to_rss(
                [
                    'elements' => $this->custom_rss,
                ],
                $xmlNode,
            );
    
            return str_replace(['<channel>', '</channel>'], '', $xmlNode->asXML());
    
        }
    
        /**
         * Saves custom rss tag into json
         *
         * @param string $customRssString
         */
    
        function setCustomRssString($customRssString): self
    
            helper('rss');
            $customRssArray = rss_to_array(
                simplexml_load_string(
                    '<?xml version="1.0" encoding="utf-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:podcast="https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"><channel>' .
                        $customRssString .
    
            if (array_key_exists('elements', $customRssArray)) {
                $this->attributes['custom_rss'] = json_encode(