ldc-cocotte/app/Http/Controllers/ProductsController.php

168 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2022-10-05 16:41:09 +02:00
use App\Http\Requests\ProductsFilterRequest;
use App\Http\Resources\ProductResource;
use App\Models\Product;
use App\Models\Range;
use App\Models\Tag;
2022-10-05 16:41:09 +02:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ProductsController extends Controller
{
2021-04-14 09:34:23 +02:00
/**
2022-09-08 13:02: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-08 13:02:16 +02:00
// @phpcs:enable
// $ranges shared in AppServiceProvider.
return view('ranges');
}
2021-04-14 09:34:23 +02:00
/**
2022-09-08 13:02: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-08 13:02:16 +02:00
public function range(Request $request, string $range_slug)
{
2022-09-08 13:02: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-08 13:02:16 +02:00
// Find promo within range.
$offres = Product::where('range_id', $range->id)
->where('status', 1)
->has('promotions')
->with('promotions')->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-08 13:02: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-08 13:02:16 +02:00
public function product(Request $request, string $product_slug)
2021-05-05 09:08:57 +02:00
{
2022-09-08 13:02: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-08 13:02: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-08 13:02: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');
2022-10-04 16:18:01 +02:00
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;
2022-10-05 16:41:09 +02:00
$ranges = Range::all();
$tags = Tag::all();
2022-10-05 16:41:09 +02:00
return view('ranges', compact('products', 'ranges', 'tags', 'search_title', 'filter'));
2021-04-13 10:18:50 +02:00
}
2021-04-14 09:34:23 +02:00
/**
2022-09-08 13:02: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-08 13:02:16 +02:00
public function tagFiltered(Request $request, string $tag_slug)
2021-04-13 10:18:50 +02:00
{
2022-09-08 13:02:16 +02:00
// @phpcs:enable
2021-04-13 10:18:50 +02:00
$products = Product::where('status', 1)
2022-10-05 16:41:09 +02:00
->whereHas('tags', function ($q) use ($tag_slug) {
$q->where('slug', '=', $tag_slug);
})
->paginate(12);
2021-04-13 10:18:50 +02:00
$tag = Tag::where('slug', $tag_slug)->firstOrFail();
2021-04-13 10:18:50 +02:00
$search_title = $tag->label;
2022-10-04 16:18:01 +02:00
return view('ranges', compact('products', 'search_title', 'tag'));
}
2022-10-05 16:41:09 +02:00
/**
* tous les produits filtrés
*
* @param \App\Http\Requests\ProductsFilterRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function allProductsFiltered(ProductsFilterRequest $request): JsonResponse
{
/** @var Illuminate\Database\Eloquent\Builder */
$productsQuery = Product::where('status', true);
if (!empty($request->promotion)) {
$productsQuery->whereHas('promotions');
}
if (!empty($request->ranges)) {
$productsQuery->whereHas('range', function ($q) use ($request) {
$q->whereIn('id', $request->ranges);
});
}
if (!empty($request->tags)) {
$productsQuery->whereHas('tags', function ($q) use ($request) {
$q->whereIn('id', $request->tags);
});
}
if (!empty($request->brands)) {
$productsQuery->whereIn('brand', $request->brands);
}
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Product> */
$products = $productsQuery->get();
return \response()->json(ProductResource::collection($products));
}
}