src/OceanExpertBundle/Controller/DocumentController.php line 1344

Open in your IDE?
  1. <?php
  2. namespace OceanExpertBundle\Controller;
  3. use DateTime;
  4. use OceanExpertBundle\Entity\AgendaitemDocuments;
  5. use OceanExpertBundle\Entity\DoclistDocuments;
  6. use OceanExpertBundle\Entity\DoclistGroups;
  7. use OceanExpertBundle\Entity\Doclists;
  8. use OceanExpertBundle\Entity\DocumentFiles;
  9. use OceanExpertBundle\Entity\Documents;
  10. use OceanExpertBundle\Entity\EventBackgrounddocs;
  11. use OceanExpertBundle\Entity\EventOtherdocs;
  12. use OceanExpertBundle\Entity\EventPresentations;
  13. use OceanExpertBundle\Entity\EventReports;
  14. use OceanExpertBundle\Repository\DoclistsRepository;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  20. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  21. /**
  22.  * Class DocumentController
  23.  * @package OceanExpertBundle\Controller
  24.  */
  25. class DocumentController extends AbstractController
  26. {
  27.     private DoclistsRepository $doclistsRepository;
  28.     public function __construct(DoclistsRepository $doclistsRepository)
  29.     {
  30.         $this->doclistsRepository $doclistsRepository;
  31.     }
  32.     /**
  33.      * @param $name
  34.      *
  35.      * @return Response
  36.      */
  37.     public function indexAction($name): Response
  38.     {
  39.         return $this->render('', array('name' => $name));
  40.     }
  41.     /**
  42.      * @param Request $request
  43.      * @return Response
  44.      */
  45.     public function searchDocumentAction(Request $request)
  46.     {
  47.         $searchTerm $request->request->get('searchterm');
  48.         $request->request->get('doctype');
  49.         $em $this->getDoctrine()->getManager();
  50.         $result $em->getRepository('OceanExpertBundle:Documents')->createQueryBuilder('d')
  51.             ->select('d.idDoc,d.title,d.authorText,dt.doctypename')
  52.             ->leftJoin('OceanExpertBundle:Doctypes''dt''WITH''d.idDoctype = dt.idDoctype')
  53.             ->where('d.title LIKE :searchterm')
  54.             ->orWhere('d.summary LIKE :searchterm')
  55.             ->orWhere('d.authorText LIKE :searchterm')
  56.             ->orWhere('d.docCode LIKE :searchterm')
  57.             ->andWhere('d.approved = 1')
  58.             ->setParameter('searchterm''%' $searchTerm '%')
  59.             ->getQuery()
  60.             ->getResult();
  61.         return new JsonResponse(array('data' => $result));
  62.     }
  63.     /**
  64.      * @param Request $request
  65.      * @param $idDoc
  66.      * @return Response
  67.      */
  68.     public function getDocumentByIdAction(Request $request$idDoc)
  69.     {
  70.         if ($idDoc == 0) {
  71.             $idDoc $request->query->get('idDoc');
  72.         }
  73.         $em $this->getDoctrine()->getManager();
  74.         $result $em->getRepository('OceanExpertBundle:Documents')->createQueryBuilder('d')
  75.             ->select('d.docCode, d.idDoc, d.title, d.summary, d.authorText, d.publishedOn, d.notes, d.keywords, d.createdAt, d.updatedAt,d.idCreator,d.lastEditBy,dt.doctypename')
  76.             ->leftJoin('OceanExpertBundle:Doctypes''dt''WITH''d.idDoctype = dt.idDoctype')
  77.             ->where('d.idDoc = :idDoc')
  78.             ->setParameter('idDoc'$idDoc)
  79.             ->getQuery()
  80.             ->getResult()[0];
  81.         if ($result['idCreator'] != '') {
  82.             $indiv $this->getDoctrine()->getRepository('OceanExpertBundle:Indiv')->findOneByIdInd($result['idCreator']);
  83.             if ($indiv) {
  84.                 $result['createdBy'] = $indiv->getFname() . ' ' $indiv->getSname();
  85.             } else {
  86.                 $result['createdBy'] = '';
  87.             }
  88.         }
  89.         if ($result['lastEditBy'] != '') {
  90.             $indiv $this->getDoctrine()->getRepository('OceanExpertBundle:Indiv')->findOneByIdInd($result['lastEditBy']);
  91.             if ($indiv) {
  92.                 $result['updatedBy'] = $indiv->getFname() . ' ' $indiv->getSname();
  93.             } else {
  94.                 $result['updatedBy'] = '';
  95.             }
  96.         }
  97.         $groups $em->getRepository('OceanExpertBundle:DocumentGroups')->createQueryBuilder('d')
  98.             ->select('d.idGroup,g.groupname')
  99.             ->leftJoin('OceanExpertBundle:Groups''g''WITH''d.idGroup = g.idGroup')
  100.             ->where('d.idDoc = :idDoc')
  101.             ->setParameter('idDoc'$idDoc)
  102.             ->getQuery()
  103.             ->getResult();
  104.         $docGroupList $em->getRepository('OceanExpertBundle:DoclistDocuments')->createQueryBuilder('d')
  105.             ->select('dl.idDoclist, dl.title')
  106.             ->leftJoin('OceanExpertBundle:Doclists''dl''WITH''dl.idDoclist = d.idDoclist')
  107.             ->where('d.idDoc = :idDoc')
  108.             ->setParameter('idDoc'$idDoc)
  109.             ->getQuery()
  110.             ->getResult();
  111.         $docFiles $em->getRepository('OceanExpertBundle:DocumentFiles')->createQueryBuilder('d')
  112.             ->select('d.idFileformat, d.version, l.languagename, d.idFile, f.filename, f.url, f.size,f.content,f.path')
  113.             ->leftJoin('OceanExpertBundle:Files''f''WITH''f.idFile = d.idFile')
  114.             ->leftJoin('OceanExpertBundle:Languages''l''WITH''f.idLanguage = l.idLanguage')
  115.             ->where('d.idDoc = :idDoc')
  116.             ->setParameter('idDoc'$idDoc)
  117.             ->getQuery()
  118.             ->getResult();
  119.         $result['documentGroup'] = $groups;
  120.         $result['docGroupList'] = $docGroupList;
  121.         $result['docFiles'] = $docFiles;
  122.         return $this->render(
  123.             'Documents/viewDocumentBlock.html.twig',
  124.             array(
  125.                 'data' => $result
  126.             )
  127.         );
  128.     }
  129.     /**
  130.      *
  131.      */
  132.     public function deleteEventDocumentAction(Request $request)
  133.     {
  134.         $authorised true;
  135.         $docType $request->request->get('doctype');
  136.         $idEvent $request->request->get('idEvent');
  137.         $idDoc $request->request->get('idDoc');
  138.         if (null != ($request->request->get('idAgenda'))) {
  139.             $idAgenda $request->request->get('idAgenda');
  140.         } else {
  141.             $idAgenda '';
  142.         }
  143.         $id 0;
  144.         if ($authorised) {
  145.             $em $this->getDoctrine()->getManager();
  146.             switch ($docType) {
  147.                 case 'report':
  148.                     $report $em->getRepository('OceanExpertBundle:EventReports')
  149.                         ->findOneBy(
  150.                             array(
  151.                                 'idEvent' => $idEvent,
  152.                                 'idDoc' => $idDoc
  153.                             )
  154.                         );
  155.                     if ($report) {
  156.                         $em->remove($report);
  157.                         $em->flush();
  158.                         $id 1;
  159.                     }
  160.                     break;
  161.                 case 'presentation':
  162.                     $presentation $em->getRepository('OceanExpertBundle:EventPresentations')
  163.                         ->findOneBy(
  164.                             array(
  165.                                 'idEvent' => $idEvent,
  166.                                 'idDoc' => $idDoc
  167.                             )
  168.                         );
  169.                     if ($presentation) {
  170.                         $em->remove($presentation);
  171.                         $em->flush();
  172.                         $id 2;
  173.                     }
  174.                     break;
  175.                 case 'backgrounddocs':
  176.                     $backgroundocs $em->getRepository('OceanExpertBundle:EventBackgrounddocs')
  177.                         ->findOneBy(
  178.                             array(
  179.                                 'idEvent' => $idEvent,
  180.                                 'idDoc' => $idDoc
  181.                             )
  182.                         );
  183.                     if ($backgroundocs) {
  184.                         $em->remove($backgroundocs);
  185.                         $em->flush();
  186.                         $id 3;
  187.                     }
  188.                     break;
  189.                 case 'agendaitems':
  190.                     if ($idAgenda != '') {
  191.                         $agendaDetails $em->getRepository('OceanExpertBundle:EventAgendaitems')
  192.                             ->findOneBy(
  193.                                 array(
  194.                                     'idEvent' => $idEvent,
  195.                                     'id' => $idAgenda
  196.                                 )
  197.                             );
  198.                         if ($agendaDetails) {
  199.                             $agendaitem $agendaDetails->getIdAgendaitem();
  200.                             $agendaDocs $em->getRepository('OceanExpertBundle:AgendaitemDocuments')
  201.                                 ->findOneBy(
  202.                                     array(
  203.                                         'idEvent' => $idEvent,
  204.                                         'idDoc' => $idDoc,
  205.                                         'idAgendaitem' => $agendaitem
  206.                                     )
  207.                                 );
  208.                             if ($agendaDocs) {
  209.                                 $em->remove($agendaDocs);
  210.                                 $em->flush();
  211.                                 $id 5;
  212.                             }
  213.                         }
  214.                     }
  215.                     break;
  216.                 case 'otherdocs':
  217.                     $otherdocs $em->getRepository('OceanExpertBundle:EventOtherdocs')
  218.                         ->findOneBy(
  219.                             array(
  220.                                 'idEvent' => $idEvent,
  221.                                 'idDoc' => $idDoc
  222.                             )
  223.                         );
  224.                     if ($otherdocs) {
  225.                         $em->remove($otherdocs);
  226.                         $em->flush();
  227.                         $id 4;
  228.                     }
  229.                     break;
  230.                 default:
  231.                     echo 'No type provided';
  232.                     break;
  233.             }
  234.         }
  235.         if ($id != 0) {
  236.             $data = array(
  237.                 'status' => 1,
  238.                 'section' => $docType,
  239.                 'idAgenda' => $idAgenda,
  240.                 'idEvent' => $idEvent,
  241.                 'idDoc' => $idDoc
  242.             );
  243.             return new JsonResponse($data);
  244.         } else {
  245.             return new JsonResponse(array('status' => 0));
  246.         }
  247.     }
  248.     /**
  249.      * upload a new file that is attached to an event or a doclist
  250.      *
  251.      * @param Request $request
  252.      *
  253.      * @return JsonResponse
  254.      */
  255.     public function uploadEventDocumentAction(Request $request): JsonResponse
  256.     {
  257.         $userId $this->get('security.token_storage')
  258.             ->getToken()
  259.             ->getUser()
  260.             ->getid();
  261.         //get all the data we need
  262.         $uploadFile $request->files->get('file');
  263.         parse_str($request->request->get('formdata'), $params);
  264.         //summary and notes are in the get request for both events and doclists
  265.         $summary $request->request->get('summary');
  266.         $notes $request->request->get('notes');
  267.         //something strange is going on with the title when using doclist
  268.         //the title of the document is replaced with the title of the doclist
  269.         //try to avoid that
  270.         if ($request->request->get('doc-title') != '') {
  271.             $title $request->request->get('doc-title');
  272.         } elseif (isset($params['title'])
  273.             && $params['title'] != ''
  274.         ) {
  275.             $title $params['title'];
  276.         } else {
  277.             $title 'no title';
  278.         }
  279.         if (isset($params['doctype'])
  280.             && $params['doctype'] != ''
  281.         ) {
  282.             $docType $params['doctype'];
  283.         } else {
  284.             $docType $request->request->get('doctype');
  285.         }
  286.         if (isset($params['status'])
  287.             && $params['status'] != ''
  288.         ) {
  289.             $status $params['status'];
  290.         } else {
  291.             $status $request->request->get('status');
  292.         }
  293.         if (isset($params['author'])
  294.             && $params['author'] != ''
  295.         ) {
  296.             $authors $params['author'];
  297.         } elseif ($request->request->get('authors') !== null
  298.             && $request->request->get('authors') != ''
  299.         ) {
  300.             $authors $request->request->get('authors');
  301.         } else {
  302.             $authors '';
  303.         }
  304.         if (isset($params['keywords'])
  305.             && $params['keywords'] != ''
  306.         ) {
  307.             $keywords $params['keywords'];
  308.         } elseif ($request->request->get('keywords') !== null) {
  309.             $keywords $request->request->get('keywords');
  310.         } else {
  311.             $keywords '';
  312.         }
  313.         if (isset($params['doccode'])
  314.             && $params['doccode'] != ''
  315.         ) {
  316.             $docCode $params['doccode'];
  317.         } elseif ($request->request->get('doccode') !== null) {
  318.             $docCode $request->request->get('doccode');
  319.         } else {
  320.             $docCode '';
  321.         }
  322.         if (isset($params['websiteUrl'])
  323.             && $params['websiteUrl'] != ''
  324.         ) {
  325.             $url $params['websiteUrl'];
  326.         } else {
  327.             $url $request->request->get('url');
  328.         }
  329.         if (isset($params['doclist'])
  330.             && $params['doclist'] != ''
  331.         ) {
  332.             $docList $params['doclist'];
  333.         } else {
  334.             $docList $request->request->get('doclist');
  335.         }
  336.         if (isset($params['version'])
  337.             && is_array($params['version'])
  338.             && count($params['version'])
  339.         ) {
  340.             $version $params['version'];
  341.         } else {
  342.             //default version is 1
  343.             $version = array(1);
  344.         }
  345.         /*
  346.         dump($request->request);
  347.         dump($summary);
  348.         dump($title);
  349.         dump($notes);
  350.         dump($uploadFile);
  351.         dump($docType);
  352.         dump($status);
  353.         dump($authors);
  354.         dump($keywords);
  355.         dump($docCode);
  356.         dump($url);
  357.         dump($docList);
  358.         die();
  359.         */
  360.         //cleanup the data
  361.         $agendaItem '';
  362.         $idEvent '';
  363.         $idAgenda '';
  364.         $section '';
  365.         $idDoc '';
  366.         if ($docType != ''
  367.             && $status != ''
  368.         ) {
  369.             $em $this->getDoctrine()->getManager();
  370.             /**
  371.              * Create a document entry in documents table
  372.              */
  373.             $document = new Documents();
  374.             $document->setIdDoctype($docType);
  375.             $document->setDocCode($docCode);
  376.             $document->setTitle($title);
  377.             $document->setSummary($summary);
  378.             $document->setAuthorText($authors);
  379.             $document->setNotes($notes);
  380.             $document->setKeywords($keywords);
  381.             $document->setUrl($url);
  382.             $document->setIdDocstatus($status);
  383.             if ($status == 1) {
  384.                 $document->setPublishedOn(new DateTime('now'));
  385.             }
  386.             $document->setCreatedAt(new DateTime('now'));
  387.             $document->setUpdatedAt(new DateTime('now'));
  388.             if ($this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')) {
  389.                 $document->setApproved(1);
  390.             } else {
  391.                 $document->setApproved(0);
  392.             }
  393.             $document->setIdCreator($userId);
  394.             $document->setLastEditBy($userId);
  395.             $em->persist($document);
  396.             $em->flush();
  397.             if (is_array($docList)
  398.                 && count($docList) > 0
  399.             ) {
  400.                 // if documentlist is set then enter id documentlist table.
  401.                 foreach ($docList as $docListID) {
  402.                     $doclist $em->getRepository('OceanExpertBundle:DoclistDocuments')
  403.                         ->findOneBy(
  404.                             array(
  405.                                 'idDoclist' => $docListID,
  406.                                 'idDoc' => $document->getIdDoc()
  407.                             )
  408.                         );
  409.                     if (!$doclist) {
  410.                         $doclist = new DoclistDocuments();
  411.                     }
  412.                     $doclist->setIdDoclist($docListID);
  413.                     $doclist->setIdDoc($document->getIdDoc());
  414.                     $doclist->setAddedAt(new DateTime('now'));
  415.                     $em->persist($doclist);
  416.                     $em->flush();
  417.                 }
  418.             }
  419.             //if file is uploaded then upload the files in event or document section.
  420.             if (is_countable($uploadFile)
  421.                 && count($uploadFile) > 0
  422.             ) {
  423.                 $i 0;
  424.                 foreach ($uploadFile as $file) {
  425.                     $fileType $file->guessClientExtension();
  426.                     //$fileName = $file->getClientOriginalName();
  427.                     //cleanup the final filename
  428.                     $fileName preg_replace(
  429.                         '/[^\w\.]+/',
  430.                         '_',
  431.                         $file->getClientOriginalName()
  432.                     );
  433.                     $fileSize $file->getClientSize();
  434.                     if ($fileType == 'bin') {
  435.                         $fileType pathinfo($fileNamePATHINFO_EXTENSION);
  436.                     }
  437.                     $format $em->getRepository('OceanExpertBundle:Fileformats')
  438.                         ->findOneBy(
  439.                             array(
  440.                                 'extension' => $fileType
  441.                             )
  442.                         );
  443.                     if ($format) {
  444.                         $idFileFormat $format->getIdFileformat();
  445.                     } else {
  446.                         $idFileFormat 0;
  447.                     }
  448.                     if (array_key_exists('idEvent',$params)) {
  449.                         $directory 'uploads/events/documents/' $params['idEvent'] . '/';
  450.                     } else {
  451.                         $directory 'uploads/documents/';
  452.                     }
  453.                     $uploadFile $file->move($directory$fileName);
  454.                     $filepath $directory $fileName;
  455.                     /**
  456.                      * Create file entry in files table.
  457.                      * == filenew is added as multiple column primary indexes were set in old table,
  458.                      * cannot change as it might affect previous PPC sections.
  459.                      * Need to verify if there is no break of code in the earlier ppc.
  460.                      */
  461.                     if (isset($params['id_language'][$i])) {
  462.                         $defaultLanguage $params['id_language'][$i];
  463.                     } else {
  464.                         $defaultLanguage 1;
  465.                     }
  466.                     $conn $this->getDoctrine()->getConnection();
  467.                     $stmt $conn->prepare('INSERT INTO files 
  468.                             (version, id_language, id_fileformat, url, size, content, filename) 
  469.                         VALUES 
  470.                             (?, ?, ?, ?, ?, ?, ?)'
  471.                     );
  472.                     if (isset($version[$i])) {
  473.                         $thisVersion $version[$i];
  474.                     } else {
  475.                         $thisVersion 1;
  476.                     }
  477.                     $stmt->bindValue(1$thisVersion);
  478.                     $stmt->bindValue(2$defaultLanguage);
  479.                     $stmt->bindValue(3$idFileFormat);
  480.                     $stmt->bindValue(4utf8_encode($filepath));
  481.                     $stmt->bindValue(5$fileSize);
  482.                     $stmt->bindValue(6'');
  483.                     $stmt->bindValue(7utf8_encode($filepath));
  484.                     $stmt->execute();
  485.                     $idFile $this->getDoctrine()
  486.                         ->getConnection()
  487.                         ->lastInsertId();
  488.                     $files = new DocumentFiles();
  489.                     $files->setIdDoc($document->getIdDoc());
  490.                     $files->setIdFile($idFile);
  491.                     $files->setVersion($thisVersion);
  492.                     $files->setIdLanguage($defaultLanguage);
  493.                     $files->setIdFileformat($idFileFormat);
  494.                     $em->persist($files);
  495.                     $em->flush();
  496.                     $i++;
  497.                 }
  498.             }
  499.             if (array_key_exists('filesection',$params)) {
  500.                 if (array_key_exists('idEvent',$params)) {
  501.                     $idEvent $params['idEvent'];
  502.                 }
  503.                 if (array_key_exists('idEvent',$params)) {
  504.                     $idAgenda $params['idAgenda'];
  505.                 }
  506.                 $idDoc $document->getIdDoc();
  507.                 if ($idAgenda != '') {
  508.                     $agenda $em->getRepository('OceanExpertBundle:EventAgendaitems')
  509.                         ->findOneBy(
  510.                             array(
  511.                                 'id' => $idAgenda,
  512.                                 'idEvent' => $idEvent
  513.                             )
  514.                         );
  515.                     if ($agenda) {
  516.                         $agendaItem $agenda->getIdAgendaitem();
  517.                     }
  518.                 }
  519.                 //is this linked to some event or agenda
  520.                 if (array_key_exists('filesection',$params)) {
  521.                     $section $params['filesection'];
  522.                     switch ($section) {
  523.                         case 1:
  524.                             $report $em->getRepository('OceanExpertBundle:EventReports')
  525.                                 ->findOneBy(
  526.                                     array('idEvent' => $idEvent)
  527.                                 );
  528.                             if (!$report) {
  529.                                 $report = new EventReports();
  530.                             }
  531.                             $report->setIdEvent($idEvent);
  532.                             $report->setIdDoc($idDoc);
  533.                             $em->persist($report);
  534.                             $em->flush();
  535.                             break;
  536.                         case 2:
  537.                             $presentation = new EventPresentations();
  538.                             $presentation->setIdEvent($idEvent);
  539.                             $presentation->setIdDoc($idDoc);
  540.                             $presentation->setIdAgendaitem('');
  541.                             $em->persist($presentation);
  542.                             $em->flush();
  543.                             break;
  544.                         case 3:
  545.                             $backgrounddocs = new EventBackgrounddocs();
  546.                             $backgrounddocs->setIdEvent($idEvent);
  547.                             $backgrounddocs->setIdDoc($idDoc);
  548.                             $backgrounddocs->setIdAgendaitem('');
  549.                             $em->persist($backgrounddocs);
  550.                             $em->flush();
  551.                             break;
  552.                         case 4:
  553.                             $otherdocs = new EventOtherdocs();
  554.                             $otherdocs->setIdEvent($idEvent);
  555.                             $otherdocs->setIdDoc($idDoc);
  556.                             $em->persist($otherdocs);
  557.                             $em->flush();
  558.                             break;
  559.                         case 5:
  560.                             if (isset($agendaItem) && $agendaItem != '') {
  561.                                 $agendaItemDocs = new AgendaitemDocuments();
  562.                                 $agendaItemDocs->setIdEvent($idEvent);
  563.                                 $agendaItemDocs->setIdDoc($idDoc);
  564.                                 $agendaItemDocs->setIdAgendaitem($agendaItem);
  565.                                 $em->persist($agendaItemDocs);
  566.                                 $em->flush();
  567.                             }
  568.                             break;
  569.                         default:
  570.                             $data = array(
  571.                                 'status' => 0,
  572.                                 'message' => 'document could not be uploaded'
  573.                             );
  574.                             return new JsonResponse($data);
  575.                     }
  576.                 }
  577.             }
  578.             $data = array(
  579.                 'status' => 1,
  580.                 'section' => $section,
  581.                 'idEvent' => $idEvent,
  582.                 'idAgenda' => $idAgenda,
  583.                 'idDoc' => $idDoc,
  584.                 'docname' => $title
  585.             );
  586.         } else {
  587.             /*
  588.             print 'params:';
  589.             dump($params);
  590.             dump($summary);
  591.             dump($notes);
  592.             dump($uploadFile);
  593.             die();
  594.             */
  595.             $data = array(
  596.                 'status' => 1,
  597.                 'error' => 'something went wrong, not enough data'
  598.             );
  599.         }
  600.         return new JsonResponse($data);
  601.     }
  602.     /**
  603.      * @param $idDoclist
  604.      *
  605.      * @return Response
  606.      */
  607.     public function viewDoclistAction($idDoclist)
  608.     {
  609.        $data $this->doclistsRepository
  610.            ->getDoclistDocuments(
  611.                intval($idDoclist)
  612.            );
  613.        return $this->render(
  614.            'Documents/viewDoclist.html.twig',
  615.            array(
  616.                'data' => $data
  617.            )
  618.        );
  619.     }
  620.     /**
  621.      * show all the existing doclists
  622.      *
  623.      * @return Response
  624.      */
  625.     public function viewAllDoclistsAction(): Response
  626.     {
  627.         /*
  628.          * @todo
  629.          * this is not used
  630.          * should this be gone or will that serve for later?
  631.          * Arno 18/06/2021
  632.          *
  633.         $repository = $this->getDoctrine()
  634.             ->getManager()
  635.             ->createQueryBuilder();
  636.         $repository->add('select', 'g.idGroup, g.groupname');
  637.         $repository->add('from', 'OceanExpertBundle:Groups g');
  638.         $repository->where('g.hasSite = 1');
  639.         $allGroups = $repository->getQuery()->getResult();
  640.         */
  641.         $repository $this->getDoctrine()
  642.             ->getManager()
  643.             ->createQueryBuilder();
  644.         $repository->add(
  645.             'select',
  646.             'd.idDoclist, d.title, d.description,g.idGroup,g.groupname'
  647.         );
  648.         $repository->add(
  649.             'from',
  650.             'OceanExpertBundle:Doclists d'
  651.         );
  652.         $repository->leftJoin(
  653.             'OceanExpertBundle:DoclistGroups',
  654.             'dg',
  655.             'WITH',
  656.             'd.idDoclist = dg.idDoclist'
  657.         );
  658.         $repository->leftJoin(
  659.             'OceanExpertBundle:Groups',
  660.             'g',
  661.             'WITH',
  662.             'g.idGroup = dg.idGroup'
  663.         );
  664.         $repository->where(
  665.             'g.groupname is not null'
  666.         );
  667.         $repository->orderBy(
  668.             'g.idGroup',
  669.             'ASC'
  670.         );
  671.         $doclists $repository->getQuery()
  672.             ->getResult();
  673.         $doclistsArr = [];
  674.         $templevel=$doclists[0]['groupname'];
  675.         /*
  676.          * @todo
  677.          * this is not used
  678.          * should this be gone or will that serve for later?
  679.          * Arno 18/06/2021
  680.          *
  681.         $grouparr[$templevel] = '';
  682.         */
  683.         foreach ($doclists as $key => $val) {
  684.             if ($templevel == $val['groupname']) {
  685.                 $doclistsArr[$templevel][] = $val;
  686.             } else {
  687.                 $doclistsArr[$val['groupname']][] = $val;
  688.             }
  689.         }
  690.         return $this->render(
  691.             'Documents/viewDoclists.html.twig',
  692.             array(
  693.                 'data' => $doclistsArr
  694.             )
  695.         );
  696.     }
  697.     /**
  698.      * @param int $idDoclist
  699.      *
  700.      * @return array
  701.      */
  702.     public function getDoclistDocuments(int $idDoclist): array
  703.     {
  704.         $data = array();
  705.         $em $this->getDoctrine()->getManager();
  706.         $doclist $em->getRepository('OceanExpertBundle:Doclists')->findOneBy(array('idDoclist'=>$idDoclist));
  707.         if($doclist){
  708.             $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  709.             $repository->add('select''d.docCode, d.title, d.summary, d.authorText, d.publishedOn,d.idDoc, d.idDocstatus');
  710.             $repository->add('from''OceanExpertBundle:Documents d');
  711.             $repository->leftJoin('OceanExpertBundle:DoclistDocuments''dd''WITH''dd.idDoc = d.idDoc');
  712.             $repository->where('dd.idDoclist = :idDoclist');
  713.             $repository->setParameter('idDoclist'$idDoclist);
  714.             switch ($doclist->getOrderBy()){
  715.                 case 1:
  716.                     $repository->orderBy('d.publishedOn','desc');
  717.                     break;
  718.                 case 2:
  719.                     $repository->orderBy('d.publishedOn','asc');
  720.                     break;
  721.                 case 3:
  722.                     $repository->orderBy('dd.addedAt','desc');
  723.                     break;
  724.                 case 4:
  725.                     $repository->orderBy('dd.addedAt','asc');
  726.                     break;
  727.                 case 5:
  728.                     $repository->orderBy('d.docCode','desc');
  729.                     break;
  730.                 case 6:
  731.                     $repository->orderBy('d.docCode','asc');
  732.                     break;
  733.                 default:
  734.                     $repository->orderBy('d.title','asc');
  735.                     break;
  736.             }
  737.             $documents =  $repository->getQuery()->getResult();
  738.             $data = array(
  739.                 'doclist'=>$doclist,
  740.                 'documents'=>$documents,
  741.             );
  742.         }
  743.         return $data;
  744.     }
  745.     /**
  746.      * edit or create a doclist
  747.      * if $idDoclist is 0 then a new doclist will be created.
  748.      *
  749.      * @param int     $idDoclist
  750.      * @param Request $request
  751.      *
  752.      * @return  Response
  753.      */
  754.     public function editDoclistAction(int $idDoclistRequest $request): Response
  755.     {
  756.         $em $this->getDoctrine()->getManager();
  757.         $author =  $this->get('security.token_storage')->getToken()->getUser();
  758.         //get all possible groups we can use as a doclist group
  759.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  760.         $repository->add('select''g.idGroup, g.groupname');
  761.         $repository->add('from''OceanExpertBundle:Groups g');
  762.         $repository->where('g.hasSite = 1');
  763.         $allDoclistGroups $repository->getQuery()->getResult();
  764.         if ($request->isMethod('POST')) {
  765.             //we are updating or creating a doclist
  766.             //save the data and show the edit form again
  767.             $doclistData $request->request->all();
  768.             if ($idDoclist != 0) {
  769.                 //this is an existing doclist
  770.                 $doclist $em->getRepository('OceanExpertBundle:Doclists')
  771.                     ->findOneBy(
  772.                         array(
  773.                             'idDoclist' => $idDoclist
  774.                         )
  775.                     );
  776.             } else {
  777.                 //create a new doclist
  778.                 $doclist = new Doclists();
  779.                 $doclist->setCreatedAt(new DateTime('now'));
  780.                 $doclist->setCreatedBy($author->getId());
  781.             }
  782.             //set the data for the doclist
  783.             $doclist->setTitle(
  784.                 $doclistData['title'] ?? ''
  785.             );
  786.             $doclist->setDescription(
  787.                 $doclistData['description'] ?? ''
  788.             );
  789.             $doclist->setIsPublic(
  790.                 $doclistData['isPublic'] ?? 0
  791.             );
  792.             $doclist->setOrderBy(
  793.                 $doclistData['order'] ?? 1
  794.             );
  795.             $doclist->setUpdatedAt(new DateTime('now'));
  796.             $em->persist($doclist);
  797.             $em->flush();
  798.             $idDoclist $doclist->getIdDoclist();
  799.             //do we want to link this doclist to a doclist group?
  800.             //remove all existing groups for this doclist
  801.             $doclistGroups $em->getRepository('OceanExpertBundle:DoclistGroups')
  802.                 ->findBy(
  803.                     array(
  804.                         'idDoclist' => $idDoclist
  805.                     )
  806.                 );
  807.             foreach ($doclistGroups as $doclistGroup) {
  808.                 $em->remove($doclistGroup);
  809.             }
  810.             $em->flush();
  811.             //add the new groups to the doclist
  812.             if (isset($doclistData['websiteGroups'])
  813.                 && count($doclistData['websiteGroups'])
  814.             ) {
  815.                 foreach ($doclistData['websiteGroups'] as $websiteGroupId) {
  816.                     $doclistGroup = new DoclistGroups();
  817.                     $doclistGroup->setIdDoclist($idDoclist);
  818.                     $doclistGroup->setIdGroup($websiteGroupId);
  819.                     $em->persist($doclistGroup);
  820.                     $em->flush();
  821.                 }
  822.             }
  823.             //show the edit form again
  824.             return $this->redirect(
  825.                 $this->generateUrl(
  826.                     'edit_doclist',
  827.                     array(
  828.                         'idDoclist' => $idDoclist
  829.                     )
  830.                 )
  831.             );
  832.         }
  833.         $data $this->getDoclistDocuments($idDoclist);
  834.         $data['allDoclistGroups'] =  $allDoclistGroups;
  835.         $data['doclistOrder'] = array(
  836.           '0' => 'title',
  837.           '1' => 'date published (most recent first)',
  838.           '2' => 'date published (oldest first)',
  839.           '3' => 'date added to list (most recent first)',
  840.           '4' => 'date added to list (oldest first)',
  841.           '5' => 'document code (reverse alphabetically)',
  842.           '6' => 'document code (alphabetically)',
  843.         );
  844.         if ($idDoclist != 0) {
  845.             //get all the doclist groups for this doclist
  846.             $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  847.             $repository->add('select''g.idGroup');
  848.             $repository->add('from''OceanExpertBundle:DoclistGroups g');
  849.             $repository->where('g.idDoclist = :idDoclist');
  850.             $repository->setParameter('idDoclist'$idDoclist);
  851.             $data['doclistGroups'] = $repository->getQuery()->getResult();
  852.             //create an array with the ids of the doclist groups
  853.             // so we can use that in the form
  854.             foreach ($data['doclistGroups'] as $doclistGroup) {
  855.                 $data['doclistGroupIds'][] = $doclistGroup['idGroup'];
  856.             }
  857.         }
  858.         return $this->render(
  859.             'Documents/editDoclist.html.twig',
  860.             array(
  861.                 'data' => $data
  862.             )
  863.         );
  864.     }
  865.     /**
  866.      * @param Request $request
  867.      *
  868.      * @return Response
  869.      */
  870.     public function documentDownloadBlockAction(Request $request): Response
  871.     {
  872.         if (null != $request->request->get('idDoc')) {
  873.             $idDoc $request->request->get('idDoc');
  874.             $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  875.             $repository->add('select''d.idDoc,d.url, df.idFile, df.version, l.languagename, l.languagecode, ff.mimetype, ff.extension,f.filename, f.size, f.content');
  876.             $repository->add('from''OceanExpertBundle:Documents d');
  877.             $repository->leftJoin('OceanExpertBundle:DocumentFiles''df''WITH''d.idDoc = df.idDoc');
  878.             $repository->leftJoin('OceanExpertBundle:Languages''l''WITH''df.idLanguage = l.idLanguage');
  879.             $repository->leftJoin('OceanExpertBundle:Fileformats''ff''WITH''df.idFileformat = ff.idFileformat');
  880.             $repository->leftJoin('OceanExpertBundle:Files''f''WITH''df.idFile = f.idFile');
  881.             $repository->where('d.idDoc =:idDoc');
  882.             $repository->setParameter('idDoc'$idDoc);
  883.             $documents $repository->getQuery()->getResult();
  884.             return $this->render(
  885.                 'Documents/downloadDocument.html.twig',
  886.                 array(
  887.                     'documents' => $documents
  888.                 )
  889.             );
  890.         } else {
  891.             return new Response('No document Available');
  892.         }
  893.     }
  894.     /**
  895.      * download the file with the given file id (!= doc id)
  896.      *
  897.      * @param int $idFile
  898.      *
  899.      * @return mixed
  900.      */
  901.     public function downloadFileAction($idFile)
  902.     {
  903.         if ($idFile == 0) {
  904.             return new JsonResponse(
  905.                 array(
  906.                     'status' => 1,
  907.                     'error' => 'file id is missing, nothing to be downloaded'
  908.                 )
  909.             );
  910.         } else {
  911.             $fileData $this->getDoctrine()
  912.                 ->getRepository('OceanExpertBundle:Files')
  913.                 ->findOneBy(
  914.                     array(
  915.                         'idFile' => $idFile
  916.                     )
  917.                 );
  918.             if ($fileData) {
  919.                 /*
  920.                  * no idea why we should do that...
  921.                 $downloadFileName = preg_replace(
  922.                     '/(.+)-(.+?)$/',
  923.                     "$1.$2",
  924.                     basename($fileData->getFilename())
  925.                 );
  926.                 */
  927.                 //clean the filename that will be used to download the file
  928.                 // see #433
  929.                 $downloadFileName trim(
  930.                     basename(
  931.                         $fileData->getFilename()
  932.                     )
  933.                 );
  934.                 $downloadFileName preg_replace(
  935.                     '/[[:^print:]]/',
  936.                     '_',
  937.                     $downloadFileName
  938.                 );
  939.                 //sometimes we have like very strange filenames
  940.                 //where the file extension is glued to the filename
  941.                 //get rid of that glue and make it a file extension again
  942.                 $fileExtensionId $fileData->getIdFileFormat();
  943.                 $fileExtensionData $this->getDoctrine()
  944.                     ->getRepository('OceanExpertBundle:Fileformats')
  945.                     ->findOneBy(
  946.                         array(
  947.                             'idFileformat' => $fileExtensionId
  948.                         )
  949.                     );
  950.                 if ($fileExtensionData) {
  951.                     $fileExtension $fileExtensionData->getExtension();
  952.                 } else {
  953.                     return new JsonResponse(
  954.                         array(
  955.                             'status' => 1,
  956.                             'error' => "problem : cannot get the file extension data from $downloadFileName"
  957.                         )
  958.                     );
  959.                 }
  960.                 if (!preg_match("/(.+?)\.$fileExtension$/"$downloadFileName)) {
  961.                     $downloadFileName preg_replace(
  962.                         "/(.+)\W$fileExtension$/",
  963.                         "$1.$fileExtension",
  964.                         $downloadFileName
  965.                     );
  966.                     /*
  967.                     return new JsonResponse(
  968.                         array(
  969.                             'status' => 1,
  970.                             'error' => "correct : file ext $fileExtensionId : $fileExtension : $downloadFileName"
  971.                         )
  972.                     );
  973.                     */
  974.                 }
  975.                 /*
  976.                 return new JsonResponse(
  977.                     array(
  978.                         'status' => 1,
  979.                         'error' => "wrong : file ext $fileExtensionId : $fileExtension : $downloadFileName"
  980.                     )
  981.                 );
  982.                 */
  983.                 if ($fileData->getUrl() == '') {
  984.                     //this is a file that we have stored in the db
  985.                     // Return a response with a specific content
  986.                     $response = new Response(stream_get_contents($fileData->getContent()));
  987.                     // Create the disposition of the file
  988.                     $disposition $response->headers->makeDisposition(
  989.                         ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  990.                         $downloadFileName
  991.                     );
  992.                     // Set the content disposition
  993.                     $response->headers->set('Content-Disposition'$disposition);
  994.                     // Dispatch request
  995.                     return $response;
  996.                 } else {
  997.                     //check if the file exists
  998.                     $fileToCheck $fileData->getUrl();
  999.                     if (is_file($fileToCheck)) {
  1000.                         $response = new BinaryFileResponse($fileData->getUrl());
  1001.                         $response->setContentDisposition(
  1002.                             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  1003.                             $downloadFileName
  1004.                         );
  1005.                         return $response;
  1006.                     } else {
  1007.                         return new JsonResponse(
  1008.                             array(
  1009.                                 'status' => 3,
  1010.                                 'error' => "no file with file id '$idFile' found on the server ('$fileToCheck')"
  1011.                             )
  1012.                         );
  1013.                     }
  1014.                 }
  1015.             } else {
  1016.                 return new JsonResponse(
  1017.                     array(
  1018.                         'status' => 2,
  1019.                         'error' => "no file found with file id '$idFile'"
  1020.                     )
  1021.                 );
  1022.             }
  1023.         }
  1024.     }
  1025.     /**
  1026.      * @param int     $idDoc
  1027.      * @param Request $request
  1028.      *
  1029.      * @return Response
  1030.      */
  1031.     public function addDocumentAction(int $idDocRequest $request)
  1032.     {
  1033.         $document = array();
  1034.         $docDoclist = array();
  1035.         $files = array();
  1036.         $type '';
  1037.         $postdata '';
  1038.         $filedata '';
  1039.         $userId $this->get('security.token_storage')
  1040.             ->getToken()
  1041.             ->getUser()
  1042.             ->getid();
  1043.         $em $this->getDoctrine()
  1044.             ->getManager();
  1045.         if ($idDoc != 0) {
  1046.             $document $this->getDoctrine()
  1047.                 ->getRepository('OceanExpertBundle:Documents')
  1048.                 ->findOneBy(
  1049.                     array(
  1050.                         'idDoc' => $idDoc
  1051.                     )
  1052.                 );
  1053.         }
  1054.         if ($request->request->has('submit')) {
  1055.             $postdata $request->request->all();
  1056.             $filedata $request->files->get('userfile');
  1057.             if (array_key_exists('doctype'$postdata)
  1058.                 && $postdata['doctype'] != ''
  1059.                 && $postdata['status'] != '') {
  1060.                 /**
  1061.                  * Create a document entry in documents table
  1062.                  */
  1063.                 if ($idDoc == 0) {
  1064.                     $document = new Documents();
  1065.                 }
  1066.                 $document->setIdDoctype($postdata['doctype']);
  1067.                 $document->setDocCode(isset($postdata['doccode']) ? $postdata['doccode'] : '');
  1068.                 $document->setTitle(isset($postdata['title']) ? $postdata['title'] : '');
  1069.                 $document->setSummary(isset($postdata['summary']) ? $postdata['summary'] : '');
  1070.                 $document->setAuthorText(isset($postdata['authors']) ? $postdata['authors'] : '');
  1071.                 if ($postdata['status'] == 2) {
  1072.                     $document->setPublishedOn(new DateTime('now'));
  1073.                 }
  1074.                 $document->setNotes(isset($postdata['notes']) ? $postdata['notes'] : '');
  1075.                 $document->setKeywords(isset($postdata['keywords']) ? $postdata['keywords'] : '');
  1076.                 $document->setUrl(isset($postdata['websiteUrl']) ? $postdata['websiteUrl'] : '');
  1077.                 $document->setIdDocstatus(isset($postdata['status']) ? $postdata['status'] : '');
  1078.                 $document->setCreatedAt(new DateTime('now'));
  1079.                 $document->setUpdatedAt(new DateTime('now'));
  1080.                 if ($this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')) {
  1081.                     $document->setApproved(1);
  1082.                 } else {
  1083.                     $document->setApproved(0);
  1084.                 }
  1085.                 $document->setIdCreator($userId);
  1086.                 $document->setLastEditBy($userId);
  1087.                 $em->persist($document); //marks object to be saved in the next transaction.
  1088.                 $em->flush(); //performs all saves and transactions.
  1089.                 if (isset($postdata['doclist'])
  1090.                     && count($postdata['doclist']) > 0
  1091.                 ) {
  1092.                     /**
  1093.                      * if documentlist is set then enter id documentlist table.
  1094.                      */
  1095.                     foreach ($postdata['doclist'] as $docListID) {
  1096.                         $doclist $em->getRepository('OceanExpertBundle:DoclistDocuments')
  1097.                             ->findOneBy(
  1098.                                 array(
  1099.                                     'idDoclist' => $docListID,
  1100.                                     'idDoc' => $document->getIdDoc()
  1101.                                 )
  1102.                             );
  1103.                         if (!$doclist) {
  1104.                             $doclist = new DoclistDocuments();
  1105.                         }
  1106.                         $doclist->setIdDoclist($docListID);
  1107.                         $doclist->setIdDoc($document->getIdDoc());
  1108.                         $doclist->setAddedAt(new DateTime('now'));
  1109.                         $em->persist($doclist);
  1110.                         $em->flush();
  1111.                     }
  1112.                 }
  1113.                 if (count($filedata) > 0) {
  1114.                     /**
  1115.                      * if file is uploaded then upload the files in event section.
  1116.                      */
  1117.                     $i 0;
  1118.                     foreach ($filedata as $file) {
  1119.                         if ($file != null) {
  1120.                             $fileType $file->guessClientExtension();
  1121.                             //cleanup the filename, we don't want any strange chars here
  1122.                             $fileName $file->getClientOriginalName();
  1123.                             $fileName preg_replace(
  1124.                                 array(
  1125.                                     '/\s+/',
  1126.                                     '/\W+/',
  1127.                                     ),
  1128.                                 array(
  1129.                                     '_',
  1130.                                     '-'
  1131.                                 ),
  1132.                                 $fileName
  1133.                             );
  1134.                             if ($fileType == 'bin') {
  1135.                                 $fileType pathinfo($fileNamePATHINFO_EXTENSION);
  1136.                             }
  1137.                             $format $em->getRepository('OceanExpertBundle:Fileformats')
  1138.                                 ->findOneBy(
  1139.                                     array(
  1140.                                         'extension' => $fileType
  1141.                                     )
  1142.                                 );
  1143.                             if ($format) {
  1144.                                 $idFileFormat $format->getIdFileformat();
  1145.                             } else {
  1146.                                 $idFileFormat 0;
  1147.                             }
  1148.                             $directory 'uploads/documents/' $document->getIdDoc() . '/';
  1149.                             $uploadFile $file->move($directory$fileName);
  1150.                             $filepath $directory $fileName;
  1151.                             /**
  1152.                              * Create file entry in files table. == filenew is added as multiple column primary indexes were set in old table,
  1153.                              * cannot change as it might affect previos PPC sections. needs to verify if there is no break of code in the e
  1154.                              * earlier ppc.
  1155.                              */
  1156.                             if (isset($postdata['id_language'][$i])) {
  1157.                                 $language $postdata['id_language'][$i];
  1158.                             } else {
  1159.                                 $language 1;
  1160.                             }
  1161.                             $conn $this->getDoctrine()->getConnection();
  1162.                             $stmt $conn->prepare('INSERT 
  1163.                                 INTO files (
  1164.                                             version, 
  1165.                                             id_language, 
  1166.                                             id_fileformat, 
  1167.                                             url, 
  1168.                                             size, 
  1169.                                             content, 
  1170.                                             filename
  1171.                                             ) 
  1172.                                 VALUES (?, ?, ?, ?, ?, ?, ?)'
  1173.                             );
  1174.                             $stmt->bindValue(1$postdata['version'][$i]);
  1175.                             $stmt->bindValue(2$language);
  1176.                             $stmt->bindValue(3$idFileFormat);
  1177.                             $stmt->bindValue(4utf8_encode($filepath));
  1178.                             $stmt->bindValue(5$file->getClientSize());
  1179.                             $stmt->bindValue(6'');
  1180.                             $stmt->bindValue(7utf8_encode($filepath));
  1181.                             $stmt->execute();
  1182.                             $idFile $this->getDoctrine()->getConnection()->lastInsertId();
  1183.                             $files = new DocumentFiles();
  1184.                             $files->setIdDoc($document->getIdDoc());
  1185.                             $files->setIdFile($idFile);
  1186.                             $files->setVersion($postdata['version'][$i]);
  1187.                             $files->setIdLanguage($language);
  1188.                             $files->setIdFileformat($idFileFormat);
  1189.                             $em->persist($files);
  1190.                             $em->flush();
  1191.                             $i++;
  1192.                         }
  1193.                     }
  1194.                 }
  1195.             }
  1196.             return $this->redirectToRoute(
  1197.                 'view_document',
  1198.                 array(
  1199.                     'idDoc' => $document->getIdDoc()
  1200.                 )
  1201.             );
  1202.         }
  1203.         if ($idDoc != 0) {
  1204.             $repository $em->createQueryBuilder();
  1205.             $repository->add(
  1206.                 'select',
  1207.                 'd.idDoclist'
  1208.             );
  1209.             $repository->add('from''OceanExpertBundle:DoclistDocuments d');
  1210.             $repository->where('d.idDoc = :idDoc');
  1211.             $repository->setParameter('idDoc'$idDoc);
  1212.             $docDoclist array_column($repository->getQuery()->getResult(), 'idDoclist');
  1213.             $repository $em->createQueryBuilder();
  1214.             $repository->add(
  1215.                 'select',
  1216.                 'd.idDoc, l.languagename, f.fileformatname,d.version, d.idFileformat,d.idFile'
  1217.             );
  1218.             $repository->add('from''OceanExpertBundle:DocumentFiles d');
  1219.             $repository->leftJoin('OceanExpertBundle:Languages''l''WITH''d.idLanguage = l.idLanguage');
  1220.             $repository->leftJoin('OceanExpertBundle:Fileformats''f''WITH''f.idFileformat = d.idFileformat');
  1221.             $repository->where('d.idDoc = :idDoc');
  1222.             $repository->setParameter('idDoc'$idDoc);
  1223.             $files $repository->getQuery()->getResult();
  1224.         }
  1225.         $docType = array(
  1226.             => 'Report',
  1227.             => 'Presentation',
  1228.             => 'Information Document',
  1229.             => 'Letter',
  1230.             => 'Book',
  1231.             => 'MOU',
  1232.             => 'Other',
  1233.             => 'Link to online document',
  1234.             10 => 'Reference Document',
  1235.             11 => 'Working Document'
  1236.         );
  1237.         $data = array(
  1238.             'document' => $document,
  1239.             'docDoclist' => $docDoclist,
  1240.             'type' => $type,
  1241.             'postdata' => $postdata,
  1242.             'filedata' => $filedata,
  1243.             'files' => $files,
  1244.             'docTypes' => $docType,
  1245.             'documentList' => $this->getDocumentList(),
  1246.         );
  1247.         return $this->render(
  1248.             'Documents/editDocument.html.twig',
  1249.             array(
  1250.                 'data' => $data
  1251.             )
  1252.         );
  1253.     }
  1254.     /**
  1255.      * view the info of a document with the given doc id
  1256.      *
  1257.      * @param $idDoc
  1258.      *
  1259.      * @return Response
  1260.      */
  1261.     public function viewDocumentAction($idDoc)
  1262.     {
  1263.         $em $this->getDoctrine()->getManager();
  1264.         $queryBuilder $em->getRepository('OceanExpertBundle:Documents')->createQueryBuilder('d');
  1265.         $queryBuilder->select('
  1266.                 d.docCode, 
  1267.                 d.idDoc, 
  1268.                 d.idDocstatus, 
  1269.                 d.title, 
  1270.                 d.summary, 
  1271.                 d.authorText, 
  1272.                 d.publishedOn, 
  1273.                 d.notes, 
  1274.                 d.keywords, 
  1275.                 d.createdAt, 
  1276.                 d.updatedAt,
  1277.                 d.idCreator,
  1278.                 d.lastEditBy,
  1279.                 d.url,
  1280.                 d.idDoctype,
  1281.                 dt.doctypename ')
  1282.             ->leftJoin(
  1283.                 'OceanExpertBundle:Doctypes',
  1284.                 'dt',
  1285.                 'WITH',
  1286.                 'd.idDoctype = dt.idDoctype')
  1287.             ->where('d.idDoc = :idDoc');
  1288.         if (!($this->get('security.authorization_checker')->isGranted('ROLE_MANAGER'))) {
  1289.             $queryBuilder->andWhere('d.approved = 1');
  1290.         }
  1291.         $queryBuilder->setParameter('idDoc'$idDoc);
  1292.         $query $queryBuilder->getQuery();
  1293.         $result $query->getResult();
  1294.         if (count($result)>0) {
  1295.             $result $result[0];
  1296.             if ($result['idCreator'] != '') {
  1297.                 $indiv $this->getDoctrine()->getRepository('OceanExpertBundle:Indiv')->findOneByIdInd($result['idCreator']);
  1298.                 if ($indiv) {
  1299.                     $result['createdBy'] = $indiv->getFname() . ' ' $indiv->getSname();
  1300.                 } else {
  1301.                     $result['createdBy'] = '';
  1302.                 }
  1303.             }
  1304.             if ($result['lastEditBy'] != '') {
  1305.                 $indiv $this->getDoctrine()->getRepository('OceanExpertBundle:Indiv')->findOneByIdInd($result['lastEditBy']);
  1306.                 if ($indiv) {
  1307.                     $result['updatedBy'] = $indiv->getFname() . ' ' $indiv->getSname();
  1308.                 } else {
  1309.                     $result['updatedBy'] = '';
  1310.                 }
  1311.             }
  1312.         }
  1313.         //check in what event this document is used
  1314.         //event_backgrounddocs
  1315.         //agendaitem_documents
  1316.         //event_otherdocs
  1317.         //event_presentations
  1318.         //event_reports
  1319.         $docEventList = array();
  1320.         $tmpEventList = array();
  1321.         $relatedTables = array(
  1322.             'EventBackgrounddocs',
  1323.             'AgendaitemDocuments',
  1324.             'EventOtherdocs',
  1325.             'EventPresentations',
  1326.             'EventReports'
  1327.         );
  1328.         foreach ($relatedTables as $relatedTable) {
  1329.             /*
  1330.             $eventList = $em->getRepository('OceanExpertBundle:Events')->createQueryBuilder('e')
  1331.                 ->select('e.idEvent,e.title')
  1332.                 ->leftJoin('OceanExpertBundle:EventBackgrounddocs', 'b', 'WITH', 'e.idEvent = b.idEvent')
  1333.                 ->leftJoin('OceanExpertBundle:AgendaitemDocuments', 'a', 'WITH', 'e.idEvent = a.idEvent')
  1334.                 ->leftJoin('OceanExpertBundle:EventOtherdocs', 'o', 'WITH', 'e.idEvent = o.idEvent')
  1335.                 ->leftJoin('OceanExpertBundle:EventPresentations', 'p', 'WITH', 'e.idEvent = p.idEvent')
  1336.                 ->leftJoin('OceanExpertBundle:EventReports', 'r', 'WITH', 'e.idEvent = r.idEvent')
  1337.                 ->where('b.idDoc = :idDoc')
  1338.                 ->orWhere('a.idDoc = :idDoc')
  1339.                 ->orWhere('o.idDoc = :idDoc')
  1340.                 ->orWhere('p.idDoc = :idDoc')
  1341.                 ->orWhere('r.idDoc = :idDoc')
  1342.                 ->setParameter('idDoc', $idDoc)
  1343.                 ->getQuery()
  1344.                 ->getResult();
  1345.             */
  1346.             $leftJoin 'OceanExpertBundle:' $relatedTable;
  1347.             $eventList $em->getRepository('OceanExpertBundle:Events')->createQueryBuilder('e')
  1348.                 ->select('distinct e.idEvent,e.title')
  1349.                 ->leftJoin($leftJoin'b''WITH''e.idEvent = b.idEvent')
  1350.                 ->where('b.idDoc = :idDoc')
  1351.                 ->setParameter('idDoc'$idDoc)
  1352.                 ->getQuery()
  1353.                 ->getResult();
  1354.             if (is_array($eventList)
  1355.                 && count($eventList)
  1356.             ) {
  1357.                 $tmpEventList array_merge($tmpEventList$eventList);
  1358.                 /*
  1359.                 foreach ($eventList as $event) {
  1360.                     $docEventList[$event['idEvent']] = $event;
  1361.                 }
  1362.                 */
  1363.             }
  1364.         }
  1365.         foreach ($tmpEventList as $event) {
  1366.             $docEventList[$event['idEvent']] = $event;
  1367.         }
  1368.         //dump($docEventList);
  1369.         //die();
  1370.         $groups $em->getRepository('OceanExpertBundle:DocumentGroups')->createQueryBuilder('d')
  1371.             ->select('d.idGroup,g.groupname')
  1372.             ->leftJoin('OceanExpertBundle:Groups''g''WITH''d.idGroup = g.idGroup')
  1373.             ->where('d.idDoc = :idDoc')
  1374.             ->setParameter('idDoc'$idDoc)
  1375.             ->getQuery()
  1376.             ->getResult();
  1377.         $docGroupList $em->getRepository('OceanExpertBundle:DoclistDocuments')->createQueryBuilder('d')
  1378.             ->select('dl.idDoclist, dl.title')
  1379.             ->leftJoin('OceanExpertBundle:Doclists''dl''WITH''dl.idDoclist = d.idDoclist')
  1380.             ->where('d.idDoc = :idDoc')
  1381.             ->setParameter('idDoc'$idDoc)
  1382.             ->orderBy('dl.title','ASC')
  1383.             ->getQuery()
  1384.             ->getResult();
  1385.         $docGroupListAll $em->getRepository('OceanExpertBundle:Doclists')->createQueryBuilder('d')
  1386.             ->select("d.idDoclist, CONCAT(g.groupname, ' - ', d.title) as title")
  1387.             ->leftJoin('OceanExpertBundle:DoclistGroups''dg''WITH''d.idDoclist = dg.idDoclist')
  1388.             ->leftJoin('OceanExpertBundle:Groups''g''WITH''dg.idGroup = g.idGroup')
  1389.             ->where("g.groupname != ''")
  1390.             ->orderBy('g.groupname, d.title','ASC')
  1391.             ->getQuery()
  1392.             ->getResult();
  1393.         $docFiles $em->getRepository('OceanExpertBundle:DocumentFiles')->createQueryBuilder('d')
  1394.             ->select('d.idFileformat, d.version, l.languagename, d.idFile, f.filename, f.url, f.size,f.content,f.path')
  1395.             ->leftJoin('OceanExpertBundle:Files''f''WITH''f.idFile = d.idFile')
  1396.             ->leftJoin('OceanExpertBundle:Languages''l''WITH''f.idLanguage = l.idLanguage')
  1397.             ->where('d.idDoc = :idDoc')
  1398.             ->setParameter('idDoc'$idDoc)
  1399.             ->getQuery()
  1400.             ->getResult();
  1401.         $docGroupListArr = array();
  1402.         foreach ($docGroupList as $doclist){
  1403.             $docGroupListArr[] = $doclist['idDoclist'];
  1404.         }
  1405.         $result['documentGroup'] = $groups;
  1406.         $result['docGroupList'] = $docGroupList;
  1407.         $result['docFiles'] = $docFiles;
  1408.         $result['docGroupListArr'] = $docGroupListArr;
  1409.         $result['docGroupListAll'] = $docGroupListAll;
  1410.         $result['docEventList'] = $docEventList;
  1411.         return $this->render(
  1412.             'Documents/viewDocument.html.twig',
  1413.             array(
  1414.                 'data' => $result
  1415.             )
  1416.         );
  1417.     }
  1418.     /**
  1419.      * show all existing documents
  1420.      *
  1421.      * @param Request $request
  1422.      *
  1423.      * @return Response|void
  1424.      */
  1425.     function viewDocumentsActionRequest $request): Response
  1426.     {
  1427.         $em $this->get('doctrine')->getManager();
  1428.         $connection $em->getConnection();
  1429.         $limits = array(
  1430.             10 => 10,
  1431.             25 => 25,
  1432.             50 => 50,
  1433.             100 => 100,
  1434.             500 => 500
  1435.         );
  1436.         $limit $request->query->get('limit'10);
  1437.         $query $request->query->get('search''');
  1438.         $orderby $request->query->get('orderby''order');
  1439.         $dir $request->query->get('dir''asc');
  1440.         if (!in_array($orderby, array('id''title'))) {
  1441.             $orderby 'title';
  1442.         }
  1443.         if (!in_array($dir, array('asc''desc'))) {
  1444.             $dir 'asc';
  1445.         }
  1446.         //get all the doclists
  1447.         $repo $this->getDoctrine()
  1448.             ->getRepository('OceanExpertBundle:Doclists');
  1449.         $docListsQB $repo->createQueryBuilder('dl');
  1450.         $docListsQB->select('
  1451.             dl.idDoclist, 
  1452.             dl.title'
  1453.         );
  1454.         $docListsQuery $docListsQB->getQuery();
  1455.         $docListsTmp $docListsQuery->execute();
  1456.         foreach($docListsTmp as $docList) {
  1457.             $docLists[$docList['idDoclist']] = $docList['title'];
  1458.         }
  1459.         //get all the doclist docs, this avoids having to make an join that is veryveryvery slow
  1460.         $repo $this->getDoctrine()
  1461.             ->getRepository('OceanExpertBundle:DoclistDocuments');
  1462.         $docListDocumentsQB $repo->createQueryBuilder('dld');
  1463.         $docListDocumentsQB->select('
  1464.             dld.id,
  1465.             dld.idDoclist, 
  1466.             dld.idDoc'
  1467.         );
  1468.         $docListDocumentsQuery $docListDocumentsQB->getQuery();
  1469.         $docListDocumentsTmp $docListDocumentsQuery->execute();
  1470.         foreach($docListDocumentsTmp as $docListDocument) {
  1471.             $docListDocuments[$docListDocument['idDoc']][] = $docListDocument;
  1472.         }
  1473.         $repo $this->getDoctrine()
  1474.             ->getRepository('OceanExpertBundle:Documents');
  1475.         $documents $repo->createQueryBuilder('d');
  1476.         $documents->select('
  1477.             d.idDoc, 
  1478.             d.title,
  1479.             d.createdAt'
  1480.         );
  1481.         if (trim($query) != '') {
  1482.             $documents->andwhere('d.title LIKE :query');
  1483.             $documents->setParameter(
  1484.                 'query',
  1485.                 '%' trim($query) . '%'
  1486.             );
  1487.         }
  1488.         $documents->getQuery();
  1489.         $paginator $this->get('knp_paginator');
  1490.         $data $paginator->paginate(
  1491.             $documents,
  1492.             $request->query->getInt('page'1),
  1493.             $limit,
  1494.             array(
  1495.                 'pageParameterName' => 'page',
  1496.                 'sortDirectionParameterName' => 'dir',
  1497.                 'defaultSortFieldName' => 'd.createdAt',
  1498.                 'defaultSortDirection' => 'desc'
  1499.             )
  1500.         );
  1501.         $data->setCustomParameters([
  1502.             'limits' => $limits,
  1503.             'title' => 'Documents'
  1504.         ]);
  1505.         return $this->render(
  1506.             'Documents/viewDocuments.html.twig',
  1507.             array(
  1508.                 'documentData' => $data,
  1509.                 'doclists' => $docLists,
  1510.                 'doclistDocuments' => $docListDocuments
  1511.             )
  1512.         );
  1513.     }
  1514.     /**
  1515.      * delete a document
  1516.      * this should not be called in the browser or via a direct link
  1517.      *
  1518.      * @return Response
  1519.      */
  1520.     public function deleteDocumentAction(Request $request)
  1521.     {
  1522.         $idDoc $request->request->get('idDoc');
  1523.         if ($idDoc
  1524.             && $idDoc != ''
  1525.             && is_numeric($idDoc)
  1526.         ) {
  1527.             $em $this->getDoctrine()->getManager();
  1528.             //we will not delete files that are still in an event or doclist
  1529.             $eventList $em->getRepository('OceanExpertBundle:Events')->createQueryBuilder('e')
  1530.                 ->select('e.idEvent,e.title')
  1531.                 ->leftJoin('OceanExpertBundle:EventBackgrounddocs''b''WITH''e.idEvent = b.idEvent')
  1532.                 ->leftJoin('OceanExpertBundle:AgendaitemDocuments''a''WITH''e.idEvent = a.idEvent')
  1533.                 ->leftJoin('OceanExpertBundle:EventOtherdocs''o''WITH''e.idEvent = o.idEvent')
  1534.                 ->leftJoin('OceanExpertBundle:EventPresentations''p''WITH''e.idEvent = p.idEvent')
  1535.                 ->leftJoin('OceanExpertBundle:EventReports''r''WITH''e.idEvent = r.idEvent')
  1536.                 ->where('b.idDoc = :idDoc')
  1537.                 ->orWhere('a.idDoc = :idDoc')
  1538.                 ->orWhere('o.idDoc = :idDoc')
  1539.                 ->orWhere('p.idDoc = :idDoc')
  1540.                 ->orWhere('r.idDoc = :idDoc')
  1541.                 ->setParameter('idDoc'$idDoc)
  1542.                 ->getQuery()
  1543.                 ->getResult();
  1544.             $docList $em->getRepository('OceanExpertBundle:DoclistDocuments')->createQueryBuilder('d')
  1545.                 ->select('dl.idDoclist, dl.title')
  1546.                 ->leftJoin('OceanExpertBundle:Doclists''dl''WITH''dl.idDoclist = d.idDoclist')
  1547.                 ->where('d.idDoc = :idDoc')
  1548.                 ->setParameter('idDoc'$idDoc)
  1549.                 ->orderBy('dl.title','ASC')
  1550.                 ->getQuery()
  1551.                 ->getResult();
  1552.             if ((is_array($eventList)
  1553.                 && count($eventList))
  1554.                 || (is_array($docList)
  1555.                     && count($docList))
  1556.             ) {
  1557.                 $data = array(
  1558.                     'status' => 2,
  1559.                     'message' => "document cannot be deleted as it is still used in either a doclist or an event"
  1560.                 );
  1561.             } else {
  1562.                 $document $em->getRepository('OceanExpertBundle:Documents')
  1563.                     ->findOneBy(
  1564.                         array(
  1565.                             'idDoc' => $idDoc
  1566.                         )
  1567.                     );
  1568.                 $documentFiles $em->getRepository('OceanExpertBundle:DocumentFiles')
  1569.                     ->findBy(
  1570.                         array(
  1571.                             'idDoc' => $idDoc
  1572.                         )
  1573.                     );
  1574.                 if ($document) {
  1575.                     $em->remove($document);
  1576.                     $em->flush();
  1577.                 }
  1578.                 if ($documentFiles) {
  1579.                     foreach ($documentFiles as $documentFile) {
  1580.                         $file $em->getRepository('OceanExpertBundle:Files')
  1581.                             ->findOneBy(
  1582.                                 array(
  1583.                                     'idFile' => $documentFile->getIdFile()
  1584.                                 )
  1585.                             );
  1586.                         if ($file) {
  1587.                             if (file_exists($file->getFilename())) {
  1588.                                 unlink($file->getFilename());
  1589.                             }
  1590.                             $em->remove($file);
  1591.                             $em->flush();
  1592.                         }
  1593.                         $em->remove($documentFile);
  1594.                         $em->flush();
  1595.                     }
  1596.                 }
  1597.                 $data = array('status' => 1);
  1598.             }
  1599.         } else {
  1600.             $data = array(
  1601.                 'status' => 0,
  1602.                 'message' => "no document id found, did you call this from a browser? Don't"
  1603.             );
  1604.         }
  1605.         return new JsonResponse($data);
  1606.     }
  1607.     public function removeDocumentFileAction(Request $request)
  1608.     {
  1609.         $idDoc $request->request->get('idDoc');
  1610.         $idFile $request->request->get('idFile');
  1611.         if (isset($idDoc)
  1612.             && is_numeric($idDoc)
  1613.             && isset($idFile)
  1614.             && is_numeric($idFile)
  1615.         ) {
  1616.             $em $this->getDoctrine()->getManager();
  1617.             $docFiles $em->getRepository('OceanExpertBundle:DocumentFiles')
  1618.                 ->findOneBy(
  1619.                     array(
  1620.                         'idDoc' => $idDoc,
  1621.                         'idFile' => $idFile
  1622.                     )
  1623.                 );
  1624.             if ($docFiles) {
  1625.                 $em->remove($docFiles);
  1626.                 $em->flush();
  1627.             }
  1628.             $file $em->getRepository('OceanExpertBundle:Files')->findOneBy(array('idFile' => $idFile));
  1629.             if (file_exists($file->getFilename())) {
  1630.                 unlink($file->getFilename());
  1631.             }
  1632.             if ($file) {
  1633.                 $em->remove($file);
  1634.                 $em->flush();
  1635.             }
  1636.             $data = array('status' => 1);
  1637.         } else {
  1638.             $data = array(
  1639.                 'status' => 0,
  1640.                 'message' => 'do not call this directly in the browser, we need post data'
  1641.             );
  1642.         }
  1643.         return new JsonResponse($data);
  1644.     }
  1645.     /**
  1646.      * @return Response
  1647.      */
  1648.     public function removeDocumentFromListAction(Request $request)
  1649.     {
  1650.         $idDoc $request->request->get('idDoc');
  1651.         $idDoclist $request->request->get('idDoclist');
  1652.         $em $this->getDoctrine()->getManager();
  1653.         $document $em->getRepository('OceanExpertBundle:DoclistDocuments')->findOneBy(array('idDoc' => $idDoc,'idDoclist'=>$idDoclist));
  1654.         if ($document) {
  1655.             $em->remove($document);
  1656.             $em->flush();
  1657.         }
  1658.         $data = array('status' => 1);
  1659.         return new JsonResponse($data);
  1660.     }
  1661.     /**
  1662.      * @return Response
  1663.      */
  1664.     public function deleteDocumentListAction(Request $request)
  1665.     {
  1666.         $idDoclist $request->request->get('idDoclist');
  1667.         $em $this->getDoctrine()->getManager();
  1668.         $documentList $em->getRepository('OceanExpertBundle:Doclists')->findOneBy(array('idDoclist'=>$idDoclist));
  1669.         if ($documentList) {
  1670.             $em->remove($documentList);
  1671.             $em->flush();
  1672.         }
  1673.         $documents $em->getRepository('OceanExpertBundle:DoclistDocuments')->findBy(array('idDoclist'=>$idDoclist));
  1674.         if ($documents) {
  1675.             foreach ($documents as $document){
  1676.                 $em->remove($document);
  1677.                 $em->flush();
  1678.             }
  1679.         }
  1680.         $data = array('status' => 1);
  1681.         return new JsonResponse($data);
  1682.     }
  1683.     /**
  1684.      * get all the user groups this current active user belongs to
  1685.      *
  1686.      * @return array | boolean
  1687.      */
  1688.     function getUserGroups()
  1689.     {
  1690.         $userId $this->get('security.token_storage')
  1691.             ->getToken()
  1692.             ->getUser()
  1693.             ->getid();
  1694.         if ($userId) {
  1695.             $repository $this->getDoctrine()
  1696.                 ->getManager()
  1697.                 ->createQueryBuilder();
  1698.             $repository->add(
  1699.                 'select',
  1700.                 'g.idGroup'
  1701.             );
  1702.             $repository->add(
  1703.                 'from',
  1704.                 'OceanExpertBundle:MemberGroups g'
  1705.             );
  1706.             $repository->where(
  1707.                 'g.idInd = :userId'
  1708.             );
  1709.             $repository->setParameter(
  1710.                 'userId',
  1711.                 $userId
  1712.             );
  1713.             return $repository->getQuery()->getResult();
  1714.         } else {
  1715.             return false;
  1716.         }
  1717.     }
  1718.     /**
  1719.      * @return array
  1720.      * 
  1721.      * @todo : check if we still need this function, it is not used anywhere in the code
  1722.      */
  1723.     function getDocumentList(){
  1724.         //this is what we will return
  1725.         $documentList = array();
  1726.         //get all the user groups this current active user belongs to
  1727.         $grouplist array_map(
  1728.             'current',
  1729.             $this->getUserGroups()
  1730.         );
  1731.         //get all the doclists that are linked to these groups
  1732.         $repository $this->getDoctrine()
  1733.             ->getManager()
  1734.             ->createQueryBuilder();
  1735.         $repository->add(
  1736.             'select',
  1737.             'distinct(d.idGroup) as idGroup, g.groupname'
  1738.         );
  1739.         $repository->add(
  1740.             'from',
  1741.             'OceanExpertBundle:DoclistGroups d'
  1742.         );
  1743.         $repository->leftJoin(
  1744.             'OceanExpertBundle:Groups',
  1745.             'g',
  1746.             'WITH',
  1747.             'd.idGroup = g.idGroup'
  1748.         );
  1749.         $repository->where(
  1750.             'g.idGroup in (:grouplist)'
  1751.         );
  1752.         $repository->setParameter(
  1753.             'grouplist',
  1754.             $grouplist
  1755.         );
  1756.         $repository->orderBy('g.groupname');
  1757.         $docGroups $repository->getQuery()->getResult();
  1758.         foreach ($docGroups as $docGroup) {
  1759.             $repository $this->getDoctrine()
  1760.                 ->getManager()
  1761.                 ->createQueryBuilder();
  1762.             $repository->add(
  1763.                 'select',
  1764.                 'd.idDoclist, d.title, dg.idGroup'
  1765.             );
  1766.             $repository->add(
  1767.                 'from',
  1768.                 'OceanExpertBundle:Doclists d'
  1769.             );
  1770.             $repository->leftJoin(
  1771.                 'OceanExpertBundle:DoclistGroups',
  1772.                 'dg',
  1773.                 'WITH',
  1774.                 'dg.idDoclist = d.idDoclist'
  1775.             );
  1776.             $repository->where('dg.idGroup =:idGroup');
  1777.             $repository->setParameter(
  1778.                 'idGroup',
  1779.                 $docGroup['idGroup']
  1780.             );
  1781.             $repository->orderBy('d.title''asc');
  1782.             $documentList[$docGroup['groupname']] = $repository->getQuery()->getResult();
  1783.         }
  1784.         return $documentList;
  1785.     }
  1786.     /**
  1787.      * add a document to a doclist
  1788.      *
  1789.      * @param Request $request
  1790.      *
  1791.      * @return JsonResponse
  1792.      *
  1793.      */
  1794.     public function addDocumentToDoclistAction(Request $request)
  1795.     {
  1796.         $em $this->getDoctrine()->getManager();
  1797.         //dump($request->request->all());
  1798.         //die();
  1799.         $idDoclist $request->request->get('idDoclist');
  1800.         $idDoc $request->request->get('idDoc');
  1801.         if ($idDoclist == ''
  1802.             || $idDoclist == 'undefined'
  1803.             || $idDoc == ''
  1804.         ) {
  1805.             $message 'We really need a document id and a doclist id to do this. ';
  1806.             $message .= 'Did you call this directly in the browser? You should not do this.';
  1807.             return new JsonResponse(
  1808.                 array(
  1809.                     'status' => 2,
  1810.                     'error' => $message
  1811.                 )
  1812.             );
  1813.         }
  1814.         $doclistDocument $em->getRepository('OceanExpertBundle:DoclistDocuments')
  1815.             ->findOneBy(
  1816.                 array(
  1817.                     'idDoclist' => $idDoclist,
  1818.                     'idDoc' => $idDoc)
  1819.             );
  1820.         if (!$doclistDocument) {
  1821.             $doclistDocument = new DoclistDocuments();
  1822.         }
  1823.         $doclistDocument->setIdDoclist($idDoclist);
  1824.         $doclistDocument->setIdDoc($idDoc);
  1825.         $doclistDocument->setAddedAt(new DateTime('now'));
  1826.         $em->persist($doclistDocument);
  1827.         $em->flush();
  1828.         return new JsonResponse(
  1829.             array(
  1830.                 'status' => 1
  1831.             )
  1832.         );
  1833.     }
  1834. }