ldc-le-gaulois-pro/app/Http/Controllers/Fo/SearchController.php

184 lines
6.2 KiB
PHP
Raw Normal View History

2023-01-10 16:13:26 +01:00
<?php
namespace App\Http\Controllers\Fo;
use App\Enums\Advices\AdviceTypeEnum;
2023-01-12 13:02:51 +01:00
use App\Enums\Products\ProductSpeciesEnum;
2023-01-10 16:13:26 +01:00
use App\Http\Controllers\Controller;
use App\Models\Advice;
use App\Models\News;
2023-01-10 16:09:25 +00:00
use App\Models\Product;
2023-01-12 13:02:51 +01:00
use Illuminate\Database\Eloquent\Builder;
2023-01-10 16:09:25 +00:00
use Illuminate\Http\Request;
2023-01-12 13:02:51 +01:00
use Illuminate\Support\Str;
2023-01-10 16:13:26 +01:00
class SearchController extends Controller
{
2023-01-10 16:09:25 +00:00
/**
* Search input txt in products, articles and recipes
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function search(Request $request)
{
2023-01-12 13:02:51 +01:00
$search = $request->query('search') ?? '';
2023-01-10 16:09:25 +00:00
$model = $request->query('model');
$products = [];
$articles = [];
$recipes = [];
2023-01-12 13:02:51 +01:00
if (is_array($search)) {
2023-01-10 16:09:25 +00:00
abort(400);
}
if (is_string($model)) {
2023-01-12 13:02:51 +01:00
return $this->callAction('searchModel', [$model, $search]);
2023-01-10 16:09:25 +00:00
}
2023-01-12 13:02:51 +01:00
$productsQuery = $this->getProductQuery($search);
$productsTotal = $productsQuery->count();
$products = $productsQuery->with('subrange.range')->limit(3)->get();
$newsQuery = $this->getNewsQuery($search);
$newsTotal = $newsQuery->count();
$news = $newsQuery->with('categories')->limit(3)->get();
$recipesQuery = $this->getRecipeQuery($search);
$recipesTotal = $recipesQuery->count();
$recipes = $recipesQuery->with('tags')->limit(3)->get();
return view('fo.pages.search-summary-list', compact(
'products',
'productsTotal',
'news',
'newsTotal',
'recipes',
'recipesTotal',
'search',
));
2023-01-10 16:09:25 +00:00
}
2023-01-10 16:13:26 +01:00
/**
2023-01-11 17:21:40 +01:00
* View search for a specific model
2023-01-10 16:13:26 +01:00
*
2023-01-12 13:02:51 +01:00
* Pour des raisons de performance et de pertinence la recherche ne pourra être lancé que si le champ de recherche
* contient au moins 3 caractères autre que des espaces. Les résultats seront présentés par ordre de pertinence.
*
* @param string $modelName
* @param string $search
2023-01-10 16:09:25 +00:00
* @return \Illuminate\Http\Response
2023-01-10 16:13:26 +01:00
*/
2023-01-12 13:02:51 +01:00
public function searchModel(string $modelName, string $search)
2023-01-10 16:13:26 +01:00
{
2023-01-12 13:02:51 +01:00
$models = [];
$modelList = [
'product' => Str::singular(trans('models.classes.products')),
'news' => Str::singular(trans('models.classes.news')),
'recipe' => Str::singular(trans('models.classes.recipes'))
];
$search = htmlspecialchars($search);
2023-01-10 16:09:25 +00:00
2023-01-12 13:02:51 +01:00
switch ($modelName) {
2023-01-10 16:09:25 +00:00
case 'product':
2023-01-12 13:02:51 +01:00
$models = $this->getProductQuery($search)->with('subrange.range')->paginate(6);
2023-01-10 16:09:25 +00:00
break;
case 'news':
2023-01-12 13:02:51 +01:00
$models = $this->getNewsQuery($search)->with('categories')->paginate(6);
2023-01-10 16:09:25 +00:00
break;
case 'recipe':
2023-01-12 13:02:51 +01:00
$models = $this->getRecipeQuery($search)->with('tags')->paginate(6);
2023-01-10 16:09:25 +00:00
break;
default:
abort(400);
} //end switch
2023-01-10 16:13:26 +01:00
2023-01-12 13:02:51 +01:00
return view('fo.pages.search', compact('modelName', 'models', 'search', 'modelList'));
}
/**
* Get product query search
*
* Par leur désignation
* Par espèce
* Par gamme
* Par ingrédients
* Par atout produits
* Par type produit
*
* @param string $search
* @return \Illuminate\Database\Eloquent\Builder
*/
private function getProductQuery(string $search): Builder
{
return Product::query()->where('status', true)->where(function ($query) use ($search) {
// Par leur désignation.
$query->where('label', 'like', "%{$search}%");
// Par ingrédients.
$query->orWhere('ingredients', 'like', "%{$search}%");
// Par atout produits.
$query->orWhere('advantage', 'like', "%{$search}%");
// Par gamme.
$query->orWhereHas('subrange', function (Builder $q) use ($search) {
$q->whereHas('range', function (Builder $q) use ($search) {
$q->where('label', 'like', "%{$search}%");
});
});
// Par type de produit (sous gamme).
$query->orWhereHas('subrange', function (Builder $q) use ($search) {
$q->where('label', 'like', "%{$search}%");
});
// Par espèce.
if (\strlen($search)) {
/** @var \App\Enums\Products\ProductSpeciesEnum|null $specie */
$specie = \collect(ProductSpeciesEnum::toArray())
->filter(function (ProductSpeciesEnum $specie) use ($search) {
return \strpos(Str::lower($specie->label), Str::lower($search)) !== false;
})->first();
if ($specie) {
$query->orWhere('species', $specie);
}
}
});
}
/**
* Get news query search
*
* Par mots dans le titre
* Par mots dans le texte
*
* @param string $search
* @return \Illuminate\Database\Eloquent\Builder
*/
private function getNewsQuery(string $search): Builder
{
return News::query()->where('status', true)->where(function ($query) use ($search) {
$query->where('title', 'like', "%{$search}%");
$query->orWhere('subtitle', 'like', "%{$search}%");
$query->orWhere('content', 'like', "%{$search}%");
$query->orWhere('content2', 'like', "%{$search}%");
});
}
/**
* Get recipe query search
*
* Par mots dans le titre
* Par mots dans le texte de la recette
*
* @param string $search
* @return \Illuminate\Database\Eloquent\Builder
*/
private function getRecipeQuery(string $search): Builder
{
return Advice::query()->where('status', true)
->where('type', '=', AdviceTypeEnum::recipes()->value)
->where(function ($query) use ($search) {
$query->where('title', 'like', "%{$search}%");
$query->orWhere('preparation', 'like', "%{$search}%");
$query->orWhere('advice', 'like', "%{$search}%");
$query->orWhere('ingredients', 'like', "%{$search}%");
});
2023-01-10 16:13:26 +01:00
}
}