src/OceanExpertBundle/Controller/DocumentController.php line 1531

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.             /*
  905.             return new JsonResponse(
  906.                 array(
  907.                     'status' => 1,
  908.                     'error' => 'file id is missing, nothing to be downloaded'
  909.                 )
  910.             );
  911.             */
  912.             return $this->render(
  913.                 'Exception/error.html.twig',
  914.                 array(
  915.                     'message' => 'file id is missing, nothing to be downloaded'
  916.                 )
  917.             );
  918.         } else {
  919.             $fileData $this->getDoctrine()
  920.                 ->getRepository('OceanExpertBundle:Files')
  921.                 ->findOneBy(
  922.                     array(
  923.                         'idFile' => $idFile
  924.                     )
  925.                 );
  926.             if ($fileData) {
  927.                 /*
  928.                  * no idea why we should do that...
  929.                 $downloadFileName = preg_replace(
  930.                     '/(.+)-(.+?)$/',
  931.                     "$1.$2",
  932.                     basename($fileData->getFilename())
  933.                 );
  934.                 */
  935.                 //clean the filename that will be used to download the file
  936.                 // see #433
  937.                 $downloadFileName trim(
  938.                     basename(
  939.                         $fileData->getFilename()
  940.                     )
  941.                 );
  942.                 $downloadFileName preg_replace(
  943.                     '/[[:^print:]]/',
  944.                     '_',
  945.                     $downloadFileName
  946.                 );
  947.                 //sometimes we have like very strange filenames
  948.                 //where the file extension is glued to the filename
  949.                 //get rid of that glue and make it a file extension again
  950.                 $fileExtensionId $fileData->getIdFileFormat();
  951.                 $fileExtensionData $this->getDoctrine()
  952.                     ->getRepository('OceanExpertBundle:Fileformats')
  953.                     ->findOneBy(
  954.                         array(
  955.                             'idFileformat' => $fileExtensionId
  956.                         )
  957.                     );
  958.                 if ($fileExtensionData) {
  959.                     $fileExtension $fileExtensionData->getExtension();
  960.                 } else {
  961.                     /*
  962.                     return new JsonResponse(
  963.                         array(
  964.                             'status' => 1,
  965.                             'error' => "problem : cannot get the file extension data from $downloadFileName"
  966.                         )
  967.                     );
  968.                     */
  969.                     return $this->render(
  970.                         'Exception/error.html.twig',
  971.                         array(
  972.                             'message' => "problem : cannot get the file extension data from $downloadFileName"
  973.                         )
  974.                     );
  975.                 }
  976.                 if (!preg_match("/(.+?)\.$fileExtension$/"$downloadFileName)) {
  977.                     $downloadFileName preg_replace(
  978.                         "/(.+)\W$fileExtension$/",
  979.                         "$1.$fileExtension",
  980.                         $downloadFileName
  981.                     );
  982.                     /*
  983.                     return new JsonResponse(
  984.                         array(
  985.                             'status' => 1,
  986.                             'error' => "correct : file ext $fileExtensionId : $fileExtension : $downloadFileName"
  987.                         )
  988.                     );
  989.                     */
  990.                 }
  991.                 /*
  992.                 return new JsonResponse(
  993.                     array(
  994.                         'status' => 1,
  995.                         'error' => "wrong : file ext $fileExtensionId : $fileExtension : $downloadFileName"
  996.                     )
  997.                 );
  998.                 */
  999.                 if ($fileData->getUrl() == '') {
  1000.                     //this is a file that we have stored in the db
  1001.                     // Return a response with a specific content
  1002.                     $response = new Response(stream_get_contents($fileData->getContent()));
  1003.                     // Create the disposition of the file
  1004.                     $disposition $response->headers->makeDisposition(
  1005.                         ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  1006.                         $downloadFileName
  1007.                     );
  1008.                     // Set the content disposition
  1009.                     $response->headers->set('Content-Disposition'$disposition);
  1010.                     // Dispatch request
  1011.                     return $response;
  1012.                 } else {
  1013.                     //check if the file exists
  1014.                     $fileToCheck $fileData->getUrl();
  1015.                     if (is_file($fileToCheck)) {
  1016.                         $response = new BinaryFileResponse($fileData->getUrl());
  1017.                         $response->setContentDisposition(
  1018.                             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  1019.                             $downloadFileName
  1020.                         );
  1021.                         return $response;
  1022.                     } else {
  1023.                         /*
  1024.                         return new JsonResponse(
  1025.                             array(
  1026.                                 'status' => 3,
  1027.                                 'error' => "no file with file id '$idFile' found on the server ('$fileToCheck')"
  1028.                             )
  1029.                         );
  1030.                         */
  1031.                         return $this->render(
  1032.                             'Exception/error.html.twig',
  1033.                             array(
  1034.                                 'message' => "no file with file id '$idFile' found on the server<br />
  1035.                                  looking for '$downloadFileName'"
  1036.                             )
  1037.                         );
  1038.                     }
  1039.                 }
  1040.             } else {
  1041.                 /*
  1042.                 return new JsonResponse(
  1043.                     array(
  1044.                         'status' => 2,
  1045.                         'error' => "no file found with file id '$idFile'"
  1046.                     )
  1047.                 );
  1048.                 */
  1049.                 return $this->render(
  1050.                     'Exception/error.html.twig',
  1051.                     array(
  1052.                         'message' => "no file found with file id '$idFile'"
  1053.                     )
  1054.                 );
  1055.             }
  1056.         }
  1057.     }
  1058.     /**
  1059.      * @param int     $idDoc
  1060.      * @param Request $request
  1061.      *
  1062.      * @return Response
  1063.      */
  1064.     public function addDocumentAction(int $idDocRequest $request)
  1065.     {
  1066.         $document = array();
  1067.         $docDoclist = array();
  1068.         $files = array();
  1069.         $type '';
  1070.         $postdata '';
  1071.         $filedata '';
  1072.         $userId $this->get('security.token_storage')
  1073.             ->getToken()
  1074.             ->getUser()
  1075.             ->getid();
  1076.         $em $this->getDoctrine()
  1077.             ->getManager();
  1078.         if ($idDoc != 0) {
  1079.             $document $this->getDoctrine()
  1080.                 ->getRepository('OceanExpertBundle:Documents')
  1081.                 ->findOneBy(
  1082.                     array(
  1083.                         'idDoc' => $idDoc
  1084.                     )
  1085.                 );
  1086.         }
  1087.         if ($request->request->has('submit')) {
  1088.             $postdata $request->request->all();
  1089.             $filedata $request->files->get('userfile');
  1090.             if (array_key_exists('doctype'$postdata)
  1091.                 && $postdata['doctype'] != ''
  1092.                 && $postdata['status'] != '') {
  1093.                 /**
  1094.                  * Create a document entry in documents table
  1095.                  */
  1096.                 if ($idDoc == 0) {
  1097.                     $document = new Documents();
  1098.                 }
  1099.                 $document->setIdDoctype($postdata['doctype']);
  1100.                 $document->setDocCode(isset($postdata['doccode']) ? $postdata['doccode'] : '');
  1101.                 $document->setTitle(isset($postdata['title']) ? $postdata['title'] : '');
  1102.                 $document->setSummary(isset($postdata['summary']) ? $postdata['summary'] : '');
  1103.                 $document->setAuthorText(isset($postdata['authors']) ? $postdata['authors'] : '');
  1104.                 if ($postdata['status'] == 2) {
  1105.                     $document->setPublishedOn(new DateTime('now'));
  1106.                 }
  1107.                 $document->setNotes(isset($postdata['notes']) ? $postdata['notes'] : '');
  1108.                 $document->setKeywords(isset($postdata['keywords']) ? $postdata['keywords'] : '');
  1109.                 $document->setUrl(isset($postdata['websiteUrl']) ? $postdata['websiteUrl'] : '');
  1110.                 $document->setIdDocstatus(isset($postdata['status']) ? $postdata['status'] : '');
  1111.                 $document->setCreatedAt(new DateTime('now'));
  1112.                 $document->setUpdatedAt(new DateTime('now'));
  1113.                 if ($this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')) {
  1114.                     $document->setApproved(1);
  1115.                 } else {
  1116.                     $document->setApproved(0);
  1117.                 }
  1118.                 $document->setIdCreator($userId);
  1119.                 $document->setLastEditBy($userId);
  1120.                 $em->persist($document); //marks object to be saved in the next transaction.
  1121.                 $em->flush(); //performs all saves and transactions.
  1122.                 if (isset($postdata['doclist'])
  1123.                     && count($postdata['doclist']) > 0
  1124.                 ) {
  1125.                     /**
  1126.                      * if documentlist is set then enter id documentlist table.
  1127.                      */
  1128.                     foreach ($postdata['doclist'] as $docListID) {
  1129.                         $doclist $em->getRepository('OceanExpertBundle:DoclistDocuments')
  1130.                             ->findOneBy(
  1131.                                 array(
  1132.                                     'idDoclist' => $docListID,
  1133.                                     'idDoc' => $document->getIdDoc()
  1134.                                 )
  1135.                             );
  1136.                         if (!$doclist) {
  1137.                             $doclist = new DoclistDocuments();
  1138.                         }
  1139.                         $doclist->setIdDoclist($docListID);
  1140.                         $doclist->setIdDoc($document->getIdDoc());
  1141.                         $doclist->setAddedAt(new DateTime('now'));
  1142.                         $em->persist($doclist);
  1143.                         $em->flush();
  1144.                     }
  1145.                 }
  1146.                 if (count($filedata) > 0) {
  1147.                     /**
  1148.                      * if file is uploaded then upload the files in event section.
  1149.                      */
  1150.                     $i 0;
  1151.                     foreach ($filedata as $file) {
  1152.                         if ($file != null) {
  1153.                             $fileType $file->guessClientExtension();
  1154.                             //cleanup the filename, we don't want any strange chars here
  1155.                             $fileName $file->getClientOriginalName();
  1156.                             $fileName preg_replace(
  1157.                                 array(
  1158.                                     '/\s+/',
  1159.                                     '/\W+/',
  1160.                                     ),
  1161.                                 array(
  1162.                                     '_',
  1163.                                     '-'
  1164.                                 ),
  1165.                                 $fileName
  1166.                             );
  1167.                             if ($fileType == 'bin') {
  1168.                                 $fileType pathinfo($fileNamePATHINFO_EXTENSION);
  1169.                             }
  1170.                             $format $em->getRepository('OceanExpertBundle:Fileformats')
  1171.                                 ->findOneBy(
  1172.                                     array(
  1173.                                         'extension' => $fileType
  1174.                                     )
  1175.                                 );
  1176.                             if ($format) {
  1177.                                 $idFileFormat $format->getIdFileformat();
  1178.                             } else {
  1179.                                 $idFileFormat 0;
  1180.                             }
  1181.                             $directory 'uploads/documents/' $document->getIdDoc() . '/';
  1182.                             $uploadFile $file->move($directory$fileName);
  1183.                             $filepath $directory $fileName;
  1184.                             /**
  1185.                              * Create file entry in files table. == filenew is added as multiple column primary indexes were set in old table,
  1186.                              * cannot change as it might affect previos PPC sections. needs to verify if there is no break of code in the e
  1187.                              * earlier ppc.
  1188.                              */
  1189.                             if (isset($postdata['id_language'][$i])) {
  1190.                                 $language $postdata['id_language'][$i];
  1191.                             } else {
  1192.                                 $language 1;
  1193.                             }
  1194.                             $conn $this->getDoctrine()->getConnection();
  1195.                             $stmt $conn->prepare('INSERT 
  1196.                                 INTO files (
  1197.                                             version, 
  1198.                                             id_language, 
  1199.                                             id_fileformat, 
  1200.                                             url, 
  1201.                                             size, 
  1202.                                             content, 
  1203.                                             filename
  1204.                                             ) 
  1205.                                 VALUES (?, ?, ?, ?, ?, ?, ?)'
  1206.                             );
  1207.                             $stmt->bindValue(1$postdata['version'][$i]);
  1208.                             $stmt->bindValue(2$language);
  1209.                             $stmt->bindValue(3$idFileFormat);
  1210.                             $stmt->bindValue(4utf8_encode($filepath));
  1211.                             $stmt->bindValue(5$file->getClientSize());
  1212.                             $stmt->bindValue(6'');
  1213.                             $stmt->bindValue(7utf8_encode($filepath));
  1214.                             $stmt->execute();
  1215.                             $idFile $this->getDoctrine()->getConnection()->lastInsertId();
  1216.                             $files = new DocumentFiles();
  1217.                             $files->setIdDoc($document->getIdDoc());
  1218.                             $files->setIdFile($idFile);
  1219.                             $files->setVersion($postdata['version'][$i]);
  1220.                             $files->setIdLanguage($language);
  1221.                             $files->setIdFileformat($idFileFormat);
  1222.                             $em->persist($files);
  1223.                             $em->flush();
  1224.                             $i++;
  1225.                         }
  1226.                     }
  1227.                 }
  1228.             }
  1229.             return $this->redirectToRoute(
  1230.                 'view_document',
  1231.                 array(
  1232.                     'idDoc' => $document->getIdDoc()
  1233.                 )
  1234.             );
  1235.         }
  1236.         if ($idDoc != 0) {
  1237.             $repository $em->createQueryBuilder();
  1238.             $repository->add(
  1239.                 'select',
  1240.                 'd.idDoclist'
  1241.             );
  1242.             $repository->add('from''OceanExpertBundle:DoclistDocuments d');
  1243.             $repository->where('d.idDoc = :idDoc');
  1244.             $repository->setParameter('idDoc'$idDoc);
  1245.             $docDoclist array_column($repository->getQuery()->getResult(), 'idDoclist');
  1246.             $repository $em->createQueryBuilder();
  1247.             $repository->add(
  1248.                 'select',
  1249.                 'd.idDoc, l.languagename, f.fileformatname,d.version, d.idFileformat,d.idFile'
  1250.             );
  1251.             $repository->add('from''OceanExpertBundle:DocumentFiles d');
  1252.             $repository->leftJoin('OceanExpertBundle:Languages''l''WITH''d.idLanguage = l.idLanguage');
  1253.             $repository->leftJoin('OceanExpertBundle:Fileformats''f''WITH''f.idFileformat = d.idFileformat');
  1254.             $repository->where('d.idDoc = :idDoc');
  1255.             $repository->setParameter('idDoc'$idDoc);
  1256.             $files $repository->getQuery()->getResult();
  1257.         }
  1258.         $docType = array(
  1259.             => 'Report',
  1260.             => 'Presentation',
  1261.             => 'Information Document',
  1262.             => 'Letter',
  1263.             => 'Book',
  1264.             => 'MOU',
  1265.             => 'Other',
  1266.             => 'Link to online document',
  1267.             10 => 'Reference Document',
  1268.             11 => 'Working Document'
  1269.         );
  1270.         $data = array(
  1271.             'document' => $document,
  1272.             'docDoclist' => $docDoclist,
  1273.             'type' => $type,
  1274.             'postdata' => $postdata,
  1275.             'filedata' => $filedata,
  1276.             'files' => $files,
  1277.             'docTypes' => $docType,
  1278.             'documentList' => $this->getDocumentList(),
  1279.         );
  1280.         return $this->render(
  1281.             'Documents/editDocument.html.twig',
  1282.             array(
  1283.                 'data' => $data
  1284.             )
  1285.         );
  1286.     }
  1287.     /**
  1288.      * view the info of a document with the given doc id
  1289.      *
  1290.      * @param $idDoc
  1291.      *
  1292.      * @return Response
  1293.      */
  1294.     public function viewDocumentAction($idDoc)
  1295.     {
  1296.         $em $this->getDoctrine()->getManager();
  1297.         $queryBuilder $em->getRepository('OceanExpertBundle:Documents')->createQueryBuilder('d');
  1298.         $queryBuilder->select('
  1299.                 d.docCode, 
  1300.                 d.idDoc, 
  1301.                 d.idDocstatus, 
  1302.                 d.title, 
  1303.                 d.summary, 
  1304.                 d.authorText, 
  1305.                 d.publishedOn, 
  1306.                 d.notes, 
  1307.                 d.keywords, 
  1308.                 d.createdAt, 
  1309.                 d.updatedAt,
  1310.                 d.idCreator,
  1311.                 d.lastEditBy,
  1312.                 d.url,
  1313.                 d.idDoctype,
  1314.                 dt.doctypename ')
  1315.             ->leftJoin(
  1316.                 'OceanExpertBundle:Doctypes',
  1317.                 'dt',
  1318.                 'WITH',
  1319.                 'd.idDoctype = dt.idDoctype')
  1320.             ->where('d.idDoc = :idDoc');
  1321.         if (!($this->get('security.authorization_checker')->isGranted('ROLE_MANAGER'))) {
  1322.             $queryBuilder->andWhere('d.approved = 1');
  1323.         }
  1324.         $queryBuilder->setParameter('idDoc'$idDoc);
  1325.         $query $queryBuilder->getQuery();
  1326.         $result $query->getResult();
  1327.         if (count($result)>0) {
  1328.             $result $result[0];
  1329.             if ($result['idCreator'] != '') {
  1330.                 $indiv $this->getDoctrine()->getRepository('OceanExpertBundle:Indiv')->findOneByIdInd($result['idCreator']);
  1331.                 if ($indiv) {
  1332.                     $result['createdBy'] = $indiv->getFname() . ' ' $indiv->getSname();
  1333.                 } else {
  1334.                     $result['createdBy'] = '';
  1335.                 }
  1336.             }
  1337.             if ($result['lastEditBy'] != '') {
  1338.                 $indiv $this->getDoctrine()->getRepository('OceanExpertBundle:Indiv')->findOneByIdInd($result['lastEditBy']);
  1339.                 if ($indiv) {
  1340.                     $result['updatedBy'] = $indiv->getFname() . ' ' $indiv->getSname();
  1341.                 } else {
  1342.                     $result['updatedBy'] = '';
  1343.                 }
  1344.             }
  1345.         }
  1346.         //check in what event this document is used
  1347.         //event_backgrounddocs
  1348.         //agendaitem_documents
  1349.         //event_otherdocs
  1350.         //event_presentations
  1351.         //event_reports
  1352.         $docEventList = array();
  1353.         $tmpEventList = array();
  1354.         $relatedTables = array(
  1355.             'EventBackgrounddocs',
  1356.             'AgendaitemDocuments',
  1357.             'EventOtherdocs',
  1358.             'EventPresentations',
  1359.             'EventReports'
  1360.         );
  1361.         foreach ($relatedTables as $relatedTable) {
  1362.             /*
  1363.             $eventList = $em->getRepository('OceanExpertBundle:Events')->createQueryBuilder('e')
  1364.                 ->select('e.idEvent,e.title')
  1365.                 ->leftJoin('OceanExpertBundle:EventBackgrounddocs', 'b', 'WITH', 'e.idEvent = b.idEvent')
  1366.                 ->leftJoin('OceanExpertBundle:AgendaitemDocuments', 'a', 'WITH', 'e.idEvent = a.idEvent')
  1367.                 ->leftJoin('OceanExpertBundle:EventOtherdocs', 'o', 'WITH', 'e.idEvent = o.idEvent')
  1368.                 ->leftJoin('OceanExpertBundle:EventPresentations', 'p', 'WITH', 'e.idEvent = p.idEvent')
  1369.                 ->leftJoin('OceanExpertBundle:EventReports', 'r', 'WITH', 'e.idEvent = r.idEvent')
  1370.                 ->where('b.idDoc = :idDoc')
  1371.                 ->orWhere('a.idDoc = :idDoc')
  1372.                 ->orWhere('o.idDoc = :idDoc')
  1373.                 ->orWhere('p.idDoc = :idDoc')
  1374.                 ->orWhere('r.idDoc = :idDoc')
  1375.                 ->setParameter('idDoc', $idDoc)
  1376.                 ->getQuery()
  1377.                 ->getResult();
  1378.             */
  1379.             $leftJoin 'OceanExpertBundle:' $relatedTable;
  1380.             $eventList $em->getRepository('OceanExpertBundle:Events')->createQueryBuilder('e')
  1381.                 ->select('distinct e.idEvent,e.title')
  1382.                 ->leftJoin($leftJoin'b''WITH''e.idEvent = b.idEvent')
  1383.                 ->where('b.idDoc = :idDoc')
  1384.                 ->setParameter('idDoc'$idDoc)
  1385.                 ->getQuery()
  1386.                 ->getResult();
  1387.             if (is_array($eventList)
  1388.                 && count($eventList)
  1389.             ) {
  1390.                 $tmpEventList array_merge($tmpEventList$eventList);
  1391.                 /*
  1392.                 foreach ($eventList as $event) {
  1393.                     $docEventList[$event['idEvent']] = $event;
  1394.                 }
  1395.                 */
  1396.             }
  1397.         }
  1398.         foreach ($tmpEventList as $event) {
  1399.             $docEventList[$event['idEvent']] = $event;
  1400.         }
  1401.         //dump($docEventList);
  1402.         //die();
  1403.         $groups $em->getRepository('OceanExpertBundle:DocumentGroups')->createQueryBuilder('d')
  1404.             ->select('d.idGroup,g.groupname')
  1405.             ->leftJoin('OceanExpertBundle:Groups''g''WITH''d.idGroup = g.idGroup')
  1406.             ->where('d.idDoc = :idDoc')
  1407.             ->setParameter('idDoc'$idDoc)
  1408.             ->getQuery()
  1409.             ->getResult();
  1410.         $docGroupList $em->getRepository('OceanExpertBundle:DoclistDocuments')->createQueryBuilder('d')
  1411.             ->select('dl.idDoclist, dl.title')
  1412.             ->leftJoin('OceanExpertBundle:Doclists''dl''WITH''dl.idDoclist = d.idDoclist')
  1413.             ->where('d.idDoc = :idDoc')
  1414.             ->setParameter('idDoc'$idDoc)
  1415.             ->orderBy('dl.title','ASC')
  1416.             ->getQuery()
  1417.             ->getResult();
  1418.         $docGroupListAll $em->getRepository('OceanExpertBundle:Doclists')->createQueryBuilder('d')
  1419.             ->select("d.idDoclist, CONCAT(g.groupname, ' - ', d.title) as title")
  1420.             ->leftJoin('OceanExpertBundle:DoclistGroups''dg''WITH''d.idDoclist = dg.idDoclist')
  1421.             ->leftJoin('OceanExpertBundle:Groups''g''WITH''dg.idGroup = g.idGroup')
  1422.             ->where("g.groupname != ''")
  1423.             ->orderBy('g.groupname, d.title','ASC')
  1424.             ->getQuery()
  1425.             ->getResult();
  1426.         $docFiles $em->getRepository('OceanExpertBundle:DocumentFiles')->createQueryBuilder('d')
  1427.             ->select('d.idFileformat, d.version, l.languagename, d.idFile, f.filename, f.url, f.size,f.content,f.path')
  1428.             ->leftJoin('OceanExpertBundle:Files''f''WITH''f.idFile = d.idFile')
  1429.             ->leftJoin('OceanExpertBundle:Languages''l''WITH''f.idLanguage = l.idLanguage')
  1430.             ->where('d.idDoc = :idDoc')
  1431.             ->setParameter('idDoc'$idDoc)
  1432.             ->getQuery()
  1433.             ->getResult();
  1434.         $docGroupListArr = array();
  1435.         foreach ($docGroupList as $doclist){
  1436.             $docGroupListArr[] = $doclist['idDoclist'];
  1437.         }
  1438.         $result['documentGroup'] = $groups;
  1439.         $result['docGroupList'] = $docGroupList;
  1440.         $result['docFiles'] = $docFiles;
  1441.         $result['docGroupListArr'] = $docGroupListArr;
  1442.         $result['docGroupListAll'] = $docGroupListAll;
  1443.         $result['docEventList'] = $docEventList;
  1444.         return $this->render(
  1445.             'Documents/viewDocument.html.twig',
  1446.             array(
  1447.                 'data' => $result
  1448.             )
  1449.         );
  1450.     }
  1451.     /**
  1452.      * show all existing documents
  1453.      *
  1454.      * @param Request $request
  1455.      *
  1456.      * @return Response|void
  1457.      */
  1458.     function viewDocumentsActionRequest $request): Response
  1459.     {
  1460.         $em $this->get('doctrine')->getManager();
  1461.         $connection $em->getConnection();
  1462.         $limits = array(
  1463.             10 => 10,
  1464.             25 => 25,
  1465.             50 => 50,
  1466.             100 => 100,
  1467.             500 => 500
  1468.         );
  1469.         $limit $request->query->get('limit'10);
  1470.         $query $request->query->get('search''');
  1471.         $orderby $request->query->get('orderby''order');
  1472.         $dir $request->query->get('dir''asc');
  1473.         if (!in_array($orderby, array('id''title'))) {
  1474.             $orderby 'title';
  1475.         }
  1476.         if (!in_array($dir, array('asc''desc'))) {
  1477.             $dir 'asc';
  1478.         }
  1479.         //get all the doclists
  1480.         $repo $this->getDoctrine()
  1481.             ->getRepository('OceanExpertBundle:Doclists');
  1482.         $docListsQB $repo->createQueryBuilder('dl');
  1483.         $docListsQB->select('
  1484.             dl.idDoclist, 
  1485.             dl.title'
  1486.         );
  1487.         $docListsQuery $docListsQB->getQuery();
  1488.         $docListsTmp $docListsQuery->execute();
  1489.         foreach($docListsTmp as $docList) {
  1490.             $docLists[$docList['idDoclist']] = $docList['title'];
  1491.         }
  1492.         //get all the doclist docs, this avoids having to make an join that is veryveryvery slow
  1493.         $repo $this->getDoctrine()
  1494.             ->getRepository('OceanExpertBundle:DoclistDocuments');
  1495.         $docListDocumentsQB $repo->createQueryBuilder('dld');
  1496.         $docListDocumentsQB->select('
  1497.             dld.id,
  1498.             dld.idDoclist, 
  1499.             dld.idDoc'
  1500.         );
  1501.         $docListDocumentsQuery $docListDocumentsQB->getQuery();
  1502.         $docListDocumentsTmp $docListDocumentsQuery->execute();
  1503.         foreach($docListDocumentsTmp as $docListDocument) {
  1504.             $docListDocuments[$docListDocument['idDoc']][] = $docListDocument;
  1505.         }
  1506.         $repo $this->getDoctrine()
  1507.             ->getRepository('OceanExpertBundle:Documents');
  1508.         $documents $repo->createQueryBuilder('d');
  1509.         $documents->select('
  1510.             d.idDoc, 
  1511.             d.title,
  1512.             d.createdAt'
  1513.         );
  1514.         if (trim($query) != '') {
  1515.             $documents->andwhere('d.title LIKE :query');
  1516.             $documents->setParameter(
  1517.                 'query',
  1518.                 '%' trim($query) . '%'
  1519.             );
  1520.         }
  1521.         $documents->getQuery();
  1522.         $paginator $this->get('knp_paginator');
  1523.         $data $paginator->paginate(
  1524.             $documents,
  1525.             $request->query->getInt('page'1),
  1526.             $limit,
  1527.             array(
  1528.                 'pageParameterName' => 'page',
  1529.                 'sortDirectionParameterName' => 'dir',
  1530.                 'defaultSortFieldName' => 'd.createdAt',
  1531.                 'defaultSortDirection' => 'desc'
  1532.             )
  1533.         );
  1534.         $data->setCustomParameters([
  1535.             'limits' => $limits,
  1536.             'title' => 'Documents'
  1537.         ]);
  1538.         return $this->render(
  1539.             'Documents/viewDocuments.html.twig',
  1540.             array(
  1541.                 'documentData' => $data,
  1542.                 'doclists' => $docLists,
  1543.                 'doclistDocuments' => $docListDocuments
  1544.             )
  1545.         );
  1546.     }
  1547.     /**
  1548.      * delete a document
  1549.      * this should not be called in the browser or via a direct link
  1550.      *
  1551.      * @return Response
  1552.      */
  1553.     public function deleteDocumentAction(Request $request)
  1554.     {
  1555.         $idDoc $request->request->get('idDoc');
  1556.         if ($idDoc
  1557.             && $idDoc != ''
  1558.             && is_numeric($idDoc)
  1559.         ) {
  1560.             $em $this->getDoctrine()->getManager();
  1561.             //we will not delete files that are still in an event or doclist
  1562.             $eventList $em->getRepository('OceanExpertBundle:Events')->createQueryBuilder('e')
  1563.                 ->select('e.idEvent,e.title')
  1564.                 ->leftJoin('OceanExpertBundle:EventBackgrounddocs''b''WITH''e.idEvent = b.idEvent')
  1565.                 ->leftJoin('OceanExpertBundle:AgendaitemDocuments''a''WITH''e.idEvent = a.idEvent')
  1566.                 ->leftJoin('OceanExpertBundle:EventOtherdocs''o''WITH''e.idEvent = o.idEvent')
  1567.                 ->leftJoin('OceanExpertBundle:EventPresentations''p''WITH''e.idEvent = p.idEvent')
  1568.                 ->leftJoin('OceanExpertBundle:EventReports''r''WITH''e.idEvent = r.idEvent')
  1569.                 ->where('b.idDoc = :idDoc')
  1570.                 ->orWhere('a.idDoc = :idDoc')
  1571.                 ->orWhere('o.idDoc = :idDoc')
  1572.                 ->orWhere('p.idDoc = :idDoc')
  1573.                 ->orWhere('r.idDoc = :idDoc')
  1574.                 ->setParameter('idDoc'$idDoc)
  1575.                 ->getQuery()
  1576.                 ->getResult();
  1577.             $docList $em->getRepository('OceanExpertBundle:DoclistDocuments')->createQueryBuilder('d')
  1578.                 ->select('dl.idDoclist, dl.title')
  1579.                 ->leftJoin('OceanExpertBundle:Doclists''dl''WITH''dl.idDoclist = d.idDoclist')
  1580.                 ->where('d.idDoc = :idDoc')
  1581.                 ->setParameter('idDoc'$idDoc)
  1582.                 ->orderBy('dl.title','ASC')
  1583.                 ->getQuery()
  1584.                 ->getResult();
  1585.             if ((is_array($eventList)
  1586.                 && count($eventList))
  1587.                 || (is_array($docList)
  1588.                     && count($docList))
  1589.             ) {
  1590.                 $data = array(
  1591.                     'status' => 2,
  1592.                     'message' => "document cannot be deleted as it is still used in either a doclist or an event"
  1593.                 );
  1594.             } else {
  1595.                 $document $em->getRepository('OceanExpertBundle:Documents')
  1596.                     ->findOneBy(
  1597.                         array(
  1598.                             'idDoc' => $idDoc
  1599.                         )
  1600.                     );
  1601.                 $documentFiles $em->getRepository('OceanExpertBundle:DocumentFiles')
  1602.                     ->findBy(
  1603.                         array(
  1604.                             'idDoc' => $idDoc
  1605.                         )
  1606.                     );
  1607.                 if ($document) {
  1608.                     $em->remove($document);
  1609.                     $em->flush();
  1610.                 }
  1611.                 if ($documentFiles) {
  1612.                     foreach ($documentFiles as $documentFile) {
  1613.                         $file $em->getRepository('OceanExpertBundle:Files')
  1614.                             ->findOneBy(
  1615.                                 array(
  1616.                                     'idFile' => $documentFile->getIdFile()
  1617.                                 )
  1618.                             );
  1619.                         if ($file) {
  1620.                             if (file_exists($file->getFilename())) {
  1621.                                 unlink($file->getFilename());
  1622.                             }
  1623.                             $em->remove($file);
  1624.                             $em->flush();
  1625.                         }
  1626.                         $em->remove($documentFile);
  1627.                         $em->flush();
  1628.                     }
  1629.                 }
  1630.                 $data = array('status' => 1);
  1631.             }
  1632.         } else {
  1633.             $data = array(
  1634.                 'status' => 0,
  1635.                 'message' => "no document id found, did you call this from a browser? Don't"
  1636.             );
  1637.         }
  1638.         return new JsonResponse($data);
  1639.     }
  1640.     public function removeDocumentFileAction(Request $request)
  1641.     {
  1642.         $idDoc $request->request->get('idDoc');
  1643.         $idFile $request->request->get('idFile');
  1644.         if (isset($idDoc)
  1645.             && is_numeric($idDoc)
  1646.             && isset($idFile)
  1647.             && is_numeric($idFile)
  1648.         ) {
  1649.             $em $this->getDoctrine()->getManager();
  1650.             $docFiles $em->getRepository('OceanExpertBundle:DocumentFiles')
  1651.                 ->findOneBy(
  1652.                     array(
  1653.                         'idDoc' => $idDoc,
  1654.                         'idFile' => $idFile
  1655.                     )
  1656.                 );
  1657.             if ($docFiles) {
  1658.                 $em->remove($docFiles);
  1659.                 $em->flush();
  1660.             }
  1661.             $file $em->getRepository('OceanExpertBundle:Files')->findOneBy(array('idFile' => $idFile));
  1662.             if (file_exists($file->getFilename())) {
  1663.                 unlink($file->getFilename());
  1664.             }
  1665.             if ($file) {
  1666.                 $em->remove($file);
  1667.                 $em->flush();
  1668.             }
  1669.             $data = array('status' => 1);
  1670.         } else {
  1671.             $data = array(
  1672.                 'status' => 0,
  1673.                 'message' => 'do not call this directly in the browser, we need post data'
  1674.             );
  1675.         }
  1676.         return new JsonResponse($data);
  1677.     }
  1678.     /**
  1679.      * @return Response
  1680.      */
  1681.     public function removeDocumentFromListAction(Request $request)
  1682.     {
  1683.         $idDoc $request->request->get('idDoc');
  1684.         $idDoclist $request->request->get('idDoclist');
  1685.         $em $this->getDoctrine()->getManager();
  1686.         $document $em->getRepository('OceanExpertBundle:DoclistDocuments')->findOneBy(array('idDoc' => $idDoc,'idDoclist'=>$idDoclist));
  1687.         if ($document) {
  1688.             $em->remove($document);
  1689.             $em->flush();
  1690.         }
  1691.         $data = array('status' => 1);
  1692.         return new JsonResponse($data);
  1693.     }
  1694.     /**
  1695.      * @return Response
  1696.      */
  1697.     public function deleteDocumentListAction(Request $request)
  1698.     {
  1699.         $idDoclist $request->request->get('idDoclist');
  1700.         $em $this->getDoctrine()->getManager();
  1701.         $documentList $em->getRepository('OceanExpertBundle:Doclists')->findOneBy(array('idDoclist'=>$idDoclist));
  1702.         if ($documentList) {
  1703.             $em->remove($documentList);
  1704.             $em->flush();
  1705.         }
  1706.         $documents $em->getRepository('OceanExpertBundle:DoclistDocuments')->findBy(array('idDoclist'=>$idDoclist));
  1707.         if ($documents) {
  1708.             foreach ($documents as $document){
  1709.                 $em->remove($document);
  1710.                 $em->flush();
  1711.             }
  1712.         }
  1713.         $data = array('status' => 1);
  1714.         return new JsonResponse($data);
  1715.     }
  1716.     /**
  1717.      * get all the user groups this current active user belongs to
  1718.      *
  1719.      * @return array | boolean
  1720.      */
  1721.     function getUserGroups()
  1722.     {
  1723.         $userId $this->get('security.token_storage')
  1724.             ->getToken()
  1725.             ->getUser()
  1726.             ->getid();
  1727.         if ($userId) {
  1728.             $repository $this->getDoctrine()
  1729.                 ->getManager()
  1730.                 ->createQueryBuilder();
  1731.             $repository->add(
  1732.                 'select',
  1733.                 'g.idGroup'
  1734.             );
  1735.             $repository->add(
  1736.                 'from',
  1737.                 'OceanExpertBundle:MemberGroups g'
  1738.             );
  1739.             $repository->where(
  1740.                 'g.idInd = :userId'
  1741.             );
  1742.             $repository->setParameter(
  1743.                 'userId',
  1744.                 $userId
  1745.             );
  1746.             return $repository->getQuery()->getResult();
  1747.         } else {
  1748.             return false;
  1749.         }
  1750.     }
  1751.     /**
  1752.      * @return array
  1753.      *
  1754.      * @todo : check if we still need this function, it is not used anywhere in the code
  1755.      */
  1756.     function getDocumentList(){
  1757.         //this is what we will return
  1758.         $documentList = array();
  1759.         //get all the user groups this current active user belongs to
  1760.         $grouplist array_map(
  1761.             'current',
  1762.             $this->getUserGroups()
  1763.         );
  1764.         //get all the doclists that are linked to these groups
  1765.         $repository $this->getDoctrine()
  1766.             ->getManager()
  1767.             ->createQueryBuilder();
  1768.         $repository->add(
  1769.             'select',
  1770.             'distinct(d.idGroup) as idGroup, g.groupname'
  1771.         );
  1772.         $repository->add(
  1773.             'from',
  1774.             'OceanExpertBundle:DoclistGroups d'
  1775.         );
  1776.         $repository->leftJoin(
  1777.             'OceanExpertBundle:Groups',
  1778.             'g',
  1779.             'WITH',
  1780.             'd.idGroup = g.idGroup'
  1781.         );
  1782.         $repository->where(
  1783.             'g.idGroup in (:grouplist)'
  1784.         );
  1785.         $repository->setParameter(
  1786.             'grouplist',
  1787.             $grouplist
  1788.         );
  1789.         $repository->orderBy('g.groupname');
  1790.         $docGroups $repository->getQuery()->getResult();
  1791.         foreach ($docGroups as $docGroup) {
  1792.             $repository $this->getDoctrine()
  1793.                 ->getManager()
  1794.                 ->createQueryBuilder();
  1795.             $repository->add(
  1796.                 'select',
  1797.                 'd.idDoclist, d.title, dg.idGroup'
  1798.             );
  1799.             $repository->add(
  1800.                 'from',
  1801.                 'OceanExpertBundle:Doclists d'
  1802.             );
  1803.             $repository->leftJoin(
  1804.                 'OceanExpertBundle:DoclistGroups',
  1805.                 'dg',
  1806.                 'WITH',
  1807.                 'dg.idDoclist = d.idDoclist'
  1808.             );
  1809.             $repository->where('dg.idGroup =:idGroup');
  1810.             $repository->setParameter(
  1811.                 'idGroup',
  1812.                 $docGroup['idGroup']
  1813.             );
  1814.             $repository->orderBy('d.title''asc');
  1815.             $documentList[$docGroup['groupname']] = $repository->getQuery()->getResult();
  1816.         }
  1817.         return $documentList;
  1818.     }
  1819.     /**
  1820.      * add a document to a doclist
  1821.      *
  1822.      * @param Request $request
  1823.      *
  1824.      * @return JsonResponse
  1825.      *
  1826.      */
  1827.     public function addDocumentToDoclistAction(Request $request)
  1828.     {
  1829.         $em $this->getDoctrine()->getManager();
  1830.         //dump($request->request->all());
  1831.         //die();
  1832.         $idDoclist $request->request->get('idDoclist');
  1833.         $idDoc $request->request->get('idDoc');
  1834.         if ($idDoclist == ''
  1835.             || $idDoclist == 'undefined'
  1836.             || $idDoc == ''
  1837.         ) {
  1838.             $message 'We really need a document id and a doclist id to do this. ';
  1839.             $message .= 'Did you call this directly in the browser? You should not do this.';
  1840.             return new JsonResponse(
  1841.                 array(
  1842.                     'status' => 2,
  1843.                     'error' => $message
  1844.                 )
  1845.             );
  1846.         }
  1847.         $doclistDocument $em->getRepository('OceanExpertBundle:DoclistDocuments')
  1848.             ->findOneBy(
  1849.                 array(
  1850.                     'idDoclist' => $idDoclist,
  1851.                     'idDoc' => $idDoc)
  1852.             );
  1853.         if (!$doclistDocument) {
  1854.             $doclistDocument = new DoclistDocuments();
  1855.         }
  1856.         $doclistDocument->setIdDoclist($idDoclist);
  1857.         $doclistDocument->setIdDoc($idDoc);
  1858.         $doclistDocument->setAddedAt(new DateTime('now'));
  1859.         $em->persist($doclistDocument);
  1860.         $em->flush();
  1861.         return new JsonResponse(
  1862.             array(
  1863.                 'status' => 1
  1864.             )
  1865.         );
  1866.     }
  1867. }