src/OceanExpertBundle/Controller/DocumentController.php line 1316

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