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;
|
|
|
|
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
|
|
class CategoryModel extends Model
|
|
|
|
{
|
2020-05-31 19:15:52 +00:00
|
|
|
protected $table = 'categories';
|
2020-05-29 16:25:17 +00:00
|
|
|
protected $primaryKey = 'id';
|
|
|
|
|
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
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
protected $returnType = \App\Entities\Category::class;
|
2020-05-29 16:25:17 +00:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
2020-05-31 19:15:52 +00:00
|
|
|
protected $useTimestamps = false;
|
2020-08-04 11:25:22 +00:00
|
|
|
|
|
|
|
public function findParent($parentId)
|
|
|
|
{
|
|
|
|
return $this->find($parentId);
|
|
|
|
}
|
2020-09-04 09:09:26 +00:00
|
|
|
|
|
|
|
public function getCategoryOptions()
|
|
|
|
{
|
|
|
|
if (!($options = cache('category_options'))) {
|
|
|
|
$categories = $this->findAll();
|
|
|
|
|
|
|
|
$options = array_reduce(
|
|
|
|
$categories,
|
|
|
|
function ($result, $category) {
|
|
|
|
$result[$category->id] = lang(
|
|
|
|
'Podcast.category_options.' . $category->code
|
|
|
|
);
|
|
|
|
return $result;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
cache()->save('category_options', $options, DECADE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
2020-10-02 15:38:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets categories for a given podcast
|
|
|
|
*
|
|
|
|
* @param int $podcastId
|
|
|
|
* @param array $categories
|
|
|
|
*
|
|
|
|
* @return integer|false Number of rows inserted or FALSE on failure
|
|
|
|
*/
|
|
|
|
public function setPodcastCategories($podcastId, $categories)
|
|
|
|
{
|
|
|
|
cache()->delete("podcasts{$podcastId}_categories");
|
|
|
|
|
|
|
|
// Remove already previously set categories to overwrite them
|
|
|
|
$this->db
|
|
|
|
->table('podcasts_categories')
|
|
|
|
->delete(['podcast_id' => $podcastId]);
|
|
|
|
|
|
|
|
if (!empty($categories)) {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// no row has been inserted after deletion
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all the podcast categories
|
|
|
|
*
|
|
|
|
* @param int $podcastId
|
|
|
|
*
|
|
|
|
* @return \App\Entities\Category[]
|
|
|
|
*/
|
|
|
|
public function getPodcastCategories($podcastId)
|
|
|
|
{
|
|
|
|
if (!($categories = cache("podcasts{$podcastId}_categories"))) {
|
|
|
|
$categories = $this->select('categories.*')
|
|
|
|
->join(
|
|
|
|
'podcasts_categories',
|
|
|
|
'podcasts_categories.category_id = categories.id'
|
|
|
|
)
|
|
|
|
->where('podcasts_categories.podcast_id', $podcastId)
|
|
|
|
->findAll();
|
|
|
|
|
|
|
|
cache()->save(
|
|
|
|
"podcasts{$podcastId}_categories",
|
|
|
|
$categories,
|
|
|
|
DECADE
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $categories;
|
|
|
|
}
|
2020-05-29 16:25:17 +00:00
|
|
|
}
|