117 lines
4.7 KiB
PHP
117 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Category;
|
|
use App\Models\ContentType;
|
|
use App\Models\Artwork;
|
|
use App\Services\ArtworkService;
|
|
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');
|
|
|
|
// Load artworks for this content type (show gallery on the root page)
|
|
$perPage = 40;
|
|
$artworks = Artwork::whereHas('categories', function ($q) use ($contentType) {
|
|
$q->where('categories.content_type_id', $contentType->id);
|
|
})
|
|
->published()->public()
|
|
->with([
|
|
'user:id,name',
|
|
'categories' => function ($q) {
|
|
$q->select('categories.id', 'categories.content_type_id', 'categories.parent_id', 'categories.name', 'categories.slug', 'categories.sort_order')
|
|
->with(['parent:id,parent_id,content_type_id,name,slug', 'contentType:id,slug,name']);
|
|
},
|
|
])
|
|
->orderBy('published_at', 'desc')
|
|
->paginate($perPage)
|
|
->withQueryString();
|
|
|
|
return view('legacy.content-type', compact(
|
|
'contentType',
|
|
'rootCategories',
|
|
'artworks',
|
|
'page_title',
|
|
'page_meta_description'
|
|
));
|
|
}
|
|
|
|
$segments = array_filter(explode('/', $categoryPath));
|
|
$slugs = array_values(array_map('strtolower', $segments));
|
|
if (empty($slugs)) {
|
|
return redirect('/browse-categories');
|
|
}
|
|
|
|
// If the first slug exists but under a different content type, redirect to its canonical URL
|
|
$firstSlug = $slugs[0];
|
|
$globalRoot = Category::whereNull('parent_id')->where('slug', $firstSlug)->first();
|
|
if ($globalRoot && $globalRoot->contentType && $globalRoot->contentType->slug !== strtolower($contentType->slug)) {
|
|
$redirectPath = '/' . $globalRoot->contentType->slug . '/' . implode('/', $slugs);
|
|
return redirect($redirectPath, 301);
|
|
}
|
|
|
|
// Resolve category by path using the helper that validates parent chain and content type
|
|
$category = Category::findByPath($contentType->slug, $slugs);
|
|
if (! $category) {
|
|
abort(404);
|
|
}
|
|
$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 via ArtworkService to support arbitrary-depth category paths
|
|
$perPage = 40;
|
|
try {
|
|
$service = app(ArtworkService::class);
|
|
// service expects an array with contentType slug first, then category slugs
|
|
$pathSlugs = array_merge([strtolower($contentTypeSlug)], $slugs);
|
|
$artworks = $service->getArtworksByCategoryPath($pathSlugs, $perPage);
|
|
} catch (\Throwable $e) {
|
|
abort(404);
|
|
}
|
|
|
|
$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';
|
|
|
|
// resolved category and breadcrumbs are used by the view
|
|
|
|
return view('legacy.category-slug', compact(
|
|
'contentType',
|
|
'category',
|
|
'subcategories',
|
|
'rootCategories',
|
|
'artworks',
|
|
'page_title',
|
|
'page_meta_description',
|
|
'page_meta_keywords'
|
|
));
|
|
}
|
|
}
|