src/OceanExpertBundle/Controller/DocumentController.php line 942

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