src/Controller/Import/ImportController.php line 23
<?phpnamespace App\Controller\Import;use App\Entity\Document;use App\Entity\DocxGenerated;use App\Entity\DocxUploaded;use App\Form\DocxUploadType;use App\Services\Docx;use App\Services\FileUpload;use Doctrine\ORM\EntityManagerInterface;use Doctrine\Persistence\ManagerRegistry;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;class ImportController extends AbstractController{#[Route('/import', name: 'import_index')]public function index(Request $request, FileUpload $fileUpload, ManagerRegistry $doctrine): Response{// upload form$docx = new Document();$form = $this->createForm(DocxUploadType::class, $docx, ["setData" => true]);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {/** @var Document $docx */$docx = $form->getData();$em = $doctrine->getManager();$uploaded = $form['tmpFile']->getData();$docFile = $fileUpload->save($uploaded, 'docxTpl');if($docFile) {$em->persist($docFile);$docx->setFile($docFile);$docx->setOwner($this->getUser());$docx->setDateCreated(new \DateTime());$docx->setDateUpdated(new \DateTime());$docx->setVersion(new \DateTime());$docx->setVersionOf($docx);$docx->setType(Document::TYPE_DOCX);$em->persist($docx);$em->flush();return $this->redirectToRoute('import_edit', ["id" => $docx->getId()]);}return $this->redirectToRoute('import_index');}//$documents = $doctrine->getRepository(DocxUploaded::class)->findAll();//$documents = $doctrine->getRepository(Document::class)->findAllLastVersion($this->getUser(), Document::TYPE_DOCX);$documents = $this->getUser() ? $doctrine->getRepository(Document::class)->findBy(["owner" => $this->getUser(), "isLastVersion" => true, "type" => Document::TYPE_DOCX], ["dateUpdated" => "DESC"]) : null;return $this->render("import/index.html.twig", ["docxupload" => $form->createView(),"documents" => $documents]);}#[Route('/import/edit/{id}', name: 'import_edit')]public function edit(Request $request, $id, ManagerRegistry $doctrine, Docx $docx, FileUpload $fileUpload): Response{$em = $doctrine->getManager();/** @var Document $document */$document = $doctrine->getManager()->getRepository(Document::class)->find($id);$parent= $document->getVersionOf();$generatedDocs = $doctrine->getManager()->getRepository(DocxGenerated::class)->findBy(["fromDocx" => $document]);$tags = $docx->getTags($document);// update doc information form$updateForm = $this->createForm(DocxUploadType::class, $parent, ["updateData" => true, "setData" => true]);$updateForm->handleRequest($request);if($updateForm->isSubmitted() && $updateForm->isValid()) {$em->persist($parent);$em->flush();return $this->redirectToRoute('import_edit', ["id" => $document->getId()]);}$versions = $doctrine->getManager()->getRepository(Document::class)->findBy(["versionOf" => $document->getVersionOf()]);// upload version form$form = $this->createForm(DocxUploadType::class, null, ["versionOf" => $document->getVersionOf()->getId()]);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$uploaded = $form['tmpFile']->getData();$docFile = $fileUpload->save($uploaded, 'docxTpl');if($docFile) {$em->persist($docFile);$docxfile = new DocxUploaded();$docxfile->setFile($docFile);$docxfile->setReference($document->getReference());$em->persist($docxfile);$em->flush();return $this->redirectToRoute('import_edit', ["id" => $docxfile->getId()]);}return $this->redirectToRoute('import_edit', ["id" => $document->getId()]);}// field upload versionif(isset($_POST['vars'])) {$docx->generateFromTemplate($document);return $this->redirectToRoute('import_edit', ["id" => $document->getId()]);}return $this->render("import/edit.html.twig", ["docxupload" => $form->createView(),"docxupdate" => $updateForm->createView(),"document" => $document,"versions" => $versions,"generatedDocs" => $generatedDocs,"tags" => $tags,"varnames" => trim(implode(",", $tags), ",")]);}#[Route('/import/delete/{id}', name: 'import_delete')]public function delete(EntityManagerInterface $em, ParameterBagInterface $params, $id){/** @var Document $uploaded */$uploaded = $em->getRepository(Document::class)->find($id);$versionOf = $uploaded->getVersionOf();$uploadeds = $em->getRepository(Document::class)->findBy(["versionOf" => $versionOf]);/** @var Document $u */foreach ($uploadeds as $u) {$u->setVersionOf(null);$em->persist($u);}$em->flush();/** @var Document $u */foreach ($uploadeds as $u) {$u->setVersionOf(null);$file = $u->getFile();if(file_exists($params->get('file_upload_dir') . '/docxTpl/'.$file->getName()))unlink($params->get('file_upload_dir') . '/docxTpl/'.$file->getName());$em->remove($u);$em->remove($file);}$em->flush();return $this->redirectToRoute('import_index');}#[Route('/import/deletegenerated/{id}', name: 'import_deletegenerated')]public function deletegenerated(EntityManagerInterface $em, ParameterBagInterface $params, $id){/** @var DocxGenerated $generated */$generated = $em->getRepository(DocxGenerated::class)->find($id);$from = $generated->getFromDocx()->getId();$file = $generated->getFile();unlink($params->get('file_upload_dir') . '/docxGenerated/'.$file->getName());$em->remove($generated);$em->remove($file);$em->flush();return $this->redirectToRoute('import_edit', ["id" => $from]);}}