From bb00e19385a65d44497232754329e035a85ab6d3 Mon Sep 17 00:00:00 2001 From: Stepan Date: Sat, 28 Jun 2025 11:47:16 +0200 Subject: [PATCH 1/3] Updated README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1b5c135..8338817 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ The FridgeBites was developed almost entirely from scratch using PHP, without re The website is built entirely from scratch using pure PHP, without relying on any frameworks. This was done as an experimental project to enhance personal skills. Despite this, some libraries were used to add specific functionality: - [\[PHP\] PHPMailer](https://github.com/PHPMailer/PHPMailer): A library for sending emails, used for password recovery functionality. +- [\[PHP\] FPHP](https://www.fpdf.org/): library which allows to generate PDF files with pure PHP, that is to say without using the PDFlib library. - [\[JS\] Swiper.js](https://github.com/nolimits4web/swiper): A highly customizable library for creating sliders and carousels. - [\[JS\] Toastify.js](https://github.com/apvarun/toastify-js): A lightweight library for creating beautiful and customizable toast notifications. From c3967b9abca3caedd4d8ff01c94a80a9f6afa934 Mon Sep 17 00:00:00 2001 From: Stepan Date: Sun, 29 Jun 2025 00:51:43 +0200 Subject: [PATCH 2/3] Added UserMenu ajax endpoint --- apps/Ajax/Controllers/AjaxController.php | 10 +++- apps/Ajax/Templates/ajax-result.php | 2 +- apps/Ajax/ajax-actions.php | 68 ++++++++++++++++++++++-- apps/Recipes/Models/RecipeUserMenu.php | 57 ++++++++++++++++++++ includes/Const/recipes.php | 5 ++ 5 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 apps/Recipes/Models/RecipeUserMenu.php create mode 100644 includes/Const/recipes.php diff --git a/apps/Ajax/Controllers/AjaxController.php b/apps/Ajax/Controllers/AjaxController.php index 6a6f5ab..60ab3b2 100644 --- a/apps/Ajax/Controllers/AjaxController.php +++ b/apps/Ajax/Controllers/AjaxController.php @@ -3,6 +3,7 @@ namespace Lycoreco\Apps\Ajax\Controllers; use Lycoreco\Includes\BaseController; +use Lycoreco\Includes\Model\ValidationError; class AjaxController extends BaseController { @@ -32,8 +33,15 @@ class AjaxController extends BaseController try { $context['result'] = $action(); - } catch (\Exception $ex) { + } + catch (ValidationError $ex) { + $context['result'] = get_ajax_error($ex->getMessage(), 400); + return $context; + } + catch (\Exception $ex) { + http_response_code(500); $context['result'] = get_ajax_error($ex->getMessage()); + return $context; } diff --git a/apps/Ajax/Templates/ajax-result.php b/apps/Ajax/Templates/ajax-result.php index fd5f3ae..4b3fdca 100644 --- a/apps/Ajax/Templates/ajax-result.php +++ b/apps/Ajax/Templates/ajax-result.php @@ -1,2 +1,2 @@ 'obj.recipe_id', + 'type' => '=', + 'value' => $recipe_id + ], + [ + 'name' => 'obj.user_id', + 'type' => '=', + 'value' => CURRENT_USER->get_id() + ] + )); + // If user choose optiopn 'remove' + if($dayofweek == 'remove') { + if($user_menu) { + $user_menu->delete(); + $result['success'] = 'This recipe was removed from your list'; + return $result; + } + else { + return get_ajax_error("This recipe in your menu is not exists", 400); + } + } + + // If not exists, add new recipe in user menu + if(!$user_menu) { + $user_menu = new RecipeUserMenu(); + $user_menu->field_recipe_id = $recipe_id; + $user_menu->field_user_id = CURRENT_USER->get_id(); + } + + $user_menu->field_dayofweek = $dayofweek; + $user_menu->save(); + + $result['success'] = 'You have successfully added the recipe to your menu.'; + return $result; +} diff --git a/apps/Recipes/Models/RecipeUserMenu.php b/apps/Recipes/Models/RecipeUserMenu.php new file mode 100644 index 0000000..9d98187 --- /dev/null +++ b/apps/Recipes/Models/RecipeUserMenu.php @@ -0,0 +1,57 @@ + 'int', + 'dayofweek' => 'string', + 'recipe_id' => 'int', + 'user_id' => 'int', + 'created_at' => 'DateTime' + ]; + + public static function init_table() + { + $result = db_query('CREATE TABLE ' . static::$table_name . ' ( + id INT AUTO_INCREMENT PRIMARY KEY, + + dayofweek VARCHAR(150) NOT NULL, + recipe_id INT NOT NULL, + user_id INT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + + FOREIGN KEY (recipe_id) REFERENCES recipes(id) ON DELETE CASCADE, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + );'); + return $result; + } + public function valid() + { + require_once INCLUDES_PATH . '/Const/recipes.php'; + + if(!in_array($this->field_dayofweek, DAY_OF_WEEKS)) + return ['Day of Week is not valid']; + + $recipe = RecipeModel::get(array( + [ + 'name' => 'obj.id', + 'type' => '=', + 'value' => $this->field_recipe_id + ] + )); + if(!$recipe) + return ['Recipe is not exists']; + + return true; + } +} \ No newline at end of file diff --git a/includes/Const/recipes.php b/includes/Const/recipes.php new file mode 100644 index 0000000..00f8e4c --- /dev/null +++ b/includes/Const/recipes.php @@ -0,0 +1,5 @@ + Date: Sun, 29 Jun 2025 00:53:28 +0200 Subject: [PATCH 3/3] Fixed const name --- apps/Recipes/Models/RecipeUserMenu.php | 2 +- includes/Const/recipes.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/Recipes/Models/RecipeUserMenu.php b/apps/Recipes/Models/RecipeUserMenu.php index 9d98187..805a414 100644 --- a/apps/Recipes/Models/RecipeUserMenu.php +++ b/apps/Recipes/Models/RecipeUserMenu.php @@ -39,7 +39,7 @@ class RecipeUserMenu extends BaseModel { require_once INCLUDES_PATH . '/Const/recipes.php'; - if(!in_array($this->field_dayofweek, DAY_OF_WEEKS)) + if(!in_array($this->field_dayofweek, DAYS_OF_WEEK)) return ['Day of Week is not valid']; $recipe = RecipeModel::get(array( diff --git a/includes/Const/recipes.php b/includes/Const/recipes.php index 00f8e4c..8b971ca 100644 --- a/includes/Const/recipes.php +++ b/includes/Const/recipes.php @@ -1,5 +1,5 @@