← Back
Editing: MainController.php
<?php namespace App\Controller; use App\Entity\Adresse; use App\Model\Importation; use App\Session\Filter\Role\ManagerSessionFilters; use App\Session\Filter\SessionFiltersPersister; use App\Session\Filter\ValueObject\DateRange; use Doctrine\ORM\EntityManagerInterface; use PhpOffice\PhpSpreadsheet\IOFactory; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; use Symfony\Component\Routing\Annotation\Route; class MainController extends AbstractController { /** * @return Response */ #[Route(path: '/main', name: 'main')] public function index() { return $this->render( 'main/index.html.twig', [ 'controller_name' => 'MainController', ] ); } /** * @return RedirectResponse|Response * * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception */ #[Route(path: '/import-adresse', name: 'import_adresse')] public function importAdresse(Request $request, EntityManagerInterface $em, FlashBagInterface $flashBag) { $importation = new Importation(); $form = $this->createForm(FormType::class, $importation) ->add('fichier', FileType::class, ['label' => 'Sélectionnez votre fichier d\'importation (Excel)']) ->add('save', SubmitType::class, ['label' => 'Importer']); $form->handleRequest($request); if ($request->isMethod('POST')) { $file = $importation->getFichier(); // $reader = new IOFactory(); $inputFileType = IOFactory::identify($file); $reader = IOFactory::createReader($inputFileType); $reader->setReadDataOnly(true); $spreadsheet = $reader->load($file); $worksheet = $spreadsheet->getActiveSheet(); foreach ($worksheet->getRowIterator() as $key => $row) { $adresse = new Adresse(); $cellIterator = $row->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(false); $key = 0; foreach ($cellIterator as $cell) { switch ($key) { case 0: if (null === $cell->getValue()) { break; } else { $adresse->setVoie($cell->getValue()); break; } // no break case 1: if (null === $cell->getValue()) { break; } else { $adresse->setNumero($cell->getValue()); break; } // no break case 2: if (null !== $adresse->setRepetition($cell)) { $adresse->setRepetition($cell); } break; case 3: if (null === $adresse->setBatiment($cell)) { break; } else { $adresse->setBatiment($cell); break; } break; case 4: if (null === $adresse->setCodePostal($cell)) { break; } else { $adresse->setCodePostal($cell); break; } // no break case 5: if (null === $adresse->setVille($cell)) { break; } else { $adresse->setVille($cell); break; } // no break default: $flashBag->add( 'error', 'Importation annulée. Vérifiez qu\'aucunes lignes ne contient de données en trop' ); return $this->render( 'main/import-adresse.html.twig', [ 'form' => $form->createView(), ] ); break; } ++$key; } $em->persist($adresse); } $em->flush(); return $this->redirectToRoute('adresse_index'); } return $this->render( 'main/import-adresse.html.twig', [ 'form' => $form->createView(), ] ); } /** * @return JsonResponse * * @throws \Exception */ #[Route(path: 'change-dates.html', name: 'change_dates')] public function changeDates( Request $request, ManagerSessionFilters $filters, SessionFiltersPersister $sessionFiltersPersister ) { $data = $request->request->all(); $filters->setDateRange(new DateRange(new \DateTime($data['startDate']), new \DateTime($data['endDate']))); ($sessionFiltersPersister)($filters); return $this->json(['status' => 'OK']); } }
Save File
Cancel