Compare commits

..

No commits in common. "6ba14598481cd6ef5fe6d588d82b39def9ac7fda" and "2786d17e5d61c8fc97fe2e7ff9070f87e9c93f43" have entirely different histories.

View file

@ -1,4 +1,5 @@
<?php <?php
require_once 'dao.php'; require_once 'dao.php';
class PostHandler class PostHandler
@ -82,36 +83,27 @@ class PostHandler
$formatted_articles = []; $formatted_articles = [];
foreach ($articles as $article) { foreach ($articles as $article) {
$article["type"] = $this->dao->get_categories_of_article($article["id"]); $article["type"] = $this->dao->get_categories_of_article($article["id"]);
$article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
//EXPERIMENTAL //EXPERIMENTAL
$product = $this->get_openfoodfacts_product($article["code"]); $response = $this->get_openfoodfacts_product($article["code"]);
if($product != null){ $product = $response["product"];
$article["nutri-score"] = $product["nutrition_grade_fr"]; $article["image"] = $product["image_front_url"] != "" ? $product["image_front_url"] : $this->imageBaseUrl . $article["id"] . ".jpg";
if(!empty($product["ingredients_text_fr"])) $article["ingredients"] = $product["ingredients_text_fr"]; $article["nutri-score"] = !empty($product["nutrition_grade_fr"]) ? $product["nutrition_grade_fr"] : null;
else $article["ingredients"] = $product["ingredients_text"]; $article["ingredients"] = !empty($product["ingredients_text_fr"]) ? $product["ingredients_text_fr"] : !empty($product["ingredients_text"]) ? $product["ingredients_text"] : null;
if(!empty($product["generic_name_fr"])) $article["generic"] = $product["generic_name_fr"]; $article["name"] = !empty($product["product_name_fr"]) ? $product["product_name_fr"] : !empty($product["product_name"]) ? $product["product_name"] : $article["name"];
else $article["generic"] = $product["generic_name"]; $article["description"] = !empty($product["generic_name_fr"]) ? $product["generic_name_fr"] : !empty($product["generic_name"]) ? $product["generic_name"] : $article["description"];
}
else {
$article["nutri-score"] = null;
$article["generic"] = null;
$article["ingredients"] = null;
}
array_push($formatted_articles, $article); array_push($formatted_articles, $article);
} }
return $formatted_articles; return $formatted_articles;
} }
private function get_openfoodfacts_product($barcode) private function get_openfoodfacts_product($barcode){
{
$country = 'fr'; $country = 'fr';
$productSlug = 'produit'; $productSlug = 'produit';
$url = 'https://{country}.openfoodfacts.org/api/v0/{product}/{scan}.json'; $url = 'https://{country}.openfoodfacts.org/api/v0/{product}/{scan}.json';
$url = str_replace(['{country}','{product}','{scan}'],[$country,$productSlug,$barcode],$url); $url = str_replace(['{country}','{product}','{scan}'],[$country,$productSlug,$barcode],$url);
$result = json_decode(file_get_contents($url), true); $result = file_get_contents($url);
if ($result["status"] == 1) return $result["product"]; return json_decode($result, true);
else return null;
} }
private function save_image() private function save_image()