131 lines
3.9 KiB
PHP
131 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\Range;
|
|
use App\Models\Tag;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
/**
|
|
* Ranges page
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
*/
|
|
public function ranges(Request $request)
|
|
{
|
|
// @phpcs:enable
|
|
// $ranges shared in AppServiceProvider.
|
|
return view('ranges');
|
|
}
|
|
|
|
/**
|
|
* One Range page
|
|
*
|
|
* @param Request $request
|
|
* @param string $range_slug
|
|
* @return \Illuminate\Contracts\View\View
|
|
*
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function range(Request $request, string $range_slug)
|
|
{
|
|
// @phpcs:enable
|
|
$range = Range::where('slug', $range_slug)->first();
|
|
if (!$range || !$range->status) {
|
|
abort(404);
|
|
}
|
|
$products = Product::where('range_id', $range->id)->where('status', 1)->paginate(9);
|
|
// Find promo within range.
|
|
$offres = Product::where('range_id', $range->id)
|
|
->where('status', 1)
|
|
->has('discounts')
|
|
->with('discounts')->get();
|
|
return view('range', compact('range', 'products', 'offres'));
|
|
}
|
|
|
|
/**
|
|
* One product page
|
|
*
|
|
* @param Request $request
|
|
* @param string $product_slug
|
|
* @return \Illuminate\Contracts\View\View
|
|
*
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function product(Request $request, string $product_slug)
|
|
{
|
|
// @phpcs:enable
|
|
$product = Product::where('slug', $product_slug)->first();
|
|
if (!$product || !$product->status) {
|
|
abort(404);
|
|
}
|
|
$product->additionnalProductInfo = $product->additionnalProductInfo($product->product_type);
|
|
if ($product->additionnalProductInfo === null && !empty($product_type)) {
|
|
// N'existe pas on renseigne un vide.
|
|
$product_type = $product->product_type;
|
|
$modelClass = 'App\Models\\' . ucfirst($product_type);
|
|
$product->$product_type = new $modelClass();
|
|
}
|
|
|
|
$associated = $product->products;
|
|
|
|
return view('product', compact('product', 'associated'));
|
|
}
|
|
|
|
/**
|
|
* tous les produits
|
|
* recherche par nom de produit
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*
|
|
*/
|
|
public function allProducts(Request $request)
|
|
{
|
|
|
|
$filter = $request->query('filter');
|
|
|
|
if (!empty($filter)) {
|
|
$products = Product::where('status', 1)
|
|
->where('name', 'like', '%' . $filter . '%')
|
|
->paginate(12);
|
|
} else {
|
|
$products = Product::where('status', 1)->paginate(12);
|
|
}
|
|
|
|
$search_title = 'Recherche : ' . $filter;
|
|
|
|
return view('search', compact('products', 'search_title', 'filter'));
|
|
}
|
|
|
|
/**
|
|
* tous les produits
|
|
* filtrage par tag
|
|
*
|
|
* @param Request $request
|
|
* @param string $tag_slug
|
|
* @return \Illuminate\Contracts\View\View
|
|
*
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function tagFiltered(Request $request, string $tag_slug)
|
|
{
|
|
// @phpcs:enable
|
|
$products = Product::where('status', 1)
|
|
->whereHas('tags', function ($q) use ($tag_slug) {
|
|
$q->where('slug', '=', $tag_slug);
|
|
})
|
|
->paginate(12);
|
|
|
|
$tag = Tag::where('slug', $tag_slug)->firstOrFail();
|
|
$search_title = $tag->label;
|
|
|
|
return view('search', compact('products', 'search_title', 'tag'));
|
|
}
|
|
}
|