236 lines
11 KiB
PHP
236 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class XmlInco extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'id',
|
|
'no_art',
|
|
'nom',
|
|
'prod_saison',
|
|
'allegations',
|
|
'conseil_prepa',
|
|
'astuces',
|
|
'descriptif',
|
|
'arg_vente',
|
|
'nb_portion',
|
|
'typ_portion',
|
|
'pds_moy_theo',
|
|
'lib_conso',
|
|
'lst_ing',
|
|
'lib_allergene',
|
|
'nutritional_values',
|
|
'picture',
|
|
];
|
|
|
|
protected $casts = [
|
|
'picture' => 'array',
|
|
'nutritional_values' => 'array',
|
|
];
|
|
|
|
/**
|
|
* The Laravel disk name
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $disk = 'inco';
|
|
|
|
protected $imagesPath = 'images';
|
|
|
|
/**
|
|
* The files to ignore
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $ignoreFiles = [
|
|
'.gitignore', '.gitkeep'
|
|
];
|
|
|
|
/**
|
|
* retrieve a collection of medias
|
|
*/
|
|
public function getMediasAttribute() {
|
|
/** @var Filesystem */
|
|
$disk = $this->getDisk();
|
|
$files = $disk->files($this->imagesPath);
|
|
|
|
$collFiles = new Collection();
|
|
// /** @var File */
|
|
foreach ($files as $file) {
|
|
$pi = \pathinfo($file, \PATHINFO_BASENAME);
|
|
if (in_array($pi, $this->ignoreFiles)) {
|
|
continue;
|
|
}
|
|
if (!in_array($pi, array($this->id))) {
|
|
continue;
|
|
}
|
|
$image = base64_encode($disk->get($file));
|
|
$element = array(
|
|
"filename" => $pi,
|
|
"imgdata" => $image
|
|
);
|
|
$collFiles->add($element);
|
|
}
|
|
return $collFiles;
|
|
}
|
|
|
|
/**
|
|
* Get the concerned disk
|
|
*
|
|
* @return Filesystem
|
|
*/
|
|
protected function getDisk(): Filesystem
|
|
{
|
|
return Storage::disk($this->disk);
|
|
}
|
|
|
|
public function getNiceStringAttribute()
|
|
{
|
|
return sprintf('%s - %s', $this->id, $this->descriptif);
|
|
}
|
|
|
|
public function updateLocalProductInfo()
|
|
{
|
|
$foodProduct = FoodProduct::where('no_art', $this->id)->first();
|
|
|
|
if (!empty($foodProduct)) {
|
|
//info produits à suggerer lors de l'édition
|
|
// this->descriptif (doit aller dans la table products)
|
|
// $table->text('preparation')->nullable(); => $this->conseil_prepa
|
|
// $table->text('selling_argument')->nullable(); => $this->arg_vente
|
|
|
|
//infos produit maj automatique
|
|
$foodProduct->ingredients = $this->lst_ing;
|
|
$foodProduct->allergens = $this->lib_allergene;
|
|
//$foodProduct->servings = $this->nb_portion; // problem les types de correspondent pas numeric = string (exemple this->nb_portion = '1/2')
|
|
|
|
$nutri_values = $this->nutritional_values;
|
|
if (!empty($nutri_values)) {
|
|
//maj des info nutri locales
|
|
//pour la ref
|
|
$foodProduct->nv_ref_weight_g = $nutri_values["taille_ref"];
|
|
$foodProduct->nv_kj = $nutri_values["val_energ_kj_r"]; //Energie Kj
|
|
$foodProduct->nv_kcal = $nutri_values["val_energ_kcal_r"]; //Energie Kcal
|
|
$foodProduct->nv_fat_g = $nutri_values["mat_grass_r"]; //graisses
|
|
$foodProduct->nv_sat_fat_g = $nutri_values["ags_r"]; //dont acides gras saturés
|
|
$foodProduct->nv_carb_g = $nutri_values["glucides_r"]; //Glucides
|
|
$foodProduct->nv_sugar_g = $nutri_values["sucre_r"]; //dont sucres
|
|
$foodProduct->nv_fiber_g = $nutri_values["fib_alim_r"]; //fibres alimentaires
|
|
$foodProduct->nv_protein_g = $nutri_values["proteine_r"]; //protéines
|
|
$foodProduct->nv_salt_g = $nutri_values["sel_r"]; //sel
|
|
|
|
// $foodProduct-> = $nutri_values["sodium_r"];
|
|
// $foodProduct-> = $nutri_values["ener_e14_r"];
|
|
// $foodProduct-> = $nutri_values["fruits_r"];
|
|
// $foodProduct-> = $nutri_values["fruits_coque_sech_r"];
|
|
// $foodProduct-> = $nutri_values["ener_kjo_r"];
|
|
// $foodProduct-> = $nutri_values["fatsat_r"];
|
|
// $foodProduct-> = $nutri_values["choval_r"];
|
|
// $foodProduct-> = $nutri_values["sugar_r"];
|
|
// $foodProduct-> = $nutri_values["fibtg_r"];
|
|
// $foodProduct-> = $nutri_values["pro_r"];
|
|
// $foodProduct-> = $nutri_values["salteq_r"];
|
|
|
|
|
|
//valeurs nutritionnelles par portion
|
|
$foodProduct->nv_serving_size_g = $nutri_values["taille_port"]; //taille portion en grammes
|
|
$foodProduct->nv_serving_kj = $nutri_values["val_energ_kj_p"]; //Energie Kj
|
|
$foodProduct->nv_serving_kcal = $nutri_values["val_energ_kcal_p"]; //Energie Kcal
|
|
$foodProduct->nv_serving_fat_g = $nutri_values["mat_grass_p"]; //graisses
|
|
$foodProduct->nv_serving_sat_fat_g = $nutri_values["ags_p"]; //dont acides gras saturés
|
|
$foodProduct->nv_serving_carb_g = $nutri_values["glucides_p"]; //Glucides
|
|
$foodProduct->nv_serving_sugar_g = $nutri_values["sucre_p"]; //dont sucres
|
|
$foodProduct->nv_serving_fiber_g = $nutri_values["fib_alim_p"]; //fibres alimentaires
|
|
$foodProduct->nv_serving_protein_g = $nutri_values["proteine_p"]; //protéines
|
|
$foodProduct->nv_serving_salt_g = $nutri_values["sel_p"]; //sel
|
|
|
|
// $foodProduct-> = $nutri_values["sodium_p"];
|
|
// $foodProduct-> = $nutri_values["fruits_p"];
|
|
|
|
|
|
//apports journaliers
|
|
$foodProduct->nv_daily_energy = $nutri_values["pourcent_apport_ref_kjo_r"]; //Energie % recommandé
|
|
$foodProduct->nv_daily_fat = $nutri_values["pourcent_apport_ref_fat_r"]; //graisses
|
|
$foodProduct->nv_daily_sat_fat = $nutri_values["pourcent_apport_ref_fatsat_r"]; //dont acides gras saturés
|
|
$foodProduct->nv_daily_carb = $nutri_values["pourcent_apport_ref_choval_r"]; //Glucides
|
|
$foodProduct->nv_daily_sugar = $nutri_values["pourcent_apport_ref_sugar_r"]; //dont sucres
|
|
$foodProduct->nv_daily_fiber = $nutri_values["pourcent_apport_ref_fibtg_r"]; //fibres alimentaires
|
|
$foodProduct->nv_daily_protein = $nutri_values["pourcent_apport_ref_pro_r"]; //protéines
|
|
$foodProduct->nv_daily_salt = $nutri_values["pourcent_apport_ref_salteq_r"]; //sel
|
|
|
|
//$foodProduct-> = $nutri_values["pourcent_apport_ref_e14_r"];
|
|
|
|
//nutriscore
|
|
$foodProduct->nutriscore = $nutri_values["classement"];
|
|
|
|
|
|
//Infos n'ayant pas de mapping
|
|
// $foodProduct-> = $nutri_values["val_energ_kj_nut"];
|
|
// $foodProduct-> = $nutri_values["val_energ_kcal_nut"];
|
|
// $foodProduct-> = $nutri_values["mat_grass_nut"];
|
|
// $foodProduct-> = $nutri_values["ags_nut"];
|
|
// $foodProduct-> = $nutri_values["glucides_nut"];
|
|
// $foodProduct-> = $nutri_values["sucre_nut"];
|
|
// $foodProduct-> = $nutri_values["sucre_r"];
|
|
// $foodProduct-> = $nutri_values["fib_alim_nut"];
|
|
// $foodProduct-> = $nutri_values["proteine_nut"];
|
|
// $foodProduct-> = $nutri_values["sel_nut"];
|
|
// $foodProduct-> = $nutri_values["apport_ref"];
|
|
// $foodProduct-> = $nutri_values["fruits_coque_sech_p"];
|
|
// $foodProduct-> = $nutri_values["ener_e14_p"];
|
|
// $foodProduct-> = $nutri_values["pourcent_apport_ref_e14_p"];
|
|
// $foodProduct-> = $nutri_values["ener_kjo_p"];
|
|
// $foodProduct-> = $nutri_values["pourcent_apport_ref_kjo_p"];
|
|
// $foodProduct-> = $nutri_values["fat_p"];
|
|
// $foodProduct-> = $nutri_values["pourcent_apport_ref_fat_p"];
|
|
// $foodProduct-> = $nutri_values["fatsat_p"];
|
|
// $foodProduct-> = $nutri_values["pourcent_apport_ref_fatsat_p"];
|
|
// $foodProduct-> = $nutri_values["choval_p"];
|
|
// $foodProduct-> = $nutri_values["pourcent_apport_ref_choval_p"];
|
|
// $foodProduct-> = $nutri_values["sugar_p"];
|
|
// $foodProduct-> = $nutri_values["pourcent_apport_ref_sugar_p"];
|
|
// $foodProduct-> = $nutri_values["fibtg_p"];
|
|
// $foodProduct-> = $nutri_values["pourcent_apport_ref_fibtg_p"];
|
|
// $foodProduct-> = $nutri_values["pro_p"];
|
|
// $foodProduct-> = $nutri_values["pourcent_apport_ref_pro_p"];
|
|
// $foodProduct-> = $nutri_values["salteq_p"];
|
|
// $foodProduct-> = $nutri_values["pourcent_apport_ref_salteq_p"];
|
|
// $foodProduct-> = $nutri_values["apport_port"];
|
|
// $foodProduct-> = $nutri_values["part_p"];
|
|
// $foodProduct-> = $nutri_values["part_g"];
|
|
// $foodProduct-> = $nutri_values["part_l"];
|
|
// $foodProduct-> = $nutri_values["p_sur_l"];
|
|
// $foodProduct-> = $nutri_values["pourcent_ags"];
|
|
// $foodProduct-> = $nutri_values["pourcent_agi"];
|
|
// $foodProduct-> = $nutri_values["pourcent_ss"];
|
|
// $foodProduct-> = $nutri_values["pourcent_amidon"];
|
|
// $foodProduct-> = $nutri_values["composante_n"];
|
|
// $foodProduct-> = $nutri_values["composante_p"];
|
|
// $foodProduct-> = $nutri_values["pourcent_fruits"];
|
|
// $foodProduct-> = $nutri_values["mat_grass_r_prec"];
|
|
// $foodProduct-> = $nutri_values["ags_r_prec"];
|
|
// $foodProduct-> = $nutri_values["glucides_r_prec"];
|
|
// $foodProduct-> = $nutri_values["sucre_r_prec"];
|
|
// $foodProduct-> = $nutri_values["fib_alim_r_prec"];
|
|
// $foodProduct-> = $nutri_values["proteine_r_prec"];
|
|
// $foodProduct-> = $nutri_values["sel_r_prec"];
|
|
// $foodProduct-> = $nutri_values["sodium_r_prec"];
|
|
// $foodProduct-> = $nutri_values["fruits_r_prec"];
|
|
// $foodProduct-> = $nutri_values["fruits_coque_sech_r_prec"];
|
|
// $foodProduct-> = $nutri_values["score_nutri"];
|
|
}
|
|
|
|
$foodProduct->save();
|
|
}
|
|
}
|
|
}
|