ldc-boutique-cnc/app/Models/XmlInco.php

294 lines
10 KiB
PHP
Raw Permalink Normal View History

2021-04-21 11:06:40 +02:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
2021-04-27 17:34:55 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Storage;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Collection;
2021-04-21 11:06:40 +02:00
class XmlInco extends Model
{
2021-04-27 17:34:55 +02:00
use HasFactory;
2021-04-21 11:06:40 +02:00
2022-09-03 16:56:16 +02:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
2021-04-21 11:06:40 +02:00
protected $fillable = [
'id',
'no_art',
'nom',
2021-04-21 11:06:40 +02:00
'prod_saison',
'allegations',
'conseil_prepa',
'astuces',
'descriptif',
'arg_vente',
'nb_portion',
'typ_portion',
'pds_moy_theo',
'lib_conso',
'lst_ing',
2021-04-26 12:16:17 +02:00
'lib_allergene',
2021-04-21 11:06:40 +02:00
'nutritional_values',
'picture',
];
2022-09-03 16:56:16 +02:00
/**
* The attributes that should be cast to native types.
*
* @var array
*/
2021-04-26 12:16:17 +02:00
protected $casts = [
'picture' => 'array',
'nutritional_values' => 'array',
];
2021-04-27 17:34:55 +02:00
/**
* The Laravel disk name
*
* @var string
*/
protected $disk = 'inco';
2022-09-03 16:56:16 +02:00
/**
* Storage path
*
* @var string
*/
2021-04-27 17:34:55 +02:00
protected $imagesPath = 'images';
/**
* The files to ignore
*
* @var array
*/
protected $ignoreFiles = [
'.gitignore', '.gitkeep'
];
/**
* retrieve a collection of medias
2022-09-03 16:56:16 +02:00
*
* @return Collection
2021-04-27 17:34:55 +02:00
*/
2022-08-25 12:26:43 +02:00
public function getMediasAttribute()
{
2021-04-27 17:34:55 +02:00
/** @var Filesystem */
2022-08-25 12:26:43 +02:00
$disk = $this->getDisk();
2021-04-27 17:34:55 +02:00
$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;
}
2022-08-25 12:26:43 +02:00
$image = base64_encode($disk->get($file));
2021-04-27 17:34:55 +02:00
$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);
}
2022-09-03 16:56:16 +02:00
/**
* getter
*
* @return string
*/
2021-04-21 11:06:40 +02:00
public function getNiceStringAttribute()
{
return sprintf('%s - %s', $this->id, $this->descriptif);
}
2021-04-26 12:16:17 +02:00
2022-09-03 16:56:16 +02:00
/**
* update local info
*
* @return void
*/
2021-04-26 12:16:17 +02:00
public function updateLocalProductInfo()
{
$foodProduct = FoodProduct::where('no_art', $this->id)->first();
if (!empty($foodProduct)) {
2022-09-03 16:56:16 +02:00
/** 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
*/
2021-04-26 12:16:17 +02:00
$foodProduct->ingredients = $this->lst_ing;
$foodProduct->allergens = $this->lib_allergene;
2022-09-03 16:56:16 +02:00
/** Les types de correspondent pas
* numeric ($foodProduct->servings) = string (exemple this->nb_portion = '1/2').
*/
2021-04-26 12:16:17 +02:00
$nutri_values = $this->nutritional_values;
if (!empty($nutri_values)) {
2022-09-03 16:56:16 +02:00
// Maj des info nutri locales.
// Pour la ref.
2021-04-26 12:16:17 +02:00
$foodProduct->nv_ref_weight_g = $nutri_values["taille_ref"];
2022-09-03 16:56:16 +02:00
// Energie Kj.
$foodProduct->nv_kj = $nutri_values["val_energ_kj_r"];
// Energie Kcal.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_kcal = $nutri_values["val_energ_kcal_r"];
2022-09-03 16:56:16 +02:00
// Graisses.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_fat_g = $nutri_values["mat_grass_r"];
2022-09-03 16:56:16 +02:00
// Dont acides gras saturés.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_sat_fat_g = $nutri_values["ags_r"];
2022-09-03 16:56:16 +02:00
// Glucides.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_carb_g = $nutri_values["glucides_r"];
2022-09-03 16:56:16 +02:00
// Dont sucres.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_sugar_g = $nutri_values["sucre_r"];
2022-09-03 16:56:16 +02:00
// Fibres alimentaires.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_fiber_g = $nutri_values["fib_alim_r"];
2022-09-03 16:56:16 +02:00
// Protéines.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_protein_g = $nutri_values["proteine_r"];
2022-09-03 16:56:16 +02:00
// Sel.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_salt_g = $nutri_values["sel_r"];
2022-09-03 16:56:16 +02:00
/** Autres valeurs dispos
* $nutri_values["sodium_r"];
* $nutri_values["ener_e14_r"];
* $nutri_values["fruits_r"];
* $nutri_values["fruits_coque_sech_r"];
* $nutri_values["ener_kjo_r"];
* $nutri_values["fatsat_r"];
* $nutri_values["choval_r"];
* $nutri_values["sugar_r"];
* $nutri_values["fibtg_r"];
* $nutri_values["pro_r"];
* $nutri_values["salteq_r"];
*/
// Valeurs nutritionnelles par portion.
// Taille portion en grammes.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_serving_size_g = $nutri_values["taille_port"];
2022-09-03 16:56:16 +02:00
// Energie Kj.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_serving_kj = $nutri_values["val_energ_kj_p"];
2022-09-03 16:56:16 +02:00
// Energie Kcal.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_serving_kcal = $nutri_values["val_energ_kcal_p"];
2022-09-03 16:56:16 +02:00
// Graisses.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_serving_fat_g = $nutri_values["mat_grass_p"];
2022-09-03 16:56:16 +02:00
// Dont acides gras saturés.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_serving_sat_fat_g = $nutri_values["ags_p"];
2022-09-03 16:56:16 +02:00
// Glucides.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_serving_carb_g = $nutri_values["glucides_p"];
2022-09-03 16:56:16 +02:00
// Dont sucres.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_serving_sugar_g = $nutri_values["sucre_p"];
2022-09-03 16:56:16 +02:00
// Fibres alimentaires.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_serving_fiber_g = $nutri_values["fib_alim_p"];
2022-09-03 16:56:16 +02:00
// Protéines.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_serving_protein_g = $nutri_values["proteine_p"];
2022-09-03 16:56:16 +02:00
// Sel.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_serving_salt_g = $nutri_values["sel_p"];
2022-09-03 16:56:16 +02:00
/**
* autres valeurs disponibles
* $nutri_values["sodium_p"];
* $nutri_values["fruits_p"];
*/
// Apports journaliers.
// Energie % recommandé.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_daily_energy = $nutri_values["pourcent_apport_ref_kjo_r"];
2022-09-03 16:56:16 +02:00
// Graisses.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_daily_fat = $nutri_values["pourcent_apport_ref_fat_r"];
2022-09-03 16:56:16 +02:00
// Dont acides gras saturés.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_daily_sat_fat = $nutri_values["pourcent_apport_ref_fatsat_r"];
2022-09-03 16:56:16 +02:00
// Glucides.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_daily_carb = $nutri_values["pourcent_apport_ref_choval_r"];
2022-09-03 16:56:16 +02:00
// Dont sucres.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_daily_sugar = $nutri_values["pourcent_apport_ref_sugar_r"];
2022-09-03 16:56:16 +02:00
// Fibres alimentaires.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_daily_fiber = $nutri_values["pourcent_apport_ref_fibtg_r"];
2022-09-03 16:56:16 +02:00
// Protéines.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_daily_protein = $nutri_values["pourcent_apport_ref_pro_r"];
2022-09-03 16:56:16 +02:00
// Sel.
2022-08-25 12:26:43 +02:00
$foodProduct->nv_daily_salt = $nutri_values["pourcent_apport_ref_salteq_r"];
2021-04-26 12:16:17 +02:00
2022-09-03 16:56:16 +02:00
// Autre valeur dispo $nutri_values["pourcent_apport_ref_e14_r"] .
// Nutriscore.
$foodProduct->nutriscore = $nutri_values["classement"];
2021-04-26 12:16:17 +02:00
2022-09-03 16:56:16 +02:00
/**
* Infos n'ayant pas de mapping
*
* $nutri_values["val_energ_kj_nut"];
* $nutri_values["val_energ_kcal_nut"];
* $nutri_values["mat_grass_nut"];
* $nutri_values["ags_nut"];
* $nutri_values["glucides_nut"];
* $nutri_values["sucre_nut"];
* $nutri_values["sucre_r"];
* $nutri_values["fib_alim_nut"];
* $nutri_values["proteine_nut"];
* $nutri_values["sel_nut"];
* $nutri_values["apport_ref"];
* $nutri_values["fruits_coque_sech_p"];
* $nutri_values["ener_e14_p"];
* $nutri_values["pourcent_apport_ref_e14_p"];
* $nutri_values["ener_kjo_p"];
* $nutri_values["pourcent_apport_ref_kjo_p"];
* $nutri_values["fat_p"];
* $nutri_values["pourcent_apport_ref_fat_p"];
* $nutri_values["fatsat_p"];
* $nutri_values["pourcent_apport_ref_fatsat_p"];
* $nutri_values["choval_p"];
* $nutri_values["pourcent_apport_ref_choval_p"];
* $nutri_values["sugar_p"];
* $nutri_values["pourcent_apport_ref_sugar_p"];
* $nutri_values["fibtg_p"];
* $nutri_values["pourcent_apport_ref_fibtg_p"];
* $nutri_values["pro_p"];
* $nutri_values["pourcent_apport_ref_pro_p"];
* $nutri_values["salteq_p"];
* $nutri_values["pourcent_apport_ref_salteq_p"];
* $nutri_values["apport_port"];
* $nutri_values["part_p"];
* $nutri_values["part_g"];
* $nutri_values["part_l"];
* $nutri_values["p_sur_l"];
* $nutri_values["pourcent_ags"];
* $nutri_values["pourcent_agi"];
* $nutri_values["pourcent_ss"];
* $nutri_values["pourcent_amidon"];
* $nutri_values["composante_n"];
* $nutri_values["composante_p"];
* $nutri_values["pourcent_fruits"];
* $nutri_values["mat_grass_r_prec"];
* $nutri_values["ags_r_prec"];
* $nutri_values["glucides_r_prec"];
* $nutri_values["sucre_r_prec"];
* $nutri_values["fib_alim_r_prec"];
* $nutri_values["proteine_r_prec"];
* $nutri_values["sel_r_prec"];
* $nutri_values["sodium_r_prec"];
* $nutri_values["fruits_r_prec"];
* $nutri_values["fruits_coque_sech_r_prec"];
* $nutri_values["score_nutri"];
*/
2022-08-25 12:26:43 +02:00
}//end if
2021-04-26 12:16:17 +02:00
$foodProduct->save();
2022-08-25 12:26:43 +02:00
}//end if
2021-04-26 12:16:17 +02:00
}
2021-04-21 11:06:40 +02:00
}