65 lines
2.2 KiB
PHP
65 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\ArtworkDownload;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Carbon\Carbon;
|
|
|
|
class TodayDownloadsController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$hits = 30;
|
|
|
|
$today = Carbon::now()->toDateString();
|
|
|
|
$query = ArtworkDownload::with(['artwork'])
|
|
->whereDate('created_at', $today)
|
|
->whereHas('artwork', function ($q) {
|
|
$q->public()->published()->whereNull('deleted_at');
|
|
})
|
|
->selectRaw('artwork_id, COUNT(*) as num_downloads')
|
|
->groupBy('artwork_id')
|
|
->orderByDesc('num_downloads');
|
|
|
|
$paginator = $query->paginate($hits)->withQueryString();
|
|
|
|
$paginator->getCollection()->transform(function ($row) {
|
|
$art = $row->artwork ?? null;
|
|
if (! $art && isset($row->artwork_id)) {
|
|
$art = \App\Models\Artwork::find($row->artwork_id);
|
|
}
|
|
|
|
$name = $art->title ?? null;
|
|
$picture = $art->file_name ?? null;
|
|
$ext = pathinfo($picture ?? '', PATHINFO_EXTENSION) ?: 'jpg';
|
|
$encoded = null;
|
|
$present = $art ? \App\Services\ThumbnailPresenter::present($art, 'md') : null;
|
|
$thumb = $present ? $present['url'] : '/gfx/sb_join.jpg';
|
|
$categoryId = $art->categories->first()->id ?? null;
|
|
|
|
return (object) [
|
|
'id' => $art->id ?? null,
|
|
'name' => $name,
|
|
'picture' => $picture,
|
|
'slug' => $art->slug ?? Str::slug($name ?? ''),
|
|
'ext' => $ext,
|
|
'encoded' => $encoded,
|
|
'thumb' => $thumb,
|
|
'thumb_srcset' => $thumb,
|
|
'category' => $categoryId,
|
|
'num_downloads' => $row->num_downloads ?? 0,
|
|
'gid_num' => $categoryId ? ((int) $categoryId % 5) * 5 : 0,
|
|
];
|
|
});
|
|
|
|
$page_title = 'Today Downloaded Artworks';
|
|
|
|
return view('web.downloads.today', ['page_title' => $page_title, 'artworks' => $paginator]);
|
|
}
|
|
}
|