src/Controller/Content/ImportExportController.php line 97

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Content;
  3. use App\Entity\Article;
  4. use App\Entity\Page;
  5. use App\Entity\Projet;
  6. use App\Entity\UserRole;
  7. use App\Service\ArticleCSVService;
  8. use App\Service\CsvService;
  9. use App\Service\FormulaireCSVService;
  10. use App\Service\PageCSVService;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  13. use Flasher\SweetAlert\Prime\SweetAlertFactory;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class ImportExportController extends AbstractController
  19. {
  20.     private CsvService $csvService;
  21.     private PageCSVService $pageCsvService;
  22.     private FormulaireCSVService $formCsvService;
  23.     private ArticleCSVService $articleCsvService;
  24.     private ManagerRegistry $manager;
  25.     private SweetAlertFactory $flasher;
  26.     public function __construct(
  27.         CsvService $csvService,
  28.         ManagerRegistry $manager,
  29.         AdminUrlGenerator $adminUrlGenerator,
  30.         SweetAlertFactory $flasher,
  31.         PageCSVService $pageCsvService,
  32.         FormulaireCSVService $formCsvService,
  33.         ArticleCSVService $articleCsvService
  34.     ) {
  35.         $this->csvService $csvService;
  36.         $this->manager $manager;
  37.         $this->adminUrlGenerator $adminUrlGenerator;
  38.         $this->flasher $flasher;
  39.         $this->pageCsvService $pageCsvService;
  40.         $this->articleCsvService $articleCsvService;
  41.         $this->formCsvService $formCsvService;
  42.     }
  43.     /**
  44.      * @Route("/import/export", name="app_import_export")
  45.      */
  46.     public function index(): Response
  47.     {
  48.         return $this->render('import_export/index.html.twig', [
  49.             'controller_name' => 'ImportExportController',
  50.             'user' => $this->getUser(),
  51.             'roles' => $this->recherche(),
  52.         ]);
  53.     }
  54.      
  55.     public function recherche(): ?array
  56.     {
  57.         // $groupe = new Groupe();
  58.         $idProjet $this->getUser()->getProjet();
  59.         $idUser $this->getUser();
  60.         $roles $this->manager->getRepository(UserRole::class)->findBy(['user' => $idUser'projet' => $idProjet]);
  61.         return $roles;
  62.         //dd($groupe->getLabel());
  63.     }
  64.     /**
  65.      * @Route("/import/all", name="app_import_all")
  66.      */
  67.     public function importAll(Request $request)
  68.     {
  69.         $projet $this->manager->getRepository(Projet::class)->findOneBy(
  70.             ['id' => $this->getUser()->getProjet()->getId()]
  71.         );
  72.         $fileToUpload $request->get('mfileToUpload');
  73.         $nameFileToUpload $request->get('mfileNameToUpload');
  74.         $resultImport $this->csvService->importAll($fileToUpload$nameFileToUpload$projet);
  75.         //dd($resultImport);
  76.         if ($resultImport) {
  77.             $this->flasher->addSuccess('La liste des redactions du projet ' $projet ' a été importée avec succès');
  78.         } else {
  79.             $this->flasher->addError('vous avez choisi le mauvais fichier');
  80.         }
  81.         return $this->redirect($request->headers->get('referer'));
  82.     }
  83.     /**
  84.      * @Route("/export/all", name="app_export_all")
  85.      */
  86.     public function exportAll(Request $request)
  87.     {
  88.         $projet $this->manager->getRepository(Projet::class)->findOneBy(
  89.             ['id' => $this->getUser()->getProjet()->getId()]
  90.         );
  91.         $resExport $this->csvService->exportAll($projet);
  92.         if ($resExport == 'probleme ftp') {
  93.             $this->flasher->addError('Probleme FTP verifier les parametrages');
  94.         } else if ($resExport == 'ok') {
  95.             $this->flasher->addSuccess('Tout Exporter avec succès');
  96.         } else {
  97.             $this->flasher->addError('Quelque chose s\'est mal passé, Essayez de fermer les fichiers CSV ouverts');
  98.         }
  99.         return $this->redirect($request->headers->get('referer'));
  100.     }
  101.     /**
  102.      * @Route("/import/page", name="app_import_page")
  103.      */
  104.     public function importPage(Request $request)
  105.     {
  106.         $projet $this->manager->getRepository(Projet::class)->findOneBy(
  107.             ['id' => $this->getUser()->getProjet()->getId()]
  108.         );
  109.         $fileToUpload $request->get('mfileToUpload');
  110.         $nameFileToUpload $request->get('mfileNameToUpload');
  111.         $resultImport $this->pageCsvService->importPage($fileToUpload$nameFileToUpload$projet->getId());
  112.         //dd($resultImport);
  113.         if ($resultImport) {
  114.             $this->flasher->addSuccess('La liste des pages du projet ' $projet ' a été importée avec succès');
  115.         } else {
  116.             $this->flasher->addError('vous avez choisi le mauvais fichier');
  117.         }
  118.         return $this->redirect($request->headers->get('referer'));
  119.     }
  120.     /**
  121.      * @Route("/export/page", name="app_export_page")
  122.      */
  123.     public function exportPage(Request $request)
  124.     {
  125.         $projet $this->manager->getRepository(Projet::class)->findOneBy(
  126.             ['id' => $this->getUser()->getProjet()->getId()]
  127.         );
  128.         $res $this->pageCsvService->exportPage($projet);
  129.         if ($res == 'probleme ftp') {
  130.             $this->flasher->addError('Probleme FTP verifier les parametrages');
  131.         } else if ($res == 'ok') {
  132.             $this->flasher->addSuccess('La liste des pages du projet ' $projet->getProjet() . ' a été exportée avec succès');
  133.         } else {
  134.             $this->flasher->addError('Quelque chose s\'est mal passé, Essayez de fermer les fichiers CSV ouverts');
  135.         }
  136.         return $this->redirect($request->headers->get('referer'));
  137.     }
  138.     /**
  139.      * @Route("/import/article", name="app_import_article")
  140.      */
  141.     public function importArticle(Request $request)
  142.     {
  143.         $projet $this->manager->getRepository(Projet::class)->findOneBy(
  144.             ['id' => $this->getUser()->getProjet()->getId()]
  145.         );
  146.         $fileToUpload $request->get('mfileToUpload');
  147.         $nameFileToUpload $request->get('mfileNameToUpload');
  148.         $resultImport $this->articleCsvService->importAllArticles($fileToUpload$nameFileToUpload$projet);
  149.         //dd($resultImport);
  150.         if ($resultImport) {
  151.             $this->flasher->addSuccess('La liste des redactions du projet ' $projet ' a été importée avec succès');
  152.         } else {
  153.             $this->flasher->addError('vous avez choisi le mauvais fichier');
  154.         }
  155.         return $this->redirect($request->headers->get('referer'));
  156.     }
  157.     /**
  158.      * @Route("/export/article", name="app_export_article")
  159.      */
  160.     public function exportArticle(Request $request)
  161.     {
  162.         $projet $this->manager->getRepository(Projet::class)->findOneBy(
  163.             ['id' => $this->getUser()->getProjet()->getId()]
  164.         );
  165.         $res $this->articleCsvService->exportArticleType($projet);
  166.         if ($res == 'probleme ftp') {
  167.             $this->flasher->addError('Probleme FTP verifier les parametrages');
  168.         } else if ($res == 'ok') {
  169.             $this->flasher->addSuccess('La liste des redaction ' $projet->getProjet() . ' a été exportée avec succès');
  170.         } else {
  171.             $this->flasher->addError('Quelque chose s\'est mal passé, Essayez de fermer les fichiers CSV ouverts');
  172.         }
  173.         return $this->redirect($request->headers->get('referer'));
  174.     }
  175.     /**
  176.      * @Route("/import/formulaire", name="app_import_formulaire")
  177.      */
  178.     public function importFormulaire(Request $request)
  179.     {
  180.         $projet $this->manager->getRepository(Projet::class)->findOneBy(
  181.             ['id' => $this->getUser()->getProjet()->getId()]
  182.         );
  183.         $fileToUpload $request->get('mfileToUpload');
  184.         $nameFileToUpload $request->get('mfileNameToUpload');
  185.         $resultImport $this->formCsvService->importFormulaire($fileToUpload$nameFileToUpload$projet->getId());
  186.         //dd($resultImport);
  187.         if ($resultImport) {
  188.             $this->flasher->addSuccess('La liste des formulaires du projet ' $projet ' a été importée avec succès');
  189.         } else {
  190.             $this->flasher->addError('vous avez choisi le mauvais fichier');
  191.         }
  192.         return $this->redirect($request->headers->get('referer'));
  193.     }
  194.     /**
  195.      * @Route("/export/formulaire", name="app_export_formulaire")
  196.      */
  197.     public function exportFormulaire(Request $request)
  198.     {
  199.         $projet $this->manager->getRepository(Projet::class)->findOneBy(
  200.             ['id' => $this->getUser()->getProjet()->getId()]
  201.         );
  202.         $res $this->formCsvService->exportFormulaire($projet);
  203.         if ($res == 'probleme ftp') {
  204.             $this->flasher->addError('Probleme FTP verifier les parametrages');
  205.         } else if ($res == 'ok') {
  206.             $this->flasher->addSuccess('La liste des formulaires du projet ' $projet->getProjet() . ' a été exportée avec succès');
  207.         } else {
  208.             
  209.             $this->flasher->addError('Quelque chose s\'est mal passé, Essayez de fermer les fichiers CSV ouverts');
  210.         }
  211.         return $this->redirect($request->headers->get('referer'));
  212.     }
  213. }