2020-12-07 20:13:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class SoundbiteModel
|
|
|
|
* Model for podcasts_soundbites table in database
|
|
|
|
*
|
|
|
|
* @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 SoundbiteModel extends Model
|
|
|
|
{
|
|
|
|
protected $table = 'soundbites';
|
|
|
|
protected $primaryKey = 'id';
|
|
|
|
|
|
|
|
protected $allowedFields = [
|
|
|
|
'podcast_id',
|
|
|
|
'episode_id',
|
|
|
|
'label',
|
|
|
|
'start_time',
|
|
|
|
'duration',
|
|
|
|
'created_by',
|
|
|
|
'updated_by',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $returnType = \App\Entities\Soundbite::class;
|
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
|
|
|
protected $useTimestamps = true;
|
|
|
|
|
|
|
|
protected $afterInsert = ['clearCache'];
|
|
|
|
protected $afterUpdate = ['clearCache'];
|
|
|
|
protected $beforeDelete = ['clearCache'];
|
|
|
|
|
|
|
|
public function deleteSoundbite($podcastId, $episodeId, $soundbiteId)
|
|
|
|
{
|
|
|
|
return $this->delete([
|
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'episode_id' => $episodeId,
|
|
|
|
'id' => $soundbiteId,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all soundbites for an episode
|
|
|
|
*
|
|
|
|
* @param int $podcastId
|
|
|
|
* @param int $episodeId
|
|
|
|
*
|
|
|
|
* @return \App\Entities\Soundbite[]
|
|
|
|
*/
|
|
|
|
public function getEpisodeSoundbites(int $podcastId, int $episodeId): array
|
|
|
|
{
|
2021-04-20 13:43:38 +00:00
|
|
|
$cacheName = "podcast_episode#{$episodeId}_soundbites";
|
|
|
|
if (!($found = cache($cacheName))) {
|
2020-12-07 20:13:46 +00:00
|
|
|
$found = $this->where([
|
|
|
|
'episode_id' => $episodeId,
|
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
])
|
|
|
|
->orderBy('start_time')
|
|
|
|
->findAll();
|
2021-04-20 13:43:38 +00:00
|
|
|
cache()->save($cacheName, $found, DECADE);
|
2020-12-07 20:13:46 +00:00
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clearCache(array $data)
|
|
|
|
{
|
|
|
|
$episode = (new EpisodeModel())->find(
|
|
|
|
isset($data['data'])
|
|
|
|
? $data['data']['episode_id']
|
2021-04-20 13:43:38 +00:00
|
|
|
: $data['id']['episode_id'],
|
2020-12-07 20:13:46 +00:00
|
|
|
);
|
|
|
|
|
2021-04-20 13:43:38 +00:00
|
|
|
cache()->delete("podcast_episode#{$episode->id}_soundbites");
|
2020-12-07 20:13:46 +00:00
|
|
|
|
|
|
|
// delete cache for rss feed
|
2021-04-20 13:43:38 +00:00
|
|
|
cache()->deleteMatching("podcast#{$episode->podcast_id}_feed*");
|
|
|
|
|
|
|
|
cache()->deleteMatching(
|
|
|
|
"page_podcast#{$episode->podcast_id}_episode#{$episode->id}_*",
|
|
|
|
);
|
2020-12-07 20:13:46 +00:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|