Skip to content
Snippets Groups Projects
PlatformModel.php 5.55 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
     * 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/
     */
    
    namespace App\Models;
    
    
    use CodeIgniter\Model;
    
    class PlatformModel extends Model
    {
    
        protected $table = 'platforms';
    
        protected $primaryKey = 'slug';
    
        protected $allowedFields = ['slug', 'type', 'label', 'home_url', 'submit_url'];
    
        /**
         * @var string
         */
        protected $returnType = Platform::class;
    
        protected $useSoftDeletes = false;
    
    
        protected $useTimestamps = false;
    
        /**
         * @return Platform[]
         */
        public function getPlatforms(): array
    
            if (! ($found = cache('platforms'))) {
    
                $baseUrl = rtrim(config('app')->baseURL, '/');
    
                    "*, CONCAT('{$baseUrl}/assets/images/platforms/',`type`,'/',`slug`,'.svg') as icon",
    
                cache()
                    ->save('platforms', $found, DECADE);
    
        public function getPlatform(string $slug): ?Platform
    
            if (! ($found = cache($cacheName))) {
                $found = $this->where('slug', $slug)
                    ->first();
                cache()
                    ->save($cacheName, $found, DECADE);
    
        public function createPlatform(
    
            string $slug,
            string $type,
            string $label,
            string $homeUrl,
            string $submitUrl = null
    
            $data = [
                'slug' => $slug,
                'type' => $type,
                'label' => $label,
                'home_url' => $homeUrl,
                'submit_url' => $submitUrl,
            ];
    
            return $this->insert($data, false);
        }
    
    
         */
        public function getPlatformsWithLinks(int $podcastId, string $platformType): array
    
                ! ($found = cache("podcast#{$podcastId}_platforms_{$platformType}_withLinks"))
    
                    'platforms.*, podcasts_platforms.link_url, podcasts_platforms.link_content, podcasts_platforms.is_visible, podcasts_platforms.is_on_embeddable_player',
    
                        "podcasts_platforms.platform_slug = platforms.slug AND podcasts_platforms.podcast_id = {$podcastId}",
    
                    ->where('platforms.type', $platformType)
    
                    ->save("podcast#{$podcastId}_platforms_{$platformType}_withLinks", $found, DECADE);
    
        /**
         * @return Platform[]
         */
        public function getPodcastPlatforms(int $podcastId, string $platformType): array
    
            $cacheName = "podcast#{$podcastId}_platforms_{$platformType}";
    
                    'platforms.*, podcasts_platforms.link_url, podcasts_platforms.link_content, podcasts_platforms.is_visible, podcasts_platforms.is_on_embeddable_player',
    
                    ->join('podcasts_platforms', 'podcasts_platforms.platform_slug = platforms.slug')
    
                    ->where('podcasts_platforms.podcast_id', $podcastId)
                    ->where('platforms.type', $platformType)
    
                cache()
                    ->save($cacheName, $found, DECADE);
    
         * @param mixed[] $podcastsPlatformsData
    
        public function savePodcastPlatforms(
    
            int $podcastId,
            string $platformType,
            array $podcastsPlatformsData
    
            $podcastsPlatformsTable = $this->db->prefixTable('podcasts_platforms');
            $platformsTable = $this->db->prefixTable('platforms');
    
            DELETE {$podcastsPlatformsTable}
            FROM {$podcastsPlatformsTable}
            INNER JOIN {$platformsTable} ON {$platformsTable}.slug = {$podcastsPlatformsTable}.platform_slug
    
            $this->db->query($deleteJoinQuery, [$podcastId, $platformType]);
    
                ->table('podcasts_platforms')
                ->insertBatch($podcastsPlatformsData);
    
         * @param mixed[] $podcastsPlatformsData
    
        public function createPodcastPlatforms(int $podcastId, array $podcastsPlatformsData): int | false
    
            return $this->db
                ->table('podcasts_platforms')
                ->insertBatch($podcastsPlatformsData);
    
        public function removePodcastPlatform(int $podcastId, string $platformSlug): bool | string
    
            return $this->db->table('podcasts_platforms')
                ->delete([
                    'podcast_id' => $podcastId,
                    'platform_slug' => $platformSlug,
                ]);
    
        public function clearCache(int $podcastId): void
    
            cache()->deleteMatching("podcast#{$podcastId}_platforms_*");
    
            cache()
                ->deleteMatching("page_podcast#{$podcastId}*");