2020-06-08 17:01:58 +02:00
|
|
|
<?php
|
2020-08-04 13:25:22 +02:00
|
|
|
|
2020-06-08 17:01:58 +02:00
|
|
|
/**
|
|
|
|
* Class PlatformModel
|
|
|
|
* Model for platforms table in database
|
2020-06-10 17:00:12 +02:00
|
|
|
*
|
2020-06-08 17:01:58 +02:00
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
2020-08-04 13:25:22 +02:00
|
|
|
|
2020-06-08 17:01:58 +02:00
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
|
|
class PlatformModel extends Model
|
|
|
|
{
|
|
|
|
protected $table = 'platforms';
|
|
|
|
protected $primaryKey = 'id';
|
|
|
|
|
2020-10-29 16:45:19 +01:00
|
|
|
protected $allowedFields = ['name', 'label', 'home_url', 'submit_url'];
|
2020-06-08 17:01:58 +02:00
|
|
|
|
2020-08-04 13:25:22 +02:00
|
|
|
protected $returnType = \App\Entities\Platform::class;
|
2020-06-08 17:01:58 +02:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
|
|
|
protected $useTimestamps = true;
|
2020-08-27 12:05:44 +02:00
|
|
|
|
2020-09-04 11:09:26 +02:00
|
|
|
public function getPlatformsWithLinks($podcastId)
|
2020-08-27 12:05:44 +02:00
|
|
|
{
|
2020-09-04 11:09:26 +02:00
|
|
|
if (!($found = cache("podcast{$podcastId}_platforms"))) {
|
|
|
|
$found = $this->select(
|
2020-10-29 16:45:19 +01:00
|
|
|
'platforms.*, platform_links.link_url, platform_links.is_visible'
|
2020-08-27 12:05:44 +02:00
|
|
|
)
|
2020-09-04 11:09:26 +02:00
|
|
|
->join(
|
|
|
|
'platform_links',
|
|
|
|
"platform_links.platform_id = platforms.id AND platform_links.podcast_id = $podcastId",
|
|
|
|
'left'
|
|
|
|
)
|
|
|
|
->findAll();
|
|
|
|
|
|
|
|
cache()->save("podcast{$podcastId}_platforms", $found, DECADE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:45:19 +01:00
|
|
|
public function getPodcastPlatforms($podcastId)
|
2020-09-04 11:09:26 +02:00
|
|
|
{
|
2020-10-29 16:45:19 +01:00
|
|
|
if (!($found = cache("podcast{$podcastId}_podcastPlatforms"))) {
|
2020-09-04 11:09:26 +02:00
|
|
|
$found = $this->select(
|
2020-10-29 16:45:19 +01:00
|
|
|
'platforms.*, platform_links.link_url, platform_links.is_visible'
|
2020-09-04 11:09:26 +02:00
|
|
|
)
|
|
|
|
->join(
|
|
|
|
'platform_links',
|
|
|
|
'platform_links.platform_id = platforms.id'
|
|
|
|
)
|
|
|
|
->where('platform_links.podcast_id', $podcastId)
|
|
|
|
->findAll();
|
|
|
|
|
2020-10-29 16:45:19 +01:00
|
|
|
cache()->save(
|
|
|
|
"podcast{$podcastId}_podcastPlatforms",
|
|
|
|
$found,
|
|
|
|
DECADE
|
|
|
|
);
|
2020-09-04 11:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2020-08-27 12:05:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 16:45:19 +01:00
|
|
|
public function savePodcastPlatforms($podcastId, $platformLinksData)
|
2020-08-27 12:05:44 +02:00
|
|
|
{
|
2020-10-20 12:31:40 +02:00
|
|
|
$this->clearCache($podcastId);
|
2020-09-04 11:09:26 +02:00
|
|
|
|
2020-08-27 12:05:44 +02:00
|
|
|
// Remove already previously set platforms to overwrite them
|
|
|
|
$this->db
|
|
|
|
->table('platform_links')
|
|
|
|
->delete(['podcast_id' => $podcastId]);
|
|
|
|
|
2020-10-29 16:45:19 +01:00
|
|
|
// Set podcastPlatforms
|
2020-08-27 12:05:44 +02:00
|
|
|
return $this->db
|
|
|
|
->table('platform_links')
|
|
|
|
->insertBatch($platformLinksData);
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:45:19 +01:00
|
|
|
public function getPlatformId(string $platformName)
|
2020-08-27 12:05:44 +02:00
|
|
|
{
|
2020-10-29 16:45:19 +01:00
|
|
|
$p = $this->where('name', $platformName)->first();
|
2020-08-27 12:05:44 +02:00
|
|
|
|
|
|
|
if (!$p) {
|
2020-10-29 16:45:19 +01:00
|
|
|
$this->error = lang('Platform.platformNotFound', [$platformName]);
|
2020-08-27 12:05:44 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) $p->id;
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:45:19 +01:00
|
|
|
public function removePodcastPlatform($podcastId, $platformId)
|
2020-08-27 12:05:44 +02:00
|
|
|
{
|
2020-10-20 12:31:40 +02:00
|
|
|
$this->clearCache($podcastId);
|
2020-09-04 11:09:26 +02:00
|
|
|
|
2020-08-27 12:05:44 +02:00
|
|
|
return $this->db->table('platform_links')->delete([
|
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'platform_id' => $platformId,
|
|
|
|
]);
|
|
|
|
}
|
2020-10-20 12:31:40 +02:00
|
|
|
|
|
|
|
public function clearCache($podcastId)
|
|
|
|
{
|
|
|
|
cache()->delete("podcast{$podcastId}_platforms");
|
2020-10-29 16:45:19 +01:00
|
|
|
cache()->delete("podcast{$podcastId}_podcastPlatforms");
|
2020-10-20 12:31:40 +02:00
|
|
|
|
|
|
|
// delete localized podcast page cache
|
|
|
|
$episodeModel = new EpisodeModel();
|
|
|
|
$years = $episodeModel->getYears($podcastId);
|
|
|
|
$seasons = $episodeModel->getSeasons($podcastId);
|
|
|
|
$supportedLocales = config('App')->supportedLocales;
|
|
|
|
|
|
|
|
foreach ($years as $year) {
|
|
|
|
foreach ($supportedLocales as $locale) {
|
|
|
|
cache()->delete(
|
|
|
|
"page_podcast{$podcastId}_{$year['year']}_{$locale}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($seasons as $season) {
|
|
|
|
foreach ($supportedLocales as $locale) {
|
|
|
|
cache()->delete(
|
|
|
|
"page_podcast{$podcastId}_season{$season['season_number']}_{$locale}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 17:00:12 +02:00
|
|
|
}
|