Browse Source

Update Code

docjyJ 1 year ago
parent
commit
83cee48d36
1 changed files with 17 additions and 10 deletions
  1. 17
    10
      classes/postHandler.php

+ 17
- 10
classes/postHandler.php View File

@@ -1,5 +1,4 @@
1 1
 <?php
2
-
3 2
 require_once 'dao.php';
4 3
 
5 4
 class PostHandler
@@ -83,14 +82,21 @@ class PostHandler
83 82
         $formatted_articles = [];
84 83
         foreach ($articles as $article) {
85 84
             $article["type"] = $this->dao->get_categories_of_article($article["id"]);
85
+            $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
86 86
             //EXPERIMENTAL
87
-            $response = $this->get_openfoodfacts_product($article["code"]);
88
-            $product = $response["product"];
89
-            $article["image"] = $product["image_front_url"] != "" ? $product["image_front_url"] : $this->imageBaseUrl . $article["id"] . ".jpg";
90
-            $article["nutri-score"] = !empty($product["nutrition_grade_fr"])  ? $product["nutrition_grade_fr"]  : null;
91
-            $article["ingredients"] = !empty($product["ingredients_text_fr"]) ? $product["ingredients_text_fr"] : !empty($product["ingredients_text"]) ? $product["ingredients_text"] : null;
92
-            $article["name"]        = !empty($product["product_name_fr"])     ? $product["product_name_fr"]     : !empty($product["product_name"])     ? $product["product_name"]     : $article["name"];
93
-            $article["description"] = !empty($product["generic_name_fr"])     ? $product["generic_name_fr"]     : !empty($product["generic_name"])     ? $product["generic_name"]     : $article["description"];
87
+            $product = $this->get_openfoodfacts_product($article["code"]);
88
+            if($product != null){
89
+                $article["nutri-score"] = $product["nutrition_grade_fr"];
90
+                if(!empty($product["ingredients_text_fr"])) $article["ingredients"] = $product["ingredients_text_fr"];
91
+                else $article["ingredients"] = $product["ingredients_text"];
92
+                if(!empty($product["generic_name_fr"])) $article["generic"] = $product["generic_name_fr"];
93
+                else $article["generic"] = $product["generic_name"];
94
+            }
95
+            else {
96
+                $article["nutri-score"] = "";
97
+                $article["generic"] = "";
98
+                $article["ingredients"] = "";
99
+            }
94 100
             array_push($formatted_articles, $article);
95 101
         }
96 102
 
@@ -102,8 +108,9 @@ class PostHandler
102 108
         $productSlug = 'produit';
103 109
         $url = 'https://{country}.openfoodfacts.org/api/v0/{product}/{scan}.json';
104 110
         $url = str_replace(['{country}','{product}','{scan}'],[$country,$productSlug,$barcode],$url);
105
-        $result = file_get_contents($url);
106
-        return json_decode($result, true);
111
+        $result = json_decode(file_get_contents($url), true);
112
+        if ($result["status"] == 1) return $result["product"];
113
+        else return null;
107 114
     }
108 115
 
109 116
     private function save_image()

Loading…
Cancel
Save