src/Controller/Import/ImportController.php line 23

  1. <?php
  2. namespace App\Controller\Import;
  3. use App\Entity\Document;
  4. use App\Entity\DocxGenerated;
  5. use App\Entity\DocxUploaded;
  6. use App\Form\DocxUploadType;
  7. use App\Services\Docx;
  8. use App\Services\FileUpload;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. class ImportController extends AbstractController
  18. {
  19.     #[Route('/import'name'import_index')]
  20.     public function index(Request $requestFileUpload $fileUploadManagerRegistry $doctrine): Response
  21.     {
  22.         // upload form
  23.         $docx = new Document();
  24.         $form $this->createForm(DocxUploadType::class, $docx, ["setData" => true]);
  25.         $form->handleRequest($request);
  26.         if ($form->isSubmitted() && $form->isValid()) {
  27.             /** @var Document $docx */
  28.             $docx $form->getData();
  29.             $em $doctrine->getManager();
  30.             $uploaded $form['tmpFile']->getData();
  31.             $docFile $fileUpload->save($uploaded'docxTpl');
  32.             if($docFile) {
  33.                 $em->persist($docFile);
  34.                 $docx->setFile($docFile);
  35.                 $docx->setOwner($this->getUser());
  36.                 $docx->setDateCreated(new \DateTime());
  37.                 $docx->setDateUpdated(new \DateTime());
  38.                 $docx->setVersion(new \DateTime());
  39.                 $docx->setVersionOf($docx);
  40.                 $docx->setType(Document::TYPE_DOCX);
  41.                 $em->persist($docx);
  42.                 $em->flush();
  43.                 return $this->redirectToRoute('import_edit', ["id" => $docx->getId()]);
  44.             }
  45.             return $this->redirectToRoute('import_index');
  46.         }
  47.         //$documents = $doctrine->getRepository(DocxUploaded::class)->findAll();
  48.         //$documents = $doctrine->getRepository(Document::class)->findAllLastVersion($this->getUser(), Document::TYPE_DOCX);
  49.         $documents $this->getUser() ? $doctrine->getRepository(Document::class)->findBy(["owner" => $this->getUser(), "isLastVersion" => true"type" => Document::TYPE_DOCX], ["dateUpdated" => "DESC"]) : null;
  50.         return $this->render("import/index.html.twig", [
  51.             "docxupload" => $form->createView(),
  52.             "documents" => $documents
  53.         ]);
  54.     }
  55.     #[Route('/import/edit/{id}'name'import_edit')]
  56.     public function edit(Request $request$idManagerRegistry $doctrineDocx $docxFileUpload $fileUpload): Response
  57.     {
  58.         $em $doctrine->getManager();
  59.         /** @var Document $document */
  60.         $document $doctrine->getManager()->getRepository(Document::class)->find($id);
  61.         $parent$document->getVersionOf();
  62.         $generatedDocs $doctrine->getManager()->getRepository(DocxGenerated::class)->findBy(["fromDocx" => $document]);
  63.         $tags $docx->getTags($document);
  64.         // update doc information form
  65.         $updateForm $this->createForm(DocxUploadType::class, $parent, ["updateData" => true"setData" => true]);
  66.         $updateForm->handleRequest($request);
  67.         if($updateForm->isSubmitted() && $updateForm->isValid()) {
  68.             $em->persist($parent);
  69.             $em->flush();
  70.             return $this->redirectToRoute('import_edit', ["id" => $document->getId()]);
  71.         }
  72.         $versions $doctrine->getManager()->getRepository(Document::class)->findBy(["versionOf" => $document->getVersionOf()]);
  73.         // upload version form
  74.         $form $this->createForm(DocxUploadType::class, null, ["versionOf" => $document->getVersionOf()->getId()]);
  75.         $form->handleRequest($request);
  76.         if ($form->isSubmitted() && $form->isValid()) {
  77.             $uploaded $form['tmpFile']->getData();
  78.             $docFile $fileUpload->save($uploaded'docxTpl');
  79.             if($docFile) {
  80.                 $em->persist($docFile);
  81.                 $docxfile = new DocxUploaded();
  82.                 $docxfile->setFile($docFile);
  83.                 $docxfile->setReference($document->getReference());
  84.                 $em->persist($docxfile);
  85.                 $em->flush();
  86.                 return $this->redirectToRoute('import_edit', ["id" => $docxfile->getId()]);
  87.             }
  88.             return $this->redirectToRoute('import_edit', ["id" => $document->getId()]);
  89.         }
  90.         // field upload version
  91.         if(isset($_POST['vars'])) {
  92.             $docx->generateFromTemplate($document);
  93.             return $this->redirectToRoute('import_edit', ["id" => $document->getId()]);
  94.         }
  95.         return $this->render("import/edit.html.twig", [
  96.             "docxupload" => $form->createView(),
  97.             "docxupdate" => $updateForm->createView(),
  98.             "document" => $document,
  99.             "versions" => $versions,
  100.             "generatedDocs" => $generatedDocs,
  101.             "tags" => $tags,
  102.             "varnames" => trim(implode(","$tags), ",")
  103.         ]);
  104.     }
  105.     #[Route('/import/delete/{id}'name'import_delete')]
  106.     public function delete(EntityManagerInterface $emParameterBagInterface $params$id)
  107.     {
  108.         /** @var Document $uploaded */
  109.         $uploaded $em->getRepository(Document::class)->find($id);
  110.         $versionOf $uploaded->getVersionOf();
  111.         $uploadeds $em->getRepository(Document::class)->findBy(["versionOf" => $versionOf]);
  112.         /** @var Document $u */
  113.         foreach ($uploadeds as $u) {
  114.             $u->setVersionOf(null);
  115.             $em->persist($u);
  116.         }
  117.         $em->flush();
  118.         /** @var Document $u */
  119.         foreach ($uploadeds as $u) {
  120.             $u->setVersionOf(null);
  121.             $file $u->getFile();
  122.             if(file_exists($params->get('file_upload_dir') . '/docxTpl/'.$file->getName()))
  123.                 unlink($params->get('file_upload_dir') . '/docxTpl/'.$file->getName());
  124.             $em->remove($u);
  125.             $em->remove($file);
  126.         }
  127.         $em->flush();
  128.         return $this->redirectToRoute('import_index');
  129.     }
  130.     #[Route('/import/deletegenerated/{id}'name'import_deletegenerated')]
  131.     public function deletegenerated(EntityManagerInterface $emParameterBagInterface $params$id)
  132.     {
  133.         /** @var DocxGenerated $generated */
  134.         $generated $em->getRepository(DocxGenerated::class)->find($id);
  135.         $from $generated->getFromDocx()->getId();
  136.         $file $generated->getFile();
  137.         unlink($params->get('file_upload_dir') . '/docxGenerated/'.$file->getName());
  138.         $em->remove($generated);
  139.         $em->remove($file);
  140.         $em->flush();
  141.         return $this->redirectToRoute('import_edit', ["id" => $from]);
  142.     }
  143. }