Newer
Older

Yassine Doghri
committed
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Models;
use CodeIgniter\Model;
class PodcastModel extends Model
{
protected $table = 'podcasts';
protected $primaryKey = 'id';
protected $allowedFields = [
'title',
'name',
'description',
'episode_description_footer',
'parental_advisory',
'owner_name',
'owner_email',
'publisher',
'type',
'copyright',
'block',
'complete',

Benjamin Bellamy
committed
'lock',
'created_by',
'updated_by',

Yassine Doghri
committed
protected $returnType = \App\Entities\Podcast::class;
protected $useSoftDeletes = true;
protected $useTimestamps = true;
protected $validationRules = [
'title' => 'required',
'name' =>
'required|regex_match[/^[a-zA-Z0-9\_]{1,191}$/]|is_unique[podcasts.name,id,{id}]',
'description' => 'required',
'image_uri' => 'required',
'language' => 'required',
'category_id' => 'required',
'owner_email' => 'required|valid_email',
'type' => 'required',
'created_by' => 'required',
'updated_by' => 'required',
];
protected $validationMessages = [];

Yassine Doghri
committed
// clear cache before update if by any chance, the podcast name changes, so will the podcast link

Yassine Doghri
committed
protected $beforeUpdate = ['clearCache'];
protected $beforeDelete = ['clearCache'];
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;
}
/**
* Gets all the podcasts a given user is contributing to
*
* @param int $userId
*
* @return \App\Entities\Podcast[] podcasts
*/
public function getUserPodcasts($userId)
if (!($found = cache("user{$userId}_podcasts"))) {
$found = $this->select('podcasts.*')
->join(
'users_podcasts',
'users_podcasts.podcast_id = podcasts.id'
)
->where('users_podcasts.user_id', $userId)
->findAll();
cache()->save("user{$userId}_podcasts", $found, DECADE);
}
return $found;
}
public function addPodcastContributor($userId, $podcastId, $groupId)
cache()->delete("podcast{$podcastId}_contributors");
$data = [
'user_id' => (int) $userId,
'podcast_id' => (int) $podcastId,
'group_id' => (int) $groupId,
];
return $this->db->table('users_podcasts')->insert($data);
}
public function updatePodcastContributor($userId, $podcastId, $groupId)

Yassine Doghri
committed
{
cache()->delete("podcast{$podcastId}_contributors");

Yassine Doghri
committed
return $this->db
->table('users_podcasts')
->where([
'user_id' => (int) $userId,
'podcast_id' => (int) $podcastId,

Yassine Doghri
committed
])
->update(['group_id' => $groupId]);

Yassine Doghri
committed
}
public function removePodcastContributor($userId, $podcastId)
cache()->delete("podcast{$podcastId}_contributors");
return $this->db
->table('users_podcasts')
->where([
'user_id' => $userId,
'podcast_id' => $podcastId,
])
->delete();
}
public function getContributorGroupId($userId, $podcastId)

Yassine Doghri
committed
{
$user_podcast = $this->db
->table('users_podcasts')
->select('group_id')
->where([
'user_id' => $userId,
'podcast_id' => $podcastId,

Yassine Doghri
committed
])
->get()
->getResultObject();
return (int) count($user_podcast) > 0
? $user_podcast[0]->group_id
: false;
}

Yassine Doghri
committed
protected function clearCache(array $data)
$podcast = (new PodcastModel())->getPodcastById(
is_array($data['id']) ? $data['id'][0] : $data['id']
);

Yassine Doghri
committed
$supportedLocales = config('App')->supportedLocales;

Yassine Doghri
committed
// delete cache for rss feed and podcast pages
cache()->delete("podcast{$podcast->id}_feed");
foreach (\Opawg\UserAgentsPhp\UserAgentsRSS::$db as $service) {
cache()->delete("podcast{$podcast->id}_feed_{$service['slug']}");
}
// delete model requests cache
cache()->delete("podcast{$podcast->id}");
cache()->delete("podcast@{$podcast->name}");

Yassine Doghri
committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// 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");
return $data;
}