2020-05-29 16:25:17 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
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-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[]
|
|
|
|
*/
|
2020-06-26 14:34:52 +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;
|
|
|
|
/**
|
|
|
|
* @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-06 14:00:48 +00:00
|
|
|
public function getCategoryById($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
|
|
|
|
|
|
|
public function getCategoryOptions()
|
|
|
|
{
|
2021-04-20 13:43:38 +00:00
|
|
|
$locale = service('request')->getLocale();
|
|
|
|
$cacheName = "category_options_{$locale}";
|
|
|
|
|
|
|
|
if (!($options = cache($cacheName))) {
|
2020-09-04 09:09:26 +00:00
|
|
|
$categories = $this->findAll();
|
|
|
|
|
|
|
|
$options = array_reduce(
|
|
|
|
$categories,
|
|
|
|
function ($result, $category) {
|
|
|
|
$result[$category->id] = lang(
|
2021-04-20 13:43:38 +00:00
|
|
|
'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-04-20 13:43:38 +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-06 14:00:48 +00:00
|
|
|
* @return int|bool Number of rows inserted or FALSE on failure
|
2020-10-02 15:38:16 +00:00
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
public function setPodcastCategories(int $podcastId, ?array $categories)
|
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')
|
|
|
|
->delete(['podcast_id' => $podcastId]);
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
if (empty($categories)) {
|
|
|
|
// 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(
|
|
|
|
$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);
|
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";
|
|
|
|
if (!($categories = cache($cacheName))) {
|
2020-10-02 15:38:16 +00:00
|
|
|
$categories = $this->select('categories.*')
|
|
|
|
->join(
|
|
|
|
'podcasts_categories',
|
2021-04-20 13:43:38 +00:00
|
|
|
'podcasts_categories.category_id = categories.id',
|
2020-10-02 15:38:16 +00:00
|
|
|
)
|
|
|
|
->where('podcasts_categories.podcast_id', $podcastId)
|
|
|
|
->findAll();
|
|
|
|
|
2021-04-20 13:43:38 +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
|
|
|
}
|