Skip to content
Snippets Groups Projects
CategoryModel.php 3.43 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * @copyright  2020 Podlibre
     * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
     * @link       https://castopod.org/
     */
    
    use CodeIgniter\Model;
    
    class CategoryModel extends Model
    {
    
        protected $table = 'categories';
    
        protected $allowedFields = ['parent_id', 'code', 'apple_category', 'google_category'];
    
        /**
         * @var string
         */
        protected $returnType = Category::class;
    
        protected $useSoftDeletes = false;
    
    
        protected $useTimestamps = false;
    
        public function getCategoryById(int $id): ?Category
    
        /**
         * @return array<int, string>
         */
        public function getCategoryOptions(): array
    
            $locale = service('request')
                ->getLocale();
    
            $cacheName = "category_options_{$locale}";
    
    
            if (! ($options = cache($cacheName))) {
    
                $categories = $this->findAll();
    
                $options = array_reduce(
                    $categories,
    
                    function (array $result, Category $category): array {
    
                        $result[$category->id] = lang('Podcast.category_options.' . $category->code);
    
                cache()
                    ->save($cacheName, $options, DECADE);
    
    
        /**
         * Sets categories for a given podcast
         *
    
         * @return int|false Number of rows inserted or FALSE on failure
    
        public function setPodcastCategories(int $podcastId, array $categoriesIds = []): int | false
    
            cache()->delete("podcast#{$podcastId}_categories");
    
    
            // Remove already previously set categories to overwrite them
            $this->db
                ->table('podcasts_categories')
    
            // prepare data for `podcasts_categories` table
            $data = array_reduce(
    
                function (array $result, int $categoryId) use ($podcastId): array {
    
                    $result[] = [
                        'podcast_id' => $podcastId,
                        'category_id' => $categoryId,
                    ];
    
                    return $result;
                },
                [],
            );
    
            // Set podcast categories
    
            return $this->db->table('podcasts_categories')
                ->insertBatch($data);
    
        public function getPodcastCategories(int $podcastId): array
    
            $cacheName = "podcast#{$podcastId}_categories";
    
            if (! ($categories = cache($cacheName))) {
    
                $categories = $this->select('categories.*')
    
                    ->join('podcasts_categories', 'podcasts_categories.category_id = categories.id',)
    
                    ->where('podcasts_categories.podcast_id', $podcastId)
                    ->findAll();
    
    
                cache()
                    ->save($cacheName, $categories, DECADE);