ldc-dam/app/Console/Commands/ImportMedia.php
2019-08-09 10:51:42 +02:00

246 lines
8 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use App\Http\Traits\MediaTools;
use App\Article;
use App\Marque;
use App\Media;
use App\Dossier;
class ImportMedia extends Command
{
use MediaTools;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'medias:import';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import des fichiers xml Média Visuel Arcadia';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Démarrage du traitement');
$files = Storage::disk('arcadia')->files();
$this->line("-> Import du fichier DAM_MEDIA_VISUEL");
// ---- Traitement de l'import des XML Visuel
foreach ($files as $filename) {
if($filename == ".gitignore") continue;
// ---- Récupération du contenu
$oneFile = Storage::disk('arcadia')->get($filename);
$fileContent = simplexml_load_string($oneFile);
// ---- traitement du fichier
if (substr($filename, 0, 16) == "DAM_MEDIA_VISUEL") {
foreach ($fileContent->MEDIA_VISUEL as $row) {
$record = Media::where('arcadia_id', $row->code)->first();
if(!$record){ // Insert new record
$record = new Media();
$record->arcadia_id = $row->code;
if($row->extension != ""){
$record->extension = $row->extension->typeFichier->libExtension;
}
$record->libelle = $row->libelle;
$record->largeur = $row->nbPxLargeur;
$record->largeur_mm = $row->largeurMm;
$record->hauteur = $row->nbPxHauteur;
$record->hauteur_mm = $row->hauteurMm;
$record->resolution = $row->pppLargeur;
if($row->version != ""){
$record->version = $row->version;
}else{
$record->version = 1;
}
if($row->codeTypeMedia != ""){
$record->typemedia_id = $row->codeTypeMedia;
}else{
$record->typemedia_id = 1;
}
if($row->codeTypeVue != ""){
$record->typevue_id = $row->codeTypeVue;
}
$record->commentaire = $row->commentaire;
$record->sujet_id = $row->codeSujet;
if($row->langue != ""){
$record->langue_id = $row->langue;
}
if($row->copyright != ""){
$record->copyright_id = $row->copyright;
}
if($row->codeVariante != ""){
$record->variante_id = $row->codeVariante;
}
if($row->dateCreation != ""){
$row->dateCreation = substr($row->dateCreation, 0, 10);
$date = explode("/", $row->dateCreation);
$record->created_at = $date[2].'-'.$date[1].'-'.$date[0];
}
if($row->dateModification != ""){
$row->dateModification = substr($row->dateModification, 0, 10);
$date = explode("/", $row->dateModification);
$record->updated_at = $date[2].'-'.$date[1].'-'.$date[0];
}
if($row->datePublication != ""){
$row->datePublication = substr($row->datePublication, 0, 10);
$date = explode("/", $row->datePublication);
$record->published_at = $date[2].'-'.$date[1].'-'.$date[0];
}
$record->statut = config('services.statut.public_etendu');
// Affectation d'un dossier et d'un uid
$destinationPath = $this->get_mediaFolder()."/";
$record->folder = $destinationPath;
$record->uid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
$mediaFile = "/images/".$record->arcadia_id.".".$record->extension;
$fileExists = Storage::disk('arcadia')->exists($mediaFile);
if($fileExists){
// déplacement du média et renommage du fichier
$file = Storage::disk('arcadia')->get($mediaFile);
$full_path_source = Storage::disk('arcadia')->getDriver()->getAdapter()->applyPathPrefix($mediaFile);
$full_path_dest = Storage::disk('local')->getDriver()->getAdapter()->applyPathPrefix($destinationPath.$record->uid. '.' .$record->extension);
File::move($full_path_source, $full_path_dest);
// calcul de la taille du média
$record->taille = $this->formatBytes($this->get_file_size($full_path_dest));
}else{
// on applique une image par défaut
$record->extension = "jpg";
Storage::disk('local')->copy("images/default.jpg", $destinationPath.$record->uid. '.' .$record->extension);
// calcul de la taille du média
$record->taille = $this->formatBytes($this->get_file_size("public/".$destinationPath.$record->uid. '.' .$record->extension));
}
$record->save();
// ---- Creation des vignettes de base
$this->create_sys_thumb($record);
// ---- Creation des vignettes en lien avec le sujet du média
$this->create_thumb($record);
}else{
$this->line("DAM_MEDIA_VISUEL : le media n°".$row->code. " existe déjà en base");
}
}
// ---- Suppression du fichier
Storage::disk('arcadia')->delete($filename);
}
}
// ---- Traitement de l'association media <-> article
$this->line("-> Import du fichier DAM_ASSO_ARTICLE");
foreach ($files as $filename) {
if($filename == ".gitignore") continue;
// ---- Récupération du contenu
$oneFile = Storage::disk('arcadia')->get($filename);
$fileContent = simplexml_load_string($oneFile);
// ---- traitement du fichier
if (substr($filename, 0, 16) == "DAM_ASSO_ARTICLE") {
foreach ($fileContent->mediaassoarticle as $row) {
$record = Media::where('arcadia_id', $row->codeVisuel)->first();
if($record){
$article = Article::where('noArt', $row->noArt)->first();
if($article){
// attache l'article au média
$record->articles()->syncWithoutDetaching ($article->id);
// recherche un dossier lié à la marque de l'article
$marque_id = $article->codMarq;
$dossier = Dossier::whereHas("marques", function($query) use ($marque_id){
$query->where("codMarq", $marque_id);
})->first();
if($dossier){
// attache le dossier marque au Média
$record->dossier_id = $dossier->id;
$record->save();
$this->line("Liason media n°".$row->codeVisuel. " à la marque n°".$article->libMarq);
}else{
$this->line("pas de liaison de marque : le media n°".$row->codeVisuel. " ne peut être lié à l'article n°".$row->noArt);
}
}else{
$this->line("DAM_ASSO_ARTICLE : le media n°".$row->codeVisuel. " ne peut être lié à l'article n°".$row->noArt);
}
}else{
$this->line("DAM_ASSO_ARTICLE : le media n°".$row->codeVisuel. " n'existe pas");
}
}
// ---- Suppression du fichier
Storage::disk('arcadia')->delete($filename);
}
}
// ---- Traitement de l'association media <-> marque
$this->line("-> Import du fichier DAM_ASSO_MARQUE");
foreach ($files as $filename) {
if($filename == ".gitignore") continue;
// ---- Récupération du contenu
$oneFile = Storage::disk('arcadia')->get($filename);
$fileContent = simplexml_load_string($oneFile);
// ---- traitement du fichier
if (substr($filename, 0, 15) == "DAM_ASSO_MARQUE") {
foreach ($fileContent->mediaassomarque as $row) {
$record = Media::where('arcadia_id', $row->codeVisuel)->first();
if($record){
// attache la marque au média
$record->marques()->attach($row->codMarq);
}else{
$this->line("DAM_ASSO_MARQUE : le media n°".$row->codeVisuel. " n'existe pas");
}
}
// ---- Suppression du fichier
Storage::disk('arcadia')->delete($filename);
}
}
// TODO : Import de l'association des sociétés
$this->line("");
$this->info('Fin du traitement');
}
}