Commit b63c1dc9 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

feat: add downloads count to episode list

parent fc009f3d
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ use RuntimeException;
 * @property bool $is_published_on_hubs
 * @property int $posts_count
 * @property int $comments_count
 * @property int $downloads
 * @property EpisodeComment[]|null $comments
 * @property bool $is_premium
 * @property int $created_by
@@ -109,6 +110,8 @@ class Episode extends Entity

    protected ?Chapters $chapters = null;

    protected int $downloads = 0;

    /**
     * @var Person[]|null
     */
+33 −0
Original line number Diff line number Diff line
@@ -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;
    }
}
+13 −4
Original line number Diff line number Diff line
@@ -71,19 +71,28 @@ class EpisodeController extends BaseController
            // Use LIKE operator as a fallback.
            if (strlen($query) < 4) {
                $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)
                    ->orLike('description_markdown', $query)
                    ->groupBy('episodes.id')
                    ->orderBy('-`published_at`', '', false)
                    ->orderBy('created_at', 'desc');
            } else {
                $episodes = (new EpisodeModel())
                    ->where('podcast_id', $this->podcast->id)
                    ->where("MATCH (title, description_markdown) AGAINST ('{$query}')");
                    ->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)
                    ->where("MATCH (title, description_markdown) AGAINST ('{$query}')")
                    ->groupBy('episodes.id');
            }
        } else {
            $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('created_at', 'desc');
        }
+1 −0
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ return [
        }',
        'episode' => 'Episode',
        'visibility' => 'Visibility',
        'downloads' => 'Downloads',
        'comments' => 'Comments',
        'actions' => 'Actions',
    ],
+7 −0
Original line number Diff line number Diff line
@@ -82,9 +82,16 @@ data_table(
                return publication_pill(
                    $episode->published_at,
                    $episode->publication_status,
                    'text-sm'
                );
            },
        ],
        [
            'header' => lang('Episode.list.downloads'),
            'cell' => function ($episode): string {
                return downloads_abbr($episode->downloads);
            },
        ],
        [
            'header' => lang('Episode.list.comments'),
            'cell' => function ($episode): int {