2020-05-31 19:15:52 +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-05-31 19:15:52 +00:00
|
|
|
|
|
|
|
namespace App\Entities;
|
|
|
|
|
2020-09-04 09:09:26 +00:00
|
|
|
use App\Models\CategoryModel;
|
2020-06-26 14:34:52 +00:00
|
|
|
use App\Models\EpisodeModel;
|
2020-09-04 09:09:26 +00:00
|
|
|
use App\Models\PlatformModel;
|
2020-05-31 19:15:52 +00:00
|
|
|
use CodeIgniter\Entity;
|
2020-07-31 16:05:10 +00:00
|
|
|
use App\Models\UserModel;
|
2020-08-04 11:25:22 +00:00
|
|
|
use League\CommonMark\CommonMarkConverter;
|
2020-05-31 19:15:52 +00:00
|
|
|
|
|
|
|
class Podcast extends Entity
|
|
|
|
{
|
2020-08-05 17:26:04 +00:00
|
|
|
/**
|
|
|
|
* @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 \App\Entities\Episode[]
|
|
|
|
*/
|
2020-06-26 14:34:52 +00:00
|
|
|
protected $episodes;
|
2020-08-05 17:26:04 +00:00
|
|
|
|
2020-09-04 09:09:26 +00:00
|
|
|
/**
|
|
|
|
* @var \App\Entities\Category
|
|
|
|
*/
|
|
|
|
protected $category;
|
|
|
|
|
2020-10-02 15:38:16 +00:00
|
|
|
/**
|
|
|
|
* @var \App\Entities\Category[]
|
|
|
|
*/
|
|
|
|
protected $other_categories;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var integer[]
|
|
|
|
*/
|
|
|
|
protected $other_categories_ids;
|
|
|
|
|
2020-08-05 17:26:04 +00:00
|
|
|
/**
|
|
|
|
* @var \App\Entities\User[]
|
|
|
|
*/
|
2020-07-16 10:08:23 +00:00
|
|
|
protected $contributors;
|
2020-08-05 17:26:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description_html;
|
2020-06-26 14:34:52 +00:00
|
|
|
|
2020-09-04 09:09:26 +00:00
|
|
|
/**
|
|
|
|
* @var \App\Entities\Platform
|
|
|
|
*/
|
|
|
|
protected $platforms;
|
|
|
|
|
2020-05-31 19:15:52 +00:00
|
|
|
protected $casts = [
|
2020-06-12 19:31:10 +00:00
|
|
|
'id' => 'integer',
|
2020-05-31 19:15:52 +00:00
|
|
|
'title' => 'string',
|
|
|
|
'name' => 'string',
|
|
|
|
'description' => 'string',
|
2020-06-26 14:34:52 +00:00
|
|
|
'image_uri' => 'string',
|
2020-05-31 19:15:52 +00:00
|
|
|
'language' => 'string',
|
2020-08-21 08:41:09 +00:00
|
|
|
'category_id' => 'integer',
|
2020-10-02 15:38:16 +00:00
|
|
|
'parental_advisory' => '?string',
|
|
|
|
'publisher' => '?string',
|
2020-05-31 19:15:52 +00:00
|
|
|
'owner_name' => '?string',
|
|
|
|
'owner_email' => '?string',
|
2020-06-26 14:34:52 +00:00
|
|
|
'type' => 'string',
|
2020-05-31 19:15:52 +00:00
|
|
|
'copyright' => '?string',
|
2020-10-20 13:46:00 +00:00
|
|
|
'episode_description_footer' => '?string',
|
2020-05-31 19:15:52 +00:00
|
|
|
'block' => 'boolean',
|
|
|
|
'complete' => 'boolean',
|
2020-10-20 13:46:00 +00:00
|
|
|
'lock' => 'boolean',
|
2020-08-21 08:41:09 +00:00
|
|
|
'imported_feed_url' => '?string',
|
2020-10-06 15:39:27 +00:00
|
|
|
'new_feed_url' => '?string',
|
2020-10-20 13:46:00 +00:00
|
|
|
'created_by' => 'integer',
|
|
|
|
'updated_by' => 'integer',
|
2020-05-31 19:15:52 +00:00
|
|
|
];
|
2020-06-26 14:34:52 +00:00
|
|
|
|
2020-08-21 08:41:09 +00:00
|
|
|
/**
|
|
|
|
* Saves a cover image to the corresponding podcast folder in `public/media/podcast_name/`
|
|
|
|
*
|
|
|
|
* @param \CodeIgniter\HTTP\Files\UploadedFile|\CodeIgniter\Files\File $image
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setImage($image = null)
|
2020-06-30 18:17:41 +02:00
|
|
|
{
|
|
|
|
if ($image) {
|
|
|
|
helper('media');
|
|
|
|
|
|
|
|
$this->attributes['image_uri'] = save_podcast_media(
|
|
|
|
$image,
|
|
|
|
$this->attributes['name'],
|
|
|
|
'cover'
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
2020-09-08 11:45:17 +00:00
|
|
|
return $this;
|
2020-06-30 18:17:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 11:45:17 +00:00
|
|
|
public function getImage()
|
2020-06-26 14:34:52 +00:00
|
|
|
{
|
2020-09-08 11:45:17 +00:00
|
|
|
return new \App\Entities\Image($this->attributes['image_uri']);
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getLink()
|
|
|
|
{
|
2020-07-10 12:20:25 +00:00
|
|
|
return base_url(route_to('podcast', $this->attributes['name']));
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFeedUrl()
|
|
|
|
{
|
|
|
|
return base_url(route_to('podcast_feed', $this->attributes['name']));
|
|
|
|
}
|
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
/**
|
|
|
|
* Returns the podcast's episodes
|
|
|
|
*
|
|
|
|
* @return \App\Entities\Episode[]
|
|
|
|
*/
|
2020-06-26 14:34:52 +00:00
|
|
|
public function getEpisodes()
|
|
|
|
{
|
2020-07-10 12:20:25 +00:00
|
|
|
if (empty($this->id)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'Podcast must be created before getting episodes.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
if (empty($this->episodes)) {
|
2020-07-10 12:20:25 +00:00
|
|
|
$this->episodes = (new EpisodeModel())->getPodcastEpisodes(
|
2020-09-04 09:09:26 +00:00
|
|
|
$this->id,
|
|
|
|
$this->type
|
2020-07-10 12:20:25 +00:00
|
|
|
);
|
|
|
|
}
|
2020-06-26 14:34:52 +00:00
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
return $this->episodes;
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2020-09-04 09:09:26 +00:00
|
|
|
/**
|
|
|
|
* Returns the podcast category entity
|
|
|
|
*
|
|
|
|
* @return \App\Entities\Category
|
|
|
|
*/
|
|
|
|
public function getCategory()
|
|
|
|
{
|
|
|
|
if (empty($this->id)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'Podcast must be created before getting category.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->category)) {
|
|
|
|
$this->category = (new CategoryModel())->find($this->category_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->category;
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:57:48 +00:00
|
|
|
/**
|
|
|
|
* Returns all podcast contributors
|
|
|
|
*
|
2020-07-31 16:05:10 +00:00
|
|
|
* @return \App\Entities\User[]
|
2020-07-28 15:57:48 +00:00
|
|
|
*/
|
2020-07-16 10:08:23 +00:00
|
|
|
public function getContributors()
|
|
|
|
{
|
2020-07-31 16:05:10 +00:00
|
|
|
if (empty($this->id)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'Podcasts must be created before getting contributors.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->contributors)) {
|
|
|
|
$this->contributors = (new UserModel())->getPodcastContributors(
|
|
|
|
$this->id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->contributors;
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
2020-07-28 15:57:48 +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-28 15:57:48 +00:00
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
return $converter->convertToHtml($this->attributes['description']);
|
2020-07-28 15:57:48 +00:00
|
|
|
}
|
2020-08-14 18:27:57 +00:00
|
|
|
|
|
|
|
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-09-04 09:09:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the podcast's platform links
|
|
|
|
*
|
|
|
|
* @return \App\Entities\Platform[]
|
|
|
|
*/
|
|
|
|
public function getPlatforms()
|
|
|
|
{
|
|
|
|
if (empty($this->id)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'Podcast must be created before getting platform links.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->platforms)) {
|
|
|
|
$this->platforms = (new PlatformModel())->getPodcastPlatformLinks(
|
|
|
|
$this->id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->platforms;
|
|
|
|
}
|
2020-10-02 15:38:16 +00:00
|
|
|
|
|
|
|
public function getOtherCategories()
|
|
|
|
{
|
|
|
|
if (empty($this->id)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'Podcast must be created before getting other categories.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->other_categories)) {
|
|
|
|
$this->other_categories = (new CategoryModel())->getPodcastCategories(
|
|
|
|
$this->id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->other_categories;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOtherCategoriesIds()
|
|
|
|
{
|
|
|
|
if (empty($this->other_categories_ids)) {
|
|
|
|
$this->other_categories_ids = array_column(
|
|
|
|
$this->getOtherCategories(),
|
|
|
|
'id'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->other_categories_ids;
|
|
|
|
}
|
2020-05-31 19:15:52 +00:00
|
|
|
}
|