96 lines
3.1 KiB
PHP
96 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
|
|
class SearchController extends Controller
|
|
{
|
|
/**
|
|
* Search input txt in products, news and recipes
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function search(Request $request): \Illuminate\Contracts\View\View
|
|
{
|
|
$search = htmlspecialchars($request->query('search') ?? '');
|
|
$model = $request->query('model');
|
|
$products = [];
|
|
$productsTotal = 0;
|
|
$news = [];
|
|
$newsTotal = 0;
|
|
$recipes = [];
|
|
$recipesTotal = 0;
|
|
|
|
if (is_array($search)) {
|
|
abort(400);
|
|
}
|
|
if (is_string($model)) {
|
|
return $this->callAction('searchModel', [$model, $search]);
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
return view('fo.pages.search-summary-list', compact(
|
|
'products',
|
|
'productsTotal',
|
|
'news',
|
|
'newsTotal',
|
|
'recipes',
|
|
'recipesTotal',
|
|
'search',
|
|
));
|
|
}
|
|
|
|
/**
|
|
* View search for a specific model
|
|
*
|
|
* 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
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function searchModel(string $modelName, string $search): \Illuminate\Contracts\View\View
|
|
{
|
|
$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) {
|
|
case 'product':
|
|
$models = $this->getProductQuery($search)->with('subrange.range')->paginate(6);
|
|
break;
|
|
|
|
case 'news':
|
|
$models = $this->getNewsQuery($search)->with('categories')->paginate(6);
|
|
break;
|
|
|
|
case 'recipe':
|
|
$models = $this->getRecipeQuery($search)->with('tags')->paginate(6);
|
|
break;
|
|
|
|
default:
|
|
abort(400);
|
|
} //end switch
|
|
|
|
return view('fo.pages.search', compact('modelName', 'models', 'search', 'modelList'));
|
|
}
|
|
}
|