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 $link;
|
|
|
|
|
|
|
|
/**
|
2020-09-08 11:45:17 +00:00
|
|
|
* @var \App\Entities\Image
|
2020-08-05 17:26:04 +00:00
|
|
|
*/
|
|
|
|
protected $image;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \CodeIgniter\Files\File
|
|
|
|
*/
|
|
|
|
protected $enclosure;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $enclosure_media_path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $enclosure_url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description_html;
|
2020-06-26 14:34:52 +00:00
|
|
|
|
2020-08-14 18:27:57 +00:00
|
|
|
protected $dates = [
|
|
|
|
'published_at',
|
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
|
|
|
'deleted_at',
|
|
|
|
];
|
|
|
|
|
2020-06-08 20:32:42 +00:00
|
|
|
protected $casts = [
|
2020-08-21 08:41:09 +00:00
|
|
|
'guid' => 'string',
|
2020-06-08 20:32:42 +00:00
|
|
|
'slug' => 'string',
|
|
|
|
'title' => 'string',
|
2020-06-26 14:34:52 +00:00
|
|
|
'enclosure_uri' => 'string',
|
2020-09-04 09:09:26 +00:00
|
|
|
'enclosure_duration' => 'integer',
|
|
|
|
'enclosure_mimetype' => 'string',
|
|
|
|
'enclosure_filesize' => 'integer',
|
2020-06-08 20:32:42 +00:00
|
|
|
'description' => 'string',
|
2020-06-30 18:17:41 +02:00
|
|
|
'image_uri' => '?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',
|
|
|
|
'block' => 'boolean',
|
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
|
|
|
|
*
|
|
|
|
* @param \CodeIgniter\HTTP\Files\UploadedFile|\CodeIgniter\Files\File $image
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setImage($image)
|
2020-06-30 18:17:41 +02:00
|
|
|
{
|
2020-08-21 08:41:09 +00:00
|
|
|
if (
|
|
|
|
!empty($image) &&
|
|
|
|
(!($image instanceof \CodeIgniter\HTTP\Files\UploadedFile) ||
|
|
|
|
$image->isValid())
|
|
|
|
) {
|
2020-09-08 11:45:17 +00:00
|
|
|
helper('media');
|
|
|
|
|
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']
|
|
|
|
);
|
2020-09-08 11:45:17 +00:00
|
|
|
|
|
|
|
$this->image = new \App\Entities\Image(
|
|
|
|
$this->attributes['image_uri']
|
|
|
|
);
|
|
|
|
$this->image->saveSizes();
|
2020-06-30 18:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-09-08 11:45:17 +00:00
|
|
|
public function getImage(): \App\Entities\Image
|
2020-06-26 14:34:52 +00:00
|
|
|
{
|
2020-06-30 18:17:41 +02:00
|
|
|
if ($image_uri = $this->attributes['image_uri']) {
|
2020-09-08 11:45:17 +00:00
|
|
|
return new \App\Entities\Image($image_uri);
|
2020-06-30 18:17:41 +02:00
|
|
|
}
|
2020-09-08 11:45:17 +00:00
|
|
|
return $this->getPodcast()->image;
|
2020-06-30 18:17:41 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 08:41:09 +00:00
|
|
|
/**
|
|
|
|
* Saves an enclosure
|
|
|
|
*
|
|
|
|
* @param \CodeIgniter\HTTP\Files\UploadedFile|\CodeIgniter\Files\File $enclosure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setEnclosure($enclosure = null)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
!empty($enclosure) &&
|
|
|
|
(!($enclosure instanceof \CodeIgniter\HTTP\Files\UploadedFile) ||
|
|
|
|
$enclosure->isValid())
|
|
|
|
) {
|
2020-09-04 09:09:26 +00:00
|
|
|
helper(['media', 'id3']);
|
|
|
|
|
|
|
|
$enclosure_metadata = get_file_tags($enclosure);
|
2020-06-30 18:17:41 +02:00
|
|
|
|
|
|
|
$this->attributes['enclosure_uri'] = save_podcast_media(
|
|
|
|
$enclosure,
|
|
|
|
$this->getPodcast()->name,
|
|
|
|
$this->attributes['slug']
|
|
|
|
);
|
2020-09-04 09:09:26 +00:00
|
|
|
$this->attributes['enclosure_duration'] = round(
|
|
|
|
$enclosure_metadata['playtime_seconds']
|
|
|
|
);
|
|
|
|
$this->attributes['enclosure_mimetype'] =
|
|
|
|
$enclosure_metadata['mime_type'];
|
|
|
|
$this->attributes['enclosure_filesize'] =
|
|
|
|
$enclosure_metadata['filesize'];
|
2020-06-30 18:17:41 +02:00
|
|
|
|
|
|
|
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']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-21 08:41:09 +00:00
|
|
|
public function setGuid($guid = null)
|
2020-06-26 14:34:52 +00:00
|
|
|
{
|
2020-08-21 08:41:09 +00:00
|
|
|
return $this->attributes['guid'] = empty($guid)
|
|
|
|
? $this->getLink()
|
|
|
|
: $guid;
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getPodcast()
|
|
|
|
{
|
2020-09-04 09:09:26 +00:00
|
|
|
return (new PodcastModel())->getPodcastById(
|
|
|
|
$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-08-14 18:27:57 +00:00
|
|
|
|
|
|
|
public function setPublishedAt($date, $time)
|
|
|
|
{
|
|
|
|
if (empty($date)) {
|
|
|
|
$this->attributes['published_at'] = null;
|
|
|
|
} else {
|
|
|
|
$this->attributes['published_at'] = $date . ' ' . $time;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setCreatedBy(\App\Entities\User $user)
|
|
|
|
{
|
|
|
|
$this->attributes['created_by'] = $user->id;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUpdatedBy(\App\Entities\User $user)
|
|
|
|
{
|
|
|
|
$this->attributes['updated_by'] = $user->id;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-06-08 20:32:42 +00:00
|
|
|
}
|