src/Controller/Edition/EditionController.php line 38

  1. <?php
  2. namespace App\Controller\Edition;
  3. use App\Entity\Document\Block;
  4. use App\Entity\Document;
  5. use App\Entity\DocumentPermission;
  6. use App\Entity\User;
  7. use App\Form\NewDocType;
  8. use App\Repository\DocumentRepository;
  9. use App\Services\Html;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use PhpParser\Comment\Doc;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\HeaderUtils;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\StreamedResponse;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. class EditionController extends AbstractController
  20. {
  21.     #[Route('/edition'name'edition_index')]
  22.     public function index(EntityManagerInterface $em): Response
  23.     {
  24.         $documents $em->getRepository(Document::class)->findBy(["owner" => $this->getUser(), "type" => Document::TYPE_WEB"isLastVersion" => true]);
  25.         $templates $em->getRepository(Document::class)->findBy(["owner" => $this->getUser(), "type" => Document::TYPE_WEB"isLastVersion" => true"isTemplate" => true]);
  26.         $blocks $em->getRepository(Block::class)->findBy(["document" => null]);
  27.         return $this->render('edition/index.html.twig', [
  28.             "documents" => $documents,
  29.             "templates" => $templates,
  30.             "blocks" => $blocks
  31.         ]);
  32.     }
  33.     #[Route('/edition/new'name'edition_new')]
  34.     public function new(Request $requestEntityManagerInterface $em): Response
  35.     {
  36.         $document = new Document();
  37.         $form $this->createForm(NewDocType::class, $document);
  38.         $form->handleRequest($request);
  39.         if($form->isSubmitted() && $form->isValid()) {
  40.             /** @var Document $document */
  41.             $document $form->getData();
  42.             $document->setOwner($this->getUser());
  43.             $document->setDateCreated(new \DateTime());
  44.             $document->setDateUpdated(new \DateTime());
  45.             $document->setVersion(new \DateTime());
  46.             $document->setType(Document::TYPE_WEB);
  47.             $document->setVersionOf($document);
  48.             $document->setIsLastVersion(true);
  49.             $em->persist($document);
  50.             $em->flush();
  51.             return $this->redirectToRoute('edition_edit', ["id" => $document->getId()]);
  52.         }
  53.         return $this->render('edition/new.html.twig', [
  54.             'form' => $form->createView()
  55.         ]);
  56.     }
  57.     #[Route('/edition/newfromtemplate/{id}'name'edition_newfromtemplate')]
  58.     public function newfromtemplate(EntityManagerInterface $emDocument $template)
  59.     {
  60.         $document Document::clone($template);
  61.         $document->setTitle("Nouveau document (depuis le modèle " $document->getTitle() . ")");
  62.         $em->persist($document);
  63.         $document->setDateUpdated(new \DateTime());
  64.         $document->setDateCreated(new \DateTime());
  65.         $document->setVersion(new \DateTime());
  66.         $document->setType(Document::TYPE_WEB);
  67.         $document->setVersionOf($document);
  68.         $document->setIsLastVersion(true);
  69.         foreach($document->getBlocks() as $block)
  70.             $em->persist($block);
  71.         $em->flush();
  72.         return $this->redirectToRoute('edition_edit', ["id" => $document->getId()]);
  73.     }
  74.     #[Route('/edition/edit/{id}'name'edition_edit')]
  75.     public function edit(EntityManagerInterface $emDocument $document)
  76.     {
  77.         $this->denyAccessUnlessGranted('edit'$document);
  78.         $blocks $em->getRepository(Block::class)->findAll();
  79.         $blockcategories $em->getRepository(Document\BlockCategory::class)->findBy(["owner" =>$this->getUser()]);
  80.         $versions $em->getRepository(Document::class)->findBy(["versionOf" => $document->getVersionOf()], ["version" => "DESC"]);
  81.         return $this->render('edition/edit.html.twig', [
  82.             "blockcategories" => $blockcategories,
  83.             "blocks" => $blocks,
  84.             "document" => $document,
  85.             "versions" => $versions,
  86.             "users" => $em->getRepository(User::class)->findAll()
  87.         ]);
  88.     }
  89.     #[Route('/edition/editblock/{id}'name'edition_editblock')]
  90.     public function editblock(EntityManagerInterface $emBlock $block)
  91.     {
  92.         $categories $em->getRepository(Document\BlockCategory::class)->findBy(["owner" =>$this->getUser()]);
  93.         return $this->render('edition/editblock.html.twig', [
  94.             "block" => $block,
  95.             "blockcategories" => $categories,
  96.             "document" => null
  97.         ]);
  98.     }
  99.     #[Route('/edition/createblock'name'edition_createblock')]
  100.     public function createblock(EntityManagerInterface $em)
  101.     {
  102.         $categories $em->getRepository(Document\BlockCategory::class)->findBy(["owner" =>$this->getUser()]);
  103.         return $this->render('edition/editblock.html.twig', [
  104.             "block" => null,
  105.             "blockcategories" => $categories,
  106.             "document" => null
  107.         ]);
  108.     }
  109.     #[Route('/edition/test/{id}'name'edition_test')]
  110.     public function test(Document $document)
  111.     {
  112.         return $this->render('edition/edit.html.twig', [
  113.             "document" => $document
  114.         ]);
  115.     }
  116.     #[Route('/edition/delete/{id}'name'edition_delete')]
  117.     public function delete(EntityManagerInterface $emDocument $document)
  118.     {
  119.         /** @var DocumentRepository $drep */
  120.         $drep $em->getRepository(Document::class);
  121.         /** @var Document $lastVersion */
  122.         $lastVersion null;
  123.         if(!$document->isIsLastVersion())
  124.             $lastVersion $drep->findLastVersionOf($document);
  125.         $blocks $em->getRepository(Block::class)->findBy(["document" => $document]);
  126.         foreach ($blocks as $block) {
  127.             $em->remove($block);
  128.         }
  129.         $em->remove($document);
  130.         $em->flush();
  131.         if(!$lastVersion)
  132.             return $this->redirectToRoute('edition_index');
  133.         else
  134.             return $this->redirectToRoute('edition_edit', ["id" => $lastVersion->getId()]);
  135.     }
  136.     #[Route('/edition/deleteblock/{id}'name'edition_delete_block')]
  137.     public function deleteblock(EntityManagerInterface $emBlock $block)
  138.     {
  139.         $em->remove($block);
  140.         $em->flush();
  141.         return $this->redirectToRoute('edition_index');
  142.     }
  143.     #[Route('/edition/removeUser/{document}/{user}'name'edition_removeuser')]
  144.     public function removeUser(EntityManagerInterface $emDocument $documentUser $user)
  145.     {
  146.         $dps $em->getRepository(DocumentPermission::class)->findBy(["document" => $document"user" => $user]);
  147.         foreach($dps as $dp) {
  148.             $em->remove($dp);
  149.         }
  150.         $em->flush();
  151.         return $this->redirectToRoute('edition_edit', ["id" => $document->getId()]);
  152.     }
  153.     #[Route('/edition/addUser/{document}/{user}'name'edition_adduser')]
  154.     public function addUser(EntityManagerInterface $emDocument $documentUser $user)
  155.     {
  156.         $dps $em->getRepository(DocumentPermission::class)->findBy(["document" => $document"user" => $user]);
  157.         foreach($dps as $dp) {
  158.             $em->remove($dp);
  159.         }
  160.         $em->flush();
  161.         $dp = new DocumentPermission();
  162.         $dp->setDocument($document);
  163.         $dp->setUser($user);
  164.         $em->persist($dp);
  165.         $em->flush();
  166.         return $this->redirectToRoute('edition_edit', ["id" => $document->getId()]);
  167.     }
  168.     #[Route('/edition/generate_/{id}'name'edition_fakegenerate'methods: ['POST'])]
  169.     public function fakeGenerate(Document $documentRequest $request)
  170.     {
  171.         $variables = [];
  172.         $varnames explode(','$request->get('vars'));
  173.         foreach($varnames as $var) {
  174.             $variables[$var] = $request->get($var);
  175.         }
  176.         return new Response("\n<br>" 'fake generated.');
  177.     }
  178.     /*
  179.      * Generate word document.
  180.      * id: document id from the database
  181.      * write: option to write in a file instead of a stream to download. For debug purposes.
  182.      */
  183.     #[Route('/edition/generate/{id}/{write}'name'edition_generate'methods: ['POST'])]
  184.     public function generate(Request $requestDocument $document$write false)
  185.     {
  186.         /* get variables to replace in document */
  187.         $variables = [];
  188.         $varnames explode(','$request->get('vars'));
  189.         foreach($varnames as $var) {
  190.             $variables[$var] = $request->get($var);
  191.         }
  192.         //$html = '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body>';
  193.         $html "<h1>" $document->getTitle() . "</h1>";
  194.         $phpWord = new \PhpOffice\PhpWord\PhpWord();
  195.         $phpWord->addTitleStyle(1, ['name' => 'Calibri''size' => 30'color' => '1B2232''bold' => false], ['numStyle' => 'hNum''numLevel' => 0]);
  196.         $phpWord->addTitleStyle(2, ['name' => 'Calibri''size' => 24'color' => '1B2232''bold' => false], ['numStyle' => 'hNum''numLevel' => 1]);
  197.         $phpWord->addTitleStyle(3, ['name' => 'Calibri''size' => 21'color' => '1B2232''bold' => false], ['numStyle' => 'hNum''numLevel' => 2]);
  198.         $phpWord->addTitleStyle(4, ['name' => 'Calibri''size' => 18'color' => '1B2232''bold' => false], ['numStyle' => 'hNum''numLevel' => 3]);
  199.         $phpWord->addTitleStyle(5, ['name' => 'Calibri''size' => 14'color' => '1B2232''bold' => false], ['numStyle' => 'hNum''numLevel' => 4]);
  200.         $phpWord->addFontStyle('Link', array('color' => '0000FF''underline' => 'single'));
  201.         //$section0 = $phpWord->addSection();
  202.         //$section0->addTOC(['name' => 'Calibri'], [], 1, 9);
  203.         $section $phpWord->addSection();
  204.         /** @var Block $block */
  205.         foreach($document->getBlocks() as $block) {
  206.             if($block->getType() == Block::TYPE_TEXT)
  207.                 \PhpOffice\PhpWord\Shared\Html::addHtml($section$block->getHydratedContent($variables));
  208.             if($block->getType() == Block::TYPE_PAGEBREAK)
  209.                 $section->addPageBreak();
  210.         }
  211.         $section->addTOC(['name' => 'Calibri'], [], 19);
  212.         $footer $section->addFooter();
  213.         $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.');
  214.         $phpWord->getCompatibility()->setOoxmlVersion(15);
  215.         /* write */
  216.         $objWriter \PhpOffice\PhpWord\IOFactory::createWriter($phpWord'Word2007');
  217.         if($write) {
  218.             $objWriter->save($document->getTitle() . '.docx');
  219.             return new Response(".");
  220.         }
  221.         else {
  222.             $response = new StreamedResponse();
  223.             $response->setCallback(function() use ($objWriter) {
  224.                 $objWriter->save('php://output');
  225.             });
  226.             $response->headers->set('Content-Type''application/vnd.ms-word');
  227.             $response->headers->set('Cache-Control''max-age=0');
  228.             $disposition HeaderUtils::makeDisposition(
  229.                 HeaderUtils::DISPOSITION_ATTACHMENT,
  230.                 $document->getTitle() . '.docx'
  231.             );
  232.             $response->headers->set('Content-Disposition'$disposition);
  233.             return $response;
  234.         }
  235.     }
  236.     private function addtest($section) {
  237.         for($i 1$i 3$i++) {
  238.             $section->addTitle("title ".$i1);
  239.             $section->addText(
  240.                 '"The greatest accomplishment is not in never falling, '
  241.                 'but in rising again after you fall.\n" '
  242.                 '(Vince Lombardi)'
  243.                 "The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators. To achieve this, it would be necessary to have uniform grammar, pronunciation and more common words. If several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual languages. The new common language will be more simple and regular than the existing European languages. It will be as simple as Occidental; in fact, it will be Occidental. To an English person, it will seem like simplified English, as a skeptical Cambridge friend of mine told me what Occidental is. The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators. To achieve this, it would be necessary to have uniform grammar, pronunciation and more common words. If several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual languages. The new common language will be more simple and regular than the existing European languages. It will be as simple as Occidental; in fact, it will be Occidental. To an English person, it will seem like simplified English, as a skeptical Cambridge friend of mine told me what Occidental is.The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators. To achieve this, it would be necessary to have uniform grammar, pronunciation and more common words. If several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual languages. The new common language will be more simple and regular than the existing European languages."
  244.             );
  245.             $section->addTitle("title ".$i.".1"2);
  246.             $section->addTitle("title ".$i.".1.1"3);
  247.             $section->addText(
  248.                 "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.\n"
  249.                 "(Albert Einstein)\n"
  250.                 'The tears began to flow and sobs shook him. He gave himself up to them now for the first time on the island; great, shuddering spasms of grief that seemed to wrench his whole body. His voice rose under the black smoke before the burning wreckage of the island; and infected by that emotion, the other little boys began to shake and sob too. '
  251.             );
  252.             $section->addTitle("title ".$i.".2"2);
  253.             $section->addTitle("title ".$i.".2.1"3);
  254.             $section->addText(
  255.                 '"The greatest accomplishment is not in never falling, '
  256.                 'but in rising again after you fall." '
  257.                 "(Vince Lombardi)\n"
  258.                 ."Researchers suggest that both a tutor’s and student’s mindset shape an academic session:\n Embedded-tutoring programs are worthwhile investments. This research has broken ground in linking mindset changes to writing center work. I hope writing center researchers will continue to explore ways to advance interdisciplinary understanding of the power of mindsets, particularly as they relate to writing improvement. (Miller, 2020, pp. 122-123)"
  259.             );
  260.         }
  261.     }
  262.     #[Route('/edition/test')]
  263.     public function docxtest()
  264.     {
  265.         $phpWord = new \PhpOffice\PhpWord\PhpWord();
  266.         $phpWord->addTitleStyle(1, ['name' => 'Calibri''size' => 30'color' => '1B2232''bold' => false], []);
  267.         $phpWord->addTitleStyle(2, ['name' => 'Calibri''size' => 24'color' => '1B2232''bold' => false], []);
  268.         $phpWord->addTitleStyle(3, ['name' => 'Calibri''size' => 21'color' => '1B2232''bold' => false], []);
  269.         $phpWord->addTitleStyle(4, ['name' => 'Calibri''size' => 18'color' => '1B2232''bold' => false], []);
  270.         $phpWord->addTitleStyle(5, ['name' => 'Calibri''size' => 14'color' => '1B2232''bold' => false], []);
  271. // Adding an empty Section to the document...
  272.         $section $phpWord->addSection();
  273.         $section->addTOC([], []);
  274. // Adding Text element with font customized using named font style...
  275.         $fontStyleName 'oneUserDefinedStyle';
  276.         $phpWord->addFontStyle(
  277.             $fontStyleName,
  278.             array('name' => 'Tahoma''size' => 10'color' => '1B2232''bold' => true)
  279.         );
  280.         $section->addText(
  281.             '"The greatest accomplishment is not in never falling, '
  282.             'but in rising again after you fall." '
  283.             '(Vince Lombardi)',
  284.             $fontStyleName
  285.         );
  286.         $section->addTitle("title1"1);
  287.         $section->addText(
  288.             '"The greatest accomplishment is not in never falling, '
  289.             'but in rising again after you fall." '
  290.             '(Vince Lombardi)',
  291.             $fontStyleName
  292.         );
  293.         $section->addTitle("title2"1);
  294.         $section->addText(
  295.             '"The greatest accomplishment is not in never falling, '
  296.             'but in rising again after you fall." '
  297.             '(Vince Lombardi)',
  298.             $fontStyleName
  299.         );
  300.         $section->addTitle("title2.1"2);
  301.         $section->addText(
  302.             '"The greatest accomplishment is not in never falling, '
  303.             'but in rising again after you fall." '
  304.             '(Vince Lombardi)',
  305.             $fontStyleName
  306.         );
  307. // Adding Text element with font customized using explicitly created font style object...
  308.         $fontStyle = new \PhpOffice\PhpWord\Style\Font();
  309.         $fontStyle->setBold(true);
  310.         $fontStyle->setName('Tahoma');
  311.         $fontStyle->setSize(13);
  312.         $myTextElement $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
  313.         $myTextElement->setFontStyle($fontStyle);
  314. // Saving the document as OOXML file...
  315.         $objWriter \PhpOffice\PhpWord\IOFactory::createWriter($phpWord'Word2007');
  316.         $objWriter->save('helloWorld.docx');
  317.         return new Response(".");
  318.     }
  319.     #[Route('/api/{version}/edition/saveblockasmodel'name'edition_api_saveblockasmodel'methods: ['POST'])]
  320.     public function apiSaveBlockAsModel(Request $requestEntityManagerInterface $em)
  321.     {
  322.         $b $request->request->all('block');
  323.         $block = new Block();
  324.         // get category
  325.         if ($b["categoryId"] != "" && $b["categoryId"] != "+")
  326.         {
  327.             $bCat $em->getRepository(Document\BlockCategory::class)->find($b["categoryId"]);
  328.             if ($bCat)
  329.                 $block->setCategory($bCat);
  330.         }
  331.         else if($b["categoryId"] == "+" && trim($b["newCategoryTitle"]) != "") {
  332.             $bCat = new Document\BlockCategory();
  333.             $bCat->setTitle(trim($b["newCategoryTitle"]));
  334.             $bCat->setOwner($this->getUser());
  335.             $em->persist($bCat);
  336.             $block->setCategory($bCat);
  337.         }
  338.         $block->setContent($b["content"]);
  339.         $block->setTitle($b["title"]);
  340.         $block->setDescription($b["description"]);
  341.         $block->setTags($b["tags"]);
  342.         $block->setOwner($this->getUser());
  343.         $em->persist($block);
  344.         $em->flush();
  345.         $responsedata = [
  346.             "block" => $block->getContent(),
  347.         ];
  348.         return new JsonResponse($responsedata);
  349.     }
  350.     #[Route('/api/{version}/edition/saveblock'name'edition_api_saveblock'methods: ['POST'])]
  351.     public function apiSaveBlock(Request $requestEntityManagerInterface $em)
  352.     {
  353.         $b $request->request->all('block');
  354.         $block null;
  355.         if(isset($b["id"])) {
  356.             /** @var Block $block */
  357.             $block $em->getRepository(Block::class)->find($b["id"]);
  358.         }
  359.         if(!$block)
  360.             $block = new Block();
  361.         // get category
  362.         if ($b["categoryId"] != "" && $b["categoryId"] != "+")
  363.         {
  364.             $bCat $em->getRepository(Document\BlockCategory::class)->find($b["categoryId"]);
  365.             if ($bCat)
  366.                 $block->setCategory($bCat);
  367.         }
  368.         else if($b["categoryId"] == "+" && trim($b["newCategoryTitle"]) != "") {
  369.             $bCat = new Document\BlockCategory();
  370.             $bCat->setTitle(trim($b["newCategoryTitle"]));
  371.             $bCat->setOwner($this->getUser());
  372.             $em->persist($bCat);
  373.             $block->setCategory($bCat);
  374.         }
  375.         $block->setContent($b["content"]);
  376.         $block->setTitle($b["title"]);
  377.         $block->setDescription($b["description"]);
  378.         $block->setTags($b["tags"]);
  379.         $em->persist($block);
  380.         $em->flush();
  381.         $responsedata = [
  382.             "block" => $block->getContent(),
  383.             "blockId" => $block->getId(),
  384.             "data" => print_r($btrue)
  385.         ];
  386.         return new JsonResponse($responsedata);
  387.     }
  388.     #[Route('/api/{version}/edition/overwritedoc'name'edition_api_overwritedoc'methods: ['POST'])]
  389.     public function apiOverwriteDoc(Request $requestEntityManagerInterface $em)
  390.     {
  391.         // This will overwrite the existing document and linked blocks. All changes are therefore permanent.
  392.         /*
  393.          * jsondoc = {
  394.          *      id: document.id,
  395.          *      title: document.title,
  396.          *      description: document.description,
  397.          *      tags: document.tags,
  398.          *      blocks: [{
  399.          *              order: block order in document,
  400.          *              content: block content,
  401.          *              title: block title,
  402.          *              description: block description,
  403.          *              tags: block tags
  404.          *      }]
  405.          * }
  406.          *
  407.          */
  408.         $jsondoc $request->request->all('document');
  409.         /** @var Document $document */
  410.         $document $em->getRepository(Document::class)->find($jsondoc["id"]);
  411.         // deleting current blocks
  412.         $oldblocks $em->getRepository(Block::class)->findBy(["document" => $document]);
  413.         foreach($oldblocks as $ob) {
  414.             $em->remove($ob);
  415.         }
  416.         $blocks $jsondoc["blocks"];
  417.         // adding new blocks
  418.         foreach($blocks as $b) {
  419.             $oldblock $em->getRepository(Block::class)->findOneBy(["blockorder" => $b["order"], "document" => $document]);
  420.             if($oldblock)
  421.                 $em->remove($oldblock);
  422.             $block = new Block();
  423.             $block->setContent($b["content"]);
  424.             $block->setTitle($b["title"]);
  425.             $block->setDescription($b["description"]);
  426.             $block->setTags($b["tags"]);
  427.             $block->setDocument($document);
  428.             $block->setBlockorder(intval($b["order"]));
  429.             $block->setIdInDoc($b["idInDoc"]);
  430.             $block->setType($b["type"]);
  431.             $em->persist($block);
  432.         }
  433.         // updating document information
  434.         $document->setTitle($jsondoc["title"]);
  435.         $document->setDescription($jsondoc["description"]);
  436.         $document->setTags($jsondoc["tags"]);
  437.         $document->setStatus($jsondoc["status"]);
  438.         $document->setDateUpdated(new \DateTime());
  439.         $em->persist($document);
  440.         $em->flush();
  441.         $responsedata = [
  442.             "document" => $jsondoc,
  443.             "blocks" => $blocks
  444.         ];
  445.         return new JsonResponse($responsedata);
  446.     }
  447.     #[Route('/api/{version}/edition/savedoc'name'edition_api_savedoc'methods: ['POST'])]
  448.     public function apiSaveDoc(Request $requestEntityManagerInterface $em)
  449.     {
  450.         // This will save a copy of the document and linked blocks, allowing for version management
  451.         /*
  452.          * jsondoc = {
  453.          *      id: document.id,
  454.          *      title: document.title,
  455.          *      description: document.description,
  456.          *      tags: document.tags,
  457.          *      blocks: [{
  458.          *              order: block order in document,
  459.          *              content: block content,
  460.          *              title: block title,
  461.          *              description: block description,
  462.          *              tags: block tags
  463.          *      }]
  464.          * }
  465.          *
  466.          */
  467.         $jsondoc $request->request->all('document');
  468.         /** @var Document $olddocument */
  469.         $olddocument $em->getRepository(Document::class)->find($jsondoc["id"]);
  470.         /** @var Document $document */
  471.         $document = new Document();
  472.         $blocks $jsondoc["blocks"];
  473.         // adding new blocks
  474.         foreach($blocks as $b) {
  475.             $block = new Block();
  476.             // get category
  477.             if ($b["category"] != "")
  478.             {
  479.                 $bCat $em->getRepository(Document\BlockCategory::class)->find($b["category"]);
  480.                 if ($bCat)
  481.                     $block->setCategory($bCat);
  482.                 else {
  483.                     $bCat = new Document\BlockCategory();
  484.                     $bCat->setTitle(trim($b["category"]));
  485.                     $bCat->setOwner($this->getUser());
  486.                     $em->persist($bCat);
  487.                     $block->setCategory($bCat);
  488.                 }
  489.             }
  490.             $block->setContent($b["content"]);
  491.             $block->setTitle($b["title"]);
  492.             $block->setDescription($b["description"]);
  493.             $block->setTags($b["tags"]);
  494.             $block->setDocument($document);
  495.             $block->setBlockorder(intval($b["order"]));
  496.             $block->setIdInDoc($b["idInDoc"]);
  497.             $block->setType(intval($b["type"]));
  498.             $em->persist($block);
  499.         }
  500.         // all previous versions are not last version anymore
  501.         $prevdocs $em->getRepository(Document::class)->findBy(["versionOf" => $olddocument->getVersionOf()]);
  502.         foreach ($prevdocs as $prevdoc) {
  503.             /** @var Document $prevdoc */
  504.             $prevdoc->setIsLastVersion(false);
  505.             $em->persist($prevdoc);
  506.         }
  507.         // updating document information
  508.         $document->setTitle($jsondoc["title"]);
  509.         $document->setDescription($jsondoc["description"]);
  510.         $document->setTags($jsondoc["tags"]);
  511.         $document->setStatus($jsondoc["status"]);
  512.         $document->setDateCreated($olddocument->getDateCreated());
  513.         $document->setDateUpdated(new \DateTime());
  514.         $document->setVersion(new \DateTime());
  515.         $document->setType(Document::TYPE_WEB);
  516.         $document->setOwner($olddocument->getOwner());
  517.         $document->setVersionOf($olddocument->getVersionOf());
  518.         $document->setIsLastVersion(true);
  519.         $em->persist($document);
  520.         $em->flush();
  521.         $responsedata = [
  522.             "document" => $jsondoc,
  523.             "blocks" => $blocks,
  524.             "newid" => $document->getId()
  525.         ];
  526.         return new JsonResponse($responsedata);
  527.     }
  528.     #[Route('/api/{version}/edition/getblock/{id}'name'edition_api_getblock')]
  529.     public function apiGetBlock(Request $requestEntityManagerInterface $em$id)
  530.     {
  531.         /** @var Block $block */
  532.         $block $em->getRepository(Block::class)->find($id);
  533.         $responsedata = [
  534.             "block" => $block->getContent()
  535.         ];
  536.         return new JsonResponse($responsedata);
  537.     }
  538.     #[Route('/api/{version}/edition/getlastversion'name'edition_api_getlastversion'methods: ['POST'])]
  539.     public function apiGetLastVersion(Request $requestEntityManagerInterface $em)
  540.     {
  541.         /** @var Document $document */
  542.         $document $em->getRepository(Document::class)->find($request->request->get('document'));
  543.         $responsedata = [
  544.             "error" => ""
  545.         ];
  546.         if($document) {
  547.             $lastVersion $em->getRepository(Document::class)->findOneBy(["versionOf" => $document->getVersionOf(), "isLastVersion" => 1]);
  548.             $responsedata["lastVersion"] = $lastVersion->getId();
  549.         }
  550.         else $responsedata["error"] = "document not found";
  551.         return new JsonResponse($responsedata);
  552.     }
  553. }