ldc-boutique-cnc/app/Http/Controllers/ProductController.php

132 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\Range;
use App\Models\Tag;
use Illuminate\Http\Request;
2022-10-03 17:13:44 +02:00
class ProductController extends Controller
{
2021-04-14 09:34:23 +02:00
/**
2022-09-03 16:56:16 +02:00
* Ranges page
*
* @param Request $request
* @return \Illuminate\Contracts\View\View
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
2021-04-14 09:34:23 +02:00
*/
public function ranges(Request $request)
{
2022-09-03 16:56:16 +02:00
// @phpcs:enable
// $ranges shared in AppServiceProvider.
return view('ranges');
}
2021-04-14 09:34:23 +02:00
/**
2022-09-03 16:56:16 +02:00
* One Range page
*
* @param Request $request
* @param string $range_slug
* @return \Illuminate\Contracts\View\View
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
2021-04-14 09:34:23 +02:00
*/
2022-09-03 16:56:16 +02:00
public function range(Request $request, string $range_slug)
{
2022-09-03 16:56:16 +02:00
// @phpcs:enable
$range = Range::where('slug', $range_slug)->first();
2021-05-05 09:08:57 +02:00
if (!$range || !$range->status) {
2021-04-20 10:15:22 +02:00
abort(404);
}
2021-05-05 09:08:57 +02:00
$products = Product::where('range_id', $range->id)->where('status', 1)->paginate(9);
2022-09-03 16:56:16 +02:00
// Find promo within range.
$offres = Product::where('range_id', $range->id)
->where('status', 1)
2022-10-03 17:13:44 +02:00
->has('discounts')
->with('discounts')->get();
2021-05-05 09:08:57 +02:00
return view('range', compact('range', 'products', 'offres'));
2020-11-09 11:03:13 +01:00
}
2021-04-14 09:34:23 +02:00
/**
2022-09-03 16:56:16 +02:00
* One product page
*
* @param Request $request
* @param string $product_slug
* @return \Illuminate\Contracts\View\View
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
2021-04-14 09:34:23 +02:00
*/
2022-09-03 16:56:16 +02:00
public function product(Request $request, string $product_slug)
2021-05-05 09:08:57 +02:00
{
2022-09-03 16:56:16 +02:00
// @phpcs:enable
$product = Product::where('slug', $product_slug)->first();
2021-05-05 09:08:57 +02:00
if (!$product || !$product->status) {
abort(404);
}
2021-03-23 17:47:05 +01:00
$product->additionnalProductInfo = $product->additionnalProductInfo($product->product_type);
if ($product->additionnalProductInfo === null && !empty($product_type)) {
2022-09-03 16:56:16 +02:00
// N'existe pas on renseigne un vide.
2021-05-05 09:08:57 +02:00
$product_type = $product->product_type;
$modelClass = 'App\Models\\' . ucfirst($product_type);
2021-03-24 17:58:27 +01:00
$product->$product_type = new $modelClass();
}
$associated = $product->products;
2021-05-05 09:08:57 +02:00
return view('product', compact('product', 'associated'));
}
2021-04-14 09:34:23 +02:00
/**
* tous les produits
* recherche par nom de produit
2022-09-03 16:56:16 +02:00
*
* @param Request $request
* @return \Illuminate\Contracts\View\View
*
2021-04-14 09:34:23 +02:00
*/
2021-04-13 10:18:50 +02:00
public function allProducts(Request $request)
{
$filter = $request->query('filter');
if (!empty($filter)) {
$products = Product::where('status', 1)
2021-05-05 09:08:57 +02:00
->where('name', 'like', '%' . $filter . '%')
->paginate(12);
} else {
$products = Product::where('status', 1)->paginate(12);
}
2021-05-05 09:08:57 +02:00
$search_title = 'Recherche : ' . $filter;
2021-04-13 10:18:50 +02:00
return view('search', compact('products', 'search_title', 'filter'));
}
2021-04-14 09:34:23 +02:00
/**
2022-09-03 16:56:16 +02:00
* tous les produits
* filtrage par tag
*
* @param Request $request
* @param string $tag_slug
* @return \Illuminate\Contracts\View\View
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
2021-04-14 09:34:23 +02:00
*/
2022-09-03 16:56:16 +02:00
public function tagFiltered(Request $request, string $tag_slug)
2021-04-13 10:18:50 +02:00
{
2022-09-03 16:56:16 +02:00
// @phpcs:enable
2021-04-13 10:18:50 +02:00
$products = Product::where('status', 1)
2021-05-05 09:08:57 +02:00
->whereHas('tags', function ($q) use ($tag_slug) {
2021-04-13 10:18:50 +02:00
$q->where('slug', '=', $tag_slug);
})
->paginate(12);
$tag = Tag::where('slug', $tag_slug)->firstOrFail();
2021-04-13 10:18:50 +02:00
$search_title = $tag->label;
return view('search', compact('products', 'search_title', 'tag'));
}
}