2020-06-08 20:32:42 +00:00
|
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
|
/**
|
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
|
* @link https://castopod.org/
|
|
|
|
|
*/
|
2020-06-08 20:32:42 +00:00
|
|
|
|
|
|
|
|
|
namespace App\Entities;
|
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
|
use App\Libraries\SimpleRSSElement;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
use App\Models\NoteModel;
|
2021-05-14 17:59:35 +00:00
|
|
|
|
use App\Models\PersonModel;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
use App\Models\PodcastModel;
|
|
|
|
|
use App\Models\SoundbiteModel;
|
2021-05-06 14:00:48 +00:00
|
|
|
|
use CodeIgniter\Entity\Entity;
|
|
|
|
|
use CodeIgniter\Files\File;
|
|
|
|
|
use CodeIgniter\HTTP\Files\UploadedFile;
|
2020-10-22 17:41:59 +00:00
|
|
|
|
use CodeIgniter\I18n\Time;
|
2020-08-04 11:25:22 +00:00
|
|
|
|
use League\CommonMark\CommonMarkConverter;
|
2021-05-06 14:00:48 +00:00
|
|
|
|
use RuntimeException;
|
2020-06-08 20:32:42 +00:00
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* @property int $id
|
|
|
|
|
* @property int $podcast_id
|
|
|
|
|
* @property Podcast $podcast
|
|
|
|
|
* @property string $link
|
|
|
|
|
* @property string $guid
|
|
|
|
|
* @property string $slug
|
|
|
|
|
* @property string $title
|
|
|
|
|
* @property File $audio_file
|
|
|
|
|
* @property string $audio_file_url
|
|
|
|
|
* @property string $audio_file_analytics_url
|
|
|
|
|
* @property string $audio_file_web_url
|
|
|
|
|
* @property string $audio_file_opengraph_url
|
|
|
|
|
* @property string $audio_file_path
|
|
|
|
|
* @property double $audio_file_duration
|
|
|
|
|
* @property string $audio_file_mimetype
|
|
|
|
|
* @property int $audio_file_size
|
|
|
|
|
* @property int $audio_file_header_size
|
2021-05-14 17:59:35 +00:00
|
|
|
|
* @property string|null $description Holds text only description, striped of any markdown or html special characters
|
2021-05-12 14:00:25 +00:00
|
|
|
|
* @property string $description_markdown
|
|
|
|
|
* @property string $description_html
|
|
|
|
|
* @property Image $image
|
|
|
|
|
* @property string|null $image_path
|
|
|
|
|
* @property string|null $image_mimetype
|
|
|
|
|
* @property File|null $transcript_file
|
|
|
|
|
* @property string|null $transcript_file_url
|
|
|
|
|
* @property string|null $transcript_file_path
|
|
|
|
|
* @property string|null $transcript_file_remote_url
|
|
|
|
|
* @property File|null $chapters_file
|
|
|
|
|
* @property string|null $chapters_file_url
|
|
|
|
|
* @property string|null $chapters_file_path
|
|
|
|
|
* @property string|null $chapters_file_remote_url
|
|
|
|
|
* @property string|null $parental_advisory
|
|
|
|
|
* @property int $number
|
|
|
|
|
* @property int $season_number
|
|
|
|
|
* @property string $type
|
|
|
|
|
* @property bool $is_blocked
|
2021-05-17 17:11:23 +00:00
|
|
|
|
* @property Location|null $location
|
2021-05-12 14:00:25 +00:00
|
|
|
|
* @property string|null $location_name
|
|
|
|
|
* @property string|null $location_geo
|
2021-05-17 17:11:23 +00:00
|
|
|
|
* @property string|null $location_osm
|
2021-05-12 14:00:25 +00:00
|
|
|
|
* @property array|null $custom_rss
|
|
|
|
|
* @property string $custom_rss_string
|
|
|
|
|
* @property int $favourites_total
|
|
|
|
|
* @property int $reblogs_total
|
|
|
|
|
* @property int $notes_total
|
|
|
|
|
* @property int $created_by
|
|
|
|
|
* @property int $updated_by
|
|
|
|
|
* @property string $publication_status;
|
|
|
|
|
* @property Time|null $published_at;
|
|
|
|
|
* @property Time $created_at;
|
|
|
|
|
* @property Time $updated_at;
|
|
|
|
|
* @property Time|null $deleted_at;
|
|
|
|
|
*
|
2021-05-14 17:59:35 +00:00
|
|
|
|
* @property Person[] $persons;
|
2021-05-12 14:00:25 +00:00
|
|
|
|
* @property Soundbite[] $soundbites;
|
|
|
|
|
* @property string $embeddable_player_url;
|
|
|
|
|
*/
|
2020-06-08 20:32:42 +00:00
|
|
|
|
class Episode extends Entity
|
|
|
|
|
{
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected Podcast $podcast;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected string $link;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected File $audio_file;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected string $audio_file_url;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected string $audio_file_analytics_url;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected string $audio_file_web_url;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected string $audio_file_opengraph_url;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected string $embeddable_player_url;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected Image $image;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-17 17:11:23 +00:00
|
|
|
|
protected ?string $description = null;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected File $transcript_file;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected File $chapters_file;
|
2020-11-24 20:18:08 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2021-05-18 17:16:36 +00:00
|
|
|
|
* @var Person[]|null
|
2020-11-24 20:18:08 +00:00
|
|
|
|
*/
|
2021-05-18 17:16:36 +00:00
|
|
|
|
protected ?array $persons = null;
|
2021-02-10 16:20:01 +00:00
|
|
|
|
|
2020-12-07 20:13:46 +00:00
|
|
|
|
/**
|
2021-05-18 17:16:36 +00:00
|
|
|
|
* @var Soundbite[]|null
|
2020-12-07 20:13:46 +00:00
|
|
|
|
*/
|
2021-05-18 17:16:36 +00:00
|
|
|
|
protected ?array $soundbites = null;
|
2020-12-07 20:13:46 +00:00
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
|
/**
|
2021-05-18 17:16:36 +00:00
|
|
|
|
* @var Note[]|null
|
2021-04-02 17:20:02 +00:00
|
|
|
|
*/
|
2021-05-18 17:16:36 +00:00
|
|
|
|
protected ?array $notes = null;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
2021-05-17 17:11:23 +00:00
|
|
|
|
protected ?Location $location = null;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
protected string $custom_rss_string;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
|
2021-05-17 17:11:23 +00:00
|
|
|
|
protected ?string $publication_status = null;
|
2020-10-22 17:41:59 +00:00
|
|
|
|
|
2021-03-19 16:12:36 +00:00
|
|
|
|
/**
|
2021-05-12 14:00:25 +00:00
|
|
|
|
* @var string[]
|
2021-03-19 16:12:36 +00:00
|
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
|
protected $dates = ['published_at', 'created_at', 'updated_at', 'deleted_at'];
|
2020-08-14 18:27:57 +00:00
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* @var array<string, string>
|
|
|
|
|
*/
|
2020-06-08 20:32:42 +00:00
|
|
|
|
protected $casts = [
|
2020-12-07 20:13:46 +00:00
|
|
|
|
'id' => 'integer',
|
2021-04-02 17:20:02 +00:00
|
|
|
|
'podcast_id' => 'integer',
|
2020-08-21 08:41:09 +00:00
|
|
|
|
'guid' => 'string',
|
2020-06-08 20:32:42 +00:00
|
|
|
|
'slug' => 'string',
|
|
|
|
|
'title' => 'string',
|
2021-05-03 17:39:58 +00:00
|
|
|
|
'audio_file_path' => 'string',
|
2021-05-12 14:00:25 +00:00
|
|
|
|
'audio_file_duration' => 'double',
|
2021-05-03 17:39:58 +00:00
|
|
|
|
'audio_file_mimetype' => 'string',
|
|
|
|
|
'audio_file_size' => 'integer',
|
|
|
|
|
'audio_file_header_size' => 'integer',
|
2020-10-29 15:45:19 +00:00
|
|
|
|
'description_markdown' => 'string',
|
|
|
|
|
'description_html' => 'string',
|
2021-05-03 17:39:58 +00:00
|
|
|
|
'image_path' => '?string',
|
2021-04-02 17:20:02 +00:00
|
|
|
|
'image_mimetype' => '?string',
|
2021-05-03 17:39:58 +00:00
|
|
|
|
'transcript_file_path' => '?string',
|
|
|
|
|
'transcript_file_remote_url' => '?string',
|
|
|
|
|
'chapters_file_path' => '?string',
|
|
|
|
|
'chapters_file_remote_url' => '?string',
|
2020-10-02 15:38:16 +00:00
|
|
|
|
'parental_advisory' => '?string',
|
2020-08-21 08:41:09 +00:00
|
|
|
|
'number' => '?integer',
|
2020-06-08 20:32:42 +00:00
|
|
|
|
'season_number' => '?integer',
|
|
|
|
|
'type' => 'string',
|
2020-10-29 15:45:19 +00:00
|
|
|
|
'is_blocked' => 'boolean',
|
2020-12-23 14:11:38 +00:00
|
|
|
|
'location_name' => '?string',
|
|
|
|
|
'location_geo' => '?string',
|
2021-05-17 17:11:23 +00:00
|
|
|
|
'location_osm' => '?string',
|
2021-03-19 16:12:36 +00:00
|
|
|
|
'custom_rss' => '?json-array',
|
2021-04-02 17:20:02 +00:00
|
|
|
|
'favourites_total' => 'integer',
|
|
|
|
|
'reblogs_total' => 'integer',
|
|
|
|
|
'notes_total' => 'integer',
|
2020-08-14 18:27:57 +00:00
|
|
|
|
'created_by' => 'integer',
|
|
|
|
|
'updated_by' => 'integer',
|
2020-06-08 20:32:42 +00:00
|
|
|
|
];
|
2020-06-26 14:34:52 +00:00
|
|
|
|
|
2020-08-21 08:41:09 +00:00
|
|
|
|
/**
|
|
|
|
|
* Saves an episode image
|
|
|
|
|
*/
|
2021-05-14 17:59:35 +00:00
|
|
|
|
public function setImage(?Image $image = null): static
|
2020-06-30 18:17:41 +02:00
|
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
|
if ($image === null) {
|
|
|
|
|
return $this;
|
2020-06-30 18:17:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
// Save image
|
2021-06-08 09:52:11 +00:00
|
|
|
|
$image->saveImage('podcasts/' . $this->getPodcast()->name, $this->attributes['slug']);
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
|
|
|
|
$this->attributes['image_mimetype'] = $image->mimetype;
|
|
|
|
|
$this->attributes['image_path'] = $image->path;
|
|
|
|
|
|
2020-06-30 18:17:41 +02:00
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
|
public function getImage(): Image
|
2020-06-26 14:34:52 +00:00
|
|
|
|
{
|
2021-05-03 17:39:58 +00:00
|
|
|
|
if ($imagePath = $this->attributes['image_path']) {
|
2021-06-08 09:52:11 +00:00
|
|
|
|
return new Image(null, $imagePath, $this->attributes['image_mimetype']);
|
2020-06-30 18:17:41 +02:00
|
|
|
|
}
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
2021-05-25 18:00:09 +00:00
|
|
|
|
return $this->getPodcast()
|
|
|
|
|
->image;
|
2020-06-30 18:17:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 08:41:09 +00:00
|
|
|
|
/**
|
2021-05-03 17:39:58 +00:00
|
|
|
|
* Saves an audio file
|
2020-08-21 08:41:09 +00:00
|
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
|
public function setAudioFile(UploadedFile | File $audioFile): static
|
2020-08-21 08:41:09 +00:00
|
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
|
helper(['media', 'id3']);
|
2020-09-04 09:09:26 +00:00
|
|
|
|
|
2021-05-17 17:11:23 +00:00
|
|
|
|
$audioMetadata = get_file_tags($audioFile);
|
2020-06-30 18:17:41 +02:00
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
$this->attributes['audio_file_path'] = save_media(
|
|
|
|
|
$audioFile,
|
|
|
|
|
'podcasts/' . $this->getPodcast()->name,
|
|
|
|
|
$this->attributes['slug'],
|
|
|
|
|
);
|
|
|
|
|
$this->attributes['audio_file_duration'] =
|
2021-05-17 17:11:23 +00:00
|
|
|
|
$audioMetadata['playtime_seconds'];
|
|
|
|
|
$this->attributes['audio_file_mimetype'] = $audioMetadata['mime_type'];
|
|
|
|
|
$this->attributes['audio_file_size'] = $audioMetadata['filesize'];
|
2021-05-12 14:00:25 +00:00
|
|
|
|
$this->attributes['audio_file_header_size'] =
|
2021-05-17 17:11:23 +00:00
|
|
|
|
$audioMetadata['avdataoffset'];
|
2020-06-30 18:17:41 +02:00
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
return $this;
|
2020-06-30 18:17:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 20:18:08 +00:00
|
|
|
|
/**
|
2021-05-03 17:39:58 +00:00
|
|
|
|
* Saves an episode transcript file
|
2020-11-24 20:18:08 +00:00
|
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
|
public function setTranscriptFile(UploadedFile | File $transcriptFile): static
|
2020-11-24 20:18:08 +00:00
|
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
|
helper('media');
|
2020-11-24 20:18:08 +00:00
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
$this->attributes['transcript_file_path'] = save_media(
|
|
|
|
|
$transcriptFile,
|
2021-06-11 09:06:06 +00:00
|
|
|
|
'podcasts/' . $this->getPodcast()
|
2021-05-19 16:35:13 +00:00
|
|
|
|
->name,
|
2021-05-12 14:00:25 +00:00
|
|
|
|
$this->attributes['slug'] . '-transcript',
|
|
|
|
|
);
|
2020-11-24 20:18:08 +00:00
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-05-03 17:39:58 +00:00
|
|
|
|
* Saves an episode chapters file
|
2020-11-24 20:18:08 +00:00
|
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
|
public function setChaptersFile(UploadedFile | File $chaptersFile): static
|
2020-11-24 20:18:08 +00:00
|
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
|
helper('media');
|
2020-11-24 20:18:08 +00:00
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
$this->attributes['chapters_file_path'] = save_media(
|
|
|
|
|
$chaptersFile,
|
2021-06-11 09:06:06 +00:00
|
|
|
|
'podcasts/' . $this->getPodcast()
|
2021-05-19 16:35:13 +00:00
|
|
|
|
->name,
|
2021-05-12 14:00:25 +00:00
|
|
|
|
$this->attributes['slug'] . '-chapters',
|
|
|
|
|
);
|
2020-11-24 20:18:08 +00:00
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getAudioFile(): File
|
2020-06-30 18:17:41 +02:00
|
|
|
|
{
|
2021-05-03 17:39:58 +00:00
|
|
|
|
helper('media');
|
2020-06-26 14:34:52 +00:00
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
|
return new File(media_path($this->audio_file_path));
|
2020-11-24 20:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getTranscriptFile(): ?File
|
2020-11-24 20:18:08 +00:00
|
|
|
|
{
|
2021-05-03 17:39:58 +00:00
|
|
|
|
if ($this->attributes['transcript_file_path']) {
|
|
|
|
|
helper('media');
|
2020-11-24 20:18:08 +00:00
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
|
return new File(media_path($this->attributes['transcript_file_path']));
|
2021-05-03 17:39:58 +00:00
|
|
|
|
}
|
2020-06-30 18:17:41 +02:00
|
|
|
|
|
2021-05-03 17:39:58 +00:00
|
|
|
|
return null;
|
2020-06-26 14:34:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getChaptersFile(): ?File
|
2020-11-24 20:18:08 +00:00
|
|
|
|
{
|
2021-05-03 17:39:58 +00:00
|
|
|
|
if ($this->attributes['chapters_file_path']) {
|
|
|
|
|
helper('media');
|
2020-11-24 20:18:08 +00:00
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
|
return new File(media_path($this->attributes['chapters_file_path']));
|
2021-05-03 17:39:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2020-11-24 20:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getAudioFileUrl(): string
|
2020-11-24 20:18:08 +00:00
|
|
|
|
{
|
|
|
|
|
helper('media');
|
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
|
return media_base_url($this->audio_file_path);
|
2020-11-24 20:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getAudioFileAnalyticsUrl(): string
|
2020-06-26 14:34:52 +00:00
|
|
|
|
{
|
2020-10-29 17:27:16 +01:00
|
|
|
|
helper('analytics');
|
|
|
|
|
|
2021-06-11 08:53:14 +00:00
|
|
|
|
// remove 'podcasts/' from audio file path
|
|
|
|
|
$strippedAudioFilePath = substr($this->audio_file_path, 9);
|
|
|
|
|
|
2021-04-14 15:58:40 +00:00
|
|
|
|
return generate_episode_analytics_url(
|
|
|
|
|
$this->podcast_id,
|
|
|
|
|
$this->id,
|
2021-06-11 08:53:14 +00:00
|
|
|
|
$strippedAudioFilePath,
|
2021-05-03 17:39:58 +00:00
|
|
|
|
$this->audio_file_duration,
|
|
|
|
|
$this->audio_file_size,
|
|
|
|
|
$this->audio_file_header_size,
|
2021-04-14 15:58:40 +00:00
|
|
|
|
$this->published_at,
|
2020-06-26 14:34:52 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getAudioFileWebUrl(): string
|
2020-10-26 16:13:43 +00:00
|
|
|
|
{
|
2021-05-03 17:39:58 +00:00
|
|
|
|
return $this->getAudioFileAnalyticsUrl() . '?_from=-+Website+-';
|
2020-10-26 16:13:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getAudioFileOpengraphUrl(): string
|
2020-11-04 17:03:20 +00:00
|
|
|
|
{
|
2021-05-03 17:39:58 +00:00
|
|
|
|
return $this->getAudioFileAnalyticsUrl() . '?_from=-+Open+Graph+-';
|
2020-11-04 17:03:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 17:39:58 +00:00
|
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
|
* Gets transcript url from transcript file uri if it exists or returns the transcript_file_remote_url which can be
|
|
|
|
|
* null.
|
2021-05-03 17:39:58 +00:00
|
|
|
|
*/
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getTranscriptFileUrl(): ?string
|
2020-11-24 20:18:08 +00:00
|
|
|
|
{
|
2021-05-03 17:39:58 +00:00
|
|
|
|
if ($this->attributes['transcript_file_path']) {
|
2021-05-06 14:00:48 +00:00
|
|
|
|
return media_base_url($this->attributes['transcript_file_path']);
|
2021-05-03 17:39:58 +00:00
|
|
|
|
}
|
2021-05-14 17:59:35 +00:00
|
|
|
|
return $this->attributes['transcript_file_remote_url'];
|
2020-11-24 20:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 17:39:58 +00:00
|
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
|
* Gets chapters file url from chapters file uri if it exists or returns the chapters_file_remote_url which can be
|
|
|
|
|
* null.
|
2021-05-03 17:39:58 +00:00
|
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
|
public function getChaptersFileUrl(): ?string
|
2020-11-24 20:18:08 +00:00
|
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
|
if ($this->chapters_file_path) {
|
|
|
|
|
return media_base_url($this->chapters_file_path);
|
2021-05-03 17:39:58 +00:00
|
|
|
|
}
|
2021-05-06 14:00:48 +00:00
|
|
|
|
|
|
|
|
|
return $this->chapters_file_remote_url;
|
2020-11-24 20:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 16:20:01 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the episode's persons
|
|
|
|
|
*
|
2021-05-14 17:59:35 +00:00
|
|
|
|
* @return Person[]
|
2021-02-10 16:20:01 +00:00
|
|
|
|
*/
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getPersons(): array
|
2021-02-10 16:20:01 +00:00
|
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
|
if ($this->id === null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
|
throw new RuntimeException('Episode must be created before getting persons.');
|
2021-02-10 16:20:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
|
if ($this->persons === null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
|
$this->persons = (new PersonModel())->getEpisodePersons($this->podcast_id, $this->id);
|
2021-02-10 16:20:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
|
return $this->persons;
|
2021-02-10 16:20:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-07 20:13:46 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the episode’s soundbites
|
|
|
|
|
*
|
2021-05-12 14:00:25 +00:00
|
|
|
|
* @return Soundbite[]
|
2020-12-07 20:13:46 +00:00
|
|
|
|
*/
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getSoundbites(): array
|
2020-12-07 20:13:46 +00:00
|
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
|
if ($this->id === null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
|
throw new RuntimeException('Episode must be created before getting soundbites.');
|
2020-12-07 20:13:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
|
if ($this->soundbites === null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
|
$this->soundbites = (new SoundbiteModel())->getEpisodeSoundbites($this->getPodcast() ->id, $this->id);
|
2020-12-07 20:13:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->soundbites;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* @return Note[]
|
|
|
|
|
*/
|
|
|
|
|
public function getNotes(): array
|
2021-04-02 17:20:02 +00:00
|
|
|
|
{
|
2021-05-18 17:16:36 +00:00
|
|
|
|
if ($this->id === null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
|
throw new RuntimeException('Episode must be created before getting soundbites.');
|
2021-04-02 17:20:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
|
if ($this->notes === null) {
|
2021-04-02 17:20:02 +00:00
|
|
|
|
$this->notes = (new NoteModel())->getEpisodeNotes($this->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->notes;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getLink(): string
|
2020-06-26 14:34:52 +00:00
|
|
|
|
{
|
2021-06-09 12:40:22 +00:00
|
|
|
|
return base_url(route_to('episode', $this->getPodcast() ->name, $this->attributes['slug']));
|
2020-06-26 14:34:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
public function getEmbeddablePlayerUrl(string $theme = null): string
|
2021-02-27 21:21:26 +00:00
|
|
|
|
{
|
|
|
|
|
return base_url(
|
|
|
|
|
$theme
|
|
|
|
|
? route_to(
|
|
|
|
|
'embeddable-player-theme',
|
2021-05-19 16:35:13 +00:00
|
|
|
|
$this->getPodcast()
|
|
|
|
|
->name,
|
2021-02-27 21:21:26 +00:00
|
|
|
|
$this->attributes['slug'],
|
2021-04-02 17:20:02 +00:00
|
|
|
|
$theme,
|
2021-02-27 21:21:26 +00:00
|
|
|
|
)
|
2021-06-09 12:40:22 +00:00
|
|
|
|
: route_to('embeddable-player', $this->getPodcast() ->name, $this->attributes['slug']),
|
2021-02-27 21:21:26 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
public function setGuid(?string $guid = null): static
|
2020-06-26 14:34:52 +00:00
|
|
|
|
{
|
2021-05-14 17:59:35 +00:00
|
|
|
|
$this->attributes['guid'] = $guid === null ? $this->getLink() : $guid;
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
|
|
|
|
return $this;
|
2020-06-26 14:34:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
public function getPodcast(): ?Podcast
|
2020-06-26 14:34:52 +00:00
|
|
|
|
{
|
2021-06-08 09:52:11 +00:00
|
|
|
|
return (new PodcastModel())->getPodcastById($this->podcast_id);
|
2020-06-26 14:34:52 +00:00
|
|
|
|
}
|
2020-07-27 09:35:34 +00:00
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
public function setDescriptionMarkdown(string $descriptionMarkdown): static
|
2020-07-27 09:35:34 +00:00
|
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
|
$converter = new CommonMarkConverter([
|
|
|
|
|
'html_input' => 'strip',
|
|
|
|
|
'allow_unsafe_links' => false,
|
|
|
|
|
]);
|
2020-07-27 09:35:34 +00:00
|
|
|
|
|
2020-10-29 15:45:19 +00:00
|
|
|
|
$this->attributes['description_markdown'] = $descriptionMarkdown;
|
2021-06-08 09:52:11 +00:00
|
|
|
|
$this->attributes['description_html'] = $converter->convertToHtml($descriptionMarkdown);
|
2020-10-29 15:45:19 +00:00
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getDescriptionHtml(?string $serviceSlug = null): string
|
2020-10-29 15:45:19 +00:00
|
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
|
$descriptionHtml = '';
|
|
|
|
|
if (
|
2021-05-19 16:35:13 +00:00
|
|
|
|
$this->getPodcast()
|
|
|
|
|
->partner_id !== null &&
|
|
|
|
|
$this->getPodcast()
|
|
|
|
|
->partner_link_url !== null &&
|
|
|
|
|
$this->getPodcast()
|
|
|
|
|
->partner_image_url !== null
|
2021-05-12 14:00:25 +00:00
|
|
|
|
) {
|
2021-05-19 16:35:13 +00:00
|
|
|
|
$descriptionHtml .= "<div><a href=\"{$this->getPartnerLink(
|
|
|
|
|
$serviceSlug,
|
|
|
|
|
)}\" rel=\"sponsored noopener noreferrer\" target=\"_blank\"><img src=\"{$this->getPartnerImageUrl(
|
|
|
|
|
$serviceSlug,
|
2021-05-12 14:00:25 +00:00
|
|
|
|
)}\" alt=\"Partner image\" /></a></div>";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$descriptionHtml .= $this->attributes['description_html'];
|
|
|
|
|
|
|
|
|
|
if ($this->getPodcast()->episode_description_footer_html) {
|
2021-05-19 16:35:13 +00:00
|
|
|
|
$descriptionHtml .= "<footer>{$this->getPodcast()
|
|
|
|
|
->episode_description_footer_html}</footer>";
|
2021-05-12 14:00:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $descriptionHtml;
|
2020-10-29 15:45:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getDescription(): string
|
2020-10-29 15:45:19 +00:00
|
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
|
if ($this->description === null) {
|
|
|
|
|
$this->description = trim(
|
2021-06-09 12:40:22 +00:00
|
|
|
|
preg_replace('~\s+~', ' ', strip_tags($this->attributes['description_html'])),
|
2021-05-12 14:00:25 +00:00
|
|
|
|
);
|
2020-10-29 15:45:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
return $this->description;
|
2020-07-27 09:35:34 +00:00
|
|
|
|
}
|
2020-08-14 18:27:57 +00:00
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getPublicationStatus(): string
|
2020-10-22 17:41:59 +00:00
|
|
|
|
{
|
2021-05-17 17:11:23 +00:00
|
|
|
|
if ($this->publication_status === null) {
|
|
|
|
|
if ($this->published_at === null) {
|
|
|
|
|
$this->publication_status = 'not_published';
|
|
|
|
|
} elseif ($this->published_at->isBefore(Time::now())) {
|
|
|
|
|
$this->publication_status = 'published';
|
|
|
|
|
} else {
|
|
|
|
|
$this->publication_status = 'scheduled';
|
|
|
|
|
}
|
2020-12-09 17:52:30 +00:00
|
|
|
|
}
|
2020-10-22 17:41:59 +00:00
|
|
|
|
|
2021-05-17 17:11:23 +00:00
|
|
|
|
return $this->publication_status;
|
2020-10-22 17:41:59 +00:00
|
|
|
|
}
|
2020-12-23 14:11:38 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Saves the location name and fetches OpenStreetMap info
|
|
|
|
|
*/
|
2021-05-17 17:11:23 +00:00
|
|
|
|
public function setLocation(?Location $location = null): static
|
2020-12-23 14:11:38 +00:00
|
|
|
|
{
|
2021-05-17 17:11:23 +00:00
|
|
|
|
if ($location === null) {
|
2021-05-12 14:00:25 +00:00
|
|
|
|
$this->attributes['location_name'] = null;
|
|
|
|
|
$this->attributes['location_geo'] = null;
|
2021-05-17 17:11:23 +00:00
|
|
|
|
$this->attributes['location_osm'] = null;
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
2021-05-17 17:11:23 +00:00
|
|
|
|
return $this;
|
|
|
|
|
}
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
2020-12-23 14:11:38 +00:00
|
|
|
|
if (
|
2021-05-19 16:35:13 +00:00
|
|
|
|
! isset($this->attributes['location_name']) ||
|
2021-05-17 17:11:23 +00:00
|
|
|
|
$this->attributes['location_name'] !== $location->name
|
2020-12-23 14:11:38 +00:00
|
|
|
|
) {
|
2021-05-17 17:11:23 +00:00
|
|
|
|
$location->fetchOsmLocation();
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
2021-05-17 17:11:23 +00:00
|
|
|
|
$this->attributes['location_name'] = $location->name;
|
|
|
|
|
$this->attributes['location_geo'] = $location->geo;
|
|
|
|
|
$this->attributes['location_osm'] = $location->osm;
|
2020-12-23 14:11:38 +00:00
|
|
|
|
}
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
2020-12-23 14:11:38 +00:00
|
|
|
|
return $this;
|
|
|
|
|
}
|
2021-03-19 16:12:36 +00:00
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
|
public function getLocation(): ?Location
|
|
|
|
|
{
|
|
|
|
|
if ($this->location_name === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->location === null) {
|
2021-06-08 09:52:11 +00:00
|
|
|
|
$this->location = new Location($this->location_name, $this->location_geo, $this->location_osm);
|
2021-05-12 14:00:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->location;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 16:12:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* Get custom rss tag as XML String
|
|
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
|
public function getCustomRssString(): string
|
2021-03-19 16:12:36 +00:00
|
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
|
if ($this->custom_rss === null) {
|
2021-03-19 16:12:36 +00:00
|
|
|
|
return '';
|
|
|
|
|
}
|
2021-05-06 14:00:48 +00:00
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
->addChild('item');
|
2021-05-19 16:35:13 +00:00
|
|
|
|
array_to_rss([
|
|
|
|
|
'elements' => $this->custom_rss,
|
2021-06-08 09:52:11 +00:00
|
|
|
|
], $xmlNode);
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
|
return str_replace(['<item>', '</item>'], '', $xmlNode->asXML());
|
2021-03-19 16:12:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Saves custom rss tag into json
|
|
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
|
public function setCustomRssString(?string $customRssString = null): static
|
2021-03-19 16:12:36 +00:00
|
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
|
if ($customRssString === null) {
|
2021-05-06 14:00:48 +00:00
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 16:12:36 +00:00
|
|
|
|
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><item>' .
|
|
|
|
|
$customRssString .
|
2021-04-02 17:20:02 +00:00
|
|
|
|
'</item></channel></rss>',
|
|
|
|
|
),
|
2021-03-19 16:12:36 +00:00
|
|
|
|
)['elements'][0]['elements'][0];
|
2021-05-06 14:00:48 +00:00
|
|
|
|
|
2021-03-19 16:12:36 +00:00
|
|
|
|
if (array_key_exists('elements', $customRssArray)) {
|
2021-06-08 09:52:11 +00:00
|
|
|
|
$this->attributes['custom_rss'] = json_encode($customRssArray['elements']);
|
2021-03-19 16:12:36 +00:00
|
|
|
|
} else {
|
|
|
|
|
$this->attributes['custom_rss'] = null;
|
|
|
|
|
}
|
2021-05-06 14:00:48 +00:00
|
|
|
|
|
|
|
|
|
return $this;
|
2021-03-19 16:12:36 +00:00
|
|
|
|
}
|
2021-03-30 16:21:00 +00:00
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
|
public function getPartnerLink(?string $serviceSlug = null): string
|
2021-03-30 16:21:00 +00:00
|
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
|
$partnerLink =
|
|
|
|
|
rtrim($this->getPodcast()->partner_link_url, '/') .
|
2021-03-30 16:21:00 +00:00
|
|
|
|
'?pid=' .
|
2021-05-19 16:35:13 +00:00
|
|
|
|
$this->getPodcast()
|
|
|
|
|
->partner_id .
|
2021-03-30 16:21:00 +00:00
|
|
|
|
'&guid=' .
|
2021-05-12 14:00:25 +00:00
|
|
|
|
urlencode($this->attributes['guid']);
|
|
|
|
|
|
|
|
|
|
if ($serviceSlug !== null) {
|
|
|
|
|
$partnerLink .= '&_from=' . $serviceSlug;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $partnerLink;
|
2021-03-30 16:21:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
|
public function getPartnerImageUrl(string $serviceSlug = null): string
|
2021-03-30 16:21:00 +00:00
|
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
|
if ($serviceSlug !== null) {
|
2021-05-14 17:59:35 +00:00
|
|
|
|
return '&_from=' . $serviceSlug;
|
2021-05-12 14:00:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
|
return rtrim($this->getPodcast()->partner_image_url, '/') .
|
2021-05-17 17:11:23 +00:00
|
|
|
|
'?pid=' .
|
2021-05-19 16:35:13 +00:00
|
|
|
|
$this->getPodcast()
|
|
|
|
|
->partner_id .
|
2021-05-17 17:11:23 +00:00
|
|
|
|
'&guid=' .
|
|
|
|
|
urlencode($this->attributes['guid']);
|
2021-03-30 16:21:00 +00:00
|
|
|
|
}
|
2020-06-08 20:32:42 +00:00
|
|
|
|
}
|