mirror of
https://code.castopod.org/adaures/castopod.git
synced 2024-09-06 10:49:20 +02:00
- update CI process to include quality stage (tests + code review) - add captainhook to install git pre-commit & pre-push hooks - remove .devcontainer Dockerfile to use project's docker-compose services: all services can now be started automatically using vscode - update docs/setup-development.md
139 lines
3.3 KiB
PHP
139 lines
3.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @copyright 2020 Podlibre
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
* @link https://castopod.org/
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\Category;
|
|
use CodeIgniter\Model;
|
|
|
|
class CategoryModel extends Model
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $table = 'categories';
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $primaryKey = 'id';
|
|
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
protected $allowedFields = [
|
|
'parent_id',
|
|
'code',
|
|
'apple_category',
|
|
'google_category',
|
|
];
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $returnType = Category::class;
|
|
/**
|
|
* @var bool
|
|
*/
|
|
protected $useSoftDeletes = false;
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
protected $useTimestamps = false;
|
|
|
|
public function getCategoryById($id): ?Category
|
|
{
|
|
return $this->find($id);
|
|
}
|
|
|
|
public function getCategoryOptions()
|
|
{
|
|
$locale = service('request')->getLocale();
|
|
$cacheName = "category_options_{$locale}";
|
|
|
|
if (!($options = cache($cacheName))) {
|
|
$categories = $this->findAll();
|
|
|
|
$options = array_reduce(
|
|
$categories,
|
|
function ($result, $category) {
|
|
$result[$category->id] = lang(
|
|
'Podcast.category_options.' . $category->code,
|
|
);
|
|
return $result;
|
|
},
|
|
[],
|
|
);
|
|
|
|
cache()->save($cacheName, $options, DECADE);
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* Sets categories for a given podcast
|
|
*
|
|
* @return int|bool Number of rows inserted or FALSE on failure
|
|
*/
|
|
public function setPodcastCategories(int $podcastId, ?array $categories)
|
|
{
|
|
cache()->delete("podcast#{$podcastId}_categories");
|
|
|
|
// Remove already previously set categories to overwrite them
|
|
$this->db
|
|
->table('podcasts_categories')
|
|
->delete(['podcast_id' => $podcastId]);
|
|
|
|
if (empty($categories)) {
|
|
// no row has been inserted after deletion
|
|
return 0;
|
|
}
|
|
|
|
// prepare data for `podcasts_categories` table
|
|
$data = array_reduce(
|
|
$categories,
|
|
function ($result, $categoryId) use ($podcastId) {
|
|
$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
|
|
*
|
|
* @return Category[]
|
|
*/
|
|
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;
|
|
}
|
|
}
|