2020-05-29 16:25:17 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
2020-05-29 16:25:17 +00:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
use App\Entities\Category;
|
2020-05-29 16:25:17 +00:00
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
|
|
class CategoryModel extends Model
|
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-05-31 19:15:52 +00:00
|
|
|
protected $table = 'categories';
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-05-29 16:25:17 +00:00
|
|
|
protected $primaryKey = 'id';
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
protected $allowedFields = ['parent_id', 'code', 'apple_category', 'google_category'];
|
2020-05-29 16:25:17 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = Category::class;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-05-29 16:25:17 +00:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-05-31 19:15:52 +00:00
|
|
|
protected $useTimestamps = false;
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function getCategoryById(int $id): ?Category
|
2020-08-04 11:25:22 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
return $this->find($id);
|
2020-08-04 11:25:22 +00:00
|
|
|
}
|
2020-09-04 09:09:26 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @return array<int, string>
|
|
|
|
*/
|
|
|
|
public function getCategoryOptions(): array
|
2020-09-04 09:09:26 +00:00
|
|
|
{
|
2021-05-19 16:35:13 +00:00
|
|
|
$locale = service('request')
|
|
|
|
->getLocale();
|
2021-04-20 13:43:38 +00:00
|
|
|
$cacheName = "category_options_{$locale}";
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($options = cache($cacheName))) {
|
2020-09-04 09:09:26 +00:00
|
|
|
$categories = $this->findAll();
|
|
|
|
|
|
|
|
$options = array_reduce(
|
|
|
|
$categories,
|
2021-05-14 17:59:35 +00:00
|
|
|
function (array $result, Category $category): array {
|
2021-06-08 09:52:11 +00:00
|
|
|
$result[$category->id] = lang('Podcast.category_options.' . $category->code);
|
2020-09-04 09:09:26 +00:00
|
|
|
return $result;
|
|
|
|
},
|
2021-04-20 13:43:38 +00:00
|
|
|
[],
|
2020-09-04 09:09:26 +00:00
|
|
|
);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $options, DECADE);
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
2020-10-02 15:38:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets categories for a given podcast
|
|
|
|
*
|
2021-05-18 17:16:36 +00:00
|
|
|
* @param int[] $categoriesIds
|
2021-05-19 16:35:13 +00:00
|
|
|
*
|
2021-05-14 17:59:35 +00:00
|
|
|
* @return int|false Number of rows inserted or FALSE on failure
|
2020-10-02 15:38:16 +00:00
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
public function setPodcastCategories(int $podcastId, array $categoriesIds = []): int | false
|
2020-10-02 15:38:16 +00:00
|
|
|
{
|
2021-04-20 13:43:38 +00:00
|
|
|
cache()->delete("podcast#{$podcastId}_categories");
|
2020-10-02 15:38:16 +00:00
|
|
|
|
|
|
|
// Remove already previously set categories to overwrite them
|
|
|
|
$this->db
|
|
|
|
->table('podcasts_categories')
|
2021-05-19 16:35:13 +00:00
|
|
|
->delete([
|
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
]);
|
2020-10-02 15:38:16 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
if ($categoriesIds === []) {
|
2021-05-06 14:00:48 +00:00
|
|
|
// no row has been inserted after deletion
|
|
|
|
return 0;
|
2020-10-02 15:38:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
// prepare data for `podcasts_categories` table
|
|
|
|
$data = array_reduce(
|
2021-05-18 17:16:36 +00:00
|
|
|
$categoriesIds,
|
2021-05-14 17:59:35 +00:00
|
|
|
function (array $result, int $categoryId) use ($podcastId): array {
|
2021-05-06 14:00:48 +00:00
|
|
|
$result[] = [
|
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'category_id' => $categoryId,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
|
|
|
// Set podcast categories
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->db->table('podcasts_categories')
|
|
|
|
->insertBatch($data);
|
2020-10-02 15:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all the podcast categories
|
|
|
|
*
|
2021-05-06 14:00:48 +00:00
|
|
|
* @return Category[]
|
2020-10-02 15:38:16 +00:00
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getPodcastCategories(int $podcastId): array
|
2020-10-02 15:38:16 +00:00
|
|
|
{
|
2021-04-20 13:43:38 +00:00
|
|
|
$cacheName = "podcast#{$podcastId}_categories";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($categories = cache($cacheName))) {
|
2020-10-02 15:38:16 +00:00
|
|
|
$categories = $this->select('categories.*')
|
2021-06-09 12:40:22 +00:00
|
|
|
->join('podcasts_categories', 'podcasts_categories.category_id = categories.id')
|
2020-10-02 15:38:16 +00:00
|
|
|
->where('podcasts_categories.podcast_id', $podcastId)
|
|
|
|
->findAll();
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $categories, DECADE);
|
2020-10-02 15:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $categories;
|
|
|
|
}
|
2020-05-29 16:25:17 +00:00
|
|
|
}
|