Merge V3 to V2
This commit is contained in:
parent
72b5fae35f
commit
862b6a4741
1 changed files with 41 additions and 37 deletions
|
@ -1,6 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use OpenFoodFacts\Exception\BadRequestException;
|
||||||
|
use OpenFoodFacts\Exception\ProductNotFoundException;
|
||||||
|
|
||||||
require_once 'dao.php';
|
require_once 'dao.php';
|
||||||
//require_once '../openfoodfacts-php-0.2.3/src/Api.php';
|
|
||||||
|
|
||||||
class PostHandler
|
class PostHandler
|
||||||
{
|
{
|
||||||
|
@ -15,7 +18,6 @@ class PostHandler
|
||||||
private $dao;
|
private $dao;
|
||||||
private $uploadBaseDir = '../uploaded_images/';
|
private $uploadBaseDir = '../uploaded_images/';
|
||||||
private $stockFile = "../data/stock-v2.json";
|
private $stockFile = "../data/stock-v2.json";
|
||||||
private $stockFileV3 = "../data/stock-v3.json";
|
|
||||||
private $imageBaseUrl = "https://etud.insa-toulouse.fr/~proximo/uploaded_images/";
|
private $imageBaseUrl = "https://etud.insa-toulouse.fr/~proximo/uploaded_images/";
|
||||||
|
|
||||||
private $responseArray = array(
|
private $responseArray = array(
|
||||||
|
@ -66,7 +68,6 @@ class PostHandler
|
||||||
public function write_json()
|
public function write_json()
|
||||||
{
|
{
|
||||||
$result = 0;
|
$result = 0;
|
||||||
// V2
|
|
||||||
$fp = fopen($this->stockFile, "w");
|
$fp = fopen($this->stockFile, "w");
|
||||||
$array = array(
|
$array = array(
|
||||||
"types" => $this->dao->get_categories(),
|
"types" => $this->dao->get_categories(),
|
||||||
|
@ -74,14 +75,6 @@ class PostHandler
|
||||||
);
|
);
|
||||||
fwrite($fp, json_encode($array));
|
fwrite($fp, json_encode($array));
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
// V3
|
|
||||||
$fp = fopen($this->stockFileV3, "w");
|
|
||||||
$array = array(
|
|
||||||
"types" => $this->dao->get_categories(),
|
|
||||||
"articles" => $this->get_articles_json_list_V3(),
|
|
||||||
);
|
|
||||||
fwrite($fp, json_encode($array));
|
|
||||||
fclose($fp);
|
|
||||||
|
|
||||||
$this->responseArray["data"] = $result;
|
$this->responseArray["data"] = $result;
|
||||||
return $this->responseArray;
|
return $this->responseArray;
|
||||||
|
@ -89,37 +82,48 @@ class PostHandler
|
||||||
|
|
||||||
public function get_articles_json_list()
|
public function get_articles_json_list()
|
||||||
{
|
{
|
||||||
|
$api = new OpenFoodFacts\Api('food','fr');
|
||||||
$articles = $this->dao->get_articles();
|
$articles = $this->dao->get_articles();
|
||||||
$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";
|
$article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
|
||||||
array_push($formatted_articles, $article);
|
//EXPERIMENTAL
|
||||||
}
|
try {
|
||||||
|
$product = $api->getProduct('3057640385148');
|
||||||
return $formatted_articles;
|
$article["image_v2"] = $product["image_front_url"];
|
||||||
}
|
$article["nutri_score"] = $product["nutrition_grade_fr"];
|
||||||
|
$article["ingredients_fr"] = $product["ingredients_text_fr"] != "" ? $product["ingredients_text_fr"] : $product["ingredients_text"];
|
||||||
public function get_articles_json_list_V3()
|
$article["ingredients_en"] = $product["ingredients_text_en"] != "" ? $product["ingredients_text_en"] : $product["ingredients_text"];
|
||||||
{
|
$article["name_fr"] = $product["product_name_fr"] != "" ? $product["product_name_fr"] : $product["product_name"];
|
||||||
$api = new OpenFoodFacts\Api('food','fr');
|
$article["name_fr"] = $product["product_name_en"] != "" ? $product["product_name_en"] : $product["product_name"];
|
||||||
$articles = $this->dao->get_articles();
|
$article["description_fr"] = $product["generic_name_fr"];
|
||||||
$formatted_articles = [];
|
$article["description_en"] = $product["generic_name_en"];
|
||||||
foreach ($articles as $article) {
|
$article["description_fr"] = $product["generic_name_fr"] != "" ? $product["generic_name_fr"] : $product["generic_name"];
|
||||||
$product = $api->getProduct('3057640385148');
|
$article["description_en"] = $product["generic_name_en"] != "" ? $product["generic_name_en"] : $product["generic_name"];
|
||||||
$article["type"] = $this->dao->get_categories_of_article($article["id"]);
|
} catch (BadRequestException $e) {
|
||||||
$article["image"] = $product["image_front_url"];
|
$article["image_v2"] = "";
|
||||||
$article["nutri_score"] = $product["nutrition_grade_fr"];
|
$article["nutri_score"] = "";
|
||||||
// general $product["ingredients_text"]
|
$article["ingredients_fr"] = "";
|
||||||
$article["ingredients_fr"] = $product["ingredients_text_fr"];
|
$article["ingredients_en"] = "";
|
||||||
$article["ingredients_en"] = $product["ingredients_text_en"];
|
$article["name_fr"] = "";
|
||||||
// general $product["product_name"]
|
$article["name_fr"] = "";
|
||||||
$article["name_fr"] = $product["product_name_fr"];
|
$article["description_fr"] = "";
|
||||||
$article["name_en"] = $product["product_name_en"];
|
$article["description_en"] = "";
|
||||||
// general $product["generic_name"]
|
$article["description_fr"] = "";
|
||||||
$article["description_fr"] = $product["generic_name_fr"];
|
$article["description_en"] = "";
|
||||||
$article["description_en"] = $product["generic_name_en"];
|
} catch (ProductNotFoundException $e) {
|
||||||
|
$article["image_v2"] = "";
|
||||||
|
$article["nutri_score"] = "";
|
||||||
|
$article["ingredients_fr"] = "";
|
||||||
|
$article["ingredients_en"] = "";
|
||||||
|
$article["name_fr"] = "";
|
||||||
|
$article["name_fr"] = "";
|
||||||
|
$article["description_fr"] = "";
|
||||||
|
$article["description_en"] = "";
|
||||||
|
$article["description_fr"] = "";
|
||||||
|
$article["description_en"] = "";
|
||||||
|
}
|
||||||
array_push($formatted_articles, $article);
|
array_push($formatted_articles, $article);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue