Newer
Older

Yassine Doghri
committed
/**
* Class PlatformModel
* Model for platforms table in database
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/

Yassine Doghri
committed
namespace App\Models;
use CodeIgniter\Model;
class PlatformModel extends Model
{
protected $table = 'platforms';
protected $primaryKey = 'id';
protected $allowedFields = [
'name',
'home_url',
'submit_url',
'iosapp_url',
'androidapp_url',
'comment',
'display_by_default',
'ios_deeplink',
'android_deeplink',
'logo_file_name',
];

Yassine Doghri
committed
protected $returnType = \App\Entities\Platform::class;
protected $useSoftDeletes = false;
protected $useTimestamps = true;
public function getPlatformsWithLinks($podcastId)
if (!($found = cache("podcast{$podcastId}_platforms"))) {
$found = $this->select(
'platforms.*, platform_links.link_url, platform_links.visible'
->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;
}
public function getPodcastPlatformLinks($podcastId)
{
if (!($found = cache("podcast{$podcastId}_platformLinks"))) {
$found = $this->select(
'platforms.*, platform_links.link_url, platform_links.visible'
)
->join(
'platform_links',
'platform_links.platform_id = platforms.id'
)
->where('platform_links.podcast_id', $podcastId)
->findAll();
cache()->save("podcast{$podcastId}_platformLinks", $found, DECADE);
}
return $found;
}
public function savePlatformLinks($podcastId, $platformLinksData)
{

Yassine Doghri
committed
$this->clearCache($podcastId);
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Remove already previously set platforms to overwrite them
$this->db
->table('platform_links')
->delete(['podcast_id' => $podcastId]);
// Set platformLinks
return $this->db
->table('platform_links')
->insertBatch($platformLinksData);
}
public function getPlatformId($platform)
{
if (is_numeric($platform)) {
return (int) $platform;
}
$p = $this->where('name', $platform)->first();
if (!$p) {
$this->error = lang('Platform.platformNotFound', [$platform]);
return false;
}
return (int) $p->id;
}
public function removePlatformLink($podcastId, $platformId)
{

Yassine Doghri
committed
$this->clearCache($podcastId);
return $this->db->table('platform_links')->delete([
'podcast_id' => $podcastId,
'platform_id' => $platformId,
]);
}

Yassine Doghri
committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
public function clearCache($podcastId)
{
cache()->delete("podcast{$podcastId}_platforms");
cache()->delete("podcast{$podcastId}_platformLinks");
// 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}"
);
}
}
}