2020-06-08 20:32:42 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
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;
|
|
|
|
|
2020-06-26 14:34:52 +00:00
|
|
|
use App\Models\PodcastModel;
|
2020-06-08 20:32:42 +00:00
|
|
|
use CodeIgniter\Entity;
|
2020-08-04 11:25:22 +00:00
|
|
|
use League\CommonMark\CommonMarkConverter;
|
2020-06-08 20:32:42 +00:00
|
|
|
|
|
|
|
class Episode extends Entity
|
|
|
|
{
|
2020-08-05 17:26:04 +00:00
|
|
|
/**
|
|
|
|
* @var \App\Entities\Podcast
|
|
|
|
*/
|
|
|
|
protected $podcast;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $GUID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $link;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \CodeIgniter\Files\File
|
|
|
|
*/
|
|
|
|
protected $image;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $image_media_path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $image_url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \CodeIgniter\Files\File
|
|
|
|
*/
|
|
|
|
protected $enclosure;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $enclosure_media_path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $enclosure_url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $enclosure_metadata;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description_html;
|
2020-06-26 14:34:52 +00:00
|
|
|
|
2020-06-08 20:32:42 +00:00
|
|
|
protected $casts = [
|
|
|
|
'slug' => 'string',
|
|
|
|
'title' => 'string',
|
2020-06-26 14:34:52 +00:00
|
|
|
'enclosure_uri' => 'string',
|
2020-06-08 20:32:42 +00:00
|
|
|
'pub_date' => 'datetime',
|
|
|
|
'description' => 'string',
|
2020-06-30 18:17:41 +02:00
|
|
|
'image_uri' => '?string',
|
2020-06-26 14:34:52 +00:00
|
|
|
'author_name' => '?string',
|
|
|
|
'author_email' => '?string',
|
2020-06-08 20:32:42 +00:00
|
|
|
'explicit' => 'boolean',
|
2020-06-12 19:31:10 +00:00
|
|
|
'number' => 'integer',
|
2020-06-08 20:32:42 +00:00
|
|
|
'season_number' => '?integer',
|
|
|
|
'type' => 'string',
|
|
|
|
'block' => 'boolean',
|
|
|
|
];
|
2020-06-26 14:34:52 +00:00
|
|
|
|
2020-06-30 18:17:41 +02:00
|
|
|
public function setImage(?\CodeIgniter\HTTP\Files\UploadedFile $image)
|
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
if (!empty($image) && $image->isValid()) {
|
2020-06-30 18:17:41 +02:00
|
|
|
// check whether the user has inputted an image and store it
|
|
|
|
$this->attributes['image_uri'] = save_podcast_media(
|
|
|
|
$image,
|
|
|
|
$this->getPodcast()->name,
|
|
|
|
$this->attributes['slug']
|
|
|
|
);
|
|
|
|
} elseif (
|
|
|
|
$APICdata = $this->getEnclosureMetadata()['attached_picture']
|
|
|
|
) {
|
|
|
|
// if the user didn't input an image,
|
|
|
|
// check if the uploaded audio file has an attached cover and store it
|
|
|
|
$cover_image = new \CodeIgniter\Files\File('episode_cover');
|
|
|
|
file_put_contents($cover_image, $APICdata);
|
|
|
|
|
|
|
|
$this->attributes['image_uri'] = save_podcast_media(
|
|
|
|
$cover_image,
|
|
|
|
$this->getPodcast()->name,
|
|
|
|
$this->attributes['slug']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
public function getImage(): \CodeIgniter\Files\File
|
2020-06-30 18:17:41 +02:00
|
|
|
{
|
|
|
|
return new \CodeIgniter\Files\File($this->getImageMediaPath());
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
public function getImageMediaPath(): string
|
2020-06-26 14:34:52 +00:00
|
|
|
{
|
|
|
|
return media_path($this->attributes['image_uri']);
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
public function getImageUrl(): string
|
2020-06-26 14:34:52 +00:00
|
|
|
{
|
2020-06-30 18:17:41 +02:00
|
|
|
if ($image_uri = $this->attributes['image_uri']) {
|
|
|
|
return media_url($image_uri);
|
|
|
|
}
|
|
|
|
return $this->getPodcast()->image_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setEnclosure(
|
|
|
|
\CodeIgniter\HTTP\Files\UploadedFile $enclosure = null
|
|
|
|
) {
|
2020-08-04 11:25:22 +00:00
|
|
|
if (!empty($enclosure) && $enclosure->isValid()) {
|
2020-06-30 18:17:41 +02:00
|
|
|
helper('media');
|
|
|
|
|
|
|
|
$this->attributes['enclosure_uri'] = save_podcast_media(
|
|
|
|
$enclosure,
|
|
|
|
$this->getPodcast()->name,
|
|
|
|
$this->attributes['slug']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEnclosure()
|
|
|
|
{
|
|
|
|
return new \CodeIgniter\Files\File($this->getEnclosureMediaPath());
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getEnclosureMediaPath()
|
|
|
|
{
|
2020-06-30 18:17:41 +02:00
|
|
|
helper('media');
|
|
|
|
|
2020-06-26 14:34:52 +00:00
|
|
|
return media_path($this->attributes['enclosure_uri']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEnclosureUrl()
|
|
|
|
{
|
|
|
|
return base_url(
|
|
|
|
route_to(
|
|
|
|
'analytics_hit',
|
|
|
|
$this->attributes['podcast_id'],
|
|
|
|
$this->attributes['id'],
|
|
|
|
$this->attributes['enclosure_uri']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-30 18:17:41 +02:00
|
|
|
public function getEnclosureMetadata()
|
|
|
|
{
|
|
|
|
helper('id3');
|
|
|
|
|
|
|
|
return get_file_tags($this->getEnclosure());
|
|
|
|
}
|
|
|
|
|
2020-06-26 14:34:52 +00:00
|
|
|
public function getLink()
|
|
|
|
{
|
|
|
|
return base_url(
|
|
|
|
route_to(
|
2020-07-10 12:20:25 +00:00
|
|
|
'episode',
|
2020-06-26 14:34:52 +00:00
|
|
|
$this->getPodcast()->name,
|
|
|
|
$this->attributes['slug']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-30 18:17:41 +02:00
|
|
|
public function getGUID()
|
2020-06-26 14:34:52 +00:00
|
|
|
{
|
|
|
|
return $this->getLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPodcast()
|
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
return (new PodcastModel())->find($this->attributes['podcast_id']);
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
2020-07-27 09:35:34 +00:00
|
|
|
|
|
|
|
public function getDescriptionHtml()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
if (
|
2020-08-04 11:25:22 +00:00
|
|
|
$descriptionFooter = $this->getPodcast()->episode_description_footer
|
2020-07-27 09:35:34 +00:00
|
|
|
) {
|
2020-08-04 11:25:22 +00:00
|
|
|
return $converter->convertToHtml($this->attributes['description']) .
|
2020-07-28 15:57:48 +00:00
|
|
|
'<footer>' .
|
2020-08-04 11:25:22 +00:00
|
|
|
$converter->convertToHtml($descriptionFooter) .
|
2020-07-28 15:57:48 +00:00
|
|
|
'</footer>';
|
2020-07-27 09:35:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
return $converter->convertToHtml($this->attributes['description']);
|
2020-07-27 09:35:34 +00:00
|
|
|
}
|
2020-06-08 20:32:42 +00:00
|
|
|
}
|