feat: add downloads count to episode list
This commit is contained in:
parent
fc009f3d00
commit
b63c1dc9b1
|
@ -72,6 +72,7 @@ use RuntimeException;
|
||||||
* @property bool $is_published_on_hubs
|
* @property bool $is_published_on_hubs
|
||||||
* @property int $posts_count
|
* @property int $posts_count
|
||||||
* @property int $comments_count
|
* @property int $comments_count
|
||||||
|
* @property int $downloads
|
||||||
* @property EpisodeComment[]|null $comments
|
* @property EpisodeComment[]|null $comments
|
||||||
* @property bool $is_premium
|
* @property bool $is_premium
|
||||||
* @property int $created_by
|
* @property int $created_by
|
||||||
|
@ -109,6 +110,8 @@ class Episode extends Entity
|
||||||
|
|
||||||
protected ?Chapters $chapters = null;
|
protected ?Chapters $chapters = null;
|
||||||
|
|
||||||
|
protected int $downloads = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Person[]|null
|
* @var Person[]|null
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -447,3 +447,36 @@ if (! function_exists('category_label')) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
if (! function_exists('downloads_abbr')) {
|
||||||
|
function downloads_abbr(int $downloads): string
|
||||||
|
{
|
||||||
|
if ($downloads < 1000) {
|
||||||
|
return (string) $downloads;
|
||||||
|
}
|
||||||
|
|
||||||
|
$option = match (true) {
|
||||||
|
$downloads < 1_000_000 => [
|
||||||
|
'divider' => 1_000,
|
||||||
|
'suffix' => 'K',
|
||||||
|
],
|
||||||
|
$downloads < 1_000_000_000 => [
|
||||||
|
'divider' => 1_000_000,
|
||||||
|
'suffix' => 'M',
|
||||||
|
],
|
||||||
|
default => [
|
||||||
|
'divider' => 1_000_000_000,
|
||||||
|
'suffix' => 'B',
|
||||||
|
],
|
||||||
|
};
|
||||||
|
$formatter = new NumberFormatter(service('request')->getLocale(), NumberFormatter::DECIMAL);
|
||||||
|
|
||||||
|
$formatter->setPattern('#,##0.##');
|
||||||
|
|
||||||
|
$abbr = $formatter->format($downloads / $option['divider']) . $option['suffix'];
|
||||||
|
|
||||||
|
return <<<HTML
|
||||||
|
<abbr title="{$downloads}">{$abbr}</abbr>
|
||||||
|
HTML;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -71,19 +71,28 @@ class EpisodeController extends BaseController
|
||||||
// Use LIKE operator as a fallback.
|
// Use LIKE operator as a fallback.
|
||||||
if (strlen($query) < 4) {
|
if (strlen($query) < 4) {
|
||||||
$episodes = (new EpisodeModel())
|
$episodes = (new EpisodeModel())
|
||||||
->where('podcast_id', $this->podcast->id)
|
->select('episodes.*, IFNULL(SUM(ape.hits),0) as downloads')
|
||||||
|
->join('analytics_podcasts_by_episode ape', 'episodes.id=ape.episode_id', 'left')
|
||||||
|
->where('episodes.podcast_id', $this->podcast->id)
|
||||||
->like('title', $query)
|
->like('title', $query)
|
||||||
->orLike('description_markdown', $query)
|
->orLike('description_markdown', $query)
|
||||||
|
->groupBy('episodes.id')
|
||||||
->orderBy('-`published_at`', '', false)
|
->orderBy('-`published_at`', '', false)
|
||||||
->orderBy('created_at', 'desc');
|
->orderBy('created_at', 'desc');
|
||||||
} else {
|
} else {
|
||||||
$episodes = (new EpisodeModel())
|
$episodes = (new EpisodeModel())
|
||||||
->where('podcast_id', $this->podcast->id)
|
->select('episodes.*, IFNULL(SUM(ape.hits),0) as downloads')
|
||||||
->where("MATCH (title, description_markdown) AGAINST ('{$query}')");
|
->join('analytics_podcasts_by_episode ape', 'episodes.id=ape.episode_id', 'left')
|
||||||
|
->where('episodes.podcast_id', $this->podcast->id)
|
||||||
|
->where("MATCH (title, description_markdown) AGAINST ('{$query}')")
|
||||||
|
->groupBy('episodes.id');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$episodes = (new EpisodeModel())
|
$episodes = (new EpisodeModel())
|
||||||
->where('podcast_id', $this->podcast->id)
|
->select('episodes.*, IFNULL(SUM(ape.hits),0) as downloads')
|
||||||
|
->join('analytics_podcasts_by_episode ape', 'episodes.id=ape.episode_id', 'left')
|
||||||
|
->where('episodes.podcast_id', $this->podcast->id)
|
||||||
|
->groupBy('episodes.id')
|
||||||
->orderBy('-`published_at`', '', false)
|
->orderBy('-`published_at`', '', false)
|
||||||
->orderBy('created_at', 'desc');
|
->orderBy('created_at', 'desc');
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,7 @@ return [
|
||||||
}',
|
}',
|
||||||
'episode' => 'Episode',
|
'episode' => 'Episode',
|
||||||
'visibility' => 'Visibility',
|
'visibility' => 'Visibility',
|
||||||
|
'downloads' => 'Downloads',
|
||||||
'comments' => 'Comments',
|
'comments' => 'Comments',
|
||||||
'actions' => 'Actions',
|
'actions' => 'Actions',
|
||||||
],
|
],
|
||||||
|
|
|
@ -82,9 +82,16 @@ data_table(
|
||||||
return publication_pill(
|
return publication_pill(
|
||||||
$episode->published_at,
|
$episode->published_at,
|
||||||
$episode->publication_status,
|
$episode->publication_status,
|
||||||
|
'text-sm'
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
'header' => lang('Episode.list.downloads'),
|
||||||
|
'cell' => function ($episode): string {
|
||||||
|
return downloads_abbr($episode->downloads);
|
||||||
|
},
|
||||||
|
],
|
||||||
[
|
[
|
||||||
'header' => lang('Episode.list.comments'),
|
'header' => lang('Episode.list.comments'),
|
||||||
'cell' => function ($episode): int {
|
'cell' => function ($episode): int {
|
||||||
|
|
Loading…
Reference in New Issue