Tstore/app/Http/Controllers/Web/StorefrontController.php

123 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Package;
use App\Services\RepoFormatter;
use Illuminate\Http\Request;
use Illuminate\View\View;
class StorefrontController extends Controller
{
public function home(): View
{
$featuredPlugins = Package::query()
->with(['categories', 'latestStableVersion'])
->where('status', 'published')
->where('type', 'plugin')
->orderByDesc('is_featured')
->orderByDesc('sort_order')
->orderByDesc('updated_at')
->limit(6)
->get();
$featuredThemes = Package::query()
->with(['categories', 'latestStableVersion'])
->where('status', 'published')
->where('type', 'theme')
->orderByDesc('is_featured')
->orderByDesc('sort_order')
->orderByDesc('updated_at')
->limit(6)
->get();
$stats = [
'packages' => Package::query()->where('status', 'published')->count(),
'plugins' => Package::query()->where('status', 'published')->where('type', 'plugin')->count(),
'themes' => Package::query()->where('status', 'published')->where('type', 'theme')->count(),
'categories' => Category::count(),
];
return view('storefront.home', compact('featuredPlugins', 'featuredThemes', 'stats'));
}
public function packages(Request $request, string $type): View
{
abort_unless(in_array($type, ['plugin', 'theme'], true), 404);
$query = Package::query()
->with(['categories', 'latestStableVersion'])
->where('status', 'published')
->where('type', $type);
if ($keyword = trim((string) $request->query('keyword', ''))) {
$query->where(function ($q) use ($keyword) {
$q->where('name', 'like', '%' . $keyword . '%')
->orWhere('slug', 'like', '%' . $keyword . '%')
->orWhere('summary', 'like', '%' . $keyword . '%');
});
}
if ($category = (string) $request->query('category', '')) {
$query->whereHas('categories', fn ($q) => $q->where('slug', $category));
}
$sort = (string) $request->query('sort', 'latest');
if ($sort === 'name') {
$query->orderBy('name');
} else {
$query->orderByDesc('is_featured')->orderByDesc('sort_order')->orderByDesc('updated_at');
}
$packages = $query->paginate(12)->withQueryString();
$categories = Category::query()->where('type', $type)->orderByDesc('sort_order')->get();
return view('storefront.packages', [
'type' => $type,
'packages' => $packages,
'categories' => $categories,
'filters' => [
'keyword' => (string) $request->query('keyword', ''),
'category' => (string) $request->query('category', ''),
'sort' => $sort,
],
]);
}
public function show(string $type, string $slug): View
{
abort_unless(in_array($type, ['plugin', 'theme'], true), 404);
$package = Package::query()
->with([
'categories',
'screenshots',
'versions' => fn ($q) => $q->orderByDesc('published_at')->orderByDesc('id'),
])
->where('status', 'published')
->where('type', $type)
->where('slug', $slug)
->firstOrFail();
$detail = RepoFormatter::packageDetail($package, false);
$related = Package::query()
->with(['categories', 'latestStableVersion'])
->where('status', 'published')
->where('type', $type)
->where('id', '!=', $package->id)
->orderByDesc('is_featured')
->orderByDesc('updated_at')
->limit(4)
->get();
return view('storefront.show', [
'package' => $package,
'detail' => $detail,
'related' => $related,
]);
}
}