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

97 lines
3 KiB
PHP
Raw Normal View History

2023-01-10 16:13:26 +01:00
<?php
namespace App\Http\Controllers\Fo;
use App\Http\Controllers\Controller;
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, news and recipes
2023-01-10 16:09:25 +00:00
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function search(Request $request)
{
$search = htmlspecialchars($request->query('search') ?? '');
$model = $request->query('model');
$products = [];
$productsTotal = 0;
$news = [];
$newsTotal = 0;
$recipes = [];
$recipesTotal = 0;
2023-01-10 16:09:25 +00:00
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
}
if (!empty($search)) {
$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();
}
2023-01-12 13:02:51 +01:00
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'))
];
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'));
}
2023-01-10 16:13:26 +01:00
}