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;
|
2021-02-10 16:20:01 +00:00
|
|
|
use App\Models\PodcastPersonModel;
|
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
|
|
|
|
2021-02-10 16:20:01 +00:00
|
|
|
/**
|
|
|
|
* @var \App\Entities\PodcastPerson[]
|
|
|
|
*/
|
|
|
|
protected $podcast_persons;
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2020-10-29 15:45:19 +00:00
|
|
|
* @var \App\Entities\Platform
|
2020-08-05 17:26:04 +00:00
|
|
|
*/
|
2020-11-18 17:17:56 +00:00
|
|
|
protected $podcastingPlatforms;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \App\Entities\Platform
|
|
|
|
*/
|
|
|
|
protected $socialPlatforms;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \App\Entities\Platform
|
|
|
|
*/
|
|
|
|
protected $fundingPlatforms;
|
2020-06-26 14:34:52 +00:00
|
|
|
|
2020-09-04 09:09:26 +00:00
|
|
|
/**
|
2020-10-29 15:45:19 +00:00
|
|
|
* Holds text only description, striped of any markdown or html special characters
|
|
|
|
*
|
|
|
|
* @var string
|
2020-09-04 09:09:26 +00:00
|
|
|
*/
|
2020-10-29 15:45:19 +00:00
|
|
|
protected $description;
|
2020-09-04 09:09:26 +00:00
|
|
|
|
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',
|
2020-10-29 15:45:19 +00:00
|
|
|
'description_markdown' => 'string',
|
|
|
|
'description_html' => 'string',
|
2020-06-26 14:34:52 +00:00
|
|
|
'image_uri' => 'string',
|
2020-10-29 15:45:19 +00:00
|
|
|
'language_code' => '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-10-29 15:45:19 +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-29 15:45:19 +00:00
|
|
|
'episode_description_footer_markdown' => '?string',
|
|
|
|
'episode_description_footer_html' => '?string',
|
|
|
|
'is_blocked' => 'boolean',
|
|
|
|
'is_completed' => 'boolean',
|
|
|
|
'is_locked' => '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-12-23 14:11:38 +00:00
|
|
|
'location_name' => '?string',
|
|
|
|
'location_geo' => '?string',
|
|
|
|
'location_osmid' => '?string',
|
2020-11-26 18:53:52 +00:00
|
|
|
'payment_pointer' => '?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
|
|
|
|
2021-02-10 16:20:01 +00:00
|
|
|
/**
|
|
|
|
* Returns the podcast's persons
|
|
|
|
*
|
|
|
|
* @return \App\Entities\PodcastPerson[]
|
|
|
|
*/
|
|
|
|
public function getPodcastPersons()
|
|
|
|
{
|
|
|
|
if (empty($this->id)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'Podcast must be created before getting persons.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->podcast_persons)) {
|
|
|
|
$this->podcast_persons = (new PodcastPersonModel())->getPersonsByPodcastId(
|
|
|
|
$this->id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->podcast_persons;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-10-29 15:45:19 +00:00
|
|
|
public function setDescriptionMarkdown(string $descriptionMarkdown)
|
2020-07-28 15:57:48 +00:00
|
|
|
{
|
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-10-29 15:45:19 +00:00
|
|
|
$this->attributes['description_markdown'] = $descriptionMarkdown;
|
|
|
|
$this->attributes['description_html'] = $converter->convertToHtml(
|
|
|
|
$descriptionMarkdown
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setEpisodeDescriptionFooterMarkdown(
|
|
|
|
string $episodeDescriptionFooterMarkdown = null
|
|
|
|
) {
|
|
|
|
if ($episodeDescriptionFooterMarkdown) {
|
|
|
|
$converter = new CommonMarkConverter([
|
|
|
|
'html_input' => 'strip',
|
|
|
|
'allow_unsafe_links' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->attributes[
|
|
|
|
'episode_description_footer_markdown'
|
|
|
|
] = $episodeDescriptionFooterMarkdown;
|
|
|
|
$this->attributes[
|
|
|
|
'episode_description_footer_html'
|
|
|
|
] = $converter->convertToHtml($episodeDescriptionFooterMarkdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription()
|
|
|
|
{
|
|
|
|
if ($this->description) {
|
|
|
|
return $this->description;
|
|
|
|
}
|
|
|
|
|
|
|
|
return trim(
|
|
|
|
preg_replace(
|
|
|
|
'/\s+/',
|
|
|
|
' ',
|
|
|
|
strip_tags($this->attributes['description_html'])
|
|
|
|
)
|
|
|
|
);
|
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
|
|
|
|
|
|
|
/**
|
2020-11-18 17:17:56 +00:00
|
|
|
* Returns the podcast's podcasting platform links
|
2020-09-04 09:09:26 +00:00
|
|
|
*
|
|
|
|
* @return \App\Entities\Platform[]
|
|
|
|
*/
|
2020-11-18 17:17:56 +00:00
|
|
|
public function getPodcastingPlatforms()
|
2020-09-04 09:09:26 +00:00
|
|
|
{
|
|
|
|
if (empty($this->id)) {
|
|
|
|
throw new \RuntimeException(
|
2020-11-18 17:17:56 +00:00
|
|
|
'Podcast must be created before getting podcasting platform links.'
|
2020-09-04 09:09:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:17:56 +00:00
|
|
|
if (empty($this->podcastingPlatforms)) {
|
|
|
|
$this->podcastingPlatforms = (new PlatformModel())->getPodcastPlatforms(
|
|
|
|
$this->id,
|
|
|
|
'podcasting'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->podcastingPlatforms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the podcast's social platform links
|
|
|
|
*
|
|
|
|
* @return \App\Entities\Platform[]
|
|
|
|
*/
|
|
|
|
public function getSocialPlatforms()
|
|
|
|
{
|
|
|
|
if (empty($this->id)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'Podcast must be created before getting social platform links.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->socialPlatforms)) {
|
|
|
|
$this->socialPlatforms = (new PlatformModel())->getPodcastPlatforms(
|
|
|
|
$this->id,
|
|
|
|
'social'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->socialPlatforms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the podcast's funding platform links
|
|
|
|
*
|
|
|
|
* @return \App\Entities\Platform[]
|
|
|
|
*/
|
|
|
|
public function getFundingPlatforms()
|
|
|
|
{
|
|
|
|
if (empty($this->id)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'Podcast must be created before getting funding platform links.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->fundingPlatforms)) {
|
|
|
|
$this->fundingPlatforms = (new PlatformModel())->getPodcastPlatforms(
|
|
|
|
$this->id,
|
|
|
|
'funding'
|
2020-09-04 09:09:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:17:56 +00:00
|
|
|
return $this->fundingPlatforms;
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
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-12-23 14:11:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the location name and fetches OpenStreetMap info
|
|
|
|
*
|
|
|
|
* @param string $locationName
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setLocation($locationName = null)
|
|
|
|
{
|
|
|
|
helper('location');
|
|
|
|
|
|
|
|
if (
|
|
|
|
$locationName &&
|
|
|
|
(empty($this->attributes['location_name']) ||
|
|
|
|
$this->attributes['location_name'] != $locationName)
|
|
|
|
) {
|
|
|
|
$this->attributes['location_name'] = $locationName;
|
|
|
|
if ($location = fetch_osm_location($locationName)) {
|
|
|
|
$this->attributes['location_geo'] = $location['geo'];
|
|
|
|
$this->attributes['location_osmid'] = $location['osmid'];
|
|
|
|
}
|
|
|
|
} elseif (empty($locationName)) {
|
|
|
|
$this->attributes['location_name'] = null;
|
|
|
|
$this->attributes['location_geo'] = null;
|
|
|
|
$this->attributes['location_osmid'] = null;
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2020-05-31 19:15:52 +00:00
|
|
|
}
|