Upload beautify
This commit is contained in:
@@ -5,12 +5,13 @@ 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)
|
||||
public function show(Request $request, string $contentTypeSlug, ?string $categoryPath = null)
|
||||
{
|
||||
$contentType = ContentType::where('slug', strtolower($contentTypeSlug))->first();
|
||||
if (! $contentType) {
|
||||
@@ -24,38 +25,51 @@ class CategoryPageController extends Controller
|
||||
$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));
|
||||
if (empty($segments)) {
|
||||
$slugs = array_values(array_map('strtolower', $segments));
|
||||
if (empty($slugs)) {
|
||||
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 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);
|
||||
}
|
||||
|
||||
|
||||
if (! $current) {
|
||||
// Resolve category by path using the helper that validates parent chain and content type
|
||||
$category = Category::findByPath($contentType->slug, $slugs);
|
||||
if (! $category) {
|
||||
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();
|
||||
|
||||
@@ -71,21 +85,23 @@ class CategoryPageController extends Controller
|
||||
$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
|
||||
// Load artworks via ArtworkService to support arbitrary-depth category paths
|
||||
$perPage = 40;
|
||||
$artworks = $query->orderBy('published_at', 'desc')
|
||||
->paginate($perPage)
|
||||
->withQueryString();
|
||||
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',
|
||||
|
||||
Reference in New Issue
Block a user