Browse Source

Merge V3 to V2

docjyJ 1 year ago
parent
commit
862b6a4741
1 changed files with 41 additions and 37 deletions
  1. 41
    37
      classes/postHandler.php

+ 41
- 37
classes/postHandler.php View File

@@ -1,6 +1,9 @@
1 1
 <?php
2
+
3
+use OpenFoodFacts\Exception\BadRequestException;
4
+use OpenFoodFacts\Exception\ProductNotFoundException;
5
+
2 6
 require_once 'dao.php';
3
-//require_once '../openfoodfacts-php-0.2.3/src/Api.php';
4 7
 
5 8
 class PostHandler
6 9
 {
@@ -15,7 +18,6 @@ class PostHandler
15 18
     private $dao;
16 19
     private $uploadBaseDir = '../uploaded_images/';
17 20
     private $stockFile = "../data/stock-v2.json";
18
-    private $stockFileV3 = "../data/stock-v3.json";
19 21
     private $imageBaseUrl = "https://etud.insa-toulouse.fr/~proximo/uploaded_images/";
20 22
 
21 23
     private $responseArray = array(
@@ -66,7 +68,6 @@ class PostHandler
66 68
     public function write_json()
67 69
     {
68 70
         $result = 0;
69
-        // V2
70 71
         $fp = fopen($this->stockFile, "w");
71 72
         $array = array(
72 73
             "types" => $this->dao->get_categories(),
@@ -74,14 +75,6 @@ class PostHandler
74 75
         );
75 76
         fwrite($fp, json_encode($array));
76 77
         fclose($fp);
77
-        // V3
78
-        $fp = fopen($this->stockFileV3, "w");
79
-        $array = array(
80
-            "types" => $this->dao->get_categories(),
81
-            "articles" => $this->get_articles_json_list_V3(),
82
-        );
83
-        fwrite($fp, json_encode($array));
84
-        fclose($fp);
85 78
 
86 79
         $this->responseArray["data"] = $result;
87 80
         return $this->responseArray;
@@ -89,37 +82,48 @@ class PostHandler
89 82
 
90 83
     public function get_articles_json_list()
91 84
     {
92
-        $articles = $this->dao->get_articles();
93
-        $formatted_articles = [];
94
-        foreach ($articles as $article) {
95
-            $article["type"] = $this->dao->get_categories_of_article($article["id"]);
96
-            $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
97
-            array_push($formatted_articles, $article);
98
-        }
99
-
100
-        return $formatted_articles;
101
-    }
102
-
103
-    public function get_articles_json_list_V3()
104
-    {
105 85
         $api = new OpenFoodFacts\Api('food','fr');
106 86
         $articles = $this->dao->get_articles();
107 87
         $formatted_articles = [];
108 88
         foreach ($articles as $article) {
109
-            $product = $api->getProduct('3057640385148');
110 89
             $article["type"] = $this->dao->get_categories_of_article($article["id"]);
111
-            $article["image"] = $product["image_front_url"];
112
-            $article["nutri_score"] = $product["nutrition_grade_fr"];
113
-            // general $product["ingredients_text"]
114
-            $article["ingredients_fr"] = $product["ingredients_text_fr"];
115
-            $article["ingredients_en"] = $product["ingredients_text_en"];
116
-            // general $product["product_name"]
117
-            $article["name_fr"] = $product["product_name_fr"];
118
-            $article["name_en"] = $product["product_name_en"];
119
-            // general $product["generic_name"]
120
-            $article["description_fr"] = $product["generic_name_fr"];
121
-            $article["description_en"] = $product["generic_name_en"];
122
-
90
+            $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
91
+            //EXPERIMENTAL
92
+            try {
93
+                $product = $api->getProduct('3057640385148');
94
+                $article["image_v2"] = $product["image_front_url"];
95
+                $article["nutri_score"] = $product["nutrition_grade_fr"];
96
+                $article["ingredients_fr"] = $product["ingredients_text_fr"] != "" ? $product["ingredients_text_fr"] : $product["ingredients_text"];
97
+                $article["ingredients_en"] = $product["ingredients_text_en"] != "" ? $product["ingredients_text_en"] : $product["ingredients_text"];
98
+                $article["name_fr"] = $product["product_name_fr"] != "" ? $product["product_name_fr"] : $product["product_name"];
99
+                $article["name_fr"] = $product["product_name_en"] != "" ? $product["product_name_en"] : $product["product_name"];
100
+                $article["description_fr"] = $product["generic_name_fr"];
101
+                $article["description_en"] = $product["generic_name_en"];
102
+                $article["description_fr"] = $product["generic_name_fr"] != "" ? $product["generic_name_fr"] : $product["generic_name"];
103
+                $article["description_en"] = $product["generic_name_en"] != "" ? $product["generic_name_en"] : $product["generic_name"];
104
+            } catch (BadRequestException $e) {
105
+                $article["image_v2"] = "";
106
+                $article["nutri_score"] = "";
107
+                $article["ingredients_fr"] = "";
108
+                $article["ingredients_en"] = "";
109
+                $article["name_fr"] = "";
110
+                $article["name_fr"] = "";
111
+                $article["description_fr"] = "";
112
+                $article["description_en"] = "";
113
+                $article["description_fr"] = "";
114
+                $article["description_en"] = "";
115
+            } catch (ProductNotFoundException $e) {
116
+                $article["image_v2"] = "";
117
+                $article["nutri_score"] = "";
118
+                $article["ingredients_fr"] = "";
119
+                $article["ingredients_en"] = "";
120
+                $article["name_fr"] = "";
121
+                $article["name_fr"] = "";
122
+                $article["description_fr"] = "";
123
+                $article["description_en"] = "";
124
+                $article["description_fr"] = "";
125
+                $article["description_en"] = "";
126
+            }
123 127
             array_push($formatted_articles, $article);
124 128
         }
125 129
 

Loading…
Cancel
Save