2026-02-06 14:56:56 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\Category;
|
|
|
|
|
use App\Models\Product;
|
2026-02-21 16:12:57 +02:00
|
|
|
use App\Models\Tag;
|
2026-02-06 14:56:56 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2026-02-21 17:48:08 +02:00
|
|
|
use Laravel\Pennant\Feature;
|
2026-02-06 14:56:56 +02:00
|
|
|
|
|
|
|
|
class ProductController extends Controller
|
|
|
|
|
{
|
2026-02-14 16:11:09 +02:00
|
|
|
/**
|
|
|
|
|
* Display the homepage with featured products.
|
|
|
|
|
*/
|
|
|
|
|
public function homepage()
|
|
|
|
|
{
|
2026-02-21 17:48:08 +02:00
|
|
|
// Check if waitlist feature is active
|
|
|
|
|
if (Feature::for(null)->active('WaitlistPage')) {
|
|
|
|
|
return view('waitlist.index');
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 16:11:09 +02:00
|
|
|
// Get trending products (most upvoted in the last 7 days)
|
|
|
|
|
$trendingProducts = Product::query()
|
|
|
|
|
->with(['user', 'category'])
|
|
|
|
|
->approved()
|
|
|
|
|
->where('created_at', '>=', now()->subDays(7))
|
|
|
|
|
->withCount('upvotes')
|
|
|
|
|
->orderBy('upvotes_count', 'desc')
|
|
|
|
|
->limit(6)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
// Get recently added products
|
|
|
|
|
$recentProducts = Product::query()
|
|
|
|
|
->with(['user', 'category'])
|
|
|
|
|
->approved()
|
|
|
|
|
->latest()
|
|
|
|
|
->limit(12)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
// Get popular categories with product counts
|
|
|
|
|
$categories = Category::query()
|
|
|
|
|
->withCount(['products' => function($query) {
|
|
|
|
|
$query->approved();
|
|
|
|
|
}])
|
|
|
|
|
->having('products_count', '>', 0)
|
|
|
|
|
->orderBy('products_count', 'desc')
|
|
|
|
|
->limit(8)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
return view('welcome', compact('trendingProducts', 'recentProducts', 'categories'));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 14:56:56 +02:00
|
|
|
/**
|
|
|
|
|
* Display a listing of the products.
|
|
|
|
|
*/
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
2026-02-07 14:36:33 +02:00
|
|
|
$validated = $request->validate([
|
|
|
|
|
'search' => 'nullable|string|max:255',
|
|
|
|
|
'category' => 'nullable|integer|exists:categories,id',
|
|
|
|
|
'status' => 'nullable|string|in:pending,approved,rejected',
|
|
|
|
|
'date_range' => 'nullable|string|in:today,week,month,year',
|
|
|
|
|
'sort' => 'nullable|string|in:newest,oldest,popular',
|
|
|
|
|
]);
|
2026-02-06 14:56:56 +02:00
|
|
|
|
2026-02-07 14:36:33 +02:00
|
|
|
// Build the query with all filters and search
|
|
|
|
|
$query = Product::query()
|
|
|
|
|
->with(['user', 'category'])
|
|
|
|
|
->approved() // Only show approved products on the public index
|
|
|
|
|
->search($request->search)
|
|
|
|
|
->byCategory($request->category)
|
|
|
|
|
->dateRange($request->date_range)
|
|
|
|
|
->sortBy($request->sort);
|
2026-02-06 14:56:56 +02:00
|
|
|
|
2026-02-07 14:36:33 +02:00
|
|
|
// Paginate results with query parameters preserved
|
|
|
|
|
$products = $query->paginate(12)->appends($request->query());
|
|
|
|
|
|
|
|
|
|
// Get all categories for the filter dropdown
|
2026-02-06 14:56:56 +02:00
|
|
|
$categories = Category::all();
|
|
|
|
|
|
|
|
|
|
return view('products.index', compact('products', 'categories'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new product.
|
|
|
|
|
*/
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
|
|
|
|
$categories = Category::all();
|
2026-02-21 16:12:57 +02:00
|
|
|
$tags = Tag::all();
|
|
|
|
|
return view('products.create', compact('categories', 'tags'));
|
2026-02-06 14:56:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created product in storage.
|
|
|
|
|
*/
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
|
|
|
|
Gate::authorize('create-product');
|
|
|
|
|
|
|
|
|
|
$validated = $request->validate([
|
|
|
|
|
'title' => 'required|string|max:255',
|
|
|
|
|
'description' => 'required|string',
|
|
|
|
|
'website_url' => 'required|url',
|
|
|
|
|
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
|
|
|
|
|
'category_id' => 'required|exists:categories,id',
|
2026-02-21 16:12:57 +02:00
|
|
|
'tags' => 'nullable|array',
|
|
|
|
|
'tags.*' => 'exists:tags,id',
|
2026-10-17 15:56:58 +03:00
|
|
|
] + $this->detailRules());
|
|
|
|
|
|
|
|
|
|
foreach (array_keys(Product::BADGES) as $badge) {
|
|
|
|
|
$validated[$badge] = $request->boolean($badge);
|
|
|
|
|
}
|
2026-02-06 14:56:56 +02:00
|
|
|
|
|
|
|
|
$validated['user_id'] = Auth::id();
|
|
|
|
|
$validated['status'] = 'pending';
|
|
|
|
|
|
|
|
|
|
// Handle thumbnail upload
|
|
|
|
|
if ($request->hasFile('thumbnail')) {
|
|
|
|
|
$path = $request->file('thumbnail')->store('thumbnails', 'public');
|
|
|
|
|
$validated['thumbnail'] = $path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$product = Product::create($validated);
|
|
|
|
|
|
2026-02-21 16:12:57 +02:00
|
|
|
// Attach tags if provided
|
|
|
|
|
if ($request->has('tags')) {
|
|
|
|
|
$product->tags()->attach($request->tags);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 15:56:58 +03:00
|
|
|
$this->syncDetails($request, $product);
|
|
|
|
|
|
2026-02-06 14:56:56 +02:00
|
|
|
return redirect()->route('products.show', $product)
|
|
|
|
|
->with('success', 'Product submitted successfully! It will be reviewed by our moderators.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the specified product.
|
|
|
|
|
*/
|
|
|
|
|
public function show(Product $product)
|
|
|
|
|
{
|
|
|
|
|
// Only show approved products to non-owners
|
|
|
|
|
if (!$product->isApproved() && $product->user_id !== Auth::id() && !Auth::user()?->hasAnyRole(['admin', 'moderator'])) {
|
|
|
|
|
abort(404);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 15:56:58 +03:00
|
|
|
$product->load(['user', 'category', 'tags', 'comments.user', 'upvotes', 'bookmarks']);
|
2026-02-06 14:56:56 +02:00
|
|
|
|
|
|
|
|
return view('products.show', compact('product'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for editing the specified product.
|
|
|
|
|
*/
|
|
|
|
|
public function edit(Product $product)
|
|
|
|
|
{
|
|
|
|
|
Gate::authorize('edit-product', $product);
|
|
|
|
|
|
|
|
|
|
$categories = Category::all();
|
2026-02-21 16:12:57 +02:00
|
|
|
$tags = Tag::all();
|
|
|
|
|
return view('products.edit', compact('product', 'categories', 'tags'));
|
2026-02-06 14:56:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified product in storage.
|
|
|
|
|
*/
|
|
|
|
|
public function update(Request $request, Product $product)
|
|
|
|
|
{
|
|
|
|
|
Gate::authorize('edit-product', $product);
|
|
|
|
|
|
|
|
|
|
$validated = $request->validate([
|
|
|
|
|
'title' => 'required|string|max:255',
|
|
|
|
|
'description' => 'required|string',
|
|
|
|
|
'website_url' => 'required|url',
|
|
|
|
|
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
|
|
|
|
|
'category_id' => 'required|exists:categories,id',
|
2026-02-21 16:12:57 +02:00
|
|
|
'tags' => 'nullable|array',
|
|
|
|
|
'tags.*' => 'exists:tags,id',
|
2026-10-17 15:56:58 +03:00
|
|
|
] + $this->detailRules());
|
|
|
|
|
|
|
|
|
|
// Badges are locked for non-moderators once the product is approved
|
|
|
|
|
if ($product->status !== 'approved' || Auth::user()->hasAnyRole(['moderator', 'admin'])) {
|
|
|
|
|
foreach (array_keys(Product::BADGES) as $badge) {
|
|
|
|
|
$validated[$badge] = $request->boolean($badge);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$validated = array_diff_key($validated, Product::BADGES);
|
|
|
|
|
}
|
2026-02-06 14:56:56 +02:00
|
|
|
|
|
|
|
|
// Handle thumbnail upload
|
|
|
|
|
if ($request->hasFile('thumbnail')) {
|
|
|
|
|
// Delete old thumbnail if exists
|
|
|
|
|
if ($product->thumbnail) {
|
|
|
|
|
Storage::disk('public')->delete($product->thumbnail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$path = $request->file('thumbnail')->store('thumbnails', 'public');
|
|
|
|
|
$validated['thumbnail'] = $path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$product->update($validated);
|
|
|
|
|
|
2026-10-17 15:56:58 +03:00
|
|
|
$this->syncDetails($request, $product);
|
|
|
|
|
|
2026-02-21 16:12:57 +02:00
|
|
|
// Sync tags (will remove old tags and add new ones)
|
|
|
|
|
$product->tags()->sync($request->tags ?? []);
|
|
|
|
|
|
2026-02-06 14:56:56 +02:00
|
|
|
return redirect()->route('products.show', $product)
|
|
|
|
|
->with('success', 'Product updated successfully!');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 15:56:58 +03:00
|
|
|
/**
|
|
|
|
|
* Validation rules for the product detail fields.
|
|
|
|
|
*/
|
|
|
|
|
protected function detailRules(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'free_tier' => 'boolean',
|
|
|
|
|
'open_source' => 'boolean',
|
|
|
|
|
'eu_hosted' => 'boolean',
|
|
|
|
|
'eu_made' => 'boolean',
|
2026-10-17 16:00:23 +03:00
|
|
|
'screenshots' => 'nullable|array',
|
|
|
|
|
'screenshots.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
|
|
|
|
|
'remove_screenshots' => 'nullable|array',
|
|
|
|
|
'remove_screenshots.*' => 'integer',
|
2026-10-17 15:56:58 +03:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sync related product detail data (badges are stored on the product itself).
|
|
|
|
|
*/
|
|
|
|
|
protected function syncDetails(Request $request, Product $product): void
|
|
|
|
|
{
|
2026-10-17 16:00:23 +03:00
|
|
|
// Only rows belonging to this product may be removed
|
|
|
|
|
$ids = array_filter((array) $request->input('remove_screenshots', []));
|
|
|
|
|
if ($ids) {
|
|
|
|
|
$product->screenshots()->whereIn('id', $ids)->get()->each(function ($shot) {
|
|
|
|
|
Storage::disk('public')->delete($shot->path);
|
|
|
|
|
$shot->delete();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($request->hasFile('screenshots')) {
|
|
|
|
|
$position = ($product->screenshots()->max('position') ?? -1) + 1;
|
|
|
|
|
foreach ($request->file('screenshots') as $file) {
|
|
|
|
|
$product->screenshots()->create([
|
|
|
|
|
'path' => $file->store('screenshots', 'public'),
|
|
|
|
|
'position' => $position++,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-17 15:56:58 +03:00
|
|
|
}
|
|
|
|
|
|
2026-02-06 14:56:56 +02:00
|
|
|
/**
|
|
|
|
|
* Remove the specified product from storage.
|
|
|
|
|
*/
|
|
|
|
|
public function destroy(Product $product)
|
|
|
|
|
{
|
|
|
|
|
Gate::authorize('delete-product', $product);
|
|
|
|
|
|
|
|
|
|
// Delete thumbnail if exists
|
|
|
|
|
if ($product->thumbnail) {
|
|
|
|
|
Storage::disk('public')->delete($product->thumbnail);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 16:00:23 +03:00
|
|
|
foreach ($product->screenshots as $shot) {
|
|
|
|
|
Storage::disk('public')->delete($shot->path);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 14:56:56 +02:00
|
|
|
$product->delete();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('products.index')
|
|
|
|
|
->with('success', 'Product deleted successfully!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the product management dashboard for admin/moderator.
|
|
|
|
|
*/
|
|
|
|
|
public function manage(Request $request)
|
|
|
|
|
{
|
|
|
|
|
Gate::authorize('approve-product');
|
|
|
|
|
|
|
|
|
|
$query = Product::with(['user', 'category']);
|
|
|
|
|
|
|
|
|
|
// Filter by status
|
|
|
|
|
if ($request->has('status') && in_array($request->status, ['pending', 'approved', 'rejected'])) {
|
|
|
|
|
$query->byStatus($request->status);
|
|
|
|
|
} else {
|
|
|
|
|
$query->pending(); // Default to pending
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$products = $query->latest()->paginate(20);
|
|
|
|
|
|
|
|
|
|
$statusCounts = [
|
|
|
|
|
'pending' => Product::pending()->count(),
|
|
|
|
|
'approved' => Product::approved()->count(),
|
|
|
|
|
'rejected' => Product::rejected()->count(),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return view('products.manage', compact('products', 'statusCounts'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Approve a product.
|
|
|
|
|
*/
|
|
|
|
|
public function approve(Product $product)
|
|
|
|
|
{
|
|
|
|
|
Gate::authorize('approve-product');
|
|
|
|
|
|
|
|
|
|
$product->approve();
|
|
|
|
|
|
|
|
|
|
return back()->with('success', 'Product approved successfully!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reject a product.
|
|
|
|
|
*/
|
|
|
|
|
public function reject(Product $product)
|
|
|
|
|
{
|
|
|
|
|
Gate::authorize('reject-product');
|
|
|
|
|
|
|
|
|
|
$product->reject();
|
|
|
|
|
|
|
|
|
|
return back()->with('success', 'Product rejected.');
|
|
|
|
|
}
|
|
|
|
|
}
|