authorizeResource(Planning::class); } public function import() { $this->authorize('import', Planning::class); View::share('modelsClasses', CrudController::getCrudModels()); return view('back.plannings.import'); } public function importSubmit(ImportPlanningXlsFile $request) { $this->authorize('import', Planning::class); /** @var \App\Models\User */ $user = auth()->user(); /** @var \App\Models\Site */ $site = Site::where('id', $user->selected_site_id)->firstOrFail(); $planningImport = null; try { try { $planningImport = new PlanningImport($site, (new HeadingRowImport())->toArray( $request->file, null, LExcel::XLS )); Excel::import( $planningImport, $request->file, null, LExcel::XLS ); } catch (ReaderException $e) { // Assert This is XML $xmlString = $this->filterXml($request->file); // create ne temp file $stream = tmpfile(); fwrite($stream, $xmlString); $file = new UploadedFile( stream_get_meta_data($stream)['uri'], 'xml data', 'application/vnd.ms-excel' ); $planningImport = new PlanningImport($site, (new HeadingRowImport())->toArray( $file, null, LExcel::XML )); Excel::import( $planningImport, $file, null, LExcel::XML ); \fclose($stream); } } catch (ReaderException $e) { // this time cannot read anything return redirect()->back()->withErrors( // phpcs:ignore Generic.Files.LineLength.TooLong 'Echec lors de l\'import le fichier doit être conforme à celui fourni lors du developement de l\'application' ); } catch (Exception $e) { Log::error($e->getMessage()); } finally { $r = redirect()->route('bo.plannings.import'); if ($planningImport) { $r = $r->with('success', sprintf( "%d plannings ont été importés \n Les plannings suivant n'ont pu être importés \n %s", $planningImport->importedCount, collect($planningImport->importedFailed)->map(function ($d) { return $d->implode(','); })->implode("\n") )); } else { $r = $r->with('error', __('Aucun planning importé, erreur inconnue')); } return $r; } } /** * Le parser Xml ne supporte pas la lecture de cellule * dont le contenu est encapsulé avec du style... (toujours plus) * Donc on va essayer de virer ceux-ci * * NOTE: Conforme au fichier transmis par Ossan et gitté dans resources/documents à cette date * eg : * CHUPE JEAN CHARLES SSsT * * @param UploadedFile $file * @return string */ private function filterXml(UploadedFile $file): string { $xmlString = $file->get(); $xmlString = str_replace( [ '', '', '', '', '', '', '', '', //phpcs:ignore Generic.Files.LineLength.TooLong '', //phpcs:ignore Generic.Files.LineLength.TooLong "", "\n", '', '' ], '', $xmlString ); return $xmlString; } // FO public function showPlannings() { /** @var \App\Models\Employee */ $employee = auth()->user(); $plannings = $employee->getWeekAndNextPlannings(); return view('front.employees.plannings', compact('plannings')); } }