101 lines
3.4 KiB
PHP
101 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Category;
|
|
use App\Models\ContentType;
|
|
use App\Models\Artwork;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
class CategoryPageController extends Controller
|
|
{
|
|
public function show(Request $request, string $contentTypeSlug, string $categoryPath = null)
|
|
{
|
|
$contentType = ContentType::where('slug', strtolower($contentTypeSlug))->first();
|
|
if (! $contentType) {
|
|
abort(404);
|
|
}
|
|
|
|
|
|
if ($categoryPath === null || $categoryPath === '') {
|
|
// No category path: show content-type landing page (e.g., /wallpapers)
|
|
$rootCategories = $contentType->rootCategories()->orderBy('sort_order')->orderBy('name')->get();
|
|
$page_title = $contentType->name;
|
|
$page_meta_description = $contentType->description ?? ($contentType->name . ' artworks on Skinbase');
|
|
|
|
return view('legacy.content-type', compact(
|
|
'contentType',
|
|
'rootCategories',
|
|
'page_title',
|
|
'page_meta_description'
|
|
));
|
|
}
|
|
|
|
$segments = array_filter(explode('/', $categoryPath));
|
|
if (empty($segments)) {
|
|
return redirect('/browse-categories');
|
|
}
|
|
|
|
// Traverse categories by slug path within the content type
|
|
$current = Category::where('content_type_id', $contentType->id)
|
|
->whereNull('parent_id')
|
|
->where('slug', strtolower(array_shift($segments)))
|
|
->first();
|
|
|
|
|
|
if (! $current) {
|
|
abort(404);
|
|
}
|
|
|
|
foreach ($segments as $slug) {
|
|
$current = $current->children()->where('slug', strtolower($slug))->first();
|
|
if (! $current) {
|
|
abort(404);
|
|
}
|
|
}
|
|
|
|
$category = $current;
|
|
$subcategories = $category->children()->orderBy('sort_order')->orderBy('name')->get();
|
|
$rootCategories = $contentType->rootCategories()->orderBy('sort_order')->orderBy('name')->get();
|
|
|
|
// Collect category ids for the category + all descendants recursively
|
|
$collected = [];
|
|
$gather = function (Category $cat) use (&$gather, &$collected) {
|
|
$collected[] = $cat->id;
|
|
foreach ($cat->children as $child) {
|
|
$gather($child);
|
|
}
|
|
};
|
|
// Ensure children relation is loaded to avoid N+1 recursion
|
|
$category->load('children');
|
|
$gather($category);
|
|
|
|
// Load artworks that are attached to any of these categories
|
|
$query = Artwork::whereHas('categories', function ($q) use ($collected) {
|
|
$q->whereIn('categories.id', $collected);
|
|
})->published()->public();
|
|
|
|
// Paginate results
|
|
$perPage = 40;
|
|
$artworks = $query->orderBy('published_at', 'desc')
|
|
->paginate($perPage)
|
|
->withQueryString();
|
|
|
|
$page_title = $category->name;
|
|
$page_meta_description = $category->description ?? ($contentType->name . ' artworks on Skinbase');
|
|
$page_meta_keywords = strtolower($contentType->slug) . ', skinbase, artworks, wallpapers, skins, photography';
|
|
|
|
return view('legacy.category-slug', compact(
|
|
'contentType',
|
|
'category',
|
|
'subcategories',
|
|
'rootCategories',
|
|
'artworks',
|
|
'page_title',
|
|
'page_meta_description',
|
|
'page_meta_keywords'
|
|
));
|
|
}
|
|
}
|