Merge pull request #42 from ThomasRubini/search_action
This commit is contained in:
commit
c6984c7707
@ -55,4 +55,30 @@ final class RecipeController
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function searchAction(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null)
|
||||||
|
{
|
||||||
|
if (isset($A_getParams["query"])) {
|
||||||
|
self::searchQueryView($A_urlParams, $A_postParams, $A_getParams);
|
||||||
|
} else {
|
||||||
|
self::searchView($A_urlParams, $A_postParams, $A_getParams);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function searchView(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null)
|
||||||
|
{
|
||||||
|
View::show("recipe/search", array("QUERY" => null));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function searchQueryView(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null)
|
||||||
|
{
|
||||||
|
|
||||||
|
$O_recipeModel = new RecipeModel();
|
||||||
|
$A_results = $O_recipeModel->searchRecipesByName($A_getParams["query"]);
|
||||||
|
|
||||||
|
View::show("recipe/search", array(
|
||||||
|
"QUERY" => $A_getParams["query"],
|
||||||
|
"RESULTS" => $A_results,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -33,4 +33,39 @@ final class RecipeModel
|
|||||||
|
|
||||||
return $A_recipe;
|
return $A_recipe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function searchRecipesByName($S_query)
|
||||||
|
{
|
||||||
|
|
||||||
|
$O_model = Model::get();
|
||||||
|
$stmt = $O_model->prepare("
|
||||||
|
-- split search term at space
|
||||||
|
with recursive CTE as (
|
||||||
|
select
|
||||||
|
CAST(null as char(255)) as NAME,
|
||||||
|
CONCAT(:query, ' ') as NAMES
|
||||||
|
|
||||||
|
union all
|
||||||
|
select substring_index(NAMES, ' ', 1),
|
||||||
|
substr(NAMES, instr(NAMES, ' ')+1)
|
||||||
|
from CTE
|
||||||
|
where NAMES != ''
|
||||||
|
)
|
||||||
|
|
||||||
|
-- get a row per occurrence and sort by occurrences number
|
||||||
|
select RECIPE.ID, RECIPE.NAME
|
||||||
|
from CTE
|
||||||
|
JOIN RECIPE
|
||||||
|
WHERE CTE.NAME is not null
|
||||||
|
AND INSTR(RECIPE.NAME, CTE.NAME) > 0
|
||||||
|
GROUP BY RECIPE.ID
|
||||||
|
ORDER BY count(RECIPE.ID) DESC
|
||||||
|
LIMIT 10;
|
||||||
|
");
|
||||||
|
|
||||||
|
$stmt->bindParam("query", $S_query);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
return $stmt->fetchAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
# Check presence of the search term query parameter, to avoid getting a warning as the input placeholder value
|
# Check presence of the search term query parameter, to avoid getting a warning as the input placeholder value
|
||||||
$has_search_term = $A_view["search_term"] === null;
|
$has_query = $A_view["QUERY"] !== null;
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<?php View::show("common/category_list") ?>
|
<?php View::show("common/category_list") ?>
|
||||||
@ -8,13 +8,14 @@
|
|||||||
<main>
|
<main>
|
||||||
<!-- Inclure les catégories -->
|
<!-- Inclure les catégories -->
|
||||||
<form method="GET" action="/recipe/search">
|
<form method="GET" action="/recipe/search">
|
||||||
<label for="search_term">Saisissez les termes à rechercher</label>
|
<label for="query">Saisissez les termes à rechercher</label>
|
||||||
<input id="search_term" type="text" name="search_term" placeholder="<?= $has_search_term ? $A_view["search_term"] : "Votre recherche" ?>">
|
<input id="query" type="text" name="query" placeholder="<?= $has_query ? $A_view["QUERY"] : "Votre recherche" ?>">
|
||||||
<input type="submit" value="Rechercher">
|
<input type="submit" value="Rechercher">
|
||||||
</form>
|
</form>
|
||||||
<section>
|
<section>
|
||||||
<?php
|
<?php
|
||||||
$search_results = $A_view["results"];
|
if ($has_query) {
|
||||||
|
$search_results = $A_view["RESULTS"];
|
||||||
if (empty($search_results)) {
|
if (empty($search_results)) {
|
||||||
echo '<h2 class="no_results">Aucun résultat</h2>';
|
echo '<h2 class="no_results">Aucun résultat</h2>';
|
||||||
echo '<p class="no_results_description">Assurez-vous d\'avoir rentré correctement vos termes de recherche ou essayez des mots clefs différents.</p>';
|
echo '<p class="no_results_description">Assurez-vous d\'avoir rentré correctement vos termes de recherche ou essayez des mots clefs différents.</p>';
|
||||||
@ -23,6 +24,7 @@
|
|||||||
View::show("common/recipe", $value);
|
View::show("common/recipe", $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
|
Loading…
Reference in New Issue
Block a user