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\Models;
|
|
|
|
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
|
|
class EpisodeModel extends Model
|
|
|
|
{
|
|
|
|
protected $table = 'episodes';
|
|
|
|
protected $primaryKey = 'id';
|
|
|
|
|
|
|
|
protected $allowedFields = [
|
2020-06-10 15:00:12 +00:00
|
|
|
'podcast_id',
|
2020-06-08 20:32:42 +00:00
|
|
|
'title',
|
2020-06-10 15:00:12 +00:00
|
|
|
'slug',
|
2020-06-26 14:34:52 +00:00
|
|
|
'enclosure_uri',
|
2020-06-08 20:32:42 +00:00
|
|
|
'description',
|
2020-06-26 14:34:52 +00:00
|
|
|
'image_uri',
|
2020-06-08 20:32:42 +00:00
|
|
|
'explicit',
|
2020-06-12 19:31:10 +00:00
|
|
|
'number',
|
2020-06-08 20:32:42 +00:00
|
|
|
'season_number',
|
|
|
|
'type',
|
|
|
|
'block',
|
2020-08-14 18:27:57 +00:00
|
|
|
'published_at',
|
|
|
|
'created_by',
|
|
|
|
'updated_by',
|
2020-06-08 20:32:42 +00:00
|
|
|
];
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
protected $returnType = \App\Entities\Episode::class;
|
2020-06-08 20:32:42 +00:00
|
|
|
|
2020-06-30 18:17:41 +02:00
|
|
|
protected $useSoftDeletes = true;
|
2020-06-08 20:32:42 +00:00
|
|
|
protected $useTimestamps = true;
|
2020-06-30 18:17:41 +02:00
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
protected $validationRules = [
|
|
|
|
'podcast_id' => 'required',
|
|
|
|
'title' => 'required',
|
|
|
|
'slug' => 'required|regex_match[/^[a-zA-Z0-9\-]{1,191}$/]',
|
|
|
|
'enclosure_uri' => 'required',
|
|
|
|
'description' => 'required',
|
|
|
|
'image_uri' => 'required',
|
2020-08-14 18:27:57 +00:00
|
|
|
'number' => 'required|is_natural_no_zero',
|
|
|
|
'season_number' => 'required|is_natural_no_zero',
|
2020-07-16 10:08:23 +00:00
|
|
|
'type' => 'required',
|
2020-08-14 18:27:57 +00:00
|
|
|
'published_at' => 'valid_date|permit_empty',
|
|
|
|
'created_by' => 'required',
|
|
|
|
'updated_by' => 'required',
|
2020-07-16 10:08:23 +00:00
|
|
|
];
|
|
|
|
protected $validationMessages = [];
|
|
|
|
|
2020-07-02 10:08:32 +00:00
|
|
|
protected $afterInsert = ['writeEnclosureMetadata', 'clearCache'];
|
|
|
|
protected $afterUpdate = ['writeEnclosureMetadata', 'clearCache'];
|
2020-08-04 11:25:22 +00:00
|
|
|
protected $beforeDelete = ['clearCache'];
|
2020-06-30 18:17:41 +02:00
|
|
|
|
|
|
|
protected function writeEnclosureMetadata(array $data)
|
|
|
|
{
|
|
|
|
helper('id3');
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
$episode = (new EpisodeModel())->find(
|
2020-06-30 18:17:41 +02:00
|
|
|
is_array($data['id']) ? $data['id'][0] : $data['id']
|
|
|
|
);
|
|
|
|
|
|
|
|
write_enclosure_tags($episode);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2020-07-02 10:08:32 +00:00
|
|
|
protected function clearCache(array $data)
|
2020-06-30 18:17:41 +02:00
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
$episode = (new EpisodeModel())->find(
|
2020-06-30 18:17:41 +02:00
|
|
|
is_array($data['id']) ? $data['id'][0] : $data['id']
|
|
|
|
);
|
|
|
|
|
2020-07-02 10:08:32 +00:00
|
|
|
// delete cache for rss feed, podcast and episode pages
|
2020-07-10 12:20:25 +00:00
|
|
|
cache()->delete(md5($episode->podcast->feed_url));
|
|
|
|
cache()->delete(md5($episode->podcast->link));
|
|
|
|
cache()->delete(md5($episode->link));
|
|
|
|
|
|
|
|
// delete model requests cache
|
|
|
|
cache()->delete("{$episode->podcast_id}_episodes");
|
2020-08-04 11:25:22 +00:00
|
|
|
|
|
|
|
return $data;
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all episodes for a podcast
|
|
|
|
*
|
|
|
|
* @param int $podcastId
|
|
|
|
*
|
|
|
|
* @return \App\Entities\Episode[]
|
|
|
|
*/
|
|
|
|
public function getPodcastEpisodes(int $podcastId): array
|
|
|
|
{
|
|
|
|
if (!($found = cache("{$podcastId}_episodes"))) {
|
|
|
|
$found = $this->where('podcast_id', $podcastId)->findAll();
|
|
|
|
|
|
|
|
cache()->save("{$podcastId}_episodes", $found, 300);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2020-06-30 18:17:41 +02:00
|
|
|
}
|
2020-06-08 20:32:42 +00:00
|
|
|
}
|