info('Démarrage du traitement'); $files = File::glob(storage_path('export_arcadia') . '/DAM_MEDIA_VISUEL_*.*'); $this->line("-> Import du fichier DAM_MEDIA_VISUEL"); $this->xmlVisuels($files); $this->associateMediasArticles($files); $this->associateMediasMarques($files); $this->associateMediasSocietes($files); $this->line(""); $this->info('Fin du traitement'); } /** * Traitement de l'association media <-> societe * * @param array $files * @return void * @throws \ErrorException If a xml file could not be parsed. * @throws \League\Flysystem\FileNotFoundException If a file could not be read. */ private function associateMediasSocietes(array $files): void { $files = File::glob(storage_path('export_arcadia') . "/DAM_ASSO_SOCIETE_*.*"); $this->line("-> Import du fichier DAM_ASSO_SOCIETE"); foreach ($files as $filename) { $path = explode("/", $filename); $filename = $path[count($path) - 1]; // ---- Récupération du contenu if (!($oneFile = Storage::disk('arcadia')->get($filename))) { throw new FileNotFoundException($filename); } if (!($fileContent = simplexml_load_string($oneFile))) { throw new \ErrorException("Could not parse $filename from arcadia storage."); } // ---- traitement du fichier $bar = $this->output->createProgressBar(count($fileContent->mediaassosociete) - 1); $bar->start(); /** @var \SimpleXMLElement $row */ foreach ($fileContent->mediaassosociete as $row) { /** @var \App\Models\Media|null */ $record = Media::where('arcadia_id', $row->codeVisuel)->first(); if ($record) { // * attache la societe au média $record->societes()->attach($row->codSociete); } else { $this->line("\nDAM_ASSO_SOCIETE : le media n°$row->codeVisuel n'existe pas"); } $bar->advance(); } $bar->finish(); $this->deleteFile($filename); } //end foreach } /** * Traitement de l'association media <-> marque * * @param array $files * @return void * @throws \ErrorException If a xml file could not be parsed. * @throws \League\Flysystem\FileNotFoundException If a file could not be read. */ private function associateMediasMarques(array $files): void { $files = File::glob(storage_path('export_arcadia') . "/DAM_ASSO_MARQUE_*.*"); $this->line("-> Import du fichier DAM_ASSO_MARQUE"); foreach ($files as $filename) { $path = explode("/", $filename); $filename = $path[count($path) - 1]; // ---- Récupération du contenu if (!($oneFile = Storage::disk('arcadia')->get($filename))) { throw new FileNotFoundException($filename); } if (!($fileContent = simplexml_load_string($oneFile))) { throw new \ErrorException("Could not parse $filename from arcadia storage."); } // ---- traitement du fichier $bar = $this->output->createProgressBar(count($fileContent->mediaassomarque) - 1); $bar->start(); /** @var \SimpleXMLElement $row */ foreach ($fileContent->mediaassomarque as $row) { /** @var \App\Models\Media|null */ $record = Media::where('arcadia_id', $row->codeVisuel)->first(); if ($record) { if ($record->marques->contains($row->codMarq)) { $this->line("\nDAM_ASSO_MARQUE : le media n°$row->codeVisuel est déjà associé à cette marque."); } else { // * attache la marque au média $record->marques()->attach($row->codMarq); } } else { $this->line("\nDAM_ASSO_MARQUE : le media n°$row->codeVisuel n'existe pas"); } $bar->advance(); } $bar->finish(); $this->deleteFile($filename); } //end foreach } /** * Traitement de l'association media <-> article * * @param array $files * @return void * @throws \ErrorException If a xml file could not be parsed. * @throws \Illuminate\Database\Eloquent\ModelNotFoundException If a model cant be found on transaction. * @throws \League\Flysystem\FileNotFoundException If a file could not be read. * @throws \Throwable If saving fails. * @phpcs:disable Generic.Metrics.NestingLevel.TooHigh * @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber */ private function associateMediasArticles(array $files): void { $files = File::glob(storage_path('export_arcadia') . "/DAM_ASSO_ARTICLE_*.*"); $this->line("-> Import du fichier DAM_ASSO_ARTICLE"); /** @var string $filename */ foreach ($files as $filename) { $path = explode("/", $filename); $filename = $path[count($path) - 1]; // ---- Récupération du contenu if (!($oneFile = Storage::disk('arcadia')->get($filename))) { throw new FileNotFoundException($filename); } if (!($fileContent = simplexml_load_string($oneFile))) { throw new \ErrorException("Could not parse $filename from arcadia storage."); } // ---- traitement du fichier $bar = $this->output->createProgressBar(count($fileContent->mediaassoarticle) - 1); $bar->start(); /** @var \SimpleXMLElement $row */ foreach ($fileContent->mediaassoarticle as $row) { /** @var \App\Models\Media|null */ $record = Media::where('arcadia_id', $row->codeVisuel)->first(); if ($record) { /** @var \App\Models\Article|null */ $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; /** @var \App\Models\Dossier|null */ $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 { DB::transaction(function () use ($record, $marque_id) { /** @var \App\Models\Marque */ $marque = Marque::where('codMarq', '=', $marque_id)->firstOrFail(); // * On cree le dossier $dossier = new Dossier(); $dossier->libelle = $marque->libMarq; $dossier->sujet_id = 1; $dossier->system = false; $dossier->saveOrFail(); $dossier->marques()->syncWithoutDetaching($marque_id); $dossier->saveOrFail(); $record->dossier_id = $dossier->id; $record->saveOrFail(); }); } //end if } else { // phpcs:ignore Generic.Files.LineLength.TooLong $this->line("\nDAM_ASSO_ARTICLE : le media n°$row->codeVisuel ne peut être lié à l'article n°$row->noArt"); } //end if } else { $this->line("\nDAM_ASSO_ARTICLE : le media n°$row->codeVisuel n'existe pas"); } //end if $bar->advance(); } //end foreach $bar->finish(); $this->deleteFile($filename); } //end foreach } /** * Traitement de l'import des XML Visuel * * @param array $files * @return void * @throws \League\Flysystem\FileNotFoundException If a file could not be read. * @throws \ErrorException If a xml file could not be parsed. */ private function xmlVisuels(array $files): void { foreach ($files as $filename) { $path = explode("/", $filename); $filename = $path[count($path) - 1]; // ---- Récupération du contenu if (!($oneFile = Storage::disk('arcadia')->get($filename))) { throw new FileNotFoundException($filename); } if (!($fileContent = simplexml_load_string($oneFile))) { throw new \ErrorException("Could not parse $filename from arcadia storage."); } // ---- traitement du fichier $bar = $this->output->createProgressBar(count($fileContent->MEDIA_VISUEL) - 1); $bar->start(); /** @var \SimpleXMLElement $row */ foreach ($fileContent->MEDIA_VISUEL as $row) { /** @var \App\Models\Media|null */ $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; } else { $record->copyright_id = 3; } 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->getMediaFolder() . "/"; $record->folder = $destinationPath; $record->uid = Str::uuid(); $mediaFile = "/images/{$record->arcadia_id}.{$record->extension}"; if (Storage::disk('arcadia')->exists($mediaFile)) { // * déplacement du média et renommage du fichier if (!($fileContent = Storage::disk('arcadia')->get($mediaFile))) { throw new \ErrorException("Could not read $mediaFile from arcadia storage."); } // * According to config, should throw exception on failure. Storage::disk('local')->put( $fileContent, "{$destinationPath}{$record->uid}.$record->extension" ); // * calcul de la taille du média $record->taille = $this->formatBytes(Storage::disk('local') ->size("{$destinationPath}{$record->uid}.$record->extension")); } else { // * on applique une image par défaut $record->extension = "jpg"; Storage::disk('local')->copy( "assets/images/default.jpg", "{$destinationPath}{$record->uid}.{$record->extension}" ); // * calcul de la taille du média $record->taille = $this->formatBytes(Storage::disk('local') ->size("{$destinationPath}{$record->uid}.{$record->extension}")); } //end if $record->saveOrFail(); // ---- Creation des vignettes de base $this->createSysThumb($record); // ---- Creation des vignettes en lien avec le sujet du média $this->createThumb($record); } else { $this->line("\nDAM_MEDIA_VISUEL : le media n°$row->code existe déjà en base"); } //end if $bar->advance(); } //end foreach $bar->finish(); $this->deleteFile($filename); } //end foreach } /** * Delete file * * @param string $filename * @return void */ private function deleteFile(string $filename): void { if (!Storage::disk('arcadia')->delete($filename)) { Log::warning("Could not delete $filename from arcadia storage."); } } }