From 38e4e16856619cc60f8e5c1dd4ce97683fe90971 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Fri, 20 Jan 2023 11:03:53 +0100 Subject: [PATCH 1/4] add search action --- Controllers/RecipeController.php | 23 +++++++++++++++++++++++ Views/recipe/search.php | 20 +++++++++++--------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index 8382cb7..aecf6be 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -55,4 +55,27 @@ 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("search/search", array("SEARCH_TERM" => null)); + } + + private function searchQueryView(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null) + { + $A_results = array(); + View::show("search/search", array( + "SEARCH_TERM" => $A_getParams["query"], + "RESULTS" => $A_results, + )); + } + } diff --git a/Views/recipe/search.php b/Views/recipe/search.php index d9d3888..2047ec1 100644 --- a/Views/recipe/search.php +++ b/Views/recipe/search.php @@ -1,6 +1,6 @@ @@ -9,18 +9,20 @@
- "> + ">
Aucun résultat'; - echo '

Assurez-vous d\'avoir rentré correctement vos termes de recherche ou essayez des mots clefs différents.

'; - } else { - foreach ($search_results as $key => $value) { - View::show("common/recipe", $value); + if ($has_search_term) { + $search_results = $A_view["RESULTS"]; + if (empty($search_results)) { + echo '

Aucun résultat

'; + echo '

Assurez-vous d\'avoir rentré correctement vos termes de recherche ou essayez des mots clefs différents.

'; + } else { + foreach ($search_results as $key => $value) { + View::show("common/recipe", $value); + } } } ?> From 28810d0b7c2c2fa29c7803b0e35fa32e7244ac27 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Fri, 20 Jan 2023 18:21:39 +0100 Subject: [PATCH 2/4] change refs to view search/search to recipe/search --- Controllers/RecipeController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index aecf6be..90093d8 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -66,13 +66,13 @@ final class RecipeController private function searchView(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null) { - View::show("search/search", array("SEARCH_TERM" => null)); + View::show("recipe/search", array("SEARCH_TERM" => null)); } private function searchQueryView(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null) { $A_results = array(); - View::show("search/search", array( + View::show("recipe/search", array( "SEARCH_TERM" => $A_getParams["query"], "RESULTS" => $A_results, )); From 7c6a5441a7ba072ec8494a3b2193d4454f0db064 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Fri, 20 Jan 2023 18:24:37 +0100 Subject: [PATCH 3/4] refactor 'search_term' to 'query' --- Controllers/RecipeController.php | 4 ++-- Views/recipe/search.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index 90093d8..02635d7 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -66,14 +66,14 @@ final class RecipeController private function searchView(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null) { - View::show("recipe/search", array("SEARCH_TERM" => null)); + View::show("recipe/search", array("QUERY" => null)); } private function searchQueryView(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null) { $A_results = array(); View::show("recipe/search", array( - "SEARCH_TERM" => $A_getParams["query"], + "QUERY" => $A_getParams["query"], "RESULTS" => $A_results, )); } diff --git a/Views/recipe/search.php b/Views/recipe/search.php index 2047ec1..818bf28 100644 --- a/Views/recipe/search.php +++ b/Views/recipe/search.php @@ -1,6 +1,6 @@ @@ -8,13 +8,13 @@
- - "> + + ">
Aucun résultat'; From 5f61d15a89d6c57b891cba8f9c40730bfbf03403 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Fri, 20 Jan 2023 18:30:08 +0100 Subject: [PATCH 4/4] add search functionality to RecipeModel --- Controllers/RecipeController.php | 5 ++++- Models/RecipeModel.php | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index 02635d7..a453f75 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -71,7 +71,10 @@ final class RecipeController private function searchQueryView(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null) { - $A_results = array(); + + $O_recipeModel = new RecipeModel(); + $A_results = $O_recipeModel->searchRecipesByName($A_getParams["query"]); + View::show("recipe/search", array( "QUERY" => $A_getParams["query"], "RESULTS" => $A_results, diff --git a/Models/RecipeModel.php b/Models/RecipeModel.php index a6bd09d..978fa70 100644 --- a/Models/RecipeModel.php +++ b/Models/RecipeModel.php @@ -33,4 +33,39 @@ final class RecipeModel 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(); + } }