2020-05-31 21:15:52 +02:00
|
|
|
<?php
|
2020-08-04 13:25:22 +02:00
|
|
|
|
2020-06-10 17:00:12 +02:00
|
|
|
/**
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
2020-05-31 21:15:52 +02:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
|
|
class PodcastModel extends Model
|
|
|
|
{
|
|
|
|
protected $table = 'podcasts';
|
|
|
|
protected $primaryKey = 'id';
|
|
|
|
|
|
|
|
protected $allowedFields = [
|
2020-06-10 17:00:12 +02:00
|
|
|
'id',
|
2020-05-31 21:15:52 +02:00
|
|
|
'title',
|
|
|
|
'name',
|
2020-10-29 16:45:19 +01:00
|
|
|
'description_markdown',
|
|
|
|
'description_html',
|
|
|
|
'episode_description_footer_markdown',
|
|
|
|
'episode_description_footer_html',
|
2020-06-26 16:34:52 +02:00
|
|
|
'image_uri',
|
2020-10-29 16:45:19 +01:00
|
|
|
'language_code',
|
2020-08-21 10:41:09 +02:00
|
|
|
'category_id',
|
2020-10-02 17:38:16 +02:00
|
|
|
'parental_advisory',
|
2020-05-31 21:15:52 +02:00
|
|
|
'owner_name',
|
|
|
|
'owner_email',
|
2020-10-02 17:38:16 +02:00
|
|
|
'publisher',
|
2020-05-31 21:15:52 +02:00
|
|
|
'type',
|
|
|
|
'copyright',
|
2020-08-21 10:41:09 +02:00
|
|
|
'imported_feed_url',
|
2020-10-06 17:39:27 +02:00
|
|
|
'new_feed_url',
|
2020-10-29 16:45:19 +01:00
|
|
|
'is_blocked',
|
|
|
|
'is_completed',
|
|
|
|
'is_locked',
|
|
|
|
'created_by',
|
|
|
|
'updated_by',
|
2020-05-31 21:15:52 +02:00
|
|
|
];
|
|
|
|
|
2020-08-04 13:25:22 +02:00
|
|
|
protected $returnType = \App\Entities\Podcast::class;
|
2020-06-30 18:17:41 +02:00
|
|
|
protected $useSoftDeletes = true;
|
2020-05-31 21:15:52 +02:00
|
|
|
|
|
|
|
protected $useTimestamps = true;
|
2020-06-30 18:17:41 +02:00
|
|
|
|
2020-07-16 12:08:23 +02:00
|
|
|
protected $validationRules = [
|
|
|
|
'title' => 'required',
|
|
|
|
'name' =>
|
|
|
|
'required|regex_match[/^[a-zA-Z0-9\_]{1,191}$/]|is_unique[podcasts.name,id,{id}]',
|
2020-10-29 16:45:19 +01:00
|
|
|
'description_markdown' => 'required',
|
2020-07-16 12:08:23 +02:00
|
|
|
'image_uri' => 'required',
|
2020-10-29 16:45:19 +01:00
|
|
|
'language_code' => 'required',
|
2020-08-21 10:41:09 +02:00
|
|
|
'category_id' => 'required',
|
2020-07-16 12:08:23 +02:00
|
|
|
'owner_email' => 'required|valid_email',
|
|
|
|
'type' => 'required',
|
2020-08-14 20:27:57 +02:00
|
|
|
'created_by' => 'required',
|
|
|
|
'updated_by' => 'required',
|
2020-07-16 12:08:23 +02:00
|
|
|
];
|
|
|
|
protected $validationMessages = [];
|
|
|
|
|
2020-10-22 19:41:59 +02:00
|
|
|
// clear cache before update if by any chance, the podcast name changes, so will the podcast link
|
2020-08-18 18:31:28 +02:00
|
|
|
protected $beforeUpdate = ['clearCache'];
|
2020-07-16 12:08:23 +02:00
|
|
|
protected $beforeDelete = ['clearCache'];
|
|
|
|
|
2020-09-04 11:09:26 +02:00
|
|
|
public function getPodcastByName($podcastName)
|
|
|
|
{
|
|
|
|
if (!($found = cache("podcast@{$podcastName}"))) {
|
|
|
|
$found = $this->where('name', $podcastName)->first();
|
|
|
|
|
|
|
|
cache()->save("podcast@{$podcastName}", $found, DECADE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPodcastById($podcastId)
|
|
|
|
{
|
|
|
|
if (!($found = cache("podcast{$podcastId}"))) {
|
|
|
|
$found = $this->find($podcastId);
|
|
|
|
|
|
|
|
cache()->save("podcast{$podcastId}", $found, DECADE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:08:23 +02:00
|
|
|
/**
|
|
|
|
* Gets all the podcasts a given user is contributing to
|
|
|
|
*
|
2020-09-04 11:09:26 +02:00
|
|
|
* @param int $userId
|
2020-07-16 12:08:23 +02:00
|
|
|
*
|
|
|
|
* @return \App\Entities\Podcast[] podcasts
|
|
|
|
*/
|
2020-09-04 11:09:26 +02:00
|
|
|
public function getUserPodcasts($userId)
|
2020-07-16 12:08:23 +02:00
|
|
|
{
|
2020-09-04 11:09:26 +02:00
|
|
|
if (!($found = cache("user{$userId}_podcasts"))) {
|
|
|
|
$found = $this->select('podcasts.*')
|
|
|
|
->join(
|
2020-10-29 16:45:19 +01:00
|
|
|
'podcasts_users',
|
|
|
|
'podcasts_users.podcast_id = podcasts.id'
|
2020-09-04 11:09:26 +02:00
|
|
|
)
|
2020-10-29 16:45:19 +01:00
|
|
|
->where('podcasts_users.user_id', $userId)
|
2020-09-04 11:09:26 +02:00
|
|
|
->findAll();
|
|
|
|
|
|
|
|
cache()->save("user{$userId}_podcasts", $found, DECADE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2020-07-16 12:08:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:09:26 +02:00
|
|
|
public function addPodcastContributor($userId, $podcastId, $groupId)
|
2020-07-16 12:08:23 +02:00
|
|
|
{
|
2020-09-04 11:09:26 +02:00
|
|
|
cache()->delete("podcast{$podcastId}_contributors");
|
|
|
|
|
2020-07-16 12:08:23 +02:00
|
|
|
$data = [
|
2020-09-04 11:09:26 +02:00
|
|
|
'user_id' => (int) $userId,
|
|
|
|
'podcast_id' => (int) $podcastId,
|
|
|
|
'group_id' => (int) $groupId,
|
2020-07-16 12:08:23 +02:00
|
|
|
];
|
|
|
|
|
2020-10-29 16:45:19 +01:00
|
|
|
return $this->db->table('podcasts_users')->insert($data);
|
2020-07-16 12:08:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:09:26 +02:00
|
|
|
public function updatePodcastContributor($userId, $podcastId, $groupId)
|
2020-07-31 18:05:10 +02:00
|
|
|
{
|
2020-09-04 11:09:26 +02:00
|
|
|
cache()->delete("podcast{$podcastId}_contributors");
|
|
|
|
|
2020-07-31 18:05:10 +02:00
|
|
|
return $this->db
|
2020-10-29 16:45:19 +01:00
|
|
|
->table('podcasts_users')
|
2020-07-31 18:05:10 +02:00
|
|
|
->where([
|
2020-09-04 11:09:26 +02:00
|
|
|
'user_id' => (int) $userId,
|
|
|
|
'podcast_id' => (int) $podcastId,
|
2020-07-31 18:05:10 +02:00
|
|
|
])
|
2020-09-04 11:09:26 +02:00
|
|
|
->update(['group_id' => $groupId]);
|
2020-07-31 18:05:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:09:26 +02:00
|
|
|
public function removePodcastContributor($userId, $podcastId)
|
2020-07-16 12:08:23 +02:00
|
|
|
{
|
2020-09-04 11:09:26 +02:00
|
|
|
cache()->delete("podcast{$podcastId}_contributors");
|
|
|
|
|
2020-07-16 12:08:23 +02:00
|
|
|
return $this->db
|
2020-10-29 16:45:19 +01:00
|
|
|
->table('podcasts_users')
|
2020-07-16 12:08:23 +02:00
|
|
|
->where([
|
2020-09-04 11:09:26 +02:00
|
|
|
'user_id' => $userId,
|
|
|
|
'podcast_id' => $podcastId,
|
2020-07-16 12:08:23 +02:00
|
|
|
])
|
|
|
|
->delete();
|
|
|
|
}
|
2020-06-30 18:17:41 +02:00
|
|
|
|
2020-09-04 11:09:26 +02:00
|
|
|
public function getContributorGroupId($userId, $podcastId)
|
2020-07-31 18:05:10 +02:00
|
|
|
{
|
|
|
|
$user_podcast = $this->db
|
2020-10-29 16:45:19 +01:00
|
|
|
->table('podcasts_users')
|
2020-07-31 18:05:10 +02:00
|
|
|
->select('group_id')
|
|
|
|
->where([
|
2020-09-04 11:09:26 +02:00
|
|
|
'user_id' => $userId,
|
|
|
|
'podcast_id' => $podcastId,
|
2020-07-31 18:05:10 +02:00
|
|
|
])
|
|
|
|
->get()
|
|
|
|
->getResultObject();
|
|
|
|
|
|
|
|
return (int) count($user_podcast) > 0
|
|
|
|
? $user_podcast[0]->group_id
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
|
2020-07-02 12:08:32 +02:00
|
|
|
protected function clearCache(array $data)
|
2020-06-30 18:17:41 +02:00
|
|
|
{
|
2020-09-04 11:09:26 +02:00
|
|
|
$podcast = (new PodcastModel())->getPodcastById(
|
2020-06-30 18:17:41 +02:00
|
|
|
is_array($data['id']) ? $data['id'][0] : $data['id']
|
|
|
|
);
|
2020-10-20 12:31:40 +02:00
|
|
|
$supportedLocales = config('App')->supportedLocales;
|
2020-06-30 18:17:41 +02:00
|
|
|
|
2020-07-02 12:08:32 +02:00
|
|
|
// delete cache for rss feed and podcast pages
|
2020-10-21 18:04:18 +02:00
|
|
|
cache()->delete("podcast{$podcast->id}_feed");
|
|
|
|
foreach (\Opawg\UserAgentsPhp\UserAgentsRSS::$db as $service) {
|
|
|
|
cache()->delete("podcast{$podcast->id}_feed_{$service['slug']}");
|
|
|
|
}
|
2020-07-16 12:08:23 +02:00
|
|
|
|
2020-09-04 11:09:26 +02:00
|
|
|
// delete model requests cache
|
|
|
|
cache()->delete("podcast{$podcast->id}");
|
|
|
|
cache()->delete("podcast@{$podcast->name}");
|
|
|
|
|
2020-10-20 12:31:40 +02:00
|
|
|
// clear cache for every localized podcast episode page
|
|
|
|
foreach ($podcast->episodes as $episode) {
|
|
|
|
foreach ($supportedLocales as $locale) {
|
|
|
|
cache()->delete(
|
|
|
|
"page_podcast{$podcast->id}_episode{$episode->id}_{$locale}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete episode lists cache per year / season
|
|
|
|
// and localized pages
|
|
|
|
$episodeModel = new EpisodeModel();
|
|
|
|
$years = $episodeModel->getYears($podcast->id);
|
|
|
|
$seasons = $episodeModel->getSeasons($podcast->id);
|
|
|
|
|
|
|
|
foreach ($years as $year) {
|
|
|
|
cache()->delete("podcast{$podcast->id}_{$year['year']}_episodes");
|
|
|
|
foreach ($supportedLocales as $locale) {
|
|
|
|
cache()->delete(
|
|
|
|
"page_podcast{$podcast->id}_{$year['year']}_{$locale}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($seasons as $season) {
|
|
|
|
cache()->delete(
|
|
|
|
"podcast{$podcast->id}_season{$season['season_number']}_episodes"
|
|
|
|
);
|
|
|
|
foreach ($supportedLocales as $locale) {
|
|
|
|
cache()->delete(
|
|
|
|
"page_podcast{$podcast->id}_season{$season['season_number']}_{$locale}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete query cache
|
|
|
|
cache()->delete("podcast{$podcast->id}_defaultQuery");
|
|
|
|
cache()->delete("podcast{$podcast->id}_years");
|
|
|
|
cache()->delete("podcast{$podcast->id}_seasons");
|
|
|
|
|
2020-07-16 12:08:23 +02:00
|
|
|
return $data;
|
|
|
|
}
|
2020-05-31 21:15:52 +02:00
|
|
|
}
|