80 lines
2.5 KiB
PHP
80 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use App\Models\Artwork;
|
|
use App\Models\Artist;
|
|
use App\Models\Episode;
|
|
|
|
class PageController extends Controller
|
|
{
|
|
public function landing()
|
|
{
|
|
$user = auth()->user();
|
|
$headerCounters = $this->getHeaderCounters();
|
|
$recentEpisodes = $this->mostRecentEpisodes();
|
|
return view('home.page', [
|
|
'user' => $user,
|
|
'headerCounters' => $headerCounters,
|
|
'recentEpisodes' => $recentEpisodes,
|
|
]);
|
|
}
|
|
|
|
private function mostRecentEpisodes()
|
|
{
|
|
$episodes = Cache::remember('latestEpisodes', 30, function() {
|
|
return Episode::where('published', true)
|
|
->with('podcast')
|
|
->with('artwork')
|
|
->with('artwork.artist')
|
|
->orderBy('episode_date', 'desc')
|
|
->limit(10)
|
|
->get();
|
|
});
|
|
return $episodes;
|
|
}
|
|
|
|
private function getHeaderCounters()
|
|
{
|
|
$headerCounters = [];
|
|
$artworkCountNumber = Cache::remember('artworkCountNumber', 10, function() {
|
|
return Artwork::all()->count();
|
|
});
|
|
$artistCountNumber = Cache::remember('artistCountNumber', 10, function() {
|
|
return Artist::all()->count();
|
|
});
|
|
$episodeCountNumber = Cache::remember('episodeCountNumber', 10, function() {
|
|
return Episode::all()->count();
|
|
});
|
|
$headerCounters['Artworks'] = $this->shortNumberCount($artworkCountNumber);
|
|
$headerCounters['Artists'] = $this->shortNumberCount($artistCountNumber);
|
|
$headerCounters['Episodes'] = $this->shortNumberCount($episodeCountNumber);
|
|
return $headerCounters;
|
|
}
|
|
private function shortNumberCount($number)
|
|
{
|
|
$units = ['', 'K', 'M', 'B', 'T'];
|
|
for ($i = 0; $number >= 1000; $i++) {
|
|
$number /= 1000;
|
|
}
|
|
return [
|
|
'number' => $this->numberFormatPrecision($number, 1), //number_format(floatval($number), 1),
|
|
'unit' => $units[$i],
|
|
];
|
|
}
|
|
|
|
private function numberFormatPrecision($number, $precision = 2, $separator = '.')
|
|
{
|
|
$numberParts = explode($separator, $number);
|
|
$response = $numberParts[0];
|
|
if (count($numberParts)>1 && $precision > 0) {
|
|
$response .= $separator;
|
|
$response .= substr($numberParts[1], 0, $precision);
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
}
|