*/ protected $casts = [ 'id' => 'integer', 'podcast_id' => 'integer', 'guid' => 'string', 'slug' => 'string', 'title' => 'string', 'audio_id' => 'integer', 'description_markdown' => 'string', 'description_html' => 'string', 'cover_id' => '?integer', 'transcript_id' => '?integer', 'transcript_remote_url' => '?string', 'chapters_id' => '?integer', 'chapters_remote_url' => '?string', 'parental_advisory' => '?string', 'number' => '?integer', 'season_number' => '?integer', 'type' => 'string', 'is_blocked' => 'boolean', 'location_name' => '?string', 'location_geo' => '?string', 'location_osm' => '?string', 'custom_rss' => '?json-array', 'is_published_on_hubs' => 'boolean', 'posts_count' => 'integer', 'comments_count' => 'integer', 'is_premium' => 'boolean', 'created_by' => 'integer', 'updated_by' => 'integer', ]; public function setCover(UploadedFile | File $file = null): self { if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) { return $this; } if (array_key_exists('cover_id', $this->attributes) && $this->attributes['cover_id'] !== null) { $this->getCover() ->setFile($file); $this->getCover() ->updated_by = (int) user_id(); (new MediaModel('image'))->updateMedia($this->getCover()); } else { $cover = new Image([ 'file_name' => $this->attributes['slug'], 'file_directory' => 'podcasts/' . $this->getPodcast()->handle, 'sizes' => config('Images') ->podcastCoverSizes, 'uploaded_by' => user_id(), 'updated_by' => user_id(), ]); $cover->setFile($file); $this->attributes['cover_id'] = (new MediaModel('image'))->saveMedia($cover); } return $this; } public function getCover(): Image { if ($this->cover instanceof Image) { return $this->cover; } if ($this->cover_id === null) { $this->cover = $this->getPodcast() ->getCover(); return $this->cover; } $this->cover = (new MediaModel('image'))->getMediaById($this->cover_id); return $this->cover; } public function setAudio(UploadedFile | File $file = null): self { if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) { return $this; } if ($this->audio_id !== 0) { $this->getAudio() ->setFile($file); $this->getAudio() ->updated_by = (int) user_id(); (new MediaModel('audio'))->updateMedia($this->getAudio()); } else { $audio = new Audio([ 'file_name' => pathinfo($file->getRandomName(), PATHINFO_FILENAME), 'file_directory' => 'podcasts/' . $this->getPodcast()->handle, 'language_code' => $this->getPodcast() ->language_code, 'uploaded_by' => user_id(), 'updated_by' => user_id(), ]); $audio->setFile($file); $this->attributes['audio_id'] = (new MediaModel())->saveMedia($audio); } return $this; } public function getAudio(): Audio { if (! $this->audio instanceof Audio) { $this->audio = (new MediaModel('audio'))->getMediaById($this->audio_id); } return $this->audio; } public function setTranscript(UploadedFile | File $file = null): self { if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) { return $this; } if ($this->getTranscript() !== null) { $this->getTranscript() ->setFile($file); $this->getTranscript() ->updated_by = (int) user_id(); (new MediaModel('transcript'))->updateMedia($this->getTranscript()); } else { $transcript = new Transcript([ 'file_name' => $this->attributes['slug'] . '-transcript', 'file_directory' => 'podcasts/' . $this->getPodcast()->handle, 'language_code' => $this->getPodcast() ->language_code, 'uploaded_by' => user_id(), 'updated_by' => user_id(), ]); $transcript->setFile($file); $this->attributes['transcript_id'] = (new MediaModel('transcript'))->saveMedia($transcript); } return $this; } public function getTranscript(): ?Transcript { if ($this->transcript_id !== null && $this->transcript === null) { $this->transcript = (new MediaModel('transcript'))->getMediaById($this->transcript_id); } return $this->transcript; } public function setChapters(UploadedFile | File $file = null): self { if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) { return $this; } if ($this->getChapters() !== null) { $this->getChapters() ->setFile($file); $this->getChapters() ->updated_by = (int) user_id(); (new MediaModel('chapters'))->updateMedia($this->getChapters()); } else { $chapters = new Chapters([ 'file_name' => $this->attributes['slug'] . '-chapters', 'file_directory' => 'podcasts/' . $this->getPodcast()->handle, 'language_code' => $this->getPodcast() ->language_code, 'uploaded_by' => user_id(), 'updated_by' => user_id(), ]); $chapters->setFile($file); $this->attributes['chapters_id'] = (new MediaModel('chapters'))->saveMedia($chapters); } return $this; } public function getChapters(): ?Chapters { if ($this->chapters_id !== null && $this->chapters === null) { $this->chapters = (new MediaModel('chapters'))->getMediaById($this->chapters_id); } return $this->chapters; } public function getAudioUrl(): string { return url_to('episode-audio', $this->getPodcast()->handle, $this->slug); } public function getAudioWebUrl(): string { return $this->getAudioUrl() . '?_from=-+Website+-'; } public function getAudioOpengraphUrl(): string { return $this->getAudioUrl() . '?_from=-+Open+Graph+-'; } /** * Gets transcript url from transcript file uri if it exists or returns the transcript_remote_url which can be null. */ public function getTranscriptUrl(): ?string { if ($this->transcript !== null) { return $this->transcript->file_url; } return $this->transcript_remote_url; } /** * Gets chapters file url from chapters file uri if it exists or returns the chapters_remote_url which can be null. */ public function getChaptersFileUrl(): ?string { if ($this->chapters !== null) { return $this->chapters->file_url; } return $this->chapters_remote_url; } /** * Returns the episode's persons * * @return Person[] */ public function getPersons(): array { if ($this->id === null) { throw new RuntimeException('Episode must be created before getting persons.'); } if ($this->persons === null) { $this->persons = (new PersonModel())->getEpisodePersons($this->podcast_id, $this->id); } return $this->persons; } /** * Returns the episode’s clips * * @return Soundbite[] */ public function getSoundbites(): array { if ($this->id === null) { throw new RuntimeException('Episode must be created before getting soundbites.'); } if ($this->soundbites === null) { $this->soundbites = (new ClipModel())->getEpisodeSoundbites($this->getPodcast()->id, $this->id); } return $this->soundbites; } /** * @return Post[] */ public function getPosts(): array { if ($this->id === null) { throw new RuntimeException('Episode must be created before getting posts.'); } if ($this->posts === null) { $this->posts = (new PostModel())->getEpisodePosts($this->id); } return $this->posts; } /** * @return EpisodeComment[] */ public function getComments(): array { if ($this->id === null) { throw new RuntimeException('Episode must be created before getting comments.'); } if ($this->comments === null) { $this->comments = (new EpisodeCommentModel())->getEpisodeComments($this->id); } return $this->comments; } public function getLink(): string { return url_to('episode', esc($this->getPodcast()->handle), esc($this->attributes['slug'])); } public function getEmbedUrl(string $theme = null): string { return $theme ? url_to('embed-theme', esc($this->getPodcast()->handle), esc($this->attributes['slug']), $theme,) : url_to('embed', esc($this->getPodcast()->handle), esc($this->attributes['slug'])); } public function setGuid(?string $guid = null): static { $this->attributes['guid'] = $guid === null ? $this->getLink() : $guid; return $this; } public function getPodcast(): ?Podcast { return (new PodcastModel())->getPodcastById($this->podcast_id); } public function setDescriptionMarkdown(string $descriptionMarkdown): static { $config = [ 'html_input' => 'escape', 'allow_unsafe_links' => false, ]; $environment = new Environment($config); $environment->addExtension(new CommonMarkCoreExtension()); $environment->addExtension(new AutolinkExtension()); $environment->addExtension(new SmartPunctExtension()); $environment->addExtension(new DisallowedRawHtmlExtension()); $converter = new MarkdownConverter($environment); $this->attributes['description_markdown'] = $descriptionMarkdown; $this->attributes['description_html'] = $converter->convert($descriptionMarkdown); return $this; } public function getDescriptionHtml(?string $serviceSlug = null): string { $descriptionHtml = ''; if ( $this->getPodcast() ->partner_id !== null && $this->getPodcast() ->partner_link_url !== null && $this->getPodcast() ->partner_image_url !== null ) { $descriptionHtml .= "
"; } $descriptionHtml .= $this->attributes['description_html']; if ($this->getPodcast()->episode_description_footer_html) { $descriptionHtml .= ""; } return $descriptionHtml; } public function getDescription(): string { if ($this->description === null) { $this->description = trim( preg_replace('~\s+~', ' ', strip_tags((string) $this->attributes['description_html'])), ); } return $this->description; } public function getPublicationStatus(): string { if ($this->publication_status === null) { if ($this->published_at === null) { $this->publication_status = 'not_published'; } elseif ($this->getPodcast()->publication_status !== 'published') { $this->publication_status = 'with_podcast'; } elseif ($this->published_at->isBefore(Time::now())) { $this->publication_status = 'published'; } else { $this->publication_status = 'scheduled'; } } return $this->publication_status; } /** * Saves the location name and fetches OpenStreetMap info */ public function setLocation(?Location $location = null): static { if ($location === null) { $this->attributes['location_name'] = null; $this->attributes['location_geo'] = null; $this->attributes['location_osm'] = null; return $this; } if ( ! isset($this->attributes['location_name']) || $this->attributes['location_name'] !== $location->name ) { $location->fetchOsmLocation(); $this->attributes['location_name'] = $location->name; $this->attributes['location_geo'] = $location->geo; $this->attributes['location_osm'] = $location->osm; } return $this; } public function getLocation(): ?Location { if ($this->location_name === null) { return null; } if ($this->location === null) { $this->location = new Location($this->location_name, $this->location_geo, $this->location_osm); } return $this->location; } /** * Get custom rss tag as XML String */ public function getCustomRssString(): string { if ($this->custom_rss === null) { return ''; } helper('rss'); $xmlNode = (new SimpleRSSElement( '