From c374c50ca0563374a319ed400c6abeca6be02039 Mon Sep 17 00:00:00 2001 From: Stepan Date: Sat, 28 Jun 2025 00:39:25 +0200 Subject: [PATCH] Added ajax search and fixed body type for request on ajax --- apps/Ajax/Controllers/AjaxController.php | 7 +--- apps/Ajax/ajax-actions.php | 50 +++++++++++------------- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/apps/Ajax/Controllers/AjaxController.php b/apps/Ajax/Controllers/AjaxController.php index d13258a..6a6f5ab 100644 --- a/apps/Ajax/Controllers/AjaxController.php +++ b/apps/Ajax/Controllers/AjaxController.php @@ -15,10 +15,7 @@ class AjaxController extends BaseController require_once APPS_PATH . '/Ajax/ajax-actions.php'; $context['result'] = ""; - $json = file_get_contents('php://input'); - $data = json_decode($json, true); - - $action = $data['action'] ?? false; + $action = $_POST['action'] ?? false; // If request from other site if (!in_array($_SERVER['HTTP_HOST'], ALLOWED_HOSTS)) { @@ -34,7 +31,7 @@ class AjaxController extends BaseController $action = "ajax_" . $action; try { - $context['result'] = $action($data['args']); + $context['result'] = $action(); } catch (\Exception $ex) { $context['result'] = get_ajax_error($ex->getMessage()); } diff --git a/apps/Ajax/ajax-actions.php b/apps/Ajax/ajax-actions.php index 7b41556..b2ee7cc 100644 --- a/apps/Ajax/ajax-actions.php +++ b/apps/Ajax/ajax-actions.php @@ -1,5 +1,7 @@ 2, - 'name' => 'Genshin Impact' - ], - [ - 'id' => 3, - 'name' => 'Zenless zone zero' - ], - [ - 'id' => 4, - 'name' => 'Honkai Star Rail' - ], - [ - 'id' => 5, - 'name' => 'Honkai Impact' - ], - ]; - $result['results'] = []; - - foreach ($data as $key => $value) { - if(str_contains($value['name'], $search_query)) - $result['results'][] = $value; +function ajax_search() { + $search_query = $_POST['query'] ?? null; + if (!isset($search_query)) { + return get_ajax_error("Missing 'query' parameter.", 400); } - sleep(3); + if (!CURRENT_USER) { + return get_ajax_error('You are not authorized', 401); + } + $result = array(); + + $recipes = RecipeModel::filter( + array(), + array(), + 5, + 'AND', + 0, + $search_query + ); + + $result['count'] = count($recipes); + $result['result'] = $recipes; return json_encode($result, JSON_PRETTY_PRINT); } \ No newline at end of file