|
@@ -64,15 +64,19 @@ class PostHandler
|
64
|
64
|
public function write_json()
|
65
|
65
|
{
|
66
|
66
|
$result = 0;
|
67
|
|
- $fp = fopen($this->stockFile, "w");
|
68
|
67
|
$array = array(
|
69
|
68
|
"types" => $this->dao->get_categories(),
|
70
|
69
|
"articles" => $this->get_articles_json_list(),
|
71
|
70
|
);
|
72
|
|
- fwrite($fp, json_encode($array));
|
73
|
|
- fclose($fp);
|
|
71
|
+ $fp = fopen($this->stockFile, "w");
|
|
72
|
+ if ($fp) {
|
|
73
|
+ fwrite($fp, json_encode($array));
|
|
74
|
+ fclose($fp);
|
|
75
|
+ $this->responseArray["data"] = $result;
|
|
76
|
+ } else {
|
|
77
|
+ $this->setFileErrorResponse();
|
|
78
|
+ }
|
74
|
79
|
|
75
|
|
- $this->responseArray["data"] = $result;
|
76
|
80
|
return $this->responseArray;
|
77
|
81
|
}
|
78
|
82
|
|
|
@@ -83,37 +87,11 @@ class PostHandler
|
83
|
87
|
foreach ($articles as $article) {
|
84
|
88
|
$article["type"] = $this->dao->get_categories_of_article($article["id"]);
|
85
|
89
|
$article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
|
86
|
|
- //EXPERIMENTAL
|
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"] = null;
|
97
|
|
- $article["generic"] = null;
|
98
|
|
- $article["ingredients"] = null;
|
99
|
|
- }
|
100
|
90
|
array_push($formatted_articles, $article);
|
101
|
91
|
}
|
102
|
|
-
|
103
|
92
|
return $formatted_articles;
|
104
|
93
|
}
|
105
|
94
|
|
106
|
|
- private function get_openfoodfacts_product($barcode)
|
107
|
|
- {
|
108
|
|
- $country = 'fr';
|
109
|
|
- $productSlug = 'produit';
|
110
|
|
- $url = 'https://{country}.openfoodfacts.org/api/v0/{product}/{scan}.json';
|
111
|
|
- $url = str_replace(['{country}','{product}','{scan}'],[$country,$productSlug,$barcode],$url);
|
112
|
|
- $result = json_decode(file_get_contents($url), true);
|
113
|
|
- if ($result["status"] == 1) return $result["product"];
|
114
|
|
- else return null;
|
115
|
|
- }
|
116
|
|
-
|
117
|
95
|
private function save_image()
|
118
|
96
|
{
|
119
|
97
|
$success = true;
|
|
@@ -247,6 +225,12 @@ class PostHandler
|
247
|
225
|
$this->responseArray["message"] = "Error: Data processing error";
|
248
|
226
|
}
|
249
|
227
|
|
|
228
|
+ function setFileErrorResponse()
|
|
229
|
+ {
|
|
230
|
+ $this->responseArray["status"] = 5;
|
|
231
|
+ $this->responseArray["message"] = "Error: Could not open file";
|
|
232
|
+ }
|
|
233
|
+
|
250
|
234
|
private function get_action($inputData)
|
251
|
235
|
{
|
252
|
236
|
if (!in_array($inputData["action"], $this->valid_actions))
|