Newer
Older

Yassine Doghri
committed

Yassine Doghri
committed
declare(strict_types=1);
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Models;

Yassine Doghri
committed
use App\Entities\Category;
use CodeIgniter\Model;
class CategoryModel extends Model
{

Yassine Doghri
committed
/**
* @var string
*/
protected $table = 'categories';

Yassine Doghri
committed
/**
* @var string
*/
protected $primaryKey = 'id';

Yassine Doghri
committed
/**
* @var string[]
*/
protected $allowedFields = ['parent_id', 'code', 'apple_category', 'google_category'];

Yassine Doghri
committed
/**
* @var string
*/
protected $returnType = Category::class;

Yassine Doghri
committed
/**
* @var bool
*/
protected $useSoftDeletes = false;

Yassine Doghri
committed
/**
* @var bool
*/
protected $useTimestamps = false;

Yassine Doghri
committed
public function getCategoryById(int $id): ?Category

Yassine Doghri
committed
{
return $this->find($id);

Yassine Doghri
committed
}
/**
* @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 {

Yassine Doghri
committed
$result[$category->id] = lang('Podcast.category_options.' . $category->code);
return $result;
},
);
cache()
->save($cacheName, $options, DECADE);
}
return $options;
}
/**
* Sets categories for a given podcast
*
* @param int[] $categoriesIds
*
* @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')
->delete([
'podcast_id' => $podcastId,
]);
if ($categoriesIds === []) {

Yassine Doghri
committed
// no row has been inserted after deletion
return 0;
}

Yassine Doghri
committed
// prepare data for `podcasts_categories` table
$data = array_reduce(
$categoriesIds,
function (array $result, int $categoryId) use ($podcastId): array {

Yassine Doghri
committed
$result[] = [
'podcast_id' => $podcastId,
'category_id' => $categoryId,
];
return $result;
},
[],
);
// Set podcast categories
return $this->db->table('podcasts_categories')
->insertBatch($data);
}
/**
* Gets all the podcast categories
*

Yassine Doghri
committed
* @return Category[]

Yassine Doghri
committed
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);
}
return $categories;
}