src/OceanExpertBundle/Controller/EventController.php line 4815

Open in your IDE?
  1. <?php
  2. /**
  3.  * Controller for the events
  4.  *
  5.  * @author Mithun
  6.  * @author Arno Lambert <a.lambert@unesco.org>
  7.  * @since  06/10/16
  8.  */
  9. namespace OceanExpertBundle\Controller;
  10. use CommerceGuys\Addressing\Formatter\DefaultFormatter;
  11. use CommerceGuys\Addressing\Model\Address;
  12. use CommerceGuys\Addressing\Repository\AddressFormatRepository;
  13. use CommerceGuys\Addressing\Repository\CountryRepository;
  14. use CommerceGuys\Addressing\Repository\SubdivisionRepository;
  15. use DateTime;
  16. use Doctrine\ORM\AbstractQuery;
  17. use Doctrine\ORM\NoResultException;
  18. use ErrorException;
  19. use FOS\UserBundle\Model\UserManagerInterface;
  20. use FOS\UserBundle\Util\TokenGeneratorInterface;
  21. use OceanExpertBundle\Entity\DocumentFiles;
  22. use OceanExpertBundle\Entity\EventAgendaitems;
  23. use OceanExpertBundle\Entity\EventBackgrounddocs;
  24. use OceanExpertBundle\Entity\EventContacts;
  25. use OceanExpertBundle\Entity\EventFiles;
  26. use OceanExpertBundle\Entity\EventGroups;
  27. use OceanExpertBundle\Entity\EventLabels;
  28. use OceanExpertBundle\Entity\EventOtherdocs;
  29. use OceanExpertBundle\Entity\EventParticipantImportFiles;
  30. use OceanExpertBundle\Entity\EventParticipantroles;
  31. use OceanExpertBundle\Entity\EventParticipants;
  32. use OceanExpertBundle\Entity\EventParticipantUpload;
  33. use OceanExpertBundle\Entity\EventPresentations;
  34. use OceanExpertBundle\Entity\EventReports;
  35. use OceanExpertBundle\Entity\Events;
  36. use OceanExpertBundle\Entity\EventStaff;
  37. use OceanExpertBundle\Entity\MailQueueFiles;
  38. use OceanExpertBundle\Entity\Mails;
  39. use OceanExpertBundle\Entity\Messages;
  40. use PhpOffice\PhpSpreadsheet\IOFactory;
  41. use PhpOffice\PhpSpreadsheet\Reader\Exception;
  42. use Swift_Message;
  43. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  44. use Symfony\Component\HttpFoundation\JsonResponse;
  45. use Symfony\Component\HttpFoundation\RedirectResponse;
  46. use Symfony\Component\HttpFoundation\Request;
  47. use Symfony\Component\HttpFoundation\Response;
  48. use Symfony\Component\Routing\Generator\UrlGenerator;
  49. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  50. /**
  51.  * Class EventController
  52.  * @package OceanExpertBundle\Controller
  53.  */
  54. class EventController extends AbstractController
  55. {
  56.     /**
  57.      * list of possibe event labels
  58.      *
  59.      * @var array
  60.      */
  61.     private $availableEventLabels = array(
  62.         => array(
  63.             'id' => 1,
  64.             'type' => 'Official meeting'
  65.         ),
  66.         => array(
  67.             'id' => 2,
  68.             'type' => 'Training course'
  69.         ),
  70.         => array(
  71.             'id' => 3,
  72.             'type' => 'Expert assistance'
  73.         ),
  74.         => array(
  75.             'id' => 4,
  76.             'type' => 'Priority 1'
  77.         ),
  78.         => array(
  79.             'id' => 5,
  80.             'type' => 'Priority 2'
  81.         ),
  82.         => array(
  83.             'id' => 6,
  84.             'type' => 'Priority 3'
  85.         ),
  86.     );
  87.     /**
  88.      * list of possible participant status
  89.      *
  90.      * @var array
  91.      */
  92.     private $participantStatus = array(
  93.         '0' => 'Confirmed & Approved',
  94.         '1' => 'Need Confirmation',
  95.         '2' => 'Need Approval',
  96.         '3' => 'Declined',
  97.         '4' => 'Rejected',
  98.     );
  99.     private UserManagerInterface $fosUserManager;
  100.     public function __construct(UserManagerInterface $fosUserManager)
  101.     {
  102.         $this->fosUserManager $fosUserManager;
  103.     }
  104.     /**
  105.      * @return Response
  106.      */
  107.     public function indexAction()
  108.     {
  109.         return new Response('Index Page');
  110.     }
  111.     /**
  112.      * create a new event OR edit an existing one
  113.      *
  114.      * @param Request $request
  115.      * @param int $idEvent id of the event to create
  116.      *
  117.      * @return Response
  118.      *
  119.      * @throws \Exception
  120.      */
  121.     public function createEventAction(Request $request$idEvent)
  122.     {
  123.         /*
  124.          * @remark: the security check is not really needed here (but better safe than sorry)
  125.          * because we already have the line ' - { path: ^/admin, role: ROLE_USER }' in security.yaml
  126.          * and this prevents us from going to /admin/event without loging in
  127.          */
  128.         $security_context $this->get('security.authorization_checker');
  129.         if ($security_context->isGranted('IS_AUTHENTICATED_FULLY')) {
  130.             //let's check if the logged-in user has a 'real' profile
  131.             //the mandatory profile fields are all filled and the expert is active
  132.             $em $this->getDoctrine()->getManager();
  133.             $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  134.             if (!SecurityController::checkUserProfile($em$userId)) {
  135.                 return $this->redirect(
  136.                     $this->generateUrl(
  137.                         'user_profile_edit'
  138.                     )
  139.                 );
  140.             }
  141.             $participantRoles = array();
  142.             $participants = array();
  143.             $eventStaff = array();
  144.             $eventContacts = array();
  145.             $unknownParticipants = array();
  146.             $eventLabels = array();
  147.             $eventPresentations = array();
  148.             $eventBackgrounddocs = array();
  149.             $eventOtherdocs = array();
  150.             $agendaItems '';
  151.             $agendaItemsdocs = array();
  152.             $eventImage = array();
  153.             $report = array();
  154.             $idContact = array();
  155.             /*
  156.              * while the function is called createEvent, we just edit an event if a idEvent is given
  157.              * OE logic :-(
  158.              */
  159.             if ($idEvent != 0) {
  160.                 $event $em
  161.                     ->getRepository('OceanExpertBundle:Events')
  162.                     ->findOneBy(
  163.                         array(
  164.                             'idEvent' => $idEvent
  165.                         )
  166.                     );
  167.                 if ($event) {
  168.                     $isOrganiser $em->getRepository('OceanExpertBundle:EventContacts')
  169.                         ->createQueryBuilder('ec')
  170.                         ->select('count(ec.idInd)')
  171.                         ->where('ec.idInd =:idInd')
  172.                         ->andWhere('ec.idEvent =:event')
  173.                         ->setParameters(
  174.                             array(
  175.                                 'idInd' => $userId,
  176.                                 'event' => $idEvent
  177.                             )
  178.                         )
  179.                         ->getQuery()->getSingleScalarResult();
  180.                     $isCreator $em->getRepository('OceanExpertBundle:Events')
  181.                         ->createQueryBuilder('e')
  182.                         ->select('count(e.idEvent)')
  183.                         ->where('e.createdBy =:idInd')
  184.                         ->orWhere('e.lastEditBy =:idInd')
  185.                         ->orWhere('e.idContact =:idInd')
  186.                         ->andWhere('e.idEvent =:event')
  187.                         ->setParameters(array('idInd' => $userId'event' => $idEvent))
  188.                         ->getQuery()->getSingleScalarResult();
  189.                     if ($isOrganiser == 1
  190.                         || $isCreator == 1
  191.                         || $this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')
  192.                     ) {
  193.                         //@todo what happens here? Arno 21/06/21
  194.                     } else {
  195.                         return $this->redirect(
  196.                             $this->generateUrl(
  197.                                 'view_event',
  198.                                 array(
  199.                                     'event' => $idEvent
  200.                                 )
  201.                             )
  202.                         );
  203.                     }
  204.                     if (null != $event->getIdContact()) {
  205.                         $idContact $this->getDoctrine()
  206.                             ->getRepository('OceanExpertBundle:Indiv')
  207.                             ->createQueryBuilder('i')
  208.                             ->select("i.fname,i.sname, i.idInd")
  209.                             ->where('i.idInd = :idInd')
  210.                             ->setParameter('idInd'$event->getIdContact())
  211.                             ->getQuery()
  212.                             ->getSingleResult(AbstractQuery::HYDRATE_ARRAY);
  213.                         $idContact = array(
  214.                             'idInd' => $idContact['idInd'],
  215.                             'user' => $idContact['fname'] . ' ' $idContact['sname'],
  216.                         );
  217.                     }
  218.                     $eventImage $this->getDoctrine()
  219.                         ->getRepository('OceanExpertBundle:EventFiles')
  220.                         ->findOneBy(
  221.                             array(
  222.                                 'idEvent' => $event->getIdEvent()
  223.                             )
  224.                         );
  225.                     $participantRoles $this->getDoctrine()
  226.                         ->getRepository('OceanExpertBundle:EventParticipantroles')
  227.                         ->findBy(
  228.                             array('idEvent' => $idEvent),
  229.                             array('ordering' => 'asc')
  230.                         );
  231.                     $participants $this->getEventParticipants($idEvent);
  232.                     $eventStaff $this->getEventStaff($idEvent);
  233.                     $eventContacts $this->getEventOrganisers($idEvent);
  234.                     $unknownParticipants $this->getDoctrine()
  235.                         ->getRepository('OceanExpertBundle:EventUnknownparticipants')
  236.                         ->findBy(
  237.                             array('idEvent' => $idEvent)
  238.                         );
  239.                     $eventLabels $em
  240.                         ->getRepository('OceanExpertBundle:EventLabels')
  241.                         ->findBy(
  242.                             array('idEvent' => $idEvent)
  243.                         );
  244.                     $eventPresentations $this->getEventPresentations($idEvent);
  245.                     $eventBackgrounddocs $this->getBackgrounddocs($idEvent);
  246.                     $eventOtherdocs $this->getEventOtherDocs($idEvent);
  247.                     $agendaItems $this->getAgendaItems($event);
  248.                     $agendaItemsdocs $this->getDocuments(
  249.                         'OceanExpertBundle:AgendaitemDocuments',
  250.                         $idEvent
  251.                     );
  252.                     $report $this->getEventReport($idEvent);
  253.                 } else {
  254.                     return $this->render(
  255.                         'Exception/error.html.twig',
  256.                         array(
  257.                             'message' => "cannot find the event with id '$idEvent'"
  258.                         )
  259.                     );
  260.                 }
  261.             } else {
  262.                 $event = new Events();
  263.                 $event->setStatus(0);
  264.                 $event->setCreatedBy($userId);
  265.                 $event->setCreatedAt(new DateTime());
  266.                 $event->agenda '';
  267.             }
  268.             if ($request->getMethod() === 'POST') {
  269.                 $formData $request->request->all();
  270.                 $files $request->files;
  271.                 return $this->storeTheEvent(
  272.                     $event,
  273.                     $formData,
  274.                     $files,
  275.                     $userId
  276.                 );
  277.             }
  278.             /*
  279.             $participantStatus = array(
  280.                 '0' => 'Confirmed & Approved',
  281.                 '1' => 'Need Confirmation',
  282.                 '2' => 'Need Approval',
  283.                 '3' => 'Declined',
  284.                 '4' => 'Rejected',
  285.             );
  286.             */
  287.             $calenderGroups $this->getDoctrine()
  288.                 ->getRepository('OceanExpertBundle:EventGroups')
  289.                 ->createQueryBuilder('e')
  290.                 ->select('e.idGroup')
  291.                 ->where('e.idEvent = :idEvent')
  292.                 ->setParameter('idEvent'$idEvent)
  293.                 ->getQuery()
  294.                 ->getResult(AbstractQuery::HYDRATE_ARRAY);
  295.             $calenderGroups array_column($calenderGroups'idGroup');
  296.             $availableEventType $em
  297.                 ->getRepository('OceanExpertBundle:Eventtypes')
  298.                 ->findBy(
  299.                     array(),
  300.                     array(
  301.                         'eventtypeName' => 'ASC'
  302.                     )
  303.                 );
  304.             $countries $this->getDoctrine()
  305.                 ->getRepository('OceanExpertBundle:Countries')
  306.                 ->findBy(
  307.                     array(),
  308.                     ['country' => 'ASC']
  309.                 );
  310.             $instituteSeaRegion $this->getDoctrine()
  311.                 ->getRepository('OceanExpertBundle:Regions')
  312.                 ->seaRegions();
  313.             $availableCalenderGroups $this->getSitesGroup();
  314.             $documentList $this->getDocumentList();
  315.             $data = array(
  316.                 'data' => array(
  317.                     'event' => $event,
  318.                     'countries' => $countries,
  319.                     'availableCalenderGroups' => $availableCalenderGroups,
  320.                     'availableEventType' => $availableEventType,
  321.                     'availableEventLabels' => $this->availableEventLabels,
  322.                     'availableSeaRegions' => $instituteSeaRegion,
  323.                     'participantRoles' => $participantRoles,
  324.                     'calenderGroups' => $calenderGroups,
  325.                     'eventLabels' => $eventLabels,
  326.                     'participants' => $participants,
  327.                     'eventStaff' => $eventStaff,
  328.                     'eventContacts' => $eventContacts,
  329.                     'unknownParticipants' => $unknownParticipants,
  330.                     'eventPresentations' => $eventPresentations,
  331.                     'eventBackgrounddocs' => $eventBackgrounddocs,
  332.                     'eventOtherdocs' => $eventOtherdocs,
  333.                     'agendaItems' => $agendaItems,
  334.                     'agendaItemsdocs' => $agendaItemsdocs,
  335.                     'report' => $report,
  336.                     'participantStatus' => $this->participantStatus,
  337.                     'documentList' => $documentList,
  338.                     'eventEmailContact' => $idContact,
  339.                     'eventImage' => $eventImage,
  340.                 )
  341.             );
  342.             return $this->render(
  343.                 'Event/addEvent.html.twig',
  344.                 $data
  345.             );
  346.         } else {
  347.             return $this->render(
  348.                 'Exception/error.html.twig',
  349.                 array(
  350.                     'message' => 'seems you do not have permissions to do this, please log in before making an event'
  351.                 )
  352.             );
  353.         }
  354.     }
  355.     /**
  356.      * copy the event with the given idEvent
  357.      * show the new event in edit mode
  358.      *
  359.      * @param Request $request
  360.      * @param int $idEvent id of the event to copy
  361.      *
  362.      * @return Response
  363.      *
  364.      * @throws \Exception
  365.      */
  366.     public function copyEventAction(
  367.         Request $requestint $idEvent,
  368.         TokenGeneratorInterface $fosTokenGenerator,
  369.         UserManagerInterface $fosUserManager): Response
  370.     {
  371.         /*
  372.          * @remark: the security check is not really needed here (but better safe than sorry)
  373.          * because we already have the line ' - { path: ^/admin, role: ROLE_USER }' in security.yaml
  374.          * and this prevents us from going to /admin/copyEvent without loging in
  375.          */
  376.         $security_context $this->get('security.authorization_checker');
  377.         if ($security_context->isGranted('IS_AUTHENTICATED_FULLY')) {
  378.             $em $this->getDoctrine()->getManager();
  379.             $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  380.             $participantRoles = array();
  381.             $participants = array();
  382.             $eventStaffs = array();
  383.             $eventContacts = array();
  384.             $unknownParticipants = array();
  385.             $eventLabels = array();
  386.             $eventPresentations = array();
  387.             $eventBackgrounddocs = array();
  388.             $eventOtherdocs = array();
  389.             $agendaItems '';
  390.             $agendaItemsdocs = array();
  391.             $eventImage = array();
  392.             $report = array();
  393.             $idContact = array();
  394.             /**
  395.              * copy the event with the given idEvent
  396.              */
  397.             if ($idEvent != 0) {
  398.                 //this is the event we will copy
  399.                 $oldEvent $em
  400.                     ->getRepository('OceanExpertBundle:Events')
  401.                     ->findOneBy(
  402.                         array(
  403.                             'idEvent' => $idEvent
  404.                         )
  405.                     );
  406.                 if ($oldEvent) {
  407.                     //make a new title
  408.                     $eventTitle $oldEvent->getTitle() . ' (COPY)';
  409.                     //get all the other info of the event we copy
  410.                     $eventImage $this->getDoctrine()
  411.                         ->getRepository('OceanExpertBundle:EventFiles')
  412.                         ->findOneBy(
  413.                             array(
  414.                                 'idEvent' => $oldEvent->getIdEvent()
  415.                             )
  416.                         );
  417.                     if (null != $oldEvent->getIdContact()) {
  418.                         $oldIdContact $oldEvent->getIdContact();
  419.                         $idContact $this->getDoctrine()
  420.                             ->getRepository('OceanExpertBundle:Indiv')
  421.                             ->createQueryBuilder('i')
  422.                             ->select("i.fname,i.sname, i.idInd")
  423.                             ->where('i.idInd = :idInd')
  424.                             ->setParameter('idInd'$oldEvent->getIdContact())
  425.                             ->getQuery()
  426.                             ->getSingleResult(AbstractQuery::HYDRATE_ARRAY);
  427.                         $idContact = array(
  428.                             'idInd' => $idContact['idInd'],
  429.                             'user' => $idContact['fname'] . ' ' $idContact['sname'],
  430.                         );
  431.                     }
  432.                     $participantRoles $this->getDoctrine()
  433.                         ->getRepository('OceanExpertBundle:EventParticipantroles')
  434.                         ->findBy(
  435.                             array('idEvent' => $idEvent),
  436.                             array('ordering' => 'asc')
  437.                         );
  438.                     $participants $this->getEventParticipants($idEvent);
  439.                     $eventStaffs $this->getEventStaff($idEvent);
  440.                     $eventContacts $this->getEventOrganisers($idEvent);
  441.                     //don't do this, is stupid
  442.                     /*
  443.                     $unknownParticipants = $this->getDoctrine()
  444.                         ->getRepository('OceanExpertBundle:EventUnknownparticipants')
  445.                         ->findBy(
  446.                             array('idEvent' => $idEvent)
  447.                         );
  448.                     */
  449.                     $eventLabels $em
  450.                         ->getRepository('OceanExpertBundle:EventLabels')
  451.                         ->findBy(
  452.                             array('idEvent' => $idEvent)
  453.                         );
  454.                     $eventGroups $em
  455.                         ->getRepository('OceanExpertBundle:EventGroups')
  456.                         ->findBy(
  457.                             array('idEvent' => $idEvent)
  458.                         );
  459.                     //useless to copy agenda's
  460.                     /*
  461.                     $agendaItems = $this->getAgendaItems($oldEvent);
  462.                     $agendaItemsdocs = $this->getDocuments(
  463.                         'OceanExpertBundle:AgendaitemDocuments',
  464.                         $idEvent
  465.                     );
  466.                     */
  467.                     //useless to copy documents
  468.                     /*
  469.                     $eventPresentations = $this->getEventPresentations($idEvent);
  470.                     $eventBackgrounddocs = $this->getBackgrounddocs($idEvent);
  471.                     $eventOtherdocs = $this->getEventOtherDocs($idEvent);
  472.                     */
  473.                     //we cannot have a final report as this is a new event....
  474.                     //$report = $this->getEventReport($idEvent);
  475.                     //check the start and end date
  476.                     //cannot be in the past
  477.                     $startOn $oldEvent->getStartOn();
  478.                     $endOn $oldEvent->getEndOn();
  479.                     $today = new DateTime();
  480.                     if ($startOn $today) {
  481.                         $startOn $today;
  482.                         $endOn $today;
  483.                     }
  484.                     //make the new event
  485.                     $event = new Events();
  486.                     $event->setIdEventtype($oldEvent->getIdEventtype());
  487.                     $event->setTitle($eventTitle);
  488.                     $event->setShorttitle($oldEvent->getShorttitle());
  489.                     $event->setSummary($oldEvent->getSummary());
  490.                     $event->setStartOn($startOn);
  491.                     $event->setendOn($endOn);
  492.                     $event->setAddress($oldEvent->getAddress());
  493.                     $event->setCity($oldEvent->getCity());
  494.                     $event->setState($oldEvent->getState());
  495.                     $event->setPostcode($oldEvent->getPostcode());
  496.                     $event->setIdCountry($oldEvent->getIdCountry());
  497.                     $event->setIdInst($oldEvent->getIdInst());
  498.                     $event->setUseInstAddr($oldEvent->getUseInstAddr());
  499.                     $event->setNotes($oldEvent->getNotes());
  500.                     $event->setWebsite($oldEvent->getWebsite());
  501.                     $event->setRedirectWebsite($oldEvent->getRedirectWebsite());
  502.                     $event->setKeywords($oldEvent->getKeywords());
  503.                     $event->setCreatedBy($userId);
  504.                     $event->setCreatedAt(new DateTime());
  505.                     $event->setUpdatedAt(new DateTime());
  506.                     $event->setLastEditBy($userId);
  507.                     $event->setIsPublic($oldEvent->getIsPublic());
  508.                     $event->setIsOpen($oldEvent->getIsOpen());
  509.                     $event->setShowTravel($oldEvent->getShowTravel());
  510.                     $event->setEmailPeople($oldEvent->getEmailPeople());
  511.                     $event->setRegOnline($oldEvent->getRegOnline());
  512.                     $event->setStatus(0);
  513.                     if (isset($oldIdContact)) {
  514.                         $event->setIdContact($oldIdContact);
  515.                     }
  516.                     $em->persist($event);
  517.                     $em->flush();
  518.                     $idEvent $event->getIdEvent();
  519.                     //participants and their roles should be copied also
  520.                     foreach ($participantRoles as $participantRole) {
  521.                         $newParticipantRole = new EventParticipantroles();
  522.                         $newParticipantRole
  523.                             ->setIdEvent($idEvent)
  524.                             ->setOrdering($participantRole->getOrdering())
  525.                             ->setPptOrdering($participantRole->getPptOrdering())
  526.                             ->setRole($participantRole->getRole())
  527.                             ->setIdRole($participantRole->getIdRole());
  528.                         $em->persist($newParticipantRole);
  529.                         $em->flush();
  530.                     }
  531.                     //participants
  532.                     foreach ($participants as $participant) {
  533.                         $newRequest = new Request();
  534.                         $newRequest->request->set('idInd'$participant['idInd']);
  535.                         $newRequest->request->set('idEvent'$idEvent);
  536.                         $newRequest->request->set('membertype'3);
  537.                         $this->addEventMemberAction(
  538.                             $newRequest,
  539.                             $fosTokenGenerator,
  540.                             $fosUserManager
  541.                         );
  542.                     }
  543.                     foreach ($eventStaffs as $eventStaff) {
  544.                         $newRequest = new Request();
  545.                         $newRequest->request->set('idInd'$eventStaff['id']);
  546.                         $newRequest->request->set('idEvent'$idEvent);
  547.                         $newRequest->request->set('membertype'2);
  548.                         $this->addEventMemberAction(
  549.                             $newRequest,
  550.                             $fosTokenGenerator,
  551.                             $fosUserManager
  552.                         );
  553.                     }
  554.                     foreach ($eventContacts as $eventContact) {
  555.                         $newRequest = new Request();
  556.                         $newRequest->request->set('idInd'$eventContact['id']);
  557.                         $newRequest->request->set('idEvent'$idEvent);
  558.                         $newRequest->request->set('membertype'1);
  559.                         $this->addEventMemberAction(
  560.                             $newRequest,
  561.                             $fosTokenGenerator,
  562.                             $fosUserManager
  563.                         );
  564.                     }
  565.                     //labels
  566.                     foreach ($eventLabels as $eventLabel) {
  567.                         $newEventLabel = new EventLabels();
  568.                         $newEventLabel
  569.                             ->setIdEvent($idEvent)
  570.                             ->setIdGroup($eventLabel->getIdGroup())
  571.                             ->setIdLabel($eventLabel->getIdLabel());
  572.                         $em->persist($newEventLabel);
  573.                         $em->flush();
  574.                     }
  575.                     //groups
  576.                     foreach ($eventGroups as $eventGroup) {
  577.                         $newEventGroup = new EventGroups();
  578.                         $newEventGroup
  579.                             ->setIdEvent($idEvent)
  580.                             ->setIdGroup($eventGroup->getIdGroup())
  581.                             ->setInCalendar($eventGroup->getInCalendar());
  582.                         $em->persist($newEventGroup);
  583.                         $em->flush();
  584.                     }
  585.                 } else {
  586.                     return $this->render(
  587.                         'Exception/error.html.twig',
  588.                         array(
  589.                             'message' => "cannot find the event with id '$idEvent'"
  590.                         )
  591.                     );
  592.                 }
  593.             } else {
  594.                 /*
  595.                  * seems we have no 'real' idEvent,
  596.                  * strange but no panick, let's just make an empty event
  597.                  */
  598.                 $event = new Events();
  599.                 $event->setStatus(0);
  600.                 $event->setCreatedBy($userId);
  601.                 $event->setCreatedAt(new DateTime());
  602.                 $em->persist($event);
  603.                 $em->flush();
  604.                 $idEvent $event->getIdEvent();
  605.             }
  606.             /*
  607.              * DO NOT DO THIS as this will persist and flush the event again
  608.              * remove this if this is still here in 2.4.0
  609.              * #641
  610.             if ($request->getMethod() === 'POST') {
  611.                 $formData = $request->request->all();
  612.                 $files = $request->files;
  613.                 return $this->storeTheEvent(
  614.                     $event,
  615.                     $formData,
  616.                     $files,
  617.                     $userId
  618.                 );
  619.             }
  620.             */
  621.             /*
  622.             $participantStatus = array(
  623.                 '0' => 'Confirmed & Approved',
  624.                 '1' => 'Need Confirmation',
  625.                 '2' => 'Need Approval',
  626.                 '3' => 'Declined',
  627.                 '4' => 'Rejected',
  628.             );
  629.             */
  630.             $calenderGroups $this->getDoctrine()
  631.                 ->getRepository('OceanExpertBundle:EventGroups')
  632.                 ->createQueryBuilder('e')
  633.                 ->select('e.idGroup')
  634.                 ->where('e.idEvent = :idEvent')
  635.                 ->setParameter('idEvent'$idEvent)
  636.                 ->getQuery()
  637.                 ->getResult(AbstractQuery::HYDRATE_ARRAY);
  638.             $calenderGroups array_column(
  639.                 $calenderGroups,
  640.                 'idGroup'
  641.             );
  642.             $availableEventType $em
  643.                 ->getRepository('OceanExpertBundle:Eventtypes')
  644.                 ->findBy(
  645.                     array(),
  646.                     array(
  647.                         'eventtypeName' => 'ASC'
  648.                     )
  649.                 );
  650.             $countries $this->getDoctrine()
  651.                 ->getRepository('OceanExpertBundle:Countries')
  652.                 ->findBy(
  653.                     array(),
  654.                     ['country' => 'ASC']
  655.                 );
  656.             $instituteSeaRegion $this->getDoctrine()
  657.                 ->getRepository('OceanExpertBundle:Regions')
  658.                 ->seaRegions();
  659.             $availableCalenderGroups $this->getSitesGroup();
  660.             $documentList $this->getDocumentList();
  661.             $data = array(
  662.                 'data' => array(
  663.                     'event' => $event,
  664.                     'countries' => $countries,
  665.                     'availableCalenderGroups' => $availableCalenderGroups,
  666.                     'availableEventType' => $availableEventType,
  667.                     'availableEventLabels' => $this->availableEventLabels,
  668.                     'availableSeaRegions' => $instituteSeaRegion,
  669.                     'participantRoles' => $participantRoles,
  670.                     'calenderGroups' => $calenderGroups,
  671.                     'eventLabels' => $eventLabels,
  672.                     'participants' => $participants,
  673.                     'eventStaff' => $eventStaffs,
  674.                     'eventContacts' => $eventContacts,
  675.                     'unknownParticipants' => $unknownParticipants,
  676.                     'eventPresentations' => $eventPresentations,
  677.                     'eventBackgrounddocs' => $eventBackgrounddocs,
  678.                     'eventOtherdocs' => $eventOtherdocs,
  679.                     'agendaItems' => $agendaItems,
  680.                     'agendaItemsdocs' => $agendaItemsdocs,
  681.                     'report' => $report,
  682.                     'participantStatus' => $this->participantStatus,
  683.                     'documentList' => $documentList,
  684.                     'eventEmailContact' => $idContact,
  685.                     'eventImage' => $eventImage,
  686.                 )
  687.             );
  688.             return $this->redirect(
  689.                 $this->generateUrl(
  690.                     'edit_event',
  691.                     array(
  692.                         'idEvent' => $event->getIdEvent()
  693.                     )
  694.                 ) . '#overview'
  695.             );
  696.         } else {
  697.             return $this->render(
  698.                 'Exception/error.html.twig',
  699.                 array(
  700.                     'message' => 'seems you do not have permissions to do this, please log in before making an event'
  701.                 )
  702.             );
  703.         }
  704.     }
  705.     /**
  706.      * store the form and go to the next step
  707.      *
  708.      * @param Events $event    the event that has been created/edited/copied
  709.      * @param array  $formData data from the form
  710.      * @param object $files    submitted files
  711.      * @param int    $userId   id of the user doing this
  712.      *
  713.      * @return RedirectResponse
  714.      *
  715.      * @throws \Exception
  716.      *
  717.      */
  718.     public function storeTheEvent(
  719.         Events $event,
  720.         array $formData,
  721.         object $files,
  722.         int $userId
  723.     ): RedirectResponse
  724.     {
  725.         $em $this->getDoctrine()->getManager();
  726.         $event->setLastEditBy($userId);
  727.         $event->setUpdatedAt(new DateTime());
  728.         if (isset($formData['form'])
  729.             && $formData['form'] == 'peopleOption'
  730.         ) {
  731.             $event->setRegOnline(isset($formData['registerOnline']) ? $formData['registerOnline'] : 0);
  732.             $event->setEmailPeople(isset($formData['emailPeople']) ? $formData['emailPeople'] : 0);
  733.             $em->persist($event);
  734.             $em->flush();
  735.             $idEvent $event->getIdEvent();
  736.             if (array_key_exists('role'$formData)
  737.                 && count($formData['role']) == count($formData['roledisplay'])
  738.             ) {
  739.                 $count count($formData['roledisplay']);
  740.                 $eventRoles $this->getDoctrine()
  741.                     ->getRepository('OceanExpertBundle:EventParticipantroles')
  742.                     ->findBy(
  743.                         array(
  744.                             'idEvent' => $event->getIdEvent()
  745.                         )
  746.                     );
  747.                 foreach ($eventRoles as $eventRole) {
  748.                     $em->remove($eventRole);
  749.                     $em->flush();
  750.                 }
  751.                 for ($i 0$i $count$i++) {
  752.                     if ($formData['role'][$i] != '') {
  753.                         $eventRoles = new EventParticipantroles();
  754.                         $eventRoles->setIdEvent($event->getIdEvent());
  755.                         $eventRoles->setRole($formData['role'][$i]);
  756.                         $eventRoles->setIdRole($formData['idRole'][$i]);
  757.                         $eventRoles->setPptOrdering($formData['roledisplay'][$i]);
  758.                         $eventRoles->setOrdering($i);
  759.                         $em->persist($eventRoles);
  760.                         $em->flush();
  761.                     }
  762.                 }
  763.             }
  764.             //finish event
  765.             //redirect to confirmation page
  766.             //see #35
  767.             if (isset($formData['finish'])
  768.                 && $formData['finish']
  769.             ) {
  770.                 return $this->redirect(
  771.                     $this->generateUrl(
  772.                         'confirm_event_creation',
  773.                         array(
  774.                             'eventId' => $idEvent
  775.                         )
  776.                     )
  777.                 );
  778.             }
  779.             return $this->redirect(
  780.                 $this->generateUrl(
  781.                     'edit_event',
  782.                     array(
  783.                         'idEvent' => $event->getIdEvent()
  784.                     )
  785.                 ) . '#people'
  786.             );
  787.         } else {
  788.             $event->setIdEventtype($formData['eventType']);
  789.             $event->setTitle($formData['eventTitle']);
  790.             $event->setShorttitle(isset($formData['shortTitle']) ? $formData['shortTitle'] : '');
  791.             $event->setSummary(isset($formData['summary']) ? $formData['summary'] : '');
  792.             $event->setStartOn(new DateTime(str_replace('/''-'$formData['startDate'])));
  793.             $event->setEndOn(new DateTime(str_replace('/''-'$formData['endDate'])));
  794.             $event->setAddress(isset($formData['eventAddress']) ? $formData['eventAddress'] : '');
  795.             $event->setCity(isset($formData['eventCity']) ? $formData['eventCity'] : '');
  796.             $event->setState(isset($formData['eventState']) ? $formData['eventState'] : '');
  797.             $event->setPostcode(isset($formData['eventPostCode']) ? $formData['eventPostCode'] : '');
  798.             $event->setIdCountry(isset($formData['country']) ? $formData['country'] : null);
  799.             if (isset($formData['institute'])) {
  800.                 $event->setIdInst($formData['institute']);
  801.             }
  802.             $event->setNotes(isset($formData['eventNotes']) ? $formData['eventNotes'] : '');
  803.             $event->setWebsite(isset($formData['eventWebsite']) ? $formData['eventWebsite'] : '');
  804.             $event->setKeywords(isset($formData['eventKeywords']) ? $formData['eventKeywords'] : '');
  805.             $event->setUseInstAddr(isset($formData['instAddr']) ? 0);
  806.             $event->setIdContact($userId);
  807.             $event->setIsPublic(1);
  808.             $event->setIsOpen(isset($formData['is_open']) ? $formData['is_open'] : 0);
  809.             $em->persist($event);
  810.             $em->flush();
  811.             $idEvent $event->getIdEvent();
  812.             if (array_key_exists('eventType'$formData)
  813.                 && in_array($formData['eventType'], [34])
  814.             ) {
  815.                 //for Workshop and training course we always have the roles of instructor and learner
  816.                 //check if these have already been set, if not create them
  817.                 foreach (['Instructor''Learner'] as $key => $role) {
  818.                     $fixedRole $em
  819.                         ->getRepository('OceanExpertBundle:EventParticipantroles')
  820.                         ->findOneBy(
  821.                             array(
  822.                                 'role' => $role,
  823.                                 'idEvent' => $idEvent
  824.                             )
  825.                         );
  826.                     if (!$fixedRole) {
  827.                         //this role seems not to exist, make it
  828.                         //find the new value for the role id
  829.                         try {
  830.                             $highestRoleIdReq $this->getDoctrine()
  831.                                 ->getRepository('OceanExpertBundle:EventParticipantroles')
  832.                                 ->createQueryBuilder('e')
  833.                                 ->select('e.idRole')
  834.                                 ->where('e.idEvent = :idEvent')
  835.                                 ->setParameter('idEvent'$idEvent)
  836.                                 ->orderBy('e.idRole''DESC')
  837.                                 ->setMaxResults(1)
  838.                                 ->getQuery()
  839.                                 ->getSingleResult(AbstractQuery::HYDRATE_ARRAY);
  840.                             $highestRoleId $highestRoleIdReq['idRole'];
  841.                             $highestRoleId ++;
  842.                         } catch (NoResultException $e) {
  843.                             $highestRoleId 0;
  844.                         }
  845.                         $fixedRole = new EventParticipantroles();
  846.                         $fixedRole->setIdEvent($event->getIdEvent());
  847.                         $fixedRole->setRole($role);
  848.                         $fixedRole->setIdRole($highestRoleId);
  849.                         $fixedRole->setPptOrdering(0);
  850.                         $fixedRole->setOrdering($key 1);
  851.                         $em->persist($fixedRole);
  852.                         $em->flush();
  853.                     }
  854.                 }
  855.             }
  856.             //save the choosen calender groups
  857.             //mind that we have to remove the old ones first
  858.             $eventGroups $this->getDoctrine()
  859.                 ->getRepository('OceanExpertBundle:EventGroups')
  860.                 ->findBy(
  861.                     array(
  862.                         'idEvent' => $idEvent
  863.                     )
  864.                 );
  865.             foreach ($eventGroups as $eventGroup) {
  866.                 $em->remove($eventGroup);
  867.             }
  868.             $em->flush();
  869.             if (isset($formData['calenderGroups'])) {
  870.                 foreach ($formData['calenderGroups'] as $idGroup) {
  871.                     $eventGroup $em
  872.                         ->getRepository('OceanExpertBundle:EventGroups')
  873.                         ->findOneBy(
  874.                             array(
  875.                                 'idEvent' => $idEvent,
  876.                                 'idGroup' => $idGroup
  877.                             )
  878.                         );
  879.                     if (!$eventGroup) {
  880.                         $eventGroup = new EventGroups();
  881.                     }
  882.                     $eventGroup->setIdEvent($idEvent);
  883.                     $eventGroup->setIdGroup($idGroup);
  884.                     $eventGroup->setInCalendar(1);
  885.                     $em->persist($eventGroup);
  886.                     $em->flush();
  887.                 }
  888.             }
  889.             $eventLabels $this->getDoctrine()
  890.                 ->getRepository('OceanExpertBundle:EventLabels')
  891.                 ->findBy(
  892.                     array(
  893.                         'idEvent' => $idEvent
  894.                     )
  895.                 );
  896.             foreach ($eventLabels as $eachlabel) {
  897.                 $em->remove($eachlabel);
  898.             }
  899.             $em->flush();
  900.             if (isset($formData['labelGroup'])) {
  901.                 $labels array_combine($formData['labelGroup'], $formData['label']);
  902.                 foreach ($labels as $group => $grouplabel) {
  903.                     $eventLabel = new EventLabels();
  904.                     $eventLabel->setIdEvent($idEvent);
  905.                     $eventLabel->setIdGroup($group);
  906.                     $eventLabel->setIdLabel($grouplabel);
  907.                     $em->persist($eventLabel);
  908.                     $em->flush();
  909.                 }
  910.             }
  911.             if (null != $files->get('eventImage')) {
  912.                 $file $files->get('eventImage');
  913.                 $filedata = array(
  914.                     'id' => $idEvent,
  915.                     'file' => $file,
  916.                     'filename' => 'eventImage.png',
  917.                     'type' => 'image',
  918.                     'path' => "uploads/events/" $idEvent "/",
  919.                     'user' => $userId,
  920.                     'dbEntry' => 1,
  921.                 );
  922.                 $this->uploadFile($filedata);
  923.             } else {
  924.                 if (isset($formData['eventlogo'])
  925.                     && $formData['eventlogo'] == ''
  926.                 ) {
  927.                     $filename 'uploads/events/' $idEvent '/eventImage.png';
  928.                     if (file_exists($filename)) {
  929.                         unlink($filename);
  930.                     }
  931.                     $eventFile $this->getDoctrine()
  932.                         ->getManager()
  933.                         ->getRepository('OceanExpertBundle:EventFiles')
  934.                         ->findOneBy(
  935.                             array(
  936.                                 'idEvent' => $idEvent
  937.                             )
  938.                         );
  939.                     if ($eventFile) {
  940.                         $em->remove($eventFile);
  941.                         $em->flush();
  942.                     }
  943.                 }
  944.             }
  945.             //finish event
  946.             //redirect to confirmation page
  947.             //see #35
  948.             if (isset($formData['finish'])
  949.                 && $formData['finish']
  950.             ) {
  951.                 return $this->redirect(
  952.                     $this->generateUrl(
  953.                         'confirm_event_creation',
  954.                         array(
  955.                             'eventId' => $idEvent
  956.                         )
  957.                     )
  958.                 );
  959.             }
  960.             return $this->redirect(
  961.                 $this->generateUrl(
  962.                     'edit_event',
  963.                     array(
  964.                         'idEvent' => $idEvent
  965.                     )
  966.                 ) . '#peopleoptions'
  967.             );
  968.         }
  969.     }
  970.     /**
  971.      * show the confirmation page telling the event has been created
  972.      * OR updated
  973.      *
  974.      * @param Request $request
  975.      * @param $eventId
  976.      *
  977.      * @return Response
  978.      */
  979.     public function confirmEventCreationAction(Request $request$eventId)
  980.     {
  981.         //check if the event is active
  982.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  983.         $repository->add(
  984.             'select',
  985.             ' e.status'
  986.         );
  987.         $repository->add(
  988.             'from',
  989.             'OceanExpertBundle:Events e'
  990.         );
  991.         $repository->where('e.idEvent = :eventId');
  992.         $repository->setParameter('eventId'$eventId);
  993.         $active $repository->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY);
  994.         if ($active[0]['status'] == 0) {
  995.             $template 'Event/confirmEventCreation.html.twig';
  996.         } elseif ($active[0]['status'] == 1) {
  997.             $template 'Event/confirmEventUpdate.html.twig';
  998.         } else {
  999.             return $this->render(
  1000.                 'Exception/error.html.twig',
  1001.                 array(
  1002.                     'message' => 'This event seems to be deleted, so no use to update it.'
  1003.                 )
  1004.             );
  1005.         }
  1006.         return $this->render(
  1007.             $template,
  1008.             array(
  1009.                 'eventId' => $eventId,
  1010.                 'active' => $active[0]
  1011.             )
  1012.         );
  1013.     }
  1014.     /**
  1015.      * @param $event
  1016.      * @return mixed
  1017.      */
  1018.     public function getEventParticipants($event)
  1019.     {
  1020.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1021.         $repository->add(
  1022.             'select',
  1023.             'e.idRole, 
  1024.             e.idInd, 
  1025.             i.idInd as id, 
  1026.             i.title, 
  1027.             i.fname, 
  1028.             i.sname, 
  1029.             i.tel, 
  1030.             r.role, 
  1031.             i.useInstAddr, 
  1032.             c.country, 
  1033.             r.role,
  1034.             r.ordering, 
  1035.             e.status, 
  1036.             e.accommodation, 
  1037.             i.tel'
  1038.         );
  1039.         $repository->add(
  1040.             'from',
  1041.             'OceanExpertBundle:EventParticipants e'
  1042.         );
  1043.         $repository->leftJoin(
  1044.             'OceanExpertBundle:Indiv',
  1045.             'i',
  1046.             'WITH',
  1047.             'i.idInd = e.idInd'
  1048.         );
  1049.         $repository->leftJoin(
  1050.             'OceanExpertBundle:Countries',
  1051.             'c',
  1052.             'WITH',
  1053.             'c.idCountry = i.countryCode'
  1054.         );
  1055.         $repository->leftJoin(
  1056.             'OceanExpertBundle:EventParticipantroles',
  1057.             'r',
  1058.             'WITH',
  1059.             'e.idRole = r.idRole and e.idEvent = r.idEvent'
  1060.         );
  1061.         $repository->andWhere('e.idEvent = :eventId');
  1062.         $repository->orderBy('r.ordering''asc');
  1063.         $repository->addOrderBy('i.sname''asc');
  1064.         $repository->setParameter('eventId'$event);
  1065.         $participants $repository->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY);
  1066.         foreach ($participants as $key => $participant) {
  1067.             //correct the country if the expert has choosen to use the institute address
  1068.             if ($participant['useInstAddr'] === 1) {
  1069.                 //get the correct country, being the country of the institute
  1070.                 $participants[$key]['country'] = 'tbd';
  1071.                 $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1072.                 $repository->add(
  1073.                     'select',
  1074.                     'c.country'
  1075.                 );
  1076.                 $repository->add(
  1077.                     'from',
  1078.                     'OceanExpertBundle:Countries c'
  1079.                 );
  1080.                 $repository->leftJoin(
  1081.                     'OceanExpertBundle:Institutions',
  1082.                     'i',
  1083.                     'WITH',
  1084.                     'c.idCountry = i.countryCode'
  1085.                 );
  1086.                 $repository->leftJoin(
  1087.                     'OceanExpertBundle:IndivInstitution',
  1088.                     'ii',
  1089.                     'WITH',
  1090.                     'ii.idInst = i.idInst'
  1091.                 );
  1092.                 $repository->where('ii.idInd = :idInd');
  1093.                 $repository->setParameter('idInd'$participant['idInd']);
  1094.                 $country $repository->getQuery()->getResult();
  1095.                 if (isset($country[0])
  1096.                     && isset($country[0]['country'])
  1097.                     && $country[0]['country'] != ''
  1098.                 ) {
  1099.                     $participants[$key]['country'] = $country[0]['country'];
  1100.                 }
  1101.             }
  1102.         }
  1103.         return $participants;
  1104.     }
  1105.     /**
  1106.      * @param $event
  1107.      * @return mixed
  1108.      */
  1109.     public function getEventStaff($event)
  1110.     {
  1111.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1112.         $repository->add('select''i.idInd as id, i.title, i.fname, i.sname, c.country');
  1113.         $repository->add('from''OceanExpertBundle:EventStaff e');
  1114.         $repository->leftJoin('OceanExpertBundle:Indiv''i''WITH''i.idInd = e.idInd');
  1115.         $repository->leftJoin('OceanExpertBundle:Countries''c''WITH''c.idCountry = i.countryCode');
  1116.         $repository->where('e.idEvent = :eventId');
  1117.         $repository->setParameter('eventId'$event);
  1118.         return $repository->getQuery()->getResult();
  1119.     }
  1120.     /**
  1121.      * @param $event
  1122.      * @return mixed
  1123.      */
  1124.     public function getEventOrganisers($event)
  1125.     {
  1126.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1127.         $repository->add('select''i.idInd as id, i.title, i.fname, i.sname, e.contactFor, c.country');
  1128.         $repository->add('from''OceanExpertBundle:EventContacts e');
  1129.         $repository->leftJoin('OceanExpertBundle:Indiv''i''WITH''i.idInd = e.idInd');
  1130.         $repository->leftJoin('OceanExpertBundle:Countries''c''WITH''c.idCountry = i.countryCode');
  1131.         $repository->where('e.idEvent = :eventId');
  1132.         $repository->setParameter('eventId'$event);
  1133.         return $repository->getQuery()->getResult();
  1134.     }
  1135.     /**
  1136.      * @param int $event id of the event
  1137.      * @param string $order what column do we use to order the result
  1138.      *
  1139.      * @return array
  1140.      */
  1141.     public function getEventPresentations($event$order '')
  1142.     {
  1143.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1144.         $repository
  1145.             ->add(
  1146.                 'select',
  1147.                 'e.idAgendaitem, d.docCode, d.title, d.idDoc, d.authorText, d.updatedAt'
  1148.             )
  1149.             ->add(
  1150.                 'from',
  1151.                 'OceanExpertBundle:EventPresentations e'
  1152.             )
  1153.             ->leftJoin(
  1154.                 'OceanExpertBundle:Documents',
  1155.                 'd',
  1156.                 'WITH',
  1157.                 'd.idDoc = e.idDoc'
  1158.             )
  1159.             ->where(
  1160.                 'e.idEvent = :eventId'
  1161.             )
  1162.             ->setParameter(
  1163.                 'eventId',
  1164.                 $event
  1165.             );
  1166.         if ($order == "date") {
  1167.             $repository->orderBy('d.updatedAt');
  1168.         } else {
  1169.             $repository->orderBy('e.idAgendaitem');
  1170.         }
  1171.         return $repository->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY);
  1172.     }
  1173.     /**
  1174.      * @param int    $event id of the event
  1175.      * @param string $order what column will we use to order the results
  1176.      *
  1177.      * @return array
  1178.      */
  1179.     public function getBackgrounddocs(int $eventstring $order ''): array
  1180.     {
  1181.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1182.         $repository->add('select''d.docCode, d.title, d.idDoc, d.authorText, d.updatedAt ,e.idAgendaitem');
  1183.         $repository->add('from''OceanExpertBundle:EventBackgrounddocs e');
  1184.         $repository->leftJoin('OceanExpertBundle:Documents''d''WITH''d.idDoc = e.idDoc');
  1185.         $repository->where('e.idEvent = :eventId');
  1186.         $repository->setParameter('eventId'$event);
  1187.         if ($order == "date") {
  1188.             $repository->orderBy('d.updatedAt');
  1189.         } else {
  1190.             $repository->orderBy('e.idAgendaitem');
  1191.         }
  1192.         $backgrounddocs $repository->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY);
  1193.         return $backgrounddocs;
  1194.     }
  1195.     /**
  1196.      * @param $event
  1197.      * @param string $order
  1198.      * @return mixed
  1199.      */
  1200.     function getEventOtherDocs($event$order '')
  1201.     {
  1202.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1203.         $repository->add('select''d.docCode, d.title, d.idDoc, d.authorText, d.updatedAt');
  1204.         $repository->add('from''OceanExpertBundle:EventOtherdocs o');
  1205.         $repository->leftJoin('OceanExpertBundle:Documents''d''WITH''d.idDoc = o.idDoc');
  1206.         $repository->where('o.idEvent = :eventId');
  1207.         $repository->setParameter('eventId'$event);
  1208.         if ($order == "date") {
  1209.             $repository->orderBy('d.updatedAt');
  1210.         } else {
  1211.             $repository->orderBy('o.ordering');
  1212.         }
  1213.         $otherDocs $repository->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY);
  1214.         return $otherDocs;
  1215.     }
  1216.     /**
  1217.      * @param $table
  1218.      * @param $event
  1219.      *
  1220.      * @return array|DocumentFiles[]
  1221.      */
  1222.     public function getDocuments($table$event)
  1223.     {
  1224.         $idFiles $this->getDoctrine()
  1225.             ->getRepository($table)
  1226.             ->createQueryBuilder('e')
  1227.             ->select('d.idDoc')
  1228.             ->leftJoin('OceanExpertBundle:DocumentFiles''d''WITH''d.idDoc = e.idDoc')
  1229.             ->where('e.idEvent = :eventId')
  1230.             ->setParameter('eventId'$event)
  1231.             ->getQuery()
  1232.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  1233.         $idFiles array_column($idFiles'idDoc');
  1234.         $documents $this->getDoctrine()->getManager()
  1235.             ->getRepository('OceanExpertBundle:DocumentFiles')
  1236.             ->findBy(
  1237.                 array(
  1238.                     'idDoc' => $idFiles
  1239.                 )
  1240.             );
  1241.         return $documents;
  1242.     }
  1243.     /**
  1244.      * @param int $event id of the event
  1245.      *
  1246.      * @return mixed
  1247.      */
  1248.     function getEventReport($event)
  1249.     {
  1250.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1251.         $repository->add('select''d.docCode, d.title, d.idDoc, d.authorText, d.updatedAt');
  1252.         $repository->add('from''OceanExpertBundle:EventReports e');
  1253.         $repository->leftJoin('OceanExpertBundle:Documents''d''WITH''d.idDoc = e.idDoc');
  1254.         $repository->where('e.idEvent = :eventId');
  1255.         $repository->setParameter('eventId'$event);
  1256.         $otherDocs $repository->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY);
  1257.         return $otherDocs;
  1258.     }
  1259.     /**
  1260.      * @param $event
  1261.      *
  1262.      * @return string json
  1263.      */
  1264.     public function getAgendaItems($event)
  1265.     {
  1266.         $em $this->getDoctrine()->getManager();
  1267.         $checkEvent $em
  1268.             ->getRepository('OceanExpertBundle:EventAgendaitems')
  1269.             ->findOneBy(
  1270.                 array(
  1271.                     'idEvent' => $event
  1272.                 )
  1273.             );
  1274.         if (!is_null($checkEvent)) {
  1275.             $agendaItems $this->getDoctrine()
  1276.                 ->getRepository('OceanExpertBundle:EventAgendaitems')
  1277.                 ->createQueryBuilder('e')
  1278.                 ->select('e.idAgendaitem, e.id, e.title, e.parentIdevent, e.agendaOrder, e.notes, e.requiredActions')
  1279.                 ->where('e.idEvent = :eventId')
  1280.                 ->setParameter(':eventId'$event)
  1281.                 ->addOrderBy('e.agendaOrder''ASC')
  1282.                 ->getQuery()
  1283.                 ->getResult(AbstractQuery::HYDRATE_ARRAY);
  1284.             foreach ($agendaItems as $key => $item) {
  1285.                 $agendaDocuments $this->getDoctrine()
  1286.                     ->getRepository('OceanExpertBundle:AgendaitemDocuments')
  1287.                     ->createQueryBuilder('a')
  1288.                     ->select('d.title as doctitle, d.idDoc, d.docCode')
  1289.                     ->leftJoin('OceanExpertBundle:Documents''d''WITH''d.idDoc = a.idDoc')
  1290.                     ->where('a.idEvent = :eventId')
  1291.                     ->andwhere('a.idAgendaitem = :idAgendaitem')
  1292.                     ->setParameter(':eventId'$event)
  1293.                     ->setParameter(':idAgendaitem'$item['idAgendaitem'])
  1294.                     ->getQuery()
  1295.                     ->getResult(AbstractQuery::HYDRATE_ARRAY);
  1296.                 $agendaItems[$key]['documents'] = $agendaDocuments;
  1297.             }
  1298.             // do not return a json response, we stay in this controller, so no headers needed
  1299.             //return new JsonResponse(
  1300.             return json_encode(
  1301.                 array(
  1302.                     'status' => 1,
  1303.                     EventController::buildTree(
  1304.                         $agendaItems,
  1305.                         0
  1306.                     )
  1307.                 )
  1308.             );
  1309.         }
  1310.         // do not return a json response, we stay in this controller, so no headers needed
  1311.         //return new JsonResponse(
  1312.         return json_encode(
  1313.             array(
  1314.                 'status' => 0,
  1315.                 'message' => 'no agenda items found'
  1316.             )
  1317.         );
  1318.     }
  1319.     /**
  1320.      * @param $ar
  1321.      * @param null $pid
  1322.      * @return array
  1323.      */
  1324.     public static function buildTree($ar$pid null)
  1325.     {
  1326.         $op = array();
  1327.         $i 1;
  1328.         foreach ($ar as $item) {
  1329.             if ($item['parentIdevent'] == $pid) {
  1330.                 $op[$i] = array(
  1331.                     'title' => $item['title'],
  1332.                     'notes' => $item['notes'],
  1333.                     'requiredActions' => $item['requiredActions'],
  1334.                     'parentIdevent' => $item['parentIdevent'],
  1335.                     'id' => $item['id'],
  1336.                     'agendaOrder' => $item['agendaOrder'],
  1337.                     'idAgendaitem' => $item['idAgendaitem'],
  1338.                     'documents' => $item['documents']
  1339.                 );
  1340.                 //using recursion
  1341.                 $children EventController::buildTree($ar$item['id']);
  1342.                 if ($children) {
  1343.                     $op[$i]['children'] = $children;
  1344.                 }
  1345.             }
  1346.             $i++;
  1347.         }
  1348.         return $op;
  1349.     }
  1350.     /**
  1351.      * @param $data
  1352.      */
  1353.     public function uploadFile($data)
  1354.     {
  1355.         $file $data['file'];
  1356.         $id $data['id'];
  1357.         if (null !== $file) {
  1358.             $filetype $file->guessClientExtension();
  1359.             if (array_key_exists('filename'$data)) {
  1360.                 $filename $data['filename'];
  1361.             } else {
  1362.                 $filename $file->getClientOriginalName();
  1363.             }
  1364.             if (array_key_exists('path'$data)) {
  1365.                 $directory $data['path'];
  1366.             } else {
  1367.                 $directory "uploads/events/" $id "/";
  1368.             }
  1369.             $uploadfile $file->move($directory$filename);
  1370.             if ($data['dbEntry'] == 1) {
  1371.                 $em $this->getDoctrine()->getManager();
  1372.                 $eventFile $this->getDoctrine()->getManager()
  1373.                     ->getRepository('OceanExpertBundle:EventFiles')
  1374.                     ->findOneBy(
  1375.                         array(
  1376.                             'idEvent' => $id,
  1377.                             'filename' => $filename,
  1378.                             'path' => $directory
  1379.                         )
  1380.                     );
  1381.                 if (!$eventFile) {
  1382.                     $eventFile = new EventFiles();
  1383.                 }
  1384.                 $eventFile->setIdEvent($id);
  1385.                 $eventFile->setType($data['type']);
  1386.                 $eventFile->setFilename($filename);
  1387.                 $eventFile->setPath($directory);
  1388.                 $eventFile->setCreatedBy($data['user']);
  1389.                 $eventFile->setCreatedAt(new DateTime());
  1390.                 $eventFile->setDeleted(0);
  1391.                 $em->persist($eventFile);
  1392.                 $em->flush();
  1393.             }
  1394.         }
  1395.     }
  1396.     /**
  1397.      * @return mixed
  1398.      */
  1399.     public function getSitesGroup()
  1400.     {
  1401.         $sitesGroup $this->getDoctrine()
  1402.             ->getRepository('OceanExpertBundle:Groups')
  1403.             ->createQueryBuilder('g')
  1404.             ->select('g.idGroup, g.groupname, g.description')
  1405.             ->where('g.hasSite = 1')
  1406.             ->orderBy('g.groupname''ASC')
  1407.             ->getQuery()
  1408.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  1409.         return $sitesGroup;
  1410.     }
  1411.     /**
  1412.      * @return array
  1413.      */
  1414.     function getDocumentList()
  1415.     {
  1416.         $grouplist array_map('current'$this->getUserGroups());
  1417.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1418.         $repository->add('select''distinct(d.idGroup) as idGroup, g.groupname');
  1419.         $repository->add('from''OceanExpertBundle:DoclistGroups d');
  1420.         $repository->leftJoin('OceanExpertBundle:Groups''g''WITH''d.idGroup = g.idGroup');
  1421.         $repository->where('g.idGroup in (:grouplist)');
  1422.         $repository->setParameter('grouplist'$grouplist);
  1423.         $repository->orderBy('g.groupname');
  1424.         $docGroups $repository->getQuery()->getResult();
  1425.         $documentList = array();
  1426.         foreach ($docGroups as $docGroup) {
  1427.             $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1428.             $repository->add('select''d.idDoclist, d.title, dg.idGroup');
  1429.             $repository->add('from''OceanExpertBundle:Doclists d');
  1430.             $repository->leftJoin('OceanExpertBundle:DoclistGroups''dg''WITH''dg.idDoclist = d.idDoclist');
  1431.             $repository->where('dg.idGroup =:idGroup');
  1432.             $repository->setParameter('idGroup'$docGroup['idGroup']);
  1433.             $repository->orderBy('d.title''asc');
  1434.             $documentList[$docGroup['groupname']] = $repository->getQuery()->getResult();
  1435.         }
  1436.         return $documentList;
  1437.     }
  1438.     /**
  1439.      * @return array
  1440.      */
  1441.     function getUserGroups()
  1442.     {
  1443.         $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  1444.         if ($userId) {
  1445.             $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  1446.             $repository->add('select''g.idGroup');
  1447.             /*
  1448.              * @todo cleanup
  1449.              * Arno 17/02/2021
  1450.              * #62
  1451.              *
  1452.             $repository->add('from', 'OceanExpertBundle:GroupMembers g');
  1453.             */
  1454.             $repository->add('from''OceanExpertBundle:MemberGroups g');
  1455.             $repository->where('g.idInd = :userId');
  1456.             $repository->setParameter('userId'$userId);
  1457.             return $repository->getQuery()->getResult();
  1458.         } else {
  1459.             return array();
  1460.         }
  1461.     }
  1462.     /**
  1463.      * show all past and upcoming events
  1464.      *
  1465.      * @param Request $request
  1466.      *
  1467.      * @return Response
  1468.      */
  1469.     public function calendarAction(Request $request): Response
  1470.     {
  1471.         //is the current user an editor?
  1472.         $globalEditor $this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR');
  1473.         //entity manager
  1474.         $em $this->getDoctrine()->getManager();
  1475.         //get a list of all event types
  1476.         $repository $em->createQueryBuilder();
  1477.         $repository->add(
  1478.             'select',
  1479.             'et.idEventtype, 
  1480.             et.eventtypeName'
  1481.         );
  1482.         $repository->add(
  1483.             'from',
  1484.             'OceanExpertBundle:Eventtypes et'
  1485.         );
  1486.         $repository->orderBy('et.idEventtype');
  1487.         $eventTypes $repository->getQuery()->getResult();
  1488.         $availableEventTypes = array();
  1489.         foreach ($eventTypes as $eventType) {
  1490.             $availableEventTypes[$eventType['idEventtype']] = $eventType['eventtypeName'];
  1491.         }
  1492.         //get a list of all groups
  1493.         $availableEventGroups array_column(
  1494.             $this->getSitesGroup(),
  1495.             'groupname',
  1496.             'idGroup'
  1497.         );
  1498.         //some name are way too long, so we shorten them
  1499.         foreach ($availableEventGroups as $key => $group) {
  1500.             if (strlen($group) > 18) {
  1501.                 $availableEventGroups[$key] = substr($group018) . '...';
  1502.             }
  1503.         }
  1504.         /*
  1505.          * don't use the following lines to add 'All' to the types, groups and label lists
  1506.          * as this will mess up the order of the groups or the id's
  1507.          * idem for the labels of course
  1508.          *
  1509.         //$availableEventGroups[0] = 'All';
  1510.         //array_unshift($availableEventGroups, 'All');
  1511.         */
  1512.         $availableEventTypes = array(=> 'All') + $availableEventTypes;
  1513.         $availableEventGroups = array(=> 'All') + $availableEventGroups;
  1514.         $availableEventLabels = array(
  1515.             => array(
  1516.                 'id' => 0,
  1517.                 'type' => 'All'
  1518.             )
  1519.         ) + $this->availableEventLabels;
  1520.         $repository $em->createQueryBuilder();
  1521.         $repository->add(
  1522.             'select',
  1523.             'month(e.startOn) as month, 
  1524.             year(e.startOn) as year, 
  1525.             e.idEvent,
  1526.             e.title, 
  1527.             e.shorttitle, 
  1528.             e.idEventtype,
  1529.             e.startOn, 
  1530.             e.endOn, 
  1531.             e.summary, 
  1532.             e.city, 
  1533.             c.country, 
  1534.             e.isOpen, 
  1535.             e.status'
  1536.         );
  1537.         $repository->add(
  1538.             'from',
  1539.             'OceanExpertBundle:Events e'
  1540.         );
  1541.         $repository->leftJoin(
  1542.             'OceanExpertBundle:Countries',
  1543.             'c',
  1544.             'WITH',
  1545.             'c.idCountry = e.idCountry'
  1546.         );
  1547.         $repository->leftJoin(
  1548.             'OceanExpertBundle:EventGroups',
  1549.             'eg',
  1550.             'WITH',
  1551.             'eg.idEvent = e.idEvent'
  1552.         );
  1553.         $repository->leftJoin(
  1554.             'OceanExpertBundle:EventLabels',
  1555.             'l',
  1556.             'WITH',
  1557.             'l.idEvent = e.idEvent AND l.idGroup = eg.idGroup'
  1558.         );
  1559.         //only show the approved / active ones for the non editor users
  1560.         if ($globalEditor != 1) {
  1561.             $repository->where('e.status = 1');
  1562.         }
  1563.         $repository->groupBy('e.idEvent');
  1564.         $repository->orderBy('e.startOn');
  1565.         //did the user select one or more types?
  1566.         $activeEventTypes '0';
  1567.         if (trim($request->query->get('types')) != '') {
  1568.             $activeEventTypes json_decode(
  1569.                 trim(
  1570.                     $request->query->get('types')
  1571.                 )
  1572.             );
  1573.         }
  1574.         //did the user select one or more calendars?
  1575.         //this is not the calendar that is used with the labels!!!!!!!!!!
  1576.         $activeEventGroups '0';
  1577.         if (trim($request->query->get('groups')) != ''
  1578.             //&& trim($request->query->get('groups')) != 0
  1579.         ) {
  1580.             $repository->leftJoin(
  1581.                 'OceanExpertBundle:EventGroups',
  1582.                 'eg2',
  1583.                 'WITH',
  1584.                 'eg2.idEvent = e.idEvent'
  1585.             );
  1586.             $activeEventGroups json_decode(
  1587.                 trim(
  1588.                     $request->query->get(
  1589.                         'groups')
  1590.                 )
  1591.             );
  1592.         }
  1593.         //did the user select one or more labels
  1594.         //we don't care about the groups here
  1595.         $activeEventLabels '0';
  1596.         if (trim($request->query->get('labels')) != ''
  1597.             //&& trim($request->query->get('label')) != 0
  1598.         ){
  1599.             $joinCondition 'l2.idEvent = e.idEvent';
  1600.             /*
  1601.             if ($activeEventGroups != 0) {
  1602.                 $joinCondition .= ' AND l.idGroup = eg.idGroup';
  1603.             }
  1604.             */
  1605.             $repository->leftJoin(
  1606.                 'OceanExpertBundle:EventLabels',
  1607.                 'l2',
  1608.                 'WITH',
  1609.                 $joinCondition
  1610.             );
  1611.             $activeEventLabels json_decode(
  1612.                 trim(
  1613.                     $request->query->get(
  1614.                         'labels')
  1615.                 )
  1616.             );
  1617.         }
  1618.         /*
  1619.         dump($request->query);
  1620.         dump(trim($request->query->get('types')));
  1621.         dump(trim($request->query->get('groups')));
  1622.         dump(trim($request->query->get('labels')));
  1623.         dump($activeEventTypes);
  1624.         dump($activeEventGroups);
  1625.         dump($activeEventLabels);
  1626.         die;
  1627.         */
  1628.         //did the user select a start and end date?
  1629.         if ((null !== ($request->query->get('start'))
  1630.                 && trim($request->query->get('start')) != '')
  1631.                 && (null !== ($request->query->get('end'))
  1632.                     && trim($request->query->get('end')) != '')
  1633.         ) {
  1634.             //there is a start and end date in the url
  1635.             $startdate $request->query->get('start');
  1636.             $enddate $request->query->get('end');
  1637.             $repository->andWhere('e.startOn BETWEEN :startdate AND :enddate');
  1638.             $repository->orWhere('e.endOn BETWEEN :startdate AND :enddate');
  1639.             $repository->setParameters(
  1640.                 array(
  1641.                     'startdate' => $startdate,
  1642.                     'enddate' => $enddate
  1643.                 )
  1644.             );
  1645.         } elseif (trim($request->query->get('year') != '')) {
  1646.             $year $request->query->get('year');
  1647.             $startdate date("$year-01-01");
  1648.             $enddate date("$year-12-31");
  1649.             $repository->andWhere('YEAR(e.startOn) = :year');
  1650.             $repository->orWhere('YEAR(e.endOn) = :year');
  1651.             $repository->setParameter(
  1652.                 'year',
  1653.                 $year
  1654.             );
  1655.         } else {
  1656.             //use the current year
  1657.             $year date('Y');
  1658.             $startdate date(
  1659.                 'Y-m-d'
  1660.             );
  1661.             $enddate date(
  1662.                 'Y-m-t',
  1663.                 strtotime(
  1664.                     "+3 months",
  1665.                     strtotime(
  1666.                         date(
  1667.                             'Y-m-d'
  1668.                         )
  1669.                     )
  1670.                 )
  1671.             );
  1672.             $repository->andWhere('(e.startOn BETWEEN :startdate AND :enddate) OR (e.endOn BETWEEN :startdate AND :enddate)');
  1673.             $repository->orWhere('(e.startOn < :enddate) AND (e.endOn > :startdate)');
  1674.             //$repository->orWhere('e.endOn BETWEEN :startdate AND :enddate');
  1675.             $repository->setParameters(
  1676.                 array(
  1677.                     'startdate' => $startdate,
  1678.                     'enddate' => $enddate
  1679.                 )
  1680.             );
  1681.         }
  1682.         //seems like we cannot set this earlier (overwritten bij setParameter???)
  1683.         //dump($activeEventTypes);
  1684.         //dump($activeEventGroups);
  1685.         //dump($activeEventTypes);
  1686.         if (is_array($activeEventTypes) && !(count($activeEventTypes) == && $activeEventTypes[0] == 0)) {
  1687.             $repository->andWhere('e.idEventtype IN (:types)');
  1688.             $repository->setParameter('types'$activeEventTypes);
  1689.         }
  1690.         //seems like we cannot set this earlier (overwritten bij setParameter???)
  1691.         if (is_array($activeEventGroups) && !(count($activeEventGroups) == && $activeEventGroups[0] == 0)) {
  1692.             $repository->andWhere('eg.idGroup IN (:groups)');
  1693.             $repository->setParameter('groups'$activeEventGroups);
  1694.         }
  1695.         //seems like we cannot set this earlier (overwritten bij setParameter???)
  1696.         if (is_array($activeEventLabels) && !(count($activeEventLabels) == && $activeEventLabels[0] == 0)) {
  1697.             $repository->andWhere('l.idLabel IN (:labels)');
  1698.             $repository->setParameter('labels'$activeEventLabels);
  1699.         }
  1700.         //if the current user is not a global editor, only show the events that are approved
  1701.         if ($globalEditor != 1) {
  1702.             $repository->andWhere('e.status = 1');
  1703.         }
  1704.         //get all the info for the events within the date range
  1705.         //dump($repository->getQuery()->getDQL());
  1706.         //dump($repository->getQuery()->getParameters());
  1707.         //die;
  1708.         $eventData $repository->getQuery()->getResult();
  1709.         //get a list of all years
  1710.         $repository $em->createQueryBuilder();
  1711.         $repository->add(
  1712.             'select',
  1713.             'distinct(year(e.startOn))'
  1714.         );
  1715.         $repository->add(
  1716.             'from',
  1717.             'OceanExpertBundle:Events e'
  1718.         );
  1719.         $repository->where('e.startOn != 0');
  1720.         /*
  1721.         $repository->leftJoin(
  1722.             'OceanExpertBundle:EventGroups',
  1723.             'eg',
  1724.             'WITH',
  1725.             'eg.idEvent = e.idEvent'
  1726.         );
  1727.         */
  1728.         if ($globalEditor != 1) {
  1729.             $repository->andWhere('e.status = 1');
  1730.         }
  1731.         /*
  1732.         if (is_array($activeEventGroups)) {
  1733.             $repository->andWhere('eg.idGroup IN (:groups)');
  1734.             $repository->setParameter('groups', implode(',', $activeEventGroups));
  1735.         }
  1736.         */
  1737.         $repository->orderBy('e.startOn');
  1738.         $years $repository->getQuery()->getResult();
  1739.         $years array_reduce(
  1740.             $years,
  1741.             'array_merge',
  1742.             array()
  1743.         );
  1744.         $eventCollection = array();
  1745.         //get the number of participants, presentations and agenda/background/report/other docs
  1746.         foreach ($eventData as $event) {
  1747.             $repository $em->createQueryBuilder();
  1748.             $repository->add('select''count(e.idInd) as participants');
  1749.             $repository->add('from''OceanExpertBundle:EventParticipants e');
  1750.             $repository->where('e.idEvent = :eventId');
  1751.             $repository->andWhere('e.status != 3');
  1752.             $repository->setParameter('eventId'$event['idEvent']);
  1753.             $participant $repository->getQuery()->getSingleScalarResult();
  1754.             $event['participants'] = $participant;
  1755.             $repository $em->createQueryBuilder();
  1756.             $repository->add('select''count(ep.idDoc) as presentation');
  1757.             $repository->add('from''OceanExpertBundle:EventPresentations ep');
  1758.             $repository->where('ep.idEvent = :eventId');
  1759.             $repository->setParameter('eventId'$event['idEvent']);
  1760.             $presentation $repository->getQuery()->getSingleScalarResult();
  1761.             $repository $em->createQueryBuilder();
  1762.             $repository->add('select''count(ad.idDoc) as agendadocs');
  1763.             $repository->add('from''OceanExpertBundle:AgendaitemDocuments ad');
  1764.             $repository->where('ad.idEvent = :eventId');
  1765.             $repository->setParameter('eventId'$event['idEvent']);
  1766.             $agendadocs $repository->getQuery()->getSingleScalarResult();
  1767.             $repository $em->createQueryBuilder();
  1768.             $repository->add('select''count(eb.idDoc) as backgrounddocs');
  1769.             $repository->add('from''OceanExpertBundle:EventBackgrounddocs eb');
  1770.             $repository->where('eb.idEvent = :eventId');
  1771.             $repository->setParameter('eventId'$event['idEvent']);
  1772.             $backgroundDocs $repository->getQuery()->getSingleScalarResult();
  1773.             $repository $em->createQueryBuilder();
  1774.             $repository->add('select''count(eO.idDoc) as otherdocs');
  1775.             $repository->add('from''OceanExpertBundle:EventOtherdocs eO');
  1776.             $repository->where('eO.idEvent = :eventId');
  1777.             $repository->setParameter('eventId'$event['idEvent']);
  1778.             $otherDocs $repository->getQuery()->getSingleScalarResult();
  1779.             $repository $em->createQueryBuilder();
  1780.             $repository->add('select''count(eO.idDoc) as reportdocs');
  1781.             $repository->add('from''OceanExpertBundle:EventReports eO');
  1782.             $repository->where('eO.idEvent = :eventId');
  1783.             $repository->setParameter('eventId'$event['idEvent']);
  1784.             $reportDocs $repository->getQuery()->getSingleScalarResult();
  1785.             $event['documents'] = $agendadocs $presentation $otherDocs $backgroundDocs $reportDocs;
  1786.             $eventDate date(
  1787.                 'F',
  1788.                 mktime(
  1789.                     0,
  1790.                     0,
  1791.                     0,
  1792.                     $event['month'],
  1793.                     10
  1794.                 )
  1795.             );
  1796.             $eventDate .= ' ' $event['year'];
  1797.             if (empty($eventCollection)) {
  1798.                 $eventCollection = array(
  1799.                     $eventDate => array($event)
  1800.                 );
  1801.             } else {
  1802.                 if (array_key_exists($eventDate$eventCollection)) {
  1803.                     $eventCollection[$eventDate][] = $event;
  1804.                 } else {
  1805.                     $eventCollection[$eventDate] = array($event);
  1806.                 }
  1807.             }
  1808.         }
  1809.         //start 4 months before the start date
  1810.         $earlierStart date(
  1811.             'Y-m-d',
  1812.             strtotime("$startdate -4 month")
  1813.         );
  1814.         //end where we started before
  1815.         $earlierEnd $startdate;
  1816.         //start where we left off
  1817.         $laterStart $enddate;
  1818.         //end 5 months after the later start date
  1819.         $laterEnd date(
  1820.             'Y-m-d',
  1821.             strtotime("$laterStart +5 month")
  1822.         );
  1823.         //dump($activeEventLabels);
  1824.         //die();
  1825.         return $this->render('Event/calendar.html.twig', array(
  1826.                 'data' => array(
  1827.                     'events' => $eventCollection,
  1828.                     //'eventTypes' => $eventTypes,
  1829.                     'eventTypes' => array(
  1830.                         'list' => $availableEventTypes,
  1831.                         'active' => $activeEventTypes
  1832.                     ),
  1833.                     'groups' => array(
  1834.                         'list' => $availableEventGroups,
  1835.                         'active' => $activeEventGroups
  1836.                     ),
  1837.                     'eventLabels' => array(
  1838.                         'list' => $availableEventLabels,
  1839.                         'active' => $activeEventLabels
  1840.                     ),
  1841.                     'date' => array(
  1842.                         'start' => $startdate,
  1843.                         'end' => $enddate
  1844.                     ),
  1845.                     'year' => array(
  1846.                         'list' => $years,
  1847.                         'active' => date('Y'strtotime($startdate)),
  1848.                     ),
  1849.                     'range' => array(
  1850.                         'earlier' => array(
  1851.                             'start' => $earlierStart,
  1852.                             'end' => $earlierEnd
  1853.                         ),
  1854.                         'later' => array(
  1855.                             'start' => $laterStart,
  1856.                             'end' => $laterEnd
  1857.                         )
  1858.                     )
  1859.                 )
  1860.             )
  1861.         );
  1862.     }
  1863.     /**
  1864.      *
  1865.      */
  1866.     public function listUserEventsAction($idInd)
  1867.     {
  1868.         $data $idInd;
  1869.         return $this->render('Event/listUserEvents.html.twig', array('data' => $data));
  1870.     }
  1871.     /**
  1872.      * create the PlanningWheel
  1873.      * get the info of events that
  1874.      * - are active
  1875.      * - have a priority set in the labels
  1876.      * - are within the next 11 months
  1877.      *
  1878.      * @return Response
  1879.      */
  1880.     public function viewPlanningWheelAction(): Response
  1881.     {
  1882.         // startdate is today
  1883.         // enddate is +11 months (to avoid confusion with the next year(s))
  1884.         $startdate date(
  1885.             'Y-m-d'
  1886.         );
  1887.         $enddate date(
  1888.             'Y-m-t',
  1889.             strtotime(
  1890.                 "+11 months",
  1891.                 strtotime(
  1892.                     date(
  1893.                         'Y-m-d'
  1894.                     )
  1895.                 )
  1896.             )
  1897.         );
  1898.         //get all the events that are active and have a priority set
  1899.         //this means they have a id labe set to 4 or 5 or 6
  1900.         //see $this->availableEventLabels
  1901.         //entity manager
  1902.         $em $this->getDoctrine()->getManager();
  1903.         //get all the info for the events within the date range
  1904.         //mind that concat only accepts single quotes in Doctrine :-(
  1905.         $repository $em->createQueryBuilder()
  1906.             ->add(
  1907.                 'select',
  1908.                 'e.title as name,
  1909.                 l.idLabel as priority,
  1910.                 e.startOn as date,
  1911.                 concat(\'/event/\', e.idEvent) as url'
  1912.             )
  1913.             ->add(
  1914.                 'from',
  1915.                 'OceanExpertBundle:Events e'
  1916.             )
  1917.             ->leftJoin(
  1918.                 'OceanExpertBundle:EventLabels',
  1919.                 'l',
  1920.                 'WITH',
  1921.                 'l.idEvent = e.idEvent'
  1922.             )
  1923.             ->where(
  1924.                 'e.status = 1'
  1925.             )
  1926.             ->andWhere(
  1927.                 'l.idLabel IN (4,5,6)'
  1928.             )
  1929.             ->andWhere(
  1930.                 "((e.startOn BETWEEN '$startdate' AND '$enddate') 
  1931.                 OR (e.endOn BETWEEN '$startdate' AND '$enddate'))"
  1932.             )
  1933.             ->orderBy(
  1934.                 'e.startOn'
  1935.             );
  1936.         $events $repository->getQuery()->getResult();
  1937.         foreach ($events as $key => $event) {
  1938.             //format the date as expected
  1939.             $events[$key]['date'] = $event['date']->format('Y-m-d');
  1940.             //correct the priority to match the wheel
  1941.             $events[$key]['priority'] = $event['priority'] - 3;
  1942.         }
  1943.         return $this->render(
  1944.             'Event/planningWheel.html.twig',
  1945.             array(
  1946.                 'events' => json_encode($events)
  1947.             )
  1948.         );
  1949.     }
  1950.     /**
  1951.      * show the info for an event, other views are used when making an event!!!
  1952.      *
  1953.      * @param int $event id of the event we want to see
  1954.      *
  1955.      * @return Response
  1956.      */
  1957.     public function viewEventAction($event)
  1958.     {
  1959.         $user = array();
  1960.         $user['canSendEmail'] = 0;
  1961.         $user['canEditEvent'] = 0;
  1962.         $isCreator 0;
  1963.         $isOrganiser 0;
  1964.         //container for errors
  1965.         $error '';
  1966.         //get database Connection
  1967.         $em $this->getDoctrine()->getManager();
  1968.         if (TRUE === $this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
  1969.             $query $this->getDoctrine()
  1970.                 ->getRepository('OceanExpertBundle:Indiv')
  1971.                 ->createQueryBuilder('i')
  1972.                 ->select('i.idInd,i.fname,i.sname,i.email1,i.email2')
  1973.                 ->where('i.idInd = :idInd')
  1974.                 ->setParameter('idInd'$this->get('security.token_storage')->getToken()->getUser())
  1975.                 ->getQuery();
  1976.             $user $query->getOneOrNullResult();
  1977.             $user['canSendEmail'] = 0;
  1978.             $user['canEditEvent'] = 0;
  1979.             if ($user) {
  1980.                 $isParticipant $em->getRepository('OceanExpertBundle:EventParticipants')
  1981.                     ->findOneBy(
  1982.                         array(
  1983.                             'idEvent' => $event,
  1984.                             'idInd' => $user['idInd']
  1985.                         )
  1986.                     );
  1987.                 $isCreator $em->getRepository('OceanExpertBundle:Events')
  1988.                     ->createQueryBuilder('e')
  1989.                     ->select('count(e.idEvent)')
  1990.                     ->where('e.createdBy =:idInd')
  1991.                     ->orWhere('e.lastEditBy =:idInd')
  1992.                     ->orWhere('e.idContact =:idInd')
  1993.                     ->andWhere('e.idEvent =:event')
  1994.                     ->setParameters(array('idInd' => $user['idInd'], 'event' => $event))
  1995.                     ->getQuery()->getSingleScalarResult();
  1996.                 $isOrganiser $em->getRepository('OceanExpertBundle:EventContacts')
  1997.                     ->findOneBy(
  1998.                         array(
  1999.                             'idEvent' => $event,
  2000.                             'idInd' => $user['idInd']
  2001.                         )
  2002.                     );
  2003.                 if ($isCreator == 1
  2004.                     || $isOrganiser == 1
  2005.                 ) {
  2006.                     $user['canSendEmail'] = 1;
  2007.                 }
  2008.             }
  2009.         }
  2010.         if ($this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')
  2011.             || $isCreator == 1
  2012.             || $isOrganiser == 1
  2013.         ) {
  2014.             $eventDetails $em
  2015.                 ->getRepository('OceanExpertBundle:Events')
  2016.                 ->findOneBy(
  2017.                     array(
  2018.                         'idEvent' => $event
  2019.                     )
  2020.                 );
  2021.             $user['canSendEmail'] = 1;
  2022.             $user['canEditEvent'] = 1;
  2023.         } else {
  2024.             $eventDetails $em
  2025.                 ->getRepository('OceanExpertBundle:Events')
  2026.                 ->findOneBy(
  2027.                     array(
  2028.                         'idEvent' => $event,
  2029.                         'status' => 1
  2030.                     )
  2031.                 );
  2032.         }
  2033.         //get Event Type
  2034.         if (isset($eventDetails)) {
  2035.             $eventType $em->getRepository('OceanExpertBundle:Eventtypes')
  2036.                 ->findOneBy(
  2037.                     array(
  2038.                         'idEventtype' => $eventDetails->getIdEventtype()
  2039.                     )
  2040.                 );
  2041.             //get Country Events
  2042.             $eventImage $em->getRepository('OceanExpertBundle:EventFiles')
  2043.                 ->findOneBy(
  2044.                     array(
  2045.                         'idEvent' => $event
  2046.                     )
  2047.                 );
  2048.             //get the address to show in the view
  2049.             //this can be a custom address or the address of the institute
  2050.             //if there is no country set
  2051.             //  and we don't want to use the institute address,
  2052.             // we don't show the address
  2053.             $eventCountryCode 0;
  2054.             $organization '';
  2055.             if (($eventDetails->getIdCountry() != 0
  2056.                 && $eventDetails->getUseInstAddr() == 0)
  2057.                 || ($eventDetails->getUseInstAddr() != 0
  2058.                     && $eventDetails->getIdInst() != 0)
  2059.             ) {
  2060.                 if ($eventDetails->getUseInstAddr()) {
  2061.                     //get info about the institute
  2062.                     $institute $this->getDoctrine()
  2063.                         ->getRepository('OceanExpertBundle:Institutions')
  2064.                         ->findOneBy(
  2065.                             array(
  2066.                                 'idInst' => $eventDetails->getIdInst()
  2067.                             )
  2068.                         );
  2069.                     $eventAddressLine1 $institute->getInstAddress();
  2070.                     $eventPostcode $institute->getPostcode();
  2071.                     $eventCity $institute->getCity();
  2072.                     $eventState $institute->getState();
  2073.                     $eventCountryCode $institute->getCountryCode();
  2074.                     $organization $institute->getInstName();
  2075.                 } else {
  2076.                     $eventAddressLine1 $eventDetails->getAddress();
  2077.                     $eventPostcode $eventDetails->getPostcode();
  2078.                     $eventCity $eventDetails->getCity();
  2079.                     $eventState $eventDetails->getState();
  2080.                     $eventCountryCode $eventDetails->getIdCountry();
  2081.                 }
  2082.                 $country $em->getRepository('OceanExpertBundle:Countries')
  2083.                     ->findOneBy(
  2084.                         array(
  2085.                             'idCountry' => $eventCountryCode
  2086.                         )
  2087.                     );
  2088.                 //format the address with the correct country
  2089.                 $addressFormatRepository = new AddressFormatRepository();
  2090.                 $countryRepository = new CountryRepository();
  2091.                 $subdivisionRepository = new SubdivisionRepository();
  2092.                 $formatter = new DefaultFormatter(
  2093.                     $addressFormatRepository,
  2094.                     $countryRepository,
  2095.                     $subdivisionRepository
  2096.                 );
  2097.                 if (isset($country)
  2098.                     && $eventCountryCode != 0
  2099.                 ) {
  2100.                     $address = new Address();
  2101.                     $address $address
  2102.                         ->withCountryCode($country->getCountryCode())
  2103.                         ->withAdministrativeArea($eventState)
  2104.                         ->withLocality($eventCity)
  2105.                         ->withPostalCode($eventPostcode)
  2106.                         ->withAddressLine1($eventAddressLine1);
  2107.                     if ($organization != '') {
  2108.                         $address $address->withOrganization($organization);
  2109.                     }
  2110.                     $address $formatter->format($address);
  2111.                 } else {
  2112.                     $address 'no location defined or online event';
  2113.                 }
  2114.                 //Set event Address to use in view
  2115.                 $eventDetails->setAddress($address);
  2116.             } elseif ($eventDetails->getUseInstAddr() != 0
  2117.                 && $eventDetails->getIdInst() == 0) {
  2118.                 $error 'This event uses the address of an institute but no institute has been selected.';
  2119.             }
  2120.             //------------------ Get Organisers  ------------
  2121.             $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  2122.             $repository->add('select''i.idInd as id, i.title, i.fname, i.sname');
  2123.             $repository->add('from''OceanExpertBundle:EventContacts e');
  2124.             $repository->leftJoin('OceanExpertBundle:Indiv''i''WITH''i.idInd = e.idInd');
  2125.             $repository->where('e.idEvent = :eventId');
  2126.             $repository->andWhere('i.sname is not null');
  2127.             $repository->setParameter('eventId'$event);
  2128.             $repository->orderBy('i.sname''ASC');
  2129.             $organisers $repository->getQuery()->getResult();
  2130.             //------------------ Get Staff  ------------
  2131.             $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  2132.             $repository->add('select''i.idInd as id, i.title, i.fname, i.sname');
  2133.             $repository->add('from''OceanExpertBundle:EventStaff e');
  2134.             $repository->leftJoin('OceanExpertBundle:Indiv''i''WITH''i.idInd = e.idInd');
  2135.             $repository->where('e.idEvent = :eventId');
  2136.             $repository->setParameter('eventId'$event);
  2137.             $repository->orderBy('i.sname''ASC');
  2138.             $staff $repository->getQuery()->getResult();
  2139.             //------------------ Get Participant count  ------------
  2140.             $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  2141.             $repository->add('select''count(e.idInd) as participants, e.status');
  2142.             $repository->add('from''OceanExpertBundle:EventParticipants e');
  2143.             $repository->where('e.idEvent = :eventId');
  2144.             $repository->groupBy('e.status');
  2145.             $repository->setParameter('eventId'$event);
  2146.             $participantArr $repository->getQuery()->getResult();
  2147.             $participant $provisional $unapproved $rejected $declined 0;
  2148.             foreach ($participantArr as $participants) {
  2149.                 switch ($participants['status']) {
  2150.                     case 0:
  2151.                         //confirmed and approved
  2152.                         $participant $participants['participants'];
  2153.                         break;
  2154.                     case 1:
  2155.                         //needs confirmation
  2156.                         $provisional $participants['participants'];
  2157.                         break;
  2158.                     case 2:
  2159.                         //needs approval
  2160.                         $unapproved $participants['participants'];
  2161.                         break;
  2162.                     case 3:
  2163.                         //declined
  2164.                         $declined $participants['participants'];
  2165.                         break;
  2166.                     case 4:
  2167.                         //rejected
  2168.                         $rejected $participants['participants'];
  2169.                         break;
  2170.                 }
  2171.             }
  2172.             $order '';
  2173.             $agenda $this->getAgendaItems($event);
  2174.             $agendaDocuments $this->getAgendaDocuments($event$order);
  2175.             $eventPresentations $this->getEventPresentations($event$order);
  2176.             $backgroundDocs $this->getBackgrounddocs($event$order);
  2177.             $otherDocs $this->getEventOtherDocs($event);
  2178.             $report $this->getEventReport($event);
  2179.             $participantlist $this->getEventParticipants($event);
  2180.             $participantsByRole $this->getParticipantsByRole($event);
  2181.             $eventGroups $this->getEventGroups($event);
  2182.             $edits $this->getEventEdits($event);
  2183.             $labels $this->getDoctrine()
  2184.                 ->getRepository('OceanExpertBundle:EventLabels')
  2185.                 ->createQueryBuilder('el')
  2186.                 ->select('g.groupname, el.idLabel')
  2187.                 ->leftJoin('OceanExpertBundle:Groups''g''WITH''g.idGroup = el.idGroup')
  2188.                 ->where('el.idEvent = :eventId')
  2189.                 ->setParameter(':eventId'$event)
  2190.                 ->orderBy('g.groupname')
  2191.                 ->getQuery()
  2192.                 ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2193.             //add the label type to the labels
  2194.             //mind that the array id and the label id are not the same :-(
  2195.             foreach ($labels as $key => $label) {
  2196.                 $labels[$key]['labelType'] = $this->availableEventLabels[$label['idLabel']]['type'];
  2197.             }
  2198.             //make the array we need for OIH schema.org
  2199.             /*
  2200.             */
  2201.             $OIHTitle preg_replace(
  2202.                 "/[\n\r\t]+/",
  2203.                 ' - ',
  2204.                 trim(
  2205.                     strip_tags($eventDetails->getTitle())
  2206.                 )
  2207.             );
  2208.             $OIHData = array(
  2209.                 '@context' => array(
  2210.                     '@vocab' => 'https://schema.org/'
  2211.                 ),
  2212.                 "name" => $OIHTitle
  2213.             );
  2214.             if ($eventType
  2215.                 && $eventType->getIdEventtype() == 4
  2216.             ) {
  2217.                 $OIHData['@type'] = 'Course';
  2218.                 $OIHData['@id'] = 'https://oceanexpert.org/event/' $eventDetails->getIdEvent();
  2219.                 $OIHData['hasCourseInstance'] = array(
  2220.                     '@type' => 'CourseInstance',
  2221.                     "name" => $OIHTitle
  2222.                 );
  2223.                 if ($eventDetails->getStartOn() != '') {
  2224.                     $OIHData['hasCourseInstance']
  2225.                     ['startDate'] = $eventDetails->getStartOn()->format('Y-m-d');
  2226.                 }
  2227.                 if ($eventDetails->getEndOn() != '') {
  2228.                     $OIHData['hasCourseInstance']
  2229.                     ['endDate'] = $eventDetails->getEndOn()->format('Y-m-d');
  2230.                 }
  2231.                 if ($eventDetails->getAddress() != '') {
  2232.                     $OIHAddress preg_replace(
  2233.                         "/[\n\r\t]+/",
  2234.                         ' - ',
  2235.                         trim(
  2236.                             strip_tags($eventDetails->getAddress())
  2237.                         )
  2238.                     );
  2239.                     $OIHData['hasCourseInstance']
  2240.                     ['location'] = array(
  2241.                         '@type' => 'Place',
  2242.                         'name' => $OIHAddress,
  2243.                         'address' => $OIHAddress
  2244.                     );
  2245.                 }
  2246.                 //add the participants if we have any
  2247.                 $attendees = array();
  2248.                 if (count($participantlist)) {
  2249.                     foreach ($participantlist as  $oihParticipant) {
  2250.                         //this is the info we want to share
  2251.                         //OE id
  2252.                         $oihParticipantId $oihParticipant['id'];
  2253.                         //name
  2254.                         $oihParticipantName $oihParticipant['fname'] . ' ' $oihParticipant['sname'];
  2255.                         //title
  2256.                         $oihParticipantTitle $oihParticipant['title'];
  2257.                         $attendees[] = array(
  2258.                             '@type' => 'Person',
  2259.                             'name' => $oihParticipantName,
  2260.                             'jobTitle' => $oihParticipantTitle,
  2261.                             'identifier' => array(
  2262.                                 '@id' => 'ID_value_string',
  2263.                                 '@type' => 'PropertyValue',
  2264.                                 'propertyID' => 'https://oceanexpert.org/expert/' $oihParticipantId,
  2265.                                 'url' => 'https://oceanexpert.org/expert/' $oihParticipantId,
  2266.                                 'description' => 'UNESCO IOC IODE OceanExpert profile'
  2267.                             )
  2268.                         );
  2269.                     }
  2270.                 }
  2271.                 if (count($staff)) {
  2272.                     foreach ($staff as $oihStaff) {
  2273.                         //this is the info we want to share
  2274.                         //OE id
  2275.                         $oihParticipantId $oihStaff['id'];
  2276.                         //name
  2277.                         $oihParticipantName $oihStaff['fname'] . ' ' $oihStaff['sname'];
  2278.                         //title
  2279.                         $oihParticipantTitle $oihStaff['title'];
  2280.                         $attendees[] = array(
  2281.                             '@type' => 'Person',
  2282.                             'name' => $oihParticipantName,
  2283.                             'jobTitle' => $oihParticipantTitle,
  2284.                             'identifier' => array(
  2285.                                 '@id' => 'ID_value_string',
  2286.                                 '@type' => 'PropertyValue',
  2287.                                 'propertyID' => 'https://oceanexpert.org/expert/' $oihParticipantId,
  2288.                                 'url' => 'https://oceanexpert.org/expert/' $oihParticipantId,
  2289.                                 'description' => 'UNESCO IOC IODE OceanExpert profile'
  2290.                             )
  2291.                         );
  2292.                     }
  2293.                 }
  2294.                 if (count($organisers)) {
  2295.                     foreach ($organisers as $oihOrganiser) {
  2296.                         //this is the info we want to share
  2297.                         //OE id
  2298.                         $oihParticipantId $oihOrganiser['id'];
  2299.                         //name
  2300.                         $oihParticipantName $oihOrganiser['fname'] . ' ' $oihOrganiser['sname'];
  2301.                         //title
  2302.                         $oihParticipantTitle $oihOrganiser['title'];
  2303.                         $attendees[] = array(
  2304.                             '@type' => 'Person',
  2305.                             'name' => $oihParticipantName,
  2306.                             'jobTitle' => $oihParticipantTitle,
  2307.                             'identifier' => array(
  2308.                                 '@id' => 'ID_value_string',
  2309.                                 '@type' => 'PropertyValue',
  2310.                                 'propertyID' => 'https://oceanexpert.org/expert/' $oihParticipantId,
  2311.                                 'url' => 'https://oceanexpert.org/expert/' $oihParticipantId,
  2312.                                 'description' => 'UNESCO IOC IODE OceanExpert profile'
  2313.                             )
  2314.                         );
  2315.                     }
  2316.                 }
  2317.                 $OIHData['hasCourseInstance']
  2318.                 ['attendee'] = $attendees;
  2319.             } else {
  2320.                 $OIHData['@type'] = 'Event';
  2321.                 if ($eventDetails->getStartOn() != '') {
  2322.                     $OIHData['startDate'] = $eventDetails->getStartOn()->format('Y-m-d');
  2323.                 }
  2324.                 if ($eventDetails->getEndOn() != '') {
  2325.                     $OIHData['endDate'] = $eventDetails->getEndOn()->format('Y-m-d');
  2326.                 }
  2327.                 if ($eventDetails->getAddress() != '') {
  2328.                     $OIHAddress preg_replace(
  2329.                         "/[\n\r\t]+/",
  2330.                         ' - ',
  2331.                         trim(
  2332.                             strip_tags($eventDetails->getAddress())
  2333.                         )
  2334.                     );
  2335.                     $OIHData['location'] = array(
  2336.                         '@type' => 'Place',
  2337.                         'name' => $OIHAddress,
  2338.                         'address' => $OIHAddress
  2339.                     );
  2340.                 }
  2341.             }
  2342.             if ($eventDetails->getSummary() != '') {
  2343.                 $OIHSummary preg_replace(
  2344.                     "/[\n\r\t]+/",
  2345.                     ' - ',
  2346.                     trim(
  2347.                         strip_tags($eventDetails->getSummary())
  2348.                     )
  2349.                 );
  2350.                 $OIHData['description'] = $OIHSummary;
  2351.             }
  2352.             $OIHData json_encode(
  2353.                 $OIHData,
  2354.                 JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES JSON_NUMERIC_CHECK
  2355.             );
  2356.             return $this->render('Event/event.html.twig',
  2357.                 array(
  2358.                     'data' => array(
  2359.                         'OIHData' => $OIHData,
  2360.                         'event' => array(
  2361.                             'eventImage' => $eventImage,
  2362.                             'details' => $eventDetails,
  2363.                             'type' => $eventType,
  2364.                             'people' => array(
  2365.                                 'organisers' => $organisers,
  2366.                                 'staff' => $staff,
  2367.                                 'participants' => $participantlist,
  2368.                                 'participantsByRole' => $participantsByRole,
  2369.                                 'numberOfParticipants' => $participant,
  2370.                                 'numberOfProvisional' => $provisional,
  2371.                                 'numberOfUnapproved' => $unapproved,
  2372.                                 'numberOfRejected' => $rejected,
  2373.                                 'numberOfDeclined' => $declined,
  2374.                                 'total' => $participant $provisional $unapproved count($staff) + count($organisers),
  2375.                             ),
  2376.                             'agenda' => $agenda,
  2377.                             'agendaDocuments' => $agendaDocuments,
  2378.                             'eventPresentations' => $eventPresentations,
  2379.                             'backgroundDocs' => $backgroundDocs,
  2380.                             'otherDocs' => $otherDocs,
  2381.                             'report' => $report,
  2382.                             'eventGroups' => $eventGroups,
  2383.                             'eventEdits' => $edits,
  2384.                             'eventLabels' => $labels
  2385.                         ),
  2386.                         'user' => $user,
  2387.                         'error' => $error
  2388.                     )
  2389.                 )
  2390.             );
  2391.         } else {
  2392.             return new RedirectResponse($this->generateUrl('event_calendar'));
  2393.         }
  2394.     }
  2395.     /**
  2396.      * @param int $event id of the event
  2397.      * @param string $order column to be used to order the results
  2398.      *
  2399.      * @return mixed
  2400.      */
  2401.     function getAgendaDocuments($event$order '')
  2402.     {
  2403.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  2404.         $repository->add('select''a.idAgendaitem, d.docCode, d.title, d.idDoc, d.authorText, d.updatedAt');
  2405.         $repository->add('from''OceanExpertBundle:AgendaitemDocuments a');
  2406.         $repository->leftJoin('OceanExpertBundle:Documents''d''WITH''d.idDoc = a.idDoc');
  2407.         $repository->where('a.idEvent = :eventId');
  2408.         $repository->setParameter('eventId'$event);
  2409.         if ($order == "date") {
  2410.             $repository->orderBy('d.updatedAt');
  2411.         } else {
  2412.             $repository->orderBy('a.idAgendaitem');
  2413.         }
  2414.         $agendaDocuments $repository->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY);
  2415.         return $agendaDocuments;
  2416.     }
  2417.     /**
  2418.      * get CSV file with information about the participants in a given event
  2419.      * is mainly used for, but not limited to, reporting for OTGA
  2420.      *
  2421.      * @param String $idEvent
  2422.      * @param Request $request
  2423.      *
  2424.      * @return Response
  2425.      */
  2426.     public function downloadEventParticipantsInfoAction(String $idEventRequest $request): Response
  2427.     {
  2428.         $participantsInfo $this->getPrintableParticipantsByRole($idEvent);
  2429.         $header = [];
  2430.         $return = [];
  2431.         //set this in the template as first line
  2432.         foreach($participantsInfo as $role => $participantInfo) {
  2433.             $returnRow = [];
  2434.             foreach ($participantInfo as $participant) {
  2435.                 $returnRow $participant;
  2436.                 //get rid of some info we do not want
  2437.                 unset($returnRow['idRole']);
  2438.                 unset($returnRow['status']);
  2439.                 //unset($returnRow['gender']);
  2440.                 unset($returnRow['tel']);
  2441.                 unset($returnRow['fax']);
  2442.                 unset($returnRow['ordering']);
  2443.                 //the complete address is already calculated
  2444.                 unset($returnRow['useInstAddr']);
  2445.                 unset($returnRow['addr1']);
  2446.                 unset($returnRow['addr2']);
  2447.                 unset($returnRow['state']);
  2448.                 unset($returnRow['city']);
  2449.                 unset($returnRow['postcode']);
  2450.                 unset($returnRow['countryCode']);
  2451.                 unset($returnRow['country']);
  2452.                 unset($returnRow['insAddr1']);
  2453.                 unset($returnRow['instAddr2']);
  2454.                 unset($returnRow['insCity']);
  2455.                 unset($returnRow['insState']);
  2456.                 unset($returnRow['insPostcode']);
  2457.                 unset($returnRow['insCountryCode']);
  2458.                 unset($returnRow['insCountry']);
  2459.                 //add some info we want
  2460.                 $returnRow['id'] = $returnRow['idInd'];
  2461.                 unset($returnRow['idInd']);
  2462.                 $returnRow['role'] = $role;
  2463.                 $returnRow['idEvent'] = $idEvent;
  2464.                 if (count($returnRow)) {
  2465.                     //make the header using the first correct row
  2466.                     if (!count($return)) {
  2467.                         $header array_keys($returnRow);
  2468.                         $header implode(';'$header);
  2469.                     }
  2470.                     $return[] = htmlspecialchars_decode(
  2471.                         strip_tags(
  2472.                             implode(
  2473.                                 ';',
  2474.                                 $returnRow
  2475.                             )
  2476.                         )
  2477.                     );
  2478.                 }
  2479.             }
  2480.         }
  2481.         $response $this->render(
  2482.             'Event/downloadEventParticipantsInfo.html.twig',
  2483.             array(
  2484.                 'header' => $header,
  2485.                 'data' => $return
  2486.             )
  2487.         );
  2488.         $response->headers->set(
  2489.             'Content-Type',
  2490.             'text/csv'
  2491.         );
  2492.         $response->headers->set(
  2493.             'Content-Disposition',
  2494.             'attachment; filename="eventParticipantsInfo' $idEvent '_' date('Ymd') . '.csv"'
  2495.         );
  2496.         return $response;
  2497.     }
  2498.     /**
  2499.      * @param $event
  2500.      * @return array
  2501.      */
  2502.     public function getParticipantsByRole($event)
  2503.     {
  2504.         $participants = array();
  2505.         $roles $this->getDoctrine()
  2506.             ->getRepository('OceanExpertBundle:EventParticipantroles')
  2507.             ->createQueryBuilder('e')
  2508.             ->select('e.role, e.idRole, e.pptOrdering')
  2509.             ->where('e.idEvent = :eventId')
  2510.             ->setParameter('eventId'$event)
  2511.             ->orderBy('e.ordering''asc')
  2512.             ->getQuery()
  2513.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2514.         $roles[] = array(
  2515.             "role" => "Other",
  2516.             "idRole" => '',
  2517.             "pptOrdering" => '',
  2518.         );
  2519.         foreach ($roles as $role) {
  2520.             $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  2521.             $repository->add(
  2522.                 'select',
  2523.                 'e.idRole, 
  2524.                 e.idInd, 
  2525.                 i.idInd as id, 
  2526.                 i.title, 
  2527.                 i.fname, 
  2528.                 i.sname, 
  2529.                 i.useInstAddr,
  2530.                 i.tel, 
  2531.                 r.role, 
  2532.                 c.country, 
  2533.                 r.role,
  2534.                 r.ordering, 
  2535.                 e.status, 
  2536.                 e.accommodation, 
  2537.                 i.tel'
  2538.             );
  2539.             $repository->add('from''OceanExpertBundle:EventParticipants e');
  2540.             $repository->leftJoin('OceanExpertBundle:Indiv''i''WITH''i.idInd = e.idInd');
  2541.             $repository->leftJoin('OceanExpertBundle:IndivInstitution''ii''WITH''ii.idInd = i.idInd');
  2542.             $repository->leftJoin('OceanExpertBundle:Institutions''ins''WITH''ins.idInst = ii.idInst');
  2543.             $repository->leftJoin('OceanExpertBundle:Countries''c''WITH''c.idCountry = i.countryCode');
  2544.             $repository->leftJoin('OceanExpertBundle:EventParticipantroles''r''WITH''e.idRole = r.idRole and e.idEvent = r.idEvent');
  2545.             $repository->where('e.idEvent = :eventId');
  2546.             if ($role['idRole'] != '') {
  2547.                 $repository->andwhere('e.idRole = :idRole');
  2548.                 $repository->setParameter('idRole'$role['idRole']);
  2549.             } else {
  2550.                 $repository->andwhere('e.idRole is NULL');
  2551.             }
  2552.             $repository->setParameter('eventId'$event);
  2553.             switch ($role['pptOrdering']) {
  2554.                 case 0:
  2555.                     $repository->addOrderBy('i.sname''asc');
  2556.                     break;
  2557.                 case 1:
  2558.                     $repository->addOrderBy('c.country''asc');
  2559.                     break;
  2560.                 case 2:
  2561.                     $repository->addOrderBy('ins.instName''asc');
  2562.                     break;
  2563.                 default:
  2564.                     $repository->addOrderBy('i.sname''asc');
  2565.                     break;
  2566.             }
  2567.             $participantsTmp $repository->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY);
  2568.             foreach ($participantsTmp as $key => $participant) {
  2569.                 //correct the country if the expert has choosen to use the institute address
  2570.                 if ($participant['useInstAddr'] === 1) {
  2571.                     //get the correct country, being the country of the institute
  2572.                     $participantsTmp[$key]['country'] = 'tbd';
  2573.                     $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  2574.                     $repository->add(
  2575.                         'select',
  2576.                         'c.country'
  2577.                     );
  2578.                     $repository->add(
  2579.                         'from',
  2580.                         'OceanExpertBundle:Countries c'
  2581.                     );
  2582.                     $repository->leftJoin(
  2583.                         'OceanExpertBundle:Institutions',
  2584.                         'i',
  2585.                         'WITH',
  2586.                         'c.idCountry = i.countryCode'
  2587.                     );
  2588.                     $repository->leftJoin(
  2589.                         'OceanExpertBundle:IndivInstitution',
  2590.                         'ii',
  2591.                         'WITH',
  2592.                         'ii.idInst = i.idInst'
  2593.                     );
  2594.                     $repository->where('ii.idInd = :idInd');
  2595.                     $repository->setParameter('idInd'$participant['idInd']);
  2596.                     $country $repository->getQuery()->getResult();
  2597.                     if (isset($country[0])
  2598.                         && isset($country[0]['country'])
  2599.                         && $country[0]['country'] != ''
  2600.                     ) {
  2601.                         $participantsTmp[$key]['country'] = $country[0]['country'];
  2602.                     }
  2603.                 }
  2604.             }
  2605.             $participants[$role['role']] = $participantsTmp;
  2606.         }
  2607.         return $participants;
  2608.     }
  2609.     /**
  2610.      * @param $idEvent
  2611.      * @return array|EventGroups[]
  2612.      */
  2613.     private function getEventGroups($idEvent)
  2614.     {
  2615.         $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  2616.         $repository->add('select''eg.idGroup, g.groupname');
  2617.         $repository->add('from''OceanExpertBundle:EventGroups eg');
  2618.         $repository->leftJoin('OceanExpertBundle:Groups''g''WITH''g.idGroup = eg.idGroup');
  2619.         $repository->where('eg.idEvent =:idEvent');
  2620.         $repository->setParameter('idEvent'$idEvent);
  2621.         $repository->orderBy('g.groupname''asc');
  2622.         $eventGroups $repository->getQuery()->getResult();
  2623.         return $eventGroups;
  2624.     }
  2625.     /**
  2626.      * @param int $idEvent id of the event
  2627.      *
  2628.      * @return array
  2629.      */
  2630.     private function getEventEdits($idEvent)
  2631.     {
  2632.         $em $this->getDoctrine()->getManager();
  2633.         $event $em->getRepository('OceanExpertBundle:Events')->findOneBy(array('idEvent' => $idEvent));
  2634.         $lastEdit $em->getRepository('OceanExpertBundle:Indiv')->findOneBy(array('idInd' => $event->getLastEditBy()));
  2635.         $created $em->getRepository('OceanExpertBundle:Indiv')->findOneBy(array('idInd' => $event->getCreatedBy()));
  2636.         if ($lastEdit) {
  2637.             $edit['lastEditAt'] = $event->getUpdatedAt();
  2638.             $edit['lastEditBy'] = $lastEdit->getFname() . ' ' $lastEdit->getSname();
  2639.             $edit['lastEditId'] = $event->getLastEditBy();
  2640.         } else {
  2641.             $edit['lastEditAt'] = $event->getUpdatedAt();
  2642.             $edit['lastEditId'] = $event->getLastEditBy();
  2643.         }
  2644.         if ($created) {
  2645.             $edit['createdBy'] = $created->getFname() . ' ' $created->getSname();
  2646.             $edit['createdAt'] = $event->getCreatedAt();
  2647.             $edit['createdId'] = $event->getCreatedBy();
  2648.         } else {
  2649.             $edit['createdAt'] = $event->getCreatedAt();
  2650.             $edit['createdId'] = $event->getCreatedBy();
  2651.         }
  2652.         return $edit;
  2653.     }
  2654.     /**
  2655.      * @param $event
  2656.      * @return JsonResponse
  2657.      */
  2658.     public function eventImageUploadAction($event)
  2659.     {
  2660.         return new JsonResponse($_FILES);
  2661.     }
  2662.     /**
  2663.      * @param $idEvent
  2664.      * @param Request $request
  2665.      * @return JsonResponse
  2666.      */
  2667.     public function addAgendaItemAjaxAction($idEventRequest $request)
  2668.     {
  2669.         $data $request->request->all();
  2670.         $em $this->getDoctrine()->getManager();
  2671.         $agendaName = isset($data['agendaName']) ? $data['agendaName'] : '';
  2672.         $agendaNumber = isset($data['agendaNumber']) ? $data['agendaNumber'] : '';
  2673.         $agendaOrder 0;
  2674.         //the agenda item number should be unique for this event
  2675.         $em $this->getDoctrine()->getManager();
  2676.         $event $em->getRepository('OceanExpertBundle:EventAgendaitems')
  2677.             ->findOneBy(
  2678.                 array(
  2679.                     'idEvent' => $idEvent,
  2680.                     'idAgendaitem' => $agendaNumber
  2681.                 )
  2682.             );
  2683.         if ($event) {
  2684.             return new JsonResponse(
  2685.                 array(
  2686.                     'status' => 0,
  2687.                     'message' => 'Agenda item number already exists'
  2688.                 )
  2689.             );
  2690.         }
  2691.         if (isset($data['parentId']) && $data['parentId'] != "undefined") {
  2692.             $parentId $data['parentId'];
  2693.             $parent 0;
  2694.         } else {
  2695.             $parentId 0;
  2696.             $parent 1;
  2697.             $highest_id $em->createQueryBuilder()
  2698.                 ->select('MAX(e.agendaOrder)')
  2699.                 ->from('OceanExpertBundle:EventAgendaitems''e')
  2700.                 ->where('e.idEvent = :idEvent')
  2701.                 ->andWhere('e.parentIdevent = 0')
  2702.                 ->setParameter(':idEvent'$idEvent)
  2703.                 ->getQuery()
  2704.                 ->getSingleScalarResult();
  2705.             $agendaOrder $highest_id 1;
  2706.         }
  2707.         $agendaItem = new EventAgendaitems();
  2708.         $agendaItem->setIdEvent($idEvent);
  2709.         $agendaItem->setIdAgendaitem($agendaNumber);
  2710.         $agendaItem->setParentIdevent($parentId);
  2711.         $agendaItem->setTitle($agendaName);
  2712.         $agendaItem->setNotes('');
  2713.         $agendaItem->setRequiredActions('');
  2714.         $agendaItem->setAgendaOrder($agendaOrder);
  2715.         $em->persist($agendaItem);
  2716.         $em->flush();
  2717.         $this->updateEvent($idEvent);
  2718.         $id $agendaItem->getId();
  2719.         $agendaName $agendaItem->getTitle();
  2720.         return new JsonResponse(
  2721.             array(
  2722.                 'status' => 1,
  2723.                 'childGroup' => $id,
  2724.                 'groupName' => $agendaName,
  2725.                 'parent' => $parent
  2726.             )
  2727.         );
  2728.     }
  2729.     /**
  2730.      * @return boolean
  2731.      */
  2732.     public function updateEvent($idEvent)
  2733.     {
  2734.         //@todo this is not logic, normal user can copy an event but not edit it....
  2735.         if ($this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')) {
  2736.             $em $this->getDoctrine()->getManager();
  2737.             $event $em->getRepository('OceanExpertBundle:Events')->findOneBy(array('idEvent' => $idEvent));
  2738.             $author $this->get('security.token_storage')->getToken()->getUser();
  2739.             $event->setLastEditBy($author->getId());
  2740.             $event->setUpdatedAt(new DateTime("now"));
  2741.             $em->persist($event);
  2742.             $em->flush();
  2743.         }
  2744.         return true;
  2745.     }
  2746.     /**
  2747.      * @param Request $request
  2748.      * @return JsonResponse
  2749.      */
  2750.     public function deleteAgendaItemAjaxAction(Request $request)
  2751.     {
  2752.         $idAgenda $request->request->get('agendaId');
  2753.         $idEvent $request->request->get('eventId');
  2754.         $em $this->getDoctrine()->getManager();
  2755.         $repository $em->getRepository('OceanExpertBundle:EventAgendaitems')->findById($idAgenda);
  2756.         if ($repository) {
  2757.             $agendaItems $em->getRepository('OceanExpertBundle:EventAgendaitems')->findOneBy(array('id' => $idAgenda));
  2758.             $id $agendaItems->getId();
  2759.             $em->remove($agendaItems);
  2760.             $em->flush();
  2761.             do {
  2762.                 $agendaItems $em->getRepository('OceanExpertBundle:EventAgendaitems')->findBy(array('parentIdevent' => $id));
  2763.                 if ($agendaItems) {
  2764.                     foreach ($agendaItems as $agendaItem) {
  2765.                         $id $agendaItem->getId();
  2766.                         $em->remove($agendaItem);
  2767.                     }
  2768.                     $em->flush();
  2769.                 } else {
  2770.                     $id 0;
  2771.                 }
  2772.             } while ($id != 0);
  2773.             $this->updateEvent($idEvent);
  2774.         }
  2775.         return new JsonResponse($idAgenda);
  2776.     }
  2777.     /**
  2778.      * @param Request $request
  2779.      * @return JsonResponse
  2780.      */
  2781.     public function updateAgendaItemAjaxAction(Request $request)
  2782.     {
  2783.         $idAgenda $request->request->get('idAgenda');
  2784.         $idEvent $request->request->get('idEvent');
  2785.         $action $request->request->get('action');
  2786.         $em $this->getDoctrine()->getManager();
  2787.         $agenda $em
  2788.             ->getRepository('OceanExpertBundle:EventAgendaitems')
  2789.             ->findOneBy(
  2790.                 array(
  2791.                     'id' => $idAgenda
  2792.                 )
  2793.             );
  2794.         if ($action == "edit") {
  2795.             $agendaName $request->request->get('agendaName');
  2796.             $agendaNumber $request->request->get('agendaNumber');
  2797.             $notes $request->request->get('notes');
  2798.             $actions $request->request->get('actions');
  2799.             $agenda->setIdAgendaitem($agendaNumber);
  2800.             $agenda->setTitle($agendaName);
  2801.             $agenda->setNotes($notes);
  2802.             $agenda->setRequiredActions($actions);
  2803.             $em->persist($agenda);
  2804.             $em->flush();
  2805.         }
  2806.         $this->updateEvent($idEvent);
  2807.         $agendaDetails = array(
  2808.             'idAgenda' => $agenda->getId(),
  2809.             'idAgendaItem' => $agenda->getIdAgendaitem(),
  2810.             'agendaTitle' => $agenda->getTitle(),
  2811.             'notes' => $agenda->getNotes(),
  2812.             'actions' => $agenda->getRequiredActions(),
  2813.             'status' => 1,
  2814.         );
  2815.         return new JsonResponse($agendaDetails);
  2816.     }
  2817.     /**
  2818.      * (self)register for an event
  2819.      *
  2820.      * @param int $idEvent id of the event
  2821.      *
  2822.      * @return RedirectResponse|Response|null
  2823.      */
  2824.     public function registerEventAction($idEventRequest $request)
  2825.     {
  2826.         //as we need the email address
  2827.         //check that the user is logged in
  2828.         //and has a complete profile
  2829.         if ($this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
  2830.             $user $this->get('security.token_storage')->getToken()->getUser();
  2831.             //let's check if the logged-in user has a 'real' profile
  2832.             //the mandatory profile fields are all filled and the expert is active
  2833.             $em $this->getDoctrine()->getManager();
  2834.             $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  2835.             if (!SecurityController::checkUserProfile($em$userId)) {
  2836.                 return $this->redirect(
  2837.                     $this->generateUrl(
  2838.                         'user_profile_edit'
  2839.                     )
  2840.                 );
  2841.             }
  2842.             $userEmail $user->getEmail();
  2843.         } else {
  2844.             //redirect to the login page
  2845.             return $this->redirect(
  2846.                 $this->generateUrl(
  2847.                     'fos_user_security_login',
  2848.                     array(
  2849.                     )
  2850.                 )
  2851.             );
  2852.         }
  2853.         $event $em
  2854.             ->getRepository('OceanExpertBundle:Events')
  2855.             ->findOneBy(
  2856.                 array(
  2857.                     'idEvent' => $idEvent
  2858.                 )
  2859.             );
  2860.         $eventCountry $em
  2861.             ->getRepository('OceanExpertBundle:Countries')
  2862.             ->findOneBy(
  2863.                 array(
  2864.                     'idCountry' => $event->getIdCountry()
  2865.                 )
  2866.             );
  2867.         if (!isset($eventCountry)
  2868.             || !($eventCountry->getCountry())
  2869.         ) {
  2870.             $eventCountry 'country not specified (yet)';
  2871.         } else {
  2872.             $eventCountry $eventCountry->getCountry();
  2873.         }
  2874.         $status = array();
  2875.         if ($request->isMethod('POST')) {
  2876.             //user wants to (self)register
  2877.             //even while we already checked the user's email address
  2878.             // and checked that the profile exists, we still need to check
  2879.             // if the user is active
  2880.             $query $this->getDoctrine()
  2881.                 ->getRepository('OceanExpertBundle:Indiv')
  2882.                 ->createQueryBuilder('i')
  2883.                 ->select('
  2884.                     i.idInd,
  2885.                     i.fname,
  2886.                     i.sname,
  2887.                     i.email1,
  2888.                     i.email2'
  2889.                 )
  2890.                 ->where('i.email1 = :email')
  2891.                 ->orWhere('i.email2 = :email')
  2892.                 ->andWhere('i.status = 1')
  2893.                 ->setParameter('email'$userEmail)
  2894.                 ->getQuery();
  2895.             $participant $query->getOneOrNullResult();
  2896.             if ($participant) {
  2897.                 $eventParticipants $em
  2898.                     ->getRepository('OceanExpertBundle:EventParticipants')
  2899.                     ->findOneBy(
  2900.                         array(
  2901.                             'idEvent' => $idEvent,
  2902.                             'idInd' => $participant['idInd']
  2903.                         )
  2904.                     );
  2905.                 if ($eventParticipants) {
  2906.                     //get some info about the user's status as participant
  2907.                     //skip the rest and show the register page with warning about status
  2908.                     $status = array(
  2909.                         'user' => $this->getExpertById($participant['idInd']),
  2910.                         'userEventStatus' => $eventParticipants->getStatus(),
  2911.                     );
  2912.                 } else {
  2913.                     $eventParticipants = new EventParticipants();
  2914.                     $eventParticipants->setIdEvent($idEvent);
  2915.                     $eventParticipants->setIdInd($participant['idInd']);
  2916.                     $eventParticipants->setStatus(2);
  2917.                     $eventParticipants->setArriveOn(
  2918.                         new DateTime(
  2919.                             date(
  2920.                                 'd-m-Y',
  2921.                                 strtotime($request->request->get('arriveOn'))
  2922.                             )
  2923.                         )
  2924.                     );
  2925.                     $eventParticipants->setLeaveOn(
  2926.                         new DateTime(
  2927.                             date(
  2928.                                 'd-m-Y',
  2929.                                 strtotime($request->request->get('leaveOn'))
  2930.                             )
  2931.                         )
  2932.                     );
  2933.                     $eventParticipants->setAccommodation(
  2934.                         $request->request->get('accomodation')
  2935.                     );
  2936.                     $eventParticipants->setContactTel(
  2937.                         $request->request->get('contact')
  2938.                     );
  2939.                     $eventParticipants->setStatusChangedAt(
  2940.                         new DateTime()
  2941.                     );
  2942.                     $em->persist($eventParticipants);
  2943.                     $em->flush();
  2944.                     $url $this->generateUrl(
  2945.                         'view_event',
  2946.                         array(
  2947.                             'event' => $idEvent
  2948.                         )
  2949.                     );
  2950.                     return $this->redirect(
  2951.                         sprintf(
  2952.                             '%s#%s',
  2953.                             $url,
  2954.                             'participants'
  2955.                         )
  2956.                     );
  2957.                 }
  2958.             } else {
  2959.                 $status = array(
  2960.                     'userStatus' => 'not active',
  2961.                     'user' => '',
  2962.                     'userEventStatus' => ''
  2963.                 );
  2964.             }
  2965.         }
  2966.         $data = array(
  2967.             'event' => $event,
  2968.             'eventCountry' => $eventCountry,
  2969.             'status' => $status,
  2970.             'email' => $userEmail
  2971.         );
  2972.         return $this->render(
  2973.             'Event/registerEvent.html.twig',
  2974.             array(
  2975.                 'data' => $data
  2976.             )
  2977.         );
  2978.     }
  2979.     /**
  2980.      * @param int $idInd id of the expert
  2981.      *
  2982.      * @return mixed
  2983.      */
  2984.     private function getExpertById($idInd)
  2985.     {
  2986.         $query $this->getDoctrine()
  2987.             ->getRepository('OceanExpertBundle:Indiv')
  2988.             ->createQueryBuilder('i')
  2989.             ->select('i.idInd, i.title, i.fname, i.sname, i.email1, i.email2')
  2990.             ->where('i.idInd = :idInd')
  2991.             ->setParameter('idInd'$idInd)
  2992.             ->getQuery();
  2993.         return $query->getOneOrNullResult();
  2994.     }
  2995.     /**
  2996.      * @param $eventId
  2997.      */
  2998.     public function updateEventAgenda($eventId)
  2999.     {
  3000.         //@todo there is a function to get the agenda items
  3001.         $agendaItems $this->getDoctrine()->getManager()
  3002.             ->getRepository('OceanExpertBundle:EventAgendaitems')
  3003.             ->findBy(array('idEvent' => $eventId));
  3004.         $em $this->getDoctrine()->getManager();
  3005.         foreach ($agendaItems as $agendaItem) {
  3006.             $items explode('.'$agendaItem->getIdAgendaitem());
  3007.             $parentId 0;
  3008.             if (count($items) > 1) {
  3009.                 $parentArr array_slice($items0, -1);
  3010.                 $parent implode("."$parentArr);
  3011.                 $parentItem $this->getDoctrine()->getManager()
  3012.                     ->getRepository('OceanExpertBundle:EventAgendaitems')
  3013.                     ->findOneBy(
  3014.                         array(
  3015.                             'idEvent' => $eventId,
  3016.                             'idAgendaitem' => $parent
  3017.                         )
  3018.                     );
  3019.                 $parentId $parentItem->getId();
  3020.             }
  3021.             $agendaItem->setParentIdevent($parentId);
  3022.             $em->persist($agendaItem);
  3023.             $em->flush();
  3024.         }
  3025.         $this->updateEvent($eventId);
  3026.     }
  3027.     /**
  3028.      * add a participant to the list of participant for a given event
  3029.      *
  3030.      * @return JsonResponse
  3031.      */
  3032.     public function addEventMemberAction(
  3033.         Request $request,
  3034.         TokenGeneratorInterface $fosTokenGenerator,
  3035.         UserManagerInterface $fosUserManager): JsonResponse
  3036.     {
  3037.         $idInd $request->request->get('idInd');
  3038.         $idEvent $request->request->get('idEvent');
  3039.         $membertype $request->request->get('membertype');
  3040.         $name $request->request->get('name');
  3041.         $em $this->getDoctrine()->getManager();
  3042.         $participantRoles = array();
  3043.         $status 0;
  3044.         //why did the status not change to 1
  3045.         $reason '';
  3046.         //get the correct email address of the user
  3047.         $user $this->getDoctrine()->getManager()
  3048.             ->getRepository('OceanExpertBundle:Indiv')
  3049.             ->findOneBy(
  3050.                 array(
  3051.                     'idInd' => $idInd
  3052.                 )
  3053.             );
  3054.         if ($user) {
  3055.             $email1 $user->getEmail1();
  3056.             $email2 $user->getEmail2();
  3057.             if ($email2 != '') {
  3058.                 $email $email2;
  3059.             } elseif ($email1 != '') {
  3060.                 $email $email1;
  3061.             } else {
  3062.                 $email 'info@oceanexpert.org';
  3063.             }
  3064.         } else {
  3065.             $reason 'user not found';
  3066.         }
  3067.         //get the info about the event
  3068.         $eventInfo $this->getDoctrine()->getManager()
  3069.             ->getRepository('OceanExpertBundle:Events')
  3070.             ->findOneBy(
  3071.                 array(
  3072.                     'idEvent' => $idEvent
  3073.                 )
  3074.             );
  3075.         if (!$eventInfo) {
  3076.             $reason 'event not found';
  3077.         } else {
  3078.             if ($membertype == 1) {
  3079.                 $participantType 'event organiser';
  3080.                 $eventStaff $this->getDoctrine()->getManager()
  3081.                     ->getRepository('OceanExpertBundle:EventContacts')
  3082.                     ->findBy(
  3083.                         array(
  3084.                             'idEvent' => $idEvent,
  3085.                             'idInd' => $idInd
  3086.                         )
  3087.                     );
  3088.                 if (!$eventStaff) {
  3089.                     $eventContact = new EventContacts();
  3090.                     $eventContact->setIdEvent($idEvent);
  3091.                     $eventContact->setIdInd($idInd);
  3092.                     $eventContact->setContactFor('');
  3093.                     $em->persist($eventContact);
  3094.                     $em->flush();
  3095.                     $status 1;
  3096.                 } else {
  3097.                     $reason 'already in event contacts';
  3098.                 }
  3099.             } elseif ($membertype == 2) {
  3100.                 $participantType 'event staff member';
  3101.                 $eventStaff $this->getDoctrine()->getManager()
  3102.                     ->getRepository('OceanExpertBundle:EventStaff')
  3103.                     ->findBy(
  3104.                         array(
  3105.                             'idEvent' => $idEvent,
  3106.                             'idInd' => $idInd
  3107.                         )
  3108.                     );
  3109.                 if (!$eventStaff) {
  3110.                     $eventStaff = new EventStaff();
  3111.                     $eventStaff->setIdEvent($idEvent);
  3112.                     $eventStaff->setIdInd($idInd);
  3113.                     $em->persist($eventStaff);
  3114.                     $em->flush();
  3115.                     $status 1;
  3116.                 } else {
  3117.                     $reason 'is already in the event staff';
  3118.                 }
  3119.             } elseif ($membertype == 3) {
  3120.                 $participantType 'event participant';
  3121.                 $eventParticipant $this->getDoctrine()->getManager()
  3122.                     ->getRepository('OceanExpertBundle:EventParticipants')
  3123.                     ->findBy(
  3124.                         array(
  3125.                             'idEvent' => $idEvent,
  3126.                             'idInd' => $idInd
  3127.                         )
  3128.                     );
  3129.                 if (!$eventParticipant) {
  3130.                     $eventParticipant = new EventParticipants();
  3131.                     $eventParticipant->setIdEvent($idEvent);
  3132.                     $eventParticipant->setStatus(1);
  3133.                     $eventParticipant->setIdInd($idInd);
  3134.                     $em->persist($eventParticipant);
  3135.                     $em->flush();
  3136.                     $status 1;
  3137.                 } else {
  3138.                     $reason 'already in event participants';
  3139.                 }
  3140.                 $participantRoles $this->getDoctrine()
  3141.                     ->getRepository('OceanExpertBundle:EventParticipantroles')
  3142.                     ->createQueryBuilder('e')
  3143.                     ->select('e.idRole,e.role')
  3144.                     ->where('e.idEvent = :eventId')
  3145.                     ->setParameter('eventId'$idEvent)
  3146.                     ->getQuery()
  3147.                     ->getResult(AbstractQuery::HYDRATE_ARRAY);
  3148.             } elseif ($membertype == 4) {
  3149.                 $participantType 'event observer';
  3150.                 $eventInfo->setIdContact($idInd);
  3151.                 $em->persist($eventInfo);
  3152.                 $em->flush();
  3153.                 $status 1;
  3154.             }
  3155.             $this->updateEvent($idEvent);
  3156.         }
  3157.         if ($reason === '') {
  3158.             $participantCountry $this->getDoctrine()
  3159.                 ->getRepository('OceanExpertBundle:Indiv')
  3160.                 ->createQueryBuilder('i')
  3161.                 ->leftJoin('OceanExpertBundle:Countries''c''WITH''i.countryCode = c.idCountry')
  3162.                 ->select('c.country')
  3163.                 ->where('i.idInd = :idInd')
  3164.                 ->setParameter('idInd'$idInd)
  3165.                 ->getQuery()
  3166.                 ->getResult(AbstractQuery::HYDRATE_ARRAY);
  3167.         } else {
  3168.             $participantCountry = array();
  3169.             $participantCountry[0]['country'] = '';
  3170.         }
  3171.         $data = array(
  3172.             'idInd' => $idInd,
  3173.             'name' => $name,
  3174.             'participantRoles' => $participantRoles,
  3175.             'participantCountry' => $participantCountry[0]['country'],
  3176.             'memberType' => $membertype,
  3177.         );
  3178.         $event = array(
  3179.             'idEvent' => $idEvent,
  3180.             'title' => $eventInfo->getTitle(),
  3181.             'eventPeriod' => $eventInfo->getStartOn()->format('d-m-Y') . ' to ' $eventInfo->getEndOn()->format('d-m-Y'),
  3182.             'eventAddress' => $eventInfo->getAddress(),
  3183.             'eventUrl' => $this->generateUrl(
  3184.                 'view_event',
  3185.                 array(
  3186.                     'event' => $idEvent
  3187.                 ),
  3188.                 UrlGeneratorInterface::ABSOLUTE_URL
  3189.             )
  3190.         );
  3191.         //send an email to the user to let them know they have been added to the event
  3192.         if ($eventInfo->getEmailPeople()
  3193.             && $reason === ''
  3194.         ) {
  3195.             //who is sending this mail
  3196.             $sendUser $this->getDoctrine()->getManager()
  3197.                 ->getRepository('OceanExpertBundle:Indiv')
  3198.                 ->findOneBy(
  3199.                     array(
  3200.                         'idInd' => $eventInfo->getIdContact()
  3201.                     )
  3202.                 );
  3203.             if ($sendUser) {
  3204.                 $firstName $sendUser->getFName();
  3205.                 $lastName $sendUser->getSName();
  3206.                 if ($firstName != '' && $lastName != '') {
  3207.                     $fromEmail = array(
  3208.                         'info@oceanexpert.org' => 'OceanExpert Team on behalf of ' $firstName ' ' $lastName
  3209.                     );
  3210.                 } else {
  3211.                     $fromEmail = array(
  3212.                         'info@oceanexpert.org' => 'OceanExpert Team'
  3213.                     );
  3214.                 }
  3215.             } else {
  3216.                 $fromEmail =array(
  3217.                     'info@oceanexpert.org' => 'OceanExpert Team'
  3218.                 );
  3219.             }
  3220.             $message = new Swift_Message(
  3221.                 'Participation in an event',
  3222.                 $this->renderView(
  3223.                     'Email/eventAddParticipant.html.twig',
  3224.                     array(
  3225.                         'data' => $data,
  3226.                         'event' => $event,
  3227.                         'participantType' => $participantType
  3228.                     )
  3229.                 ),
  3230.                 'text/html'
  3231.             );
  3232.             $message->setFrom(
  3233.                 $fromEmail
  3234.             );
  3235.             $message->setTo(
  3236.                 array(
  3237.                     $email => $name
  3238.                 )
  3239.             );
  3240.             $this->get('mailer')->send($message);
  3241.             $comment 'sent mail to ' $email;
  3242.         } elseif (!$eventInfo->getEmailPeople()) {
  3243.             $comment 'no mail sent as this option is not set';
  3244.         }
  3245.         $return = array(
  3246.             'data' => $data,
  3247.             'status' => $status
  3248.         );
  3249.         if ($reason !== '') {
  3250.             $return['reason'] = $reason;
  3251.         }
  3252.         //add some comment if any
  3253.         if ($comment !== '') {
  3254.             $return['comment'] = $comment;
  3255.         }
  3256.         return new JsonResponse(
  3257.             $return
  3258.         );
  3259.     }
  3260.     /**
  3261.      * @return JsonResponse
  3262.      */
  3263.     public function deleteEventMemberAction(Request $request)
  3264.     {
  3265.         $idInd $request->request->get('idInd');
  3266.         $idEvent $request->request->get('idEvent');
  3267.         $membertype $request->request->get('membertype');
  3268.         $comment '';
  3269.         $em $this->getDoctrine()->getManager();
  3270.         switch ($membertype) {
  3271.             case 1:
  3272.                 $eventContact $this->getDoctrine()->getManager()
  3273.                     ->getRepository('OceanExpertBundle:EventContacts')
  3274.                     ->findOneBy(
  3275.                         array(
  3276.                             'idEvent' => $idEvent,
  3277.                             'idInd' => $idInd
  3278.                         )
  3279.                     );
  3280.                 if ($eventContact) {
  3281.                     $em->remove($eventContact);
  3282.                     $em->flush();
  3283.                 }
  3284.                 break;
  3285.             case 2:
  3286.                 $eventStaff $this->getDoctrine()->getManager()
  3287.                     ->getRepository('OceanExpertBundle:EventStaff')
  3288.                     ->findOneBy(
  3289.                         array(
  3290.                             'idEvent' => $idEvent,
  3291.                             'idInd' => $idInd
  3292.                         )
  3293.                     );
  3294.                 if ($eventStaff) {
  3295.                     $em->remove($eventStaff);
  3296.                     $em->flush();
  3297.                 }
  3298.                 break;
  3299.             case 3:
  3300.                 $eventParticipant $this->getDoctrine()->getManager()
  3301.                     ->getRepository('OceanExpertBundle:EventParticipants')
  3302.                     ->findOneBy(
  3303.                         array(
  3304.                             'idEvent' => $idEvent,
  3305.                             'idInd' => $idInd
  3306.                         )
  3307.                     );
  3308.                 if ($eventParticipant) {
  3309.                     $em->remove($eventParticipant);
  3310.                     $em->flush();
  3311.                 }
  3312.                 break;
  3313.         }
  3314.         //send an email to the user to let them know they have been removed from the event
  3315.         $eventInfo $this->getDoctrine()->getManager()
  3316.             ->getRepository('OceanExpertBundle:Events')
  3317.             ->findOneBy(
  3318.                 array(
  3319.                     'idEvent' => $idEvent
  3320.                 )
  3321.             );
  3322.         if ($eventInfo->getEmailPeople()) {
  3323.             //get the name and email of the participant
  3324.             $participant $this->getDoctrine()->getManager()
  3325.                 ->getRepository('OceanExpertBundle:Indiv')
  3326.                 ->findOneBy(
  3327.                     array(
  3328.                         'idInd' => $idInd
  3329.                     )
  3330.                 );
  3331.             //strange construction to keep this compatible with the other send mail code
  3332.             //this will make it easier to refactor this later
  3333.             $data['name'] = $participant->getTitle() . ' ' $participant->getFName() . ' ' $participant->getSName();
  3334.             $participantEmail1 $participant->getEmail1();
  3335.             $participantEmail2 $participant->getEmail2();
  3336.             if ($participantEmail2 != '') {
  3337.                 $email $participantEmail2;
  3338.             } elseif ($participantEmail1 != '') {
  3339.                 $email $participantEmail1;
  3340.             }
  3341.             //who is sending this mail
  3342.             $sendUser $this->getDoctrine()->getManager()
  3343.                 ->getRepository('OceanExpertBundle:Indiv')
  3344.                 ->findOneBy(
  3345.                     array(
  3346.                         'idInd' => $eventInfo->getIdContact()
  3347.                     )
  3348.                 );
  3349.             if ($sendUser) {
  3350.                 $firstName $sendUser->getFName();
  3351.                 $lastName $sendUser->getSName();
  3352.                 if ($firstName != '' && $lastName != '') {
  3353.                     $fromEmail = array(
  3354.                         'info@oceanexpert.org' => 'OceanExpert Team on behalf of ' $firstName ' ' $lastName
  3355.                     );
  3356.                 } else {
  3357.                     $fromEmail = array(
  3358.                         'info@oceanexpert.org' => 'OceanExpert Team'
  3359.                     );
  3360.                 }
  3361.             } else {
  3362.                 $fromEmail =array(
  3363.                     'info@oceanexpert.org' => 'OceanExpert Team'
  3364.                 );
  3365.             }
  3366.             $event['title'] = $eventInfo->getTitle();
  3367.             $event['eventAddress'] = $eventInfo->getAddress();
  3368.             $event['eventPeriod'] = $eventInfo->getStartOn()->format('d-m-Y') . ' to ' $eventInfo->getEndOn()->format('d-m-Y');
  3369.             $event['eventUrl'] = $this->generateUrl(
  3370.                 'view_event',
  3371.                 array(
  3372.                     'event' => $idEvent
  3373.                 ),
  3374.                 UrlGeneratorInterface::ABSOLUTE_URL
  3375.             );
  3376.             $message = new Swift_Message(
  3377.                 'Participation in an event',
  3378.                 $this->renderView(
  3379.                     'Email/eventDeleteParticipant.html.twig',
  3380.                     array(
  3381.                         'data' => $data,
  3382.                         'event' => $event
  3383.                     )
  3384.                 ),
  3385.                 'text/html'
  3386.             );
  3387.             $message->setFrom(
  3388.                 $fromEmail
  3389.             );
  3390.             $message->setTo(
  3391.                 array(
  3392.                     $email => $data['name']
  3393.                 )
  3394.             );
  3395.             $this->get('mailer')->send($message);
  3396.             $comment 'sent mail to ' $email;
  3397.         } elseif (!$eventInfo->getEmailPeople()) {
  3398.             $comment 'no mail sent as this option is not set';
  3399.         }
  3400.         return new JsonResponse(
  3401.             array(
  3402.                 'status' => 1,
  3403.                 'id' => $membertype,
  3404.                 'comment' => $comment
  3405.             )
  3406.         );
  3407.     }
  3408.     /**
  3409.      * change some settings of a event participant
  3410.      * this is called by AJAX when either
  3411.      * - changing the role
  3412.      * - changing the status
  3413.      * - changing the comments
  3414.      * this is not the same as editParticipantAction()
  3415.      *
  3416.      * @return JsonResponse
  3417.      */
  3418.     public function editEventMemberAction(Request $request)
  3419.     {
  3420.         $data $request->request->all();
  3421.         $idEvent $data['idEvent'];
  3422.         $idInd $data['idInd'];
  3423.         $em $this->getDoctrine()->getManager();
  3424.         $eventParticipant $em
  3425.             ->getRepository('OceanExpertBundle:EventParticipants')
  3426.             ->findOneBy(
  3427.                 array(
  3428.                     'idEvent' => $idEvent,
  3429.                     'idInd' => $idInd
  3430.                 )
  3431.             );
  3432.         if (isset($data['idRole'])) {
  3433.             $idRole $data['idRole'];
  3434.             //get information about the old role
  3435.             $oldRoleId $eventParticipant->getIdRole();
  3436.             $oldRole $this->getDoctrine()->getManager()
  3437.                 ->getRepository('OceanExpertBundle:EventParticipantroles')
  3438.                 ->findOneBy(
  3439.                     array(
  3440.                         'idRole' => $oldRoleId,
  3441.                         'idEvent' => $idEvent
  3442.                     )
  3443.                 );
  3444.             if ($oldRole) {
  3445.                 $oldRole $oldRole->getRole();
  3446.             } else {
  3447.                 $oldRole 'no role';
  3448.             }
  3449.             //set the new roleid
  3450.             $eventParticipant->setIdRole($idRole);
  3451.             //get info about the new role
  3452.             $role $this->getDoctrine()->getManager()
  3453.                 ->getRepository('OceanExpertBundle:EventParticipantroles')
  3454.                 ->findOneBy(
  3455.                     array(
  3456.                         'idRole' => $idRole,
  3457.                         'idEvent' => $idEvent
  3458.                     )
  3459.                 );
  3460.             $role $role->getRole();
  3461.             $mailContent "role changed from '$oldRole' to '$role'";
  3462.         } elseif (isset($data['comments'])) {
  3463.             $oldComments $eventParticipant->getAccommodation();
  3464.             $eventParticipant->setAccommodation($data['comments']);
  3465.             $mailContent "comments changed from '$oldComments' to '" $data['comments'] . '"';
  3466.         } elseif (isset($data['status'])) {
  3467.             $oldStatus $this->participantStatus[$eventParticipant->getStatus()];
  3468.             $status $this->participantStatus[$data['status']];
  3469.             $eventParticipant->setStatus($data['status']);
  3470.             $mailContent "status changed from '$oldStatus' to '$status'";
  3471.         }
  3472.         $em->persist($eventParticipant);
  3473.         $em->flush();
  3474.         $this->updateEvent($data['idEvent']);
  3475.         //send an email to the user to warn that something has changed
  3476.         $eventInfo $this->getDoctrine()->getManager()
  3477.             ->getRepository('OceanExpertBundle:Events')
  3478.             ->findOneBy(
  3479.                 array(
  3480.                     'idEvent' => $idEvent
  3481.                 )
  3482.             );
  3483.         if ($eventInfo->getEmailPeople()) {
  3484.             //get the name and email of the participant
  3485.             $participant $this->getDoctrine()->getManager()
  3486.                 ->getRepository('OceanExpertBundle:Indiv')
  3487.                 ->findOneBy(
  3488.                     array(
  3489.                         'idInd' => $idInd
  3490.                     )
  3491.                 );
  3492.             //strange construction to keep this compatible with the other send mail code
  3493.             //this will make it easier to refactor this later
  3494.             $data['name'] = $participant->getTitle() . ' ' $participant->getFName() . ' ' $participant->getSName();
  3495.             $participantEmail1 $participant->getEmail1();
  3496.             $participantEmail2 $participant->getEmail2();
  3497.             if ($participantEmail2 != '') {
  3498.                 $email $participantEmail2;
  3499.             } elseif ($participantEmail1 != '') {
  3500.                 $email $participantEmail1;
  3501.             }
  3502.             //who is sending this mail
  3503.             $sendUser $this->getDoctrine()->getManager()
  3504.                 ->getRepository('OceanExpertBundle:Indiv')
  3505.                 ->findOneBy(
  3506.                     array(
  3507.                         'idInd' => $eventInfo->getIdContact()
  3508.                     )
  3509.                 );
  3510.             if ($sendUser) {
  3511.                 $firstName $sendUser->getFName();
  3512.                 $lastName $sendUser->getSName();
  3513.                 if ($firstName != '' && $lastName != '') {
  3514.                     $fromEmail = array(
  3515.                         'info@oceanexpert.org' => 'OceanExpert Team on behalf of ' $firstName ' ' $lastName
  3516.                     );
  3517.                 } else {
  3518.                     $fromEmail = array(
  3519.                         'info@oceanexpert.org' => 'OceanExpert Team'
  3520.                     );
  3521.                 }
  3522.             } else {
  3523.                 $fromEmail =array(
  3524.                     'info@oceanexpert.org' => 'OceanExpert Team'
  3525.                 );
  3526.             }
  3527.             $event['title'] = $eventInfo->getTitle();
  3528.             $event['eventAddress'] = $eventInfo->getAddress();
  3529.             $event['eventPeriod'] = $eventInfo->getStartOn()->format('d-m-Y') . ' to ' $eventInfo->getEndOn()->format('d-m-Y');
  3530.             $event['eventUrl'] = $this->generateUrl(
  3531.                 'view_event',
  3532.                 array(
  3533.                     'event' => $idEvent
  3534.                 ),
  3535.                 UrlGeneratorInterface::ABSOLUTE_URL
  3536.             );
  3537.             $message = new Swift_Message(
  3538.                 'Participation in an event',
  3539.                 $this->renderView(
  3540.                     'Email/eventEditParticipant.html.twig',
  3541.                     array(
  3542.                         'data' => $data,
  3543.                         'event' => $event,
  3544.                         'mailContent' => $mailContent
  3545.                     )
  3546.                 ),
  3547.                 'text/html'
  3548.             );
  3549.             $message->setFrom(
  3550.                 $fromEmail
  3551.             );
  3552.             $message->setTo(
  3553.                 array(
  3554.                     $email => $data['name']
  3555.                 )
  3556.             );
  3557.             $this->get('mailer')->send($message);
  3558.             $comment 'sent mail to ' $email;
  3559.         } elseif (!$eventInfo->getEmailPeople()) {
  3560.             $comment 'no mail sent as this option is not set';
  3561.         }
  3562.         return new JsonResponse(
  3563.             array(
  3564.                 'status' => 1,
  3565.                 'paricipant' => $eventParticipant,
  3566.                 'comment' => $comment,
  3567.                 'mailContent' => $mailContent
  3568.             )
  3569.         );
  3570.     }
  3571.     /**
  3572.      * change the info linked to a event contact/organiser
  3573.      * this is called by AJAX
  3574.      *
  3575.      * @return JsonResponse
  3576.      */
  3577.     public function editEventContactsAction(Request $request)
  3578.     {
  3579.         $idInd $request->request->get('idInd');
  3580.         $idEvent $request->request->get('idEvent');
  3581.         $contactfor $request->request->get('contactfor');
  3582.         $em $this->getDoctrine()->getManager();
  3583.         $eventContact $em
  3584.             ->getRepository('OceanExpertBundle:EventContacts')
  3585.             ->findOneBy(
  3586.                 array(
  3587.                     'idEvent' => $idEvent,
  3588.                     'idInd' => $idInd
  3589.                 )
  3590.             );
  3591.         //get the old contactfor value
  3592.         $oldContactFor $eventContact->getContactFor();
  3593.         $mailContent "'contact for' changed from '$oldContactFor' to '$contactfor'";
  3594.         $eventContact->setContactFor($contactfor);
  3595.         $em->persist($eventContact);
  3596.         $em->flush();
  3597.         $this->updateEvent($idEvent);
  3598.         //send an email to the user to warn that something has changed
  3599.         $eventInfo $this->getDoctrine()->getManager()
  3600.             ->getRepository('OceanExpertBundle:Events')
  3601.             ->findOneBy(
  3602.                 array(
  3603.                     'idEvent' => $idEvent
  3604.                 )
  3605.             );
  3606.         if ($eventInfo->getEmailPeople()) {
  3607.             //get the name and email of the participant
  3608.             $participant $this->getDoctrine()->getManager()
  3609.                 ->getRepository('OceanExpertBundle:Indiv')
  3610.                 ->findOneBy(
  3611.                     array(
  3612.                         'idInd' => $idInd
  3613.                     )
  3614.                 );
  3615.             //strange construction to keep this compatible with the other send mail code
  3616.             //this will make it easier to refactor this later
  3617.             $data['name'] = $participant->getTitle() . ' ' $participant->getFName() . ' ' $participant->getSName();
  3618.             $participantEmail1 $participant->getEmail1();
  3619.             $participantEmail2 $participant->getEmail2();
  3620.             if ($participantEmail2 != '') {
  3621.                 $email $participantEmail2;
  3622.             } elseif ($participantEmail1 != '') {
  3623.                 $email $participantEmail1;
  3624.             }
  3625.             //who is sending this mail
  3626.             $sendUser $this->getDoctrine()->getManager()
  3627.                 ->getRepository('OceanExpertBundle:Indiv')
  3628.                 ->findOneBy(
  3629.                     array(
  3630.                         'idInd' => $eventInfo->getIdContact()
  3631.                     )
  3632.                 );
  3633.             if ($sendUser) {
  3634.                 $firstName $sendUser->getFName();
  3635.                 $lastName $sendUser->getSName();
  3636.                 if ($firstName != '' && $lastName != '') {
  3637.                     $fromEmail = array(
  3638.                         'info@oceanexpert.org' => 'OceanExpert Team on behalf of ' $firstName ' ' $lastName
  3639.                     );
  3640.                 } else {
  3641.                     $fromEmail = array(
  3642.                         'info@oceanexpert.org' => 'OceanExpert Team'
  3643.                     );
  3644.                 }
  3645.             } else {
  3646.                 $fromEmail =array(
  3647.                     'info@oceanexpert.org' => 'OceanExpert Team'
  3648.                 );
  3649.             }
  3650.             $event['title'] = $eventInfo->getTitle();
  3651.             $event['eventAddress'] = $eventInfo->getAddress();
  3652.             $event['eventPeriod'] = $eventInfo->getStartOn()->format('d-m-Y') . ' to ' $eventInfo->getEndOn()->format('d-m-Y');
  3653.             $event['eventUrl'] = $this->generateUrl(
  3654.                 'view_event',
  3655.                 array(
  3656.                     'event' => $idEvent
  3657.                 ),
  3658.                 UrlGeneratorInterface::ABSOLUTE_URL
  3659.             );
  3660.             $message = new Swift_Message(
  3661.                 'Participation in an event',
  3662.                 $this->renderView(
  3663.                     'Email/eventEditParticipant.html.twig',
  3664.                     array(
  3665.                         'data' => $data,
  3666.                         'event' => $event,
  3667.                         'mailContent' => $mailContent
  3668.                     )
  3669.                 ),
  3670.                 'text/html'
  3671.             );
  3672.             $message->setFrom(
  3673.                 $fromEmail
  3674.             );
  3675.             $message->setTo(
  3676.                 array(
  3677.                     $email => $data['name']
  3678.                 )
  3679.             );
  3680.             $this->get('mailer')->send($message);
  3681.             $comment 'sent mail to ' $email;
  3682.         } elseif (!$eventInfo->getEmailPeople()) {
  3683.             $comment 'no mail sent as this option is not set';
  3684.         }
  3685.         return new JsonResponse(
  3686.             array(
  3687.                 'status' => 1,
  3688.                 'comment' => $comment,
  3689.                 'mailContent' => $mailContent
  3690.             )
  3691.         );
  3692.     }
  3693.     /**
  3694.      * @return JsonResponse
  3695.      */
  3696.     public function eventClearContactAction(Request $request)
  3697.     {
  3698.         $idEvent $request->request->get('idEvent');
  3699.         $em $this->getDoctrine()->getManager();
  3700.         $event $em->getRepository('OceanExpertBundle:Events')->findOneBy(array('idEvent' => $idEvent));
  3701.         $event->setIdContact('');
  3702.         $em->persist($event);
  3703.         $em->flush();
  3704.         $this->updateEvent($idEvent);
  3705.         return new JsonResponse(array('status' => 1));
  3706.     }
  3707.     /**
  3708.      * @param Request $request
  3709.      *
  3710.      * @return JsonResponse
  3711.      */
  3712.     public function addEventDocumentAction(Request $request)
  3713.     {
  3714.         $documentType $request->request->get('doctype');
  3715.         $idEvent $request->request->get('idEvent');
  3716.         $idDoc $request->request->get('idDoc');
  3717.         if (null != $request->request->get('idAgenda')) {
  3718.             $idAgenda $request->request->get('idAgenda');
  3719.         }
  3720.         $em $this->getDoctrine()->getManager();
  3721.         $idAgendaItem '';
  3722.         //see doctypes in the database
  3723.         switch ($documentType) {
  3724.             case "1":
  3725.                 $report $em->getRepository('OceanExpertBundle:EventReports')
  3726.                     ->findOneBy(
  3727.                         array(
  3728.                             'idEvent' => $idEvent
  3729.                         )
  3730.                     );
  3731.                 if ($report) {
  3732.                     $em->remove($report);
  3733.                     $em->flush();
  3734.                 }
  3735.                 $report = new EventReports();
  3736.                 $report->setIdEvent($idEvent);
  3737.                 $report->setIdDoc($idDoc);
  3738.                 $em->persist($report);
  3739.                 $em->flush();
  3740.                 break;
  3741.             case "2":
  3742.                 $presentation $em->getRepository('OceanExpertBundle:EventPresentations')
  3743.                     ->findOneBy(
  3744.                         array(
  3745.                             'idEvent' => $idEvent,
  3746.                             'idDoc' => $idDoc
  3747.                         )
  3748.                     );
  3749.                 if ($presentation) {
  3750.                     $em->remove($presentation);
  3751.                     $em->flush();
  3752.                 }
  3753.                 $presentation = new EventPresentations();
  3754.                 $presentation->setIdDoc($idDoc);
  3755.                 $presentation->setIdEvent($idEvent);
  3756.                 $em->persist($presentation);
  3757.                 $em->flush();
  3758.                 break;
  3759.             case "3":
  3760.                 $backgroundDocs $em->getRepository('OceanExpertBundle:EventBackgrounddocs')
  3761.                     ->findOneBy(
  3762.                         array(
  3763.                             'idEvent' => $idEvent,
  3764.                             'idDoc' => $idDoc
  3765.                         )
  3766.                     );
  3767.                 if ($backgroundDocs) {
  3768.                     $em->remove($backgroundDocs);
  3769.                     $em->flush();
  3770.                 }
  3771.                 $backgroundDocs = new EventBackgrounddocs();
  3772.                 $backgroundDocs->setIdDoc($idDoc);
  3773.                 $backgroundDocs->setIdEvent($idEvent);
  3774.                 $em->persist($backgroundDocs);
  3775.                 $em->flush();
  3776.                 break;
  3777.             case "4":
  3778.                 $otherDocs $em->getRepository('OceanExpertBundle:EventOtherdocs')
  3779.                     ->findOneBy(
  3780.                         array(
  3781.                             'idEvent' => $idEvent,
  3782.                             'idDoc' => $idDoc
  3783.                         )
  3784.                     );
  3785.                 if ($otherDocs) {
  3786.                     $em->remove($otherDocs);
  3787.                     $em->flush();
  3788.                 }
  3789.                 $otherDocs = new EventOtherdocs();
  3790.                 $otherDocs->setIdDoc($idDoc);
  3791.                 $otherDocs->setIdEvent($idEvent);
  3792.                 $em->persist($otherDocs);
  3793.                 $em->flush();
  3794.                 break;
  3795.             case "5":
  3796.                 if ($idAgenda != '') {
  3797.                     $agendaDetails $em->getRepository('OceanExpertBundle:EventAgendaitems')
  3798.                         ->findOneBy(
  3799.                             array(
  3800.                                 'id' => $idAgenda
  3801.                             )
  3802.                         );
  3803.                     if ($agendaDetails) {
  3804.                         $idAgendaItem $agendaDetails->getIdAgendaitem();
  3805.                         $agendaDocs $em->getRepository('OceanExpertBundle:AgendaitemDocuments')
  3806.                             ->findOneBy(
  3807.                                 array(
  3808.                                     'idEvent' => $idEvent,
  3809.                                     'idDoc' => $idDoc,
  3810.                                     'idAgendaitem' => $agendaDetails->getIdAgendaitem()
  3811.                                 )
  3812.                             );
  3813.                         if ($agendaDocs) {
  3814.                             $em->remove($agendaDocs);
  3815.                             $em->flush();
  3816.                         }
  3817.                         $conn $this->getDoctrine()->getConnection();
  3818.                         $stmt $conn->prepare('INSERT INTO agendaitem_documents (id_event, id_agendaitem, id_doc) VALUES (?, ?, ?)');
  3819.                         $stmt->bindValue(1$idEvent);
  3820.                         $stmt->bindValue(2$idAgendaItem);
  3821.                         $stmt->bindValue(3$idDoc);
  3822.                         $stmt->execute();
  3823.                     }
  3824.                 }
  3825.                 break;
  3826.         }
  3827.         $data = array(
  3828.             'status' => 1,
  3829.             $request->request->all(),
  3830.             'idAgendaItem' => $idAgendaItem
  3831.         );
  3832.         return new JsonResponse($data);
  3833.     }
  3834.     /**
  3835.      * @return JsonResponse
  3836.      */
  3837.     public function saveAgendaOrderAction(Request $request): JsonResponse
  3838.     {
  3839.         $order $request->request->get('order');
  3840.         $agendaOrders json_decode($order);
  3841.         $i 0;
  3842.         foreach ($agendaOrders->groups as $agendaData) {
  3843.             $agenda $this->getDoctrine()
  3844.                 ->getRepository('OceanExpertBundle:EventAgendaitems')
  3845.                 ->findOneBy(
  3846.                     array(
  3847.                         'id' => $agendaData->agendaId
  3848.                     )
  3849.                 );
  3850.             $em $this->getDoctrine()->getManager();
  3851.             if ($agenda) {
  3852.                 $agenda->setParentIdevent($agendaData->parentId);
  3853.                 $agenda->setAgendaOrder($i);
  3854.                 $em->persist($agenda);
  3855.                 $em->flush();
  3856.                 $this->updateEvent($agenda->getIdEvent());
  3857.             }
  3858.             $i++;
  3859.         }
  3860.         return new JsonResponse($order);
  3861.     }
  3862.     /**
  3863.      * @return Response
  3864.      */
  3865.     function updatePresentationAgendaAction(Request $request)
  3866.     {
  3867.         $idDoc $request->request->get('idDoc');
  3868.         $idEvent $request->request->get('idEvent');
  3869.         $agendaItem $request->request->get('agendaItem');
  3870.         $em $this->getDoctrine()->getManager();
  3871.         $eventPresentation $em->getRepository('OceanExpertBundle:EventPresentations')
  3872.             ->findOneBy(
  3873.                 array(
  3874.                     'idEvent' => $idEvent,
  3875.                     'idDoc' => $idDoc
  3876.                 )
  3877.             );
  3878.         if ($eventPresentation) {
  3879.             $eventPresentation->setIdAgendaitem($agendaItem);
  3880.             $em->persist($eventPresentation);
  3881.             $em->flush();
  3882.             $this->updateEvent($idEvent);
  3883.         }
  3884.         return new JsonResponse(array('status' => 1));
  3885.     }
  3886.     /**
  3887.      * send an invitation to the participants that have been added to this event
  3888.      *
  3889.      * @return Response
  3890.      */
  3891.     public function sendInvitesEventParticipantsAction(Request $request)
  3892.     {
  3893.         //what is our environment?
  3894.         $env $this->container->get('kernel')->getEnvironment();
  3895.         $idEvent $request
  3896.             ->request
  3897.             ->get('idEvent');
  3898.         $em $this
  3899.             ->getDoctrine()
  3900.             ->getManager();
  3901.         $event $em
  3902.             ->getRepository('OceanExpertBundle:Events')
  3903.             ->findOneBy(
  3904.                 array(
  3905.                     'idEvent' => $idEvent
  3906.                 )
  3907.             );
  3908.         $sender $this->fosUserManager->findUserBy(
  3909.             array(
  3910.                 'id' => $event->getIdContact()
  3911.             )
  3912.         );
  3913.         $data = array(
  3914.             'userid' => $event->getIdContact(),
  3915.             'eventInvitation' => $event->getIdEvent(),
  3916.             'selfMail' => "false",
  3917.             'email' => $sender->getEmail()
  3918.         );
  3919.         $filename uniqid() . '.json';
  3920.         $filePath '/tmp/emailqueue';
  3921.         if ($env !== 'prod') {
  3922.             $filePath .= "_$env";
  3923.         }
  3924.         if (!file_exists($filePath '/')) {
  3925.             mkdir(
  3926.                 $filePath,
  3927.                 0777
  3928.             );
  3929.         }
  3930.         $fp fopen(
  3931.             $filePath '/' $filename,
  3932.             'w'
  3933.         );
  3934.         fwrite($fpjson_encode($data));
  3935.         fclose($fp);
  3936.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  3937.         $eventUrl $baseurl $this->generateUrl('view_event', array('event' => $idEvent));
  3938.         $message = new Messages();
  3939.         $message->setIdSender($event->getIdContact());
  3940.         $message->setSearchstring($eventUrl);
  3941.         $message->setSubject("Event Invitation Mail");
  3942.         $message->setMessage("Event Invitation email for all participants for event " $event->getTitle());
  3943.         $message->setIgnoreprefs(1);
  3944.         $message->setApproved(2);
  3945.         $message->setDateentered(new DateTime("now"));
  3946.         $message->setDatequeued(new DateTime("now"));
  3947.         $em->persist($message);
  3948.         $em->flush();
  3949.         $mailQueue = new MailQueueFiles();
  3950.         $mailQueue->setIdInd($event->getIdContact());
  3951.         $mailQueue->setFileName($filename);
  3952.         $mailQueue->setCreatedAt(new DateTime("now"));
  3953.         $mailQueue->setProcessedAt(null);
  3954.         $mailQueue->setProcessed(0);
  3955.         $mailQueue->setIdMessage($message->getIdMsg());
  3956.         $mailQueue->setApproved(1);
  3957.         $em->persist($mailQueue);
  3958.         $em->flush();
  3959.         return new JsonResponse(
  3960.             array(
  3961.                 'status' => 1
  3962.             )
  3963.         );
  3964.     }
  3965.     /**
  3966.      * store the confirmation of a participant to an event
  3967.      *
  3968.      * @param int     $idEvent id of the event
  3969.      * @param string  $hash    the confirmation check hash
  3970.      * @param Request $request the request object
  3971.      *
  3972.      * @return Response
  3973.      *
  3974.      * @throws \Exception
  3975.      */
  3976.     public function confirmEventParticipationAction(int $idEventstring $hashRequest $request): Response
  3977.     {
  3978.         $em $this->getDoctrine()->getManager();
  3979.         $participants $em->getRepository('OceanExpertBundle:EventParticipants')
  3980.             ->findOneBy(
  3981.                 array(
  3982.                     'idEvent' => $idEvent,
  3983.                     'hash' => $hash
  3984.                 )
  3985.             );
  3986.         $event $em->getRepository('OceanExpertBundle:Events')
  3987.             ->findOneBy(
  3988.                 array(
  3989.                     'idEvent' => $idEvent
  3990.                 )
  3991.             );
  3992.         $eventCountry $em->getRepository('OceanExpertBundle:Countries')
  3993.             ->findOneBy(
  3994.                 array(
  3995.                     'idCountry' => $event->getIdCountry()
  3996.                 )
  3997.             );
  3998.         if ($participants) {
  3999.             if ($participants->getStatus() == 1) {
  4000.                 $message = array(
  4001.                     'status' => 1,
  4002.                     'message' => '',
  4003.                 );
  4004.             } else {
  4005.                 $message = array(
  4006.                     'status' => 0,
  4007.                     'message' => "Your participation in this event has already been confirmed.",
  4008.                 );
  4009.             }
  4010.             $expert $this->getExpertById($participants->getIdInd());
  4011.         } else {
  4012.             $message = array(
  4013.                 'status' => 0,
  4014.                 'message' => "The link you are trying to use to confirm your participation is (no longer) valid.",
  4015.             );
  4016.             $expert '';
  4017.         }
  4018.         if (null != ($request->request->get('decision'))
  4019.             && $request->request->get('decision') != ''
  4020.             && null != $participants
  4021.         ) {
  4022.             $decision $request->request->get('decision');
  4023.             $reason '';
  4024.             if ($event->getIdContact() != '') {
  4025.                 $contact $this->getExpertById($event->getIdContact());
  4026.             } else {
  4027.                 $contact $this->getExpertById($event->getCreatedBy());
  4028.             }
  4029.             $fromName ucwords($contact['fname']) . ' ' ucwords($contact['sname']);
  4030.             $participantName ucwords($expert['fname']) . ' ' ucwords($expert['sname']);
  4031.             $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  4032.             $eventUrl $baseurl $this->generateUrl('view_event', array('event' => $idEvent));
  4033.             if (null != ($request->request->get('reason'))) {
  4034.                 $reason $request->request->get('reason');
  4035.             }
  4036.             if ($decision == 1) {
  4037.                 $participants->setStatus(0);
  4038.                 if (null != ($request->request->get('arriveOn'))) {
  4039.                     $arriveOn $request->request->get('arriveOn');
  4040.                     $participants->setArriveOn(
  4041.                         new DateTime(date(
  4042.                             'd-m-Y',
  4043.                             strtotime($arriveOn)
  4044.                         )
  4045.                         )
  4046.                     );
  4047.                 }
  4048.                 if (null != ($request->request->get('leaveOn'))) {
  4049.                     $leaveOn $request->request->get('leaveOn');
  4050.                     $participants->setLeaveOn(
  4051.                         new DateTime(date(
  4052.                             'd-m-Y',
  4053.                             strtotime($leaveOn)
  4054.                         )
  4055.                         )
  4056.                     );
  4057.                 }
  4058.                 if (null != ($request->request->get('contact'))) {
  4059.                     $contactTel $request->request->get('contact');
  4060.                     $participants->setContactTel($contactTel);
  4061.                 }
  4062.                 $action 'Thank you, your acceptance has been noted and a confirmation has been sent to you by email.';
  4063.                 $subject 'Confirmation of Participation in "' $event->getTitle() . '"';
  4064.                 $msg '<p>Dear ' $expert['title'] . ' ' ucwords($expert['fname']) . ' ' ucwords($expert['sname']) . ',</p>';
  4065.                 $msg .= '<p>Thank you for confirming your participation in the event "' $event->getTitle() . '". </p>';
  4066.                 $msg .= '<p>All information on this event can be found at:</p>';
  4067.                 $msg .= '<p><a href="' $eventUrl '">' $eventUrl '</a></p>';
  4068.                 $msg .= 'Regards,<br/>';
  4069.                 $msg .= $fromName;
  4070.                 if (trim($expert['email2']) != '') {
  4071.                     $recipient $expert['email2'];
  4072.                 } else {
  4073.                     $recipient $expert['email1'];
  4074.                 }
  4075.                 if (trim($contact['email2']) != '') {
  4076.                     $sender $contact['email2'];
  4077.                 } else {
  4078.                     $sender $contact['email1'];
  4079.                 }
  4080.                 $this->sendEmail(
  4081.                     $recipient,
  4082.                     $sender,
  4083.                     $subject,
  4084.                     $msg,
  4085.                     $contact['idInd']
  4086.                 );
  4087.             } else {
  4088.                 $participants->setStatus(3);
  4089.                 $action "Thank you, your response has been noted. Thanks for replying to our invitation.";
  4090.                 $subject ucwords($participantName) . " has Declined an Event Invitation";
  4091.                 $msg "<p>Dear " ucwords($fromName) . ",</p>";
  4092.                 $msg .= "<p>This is an email to let you know that " ucwords($participantName) . ' ';
  4093.                 $msg .= "has declined an invitation to take part in '" $event->getTitle() . "'.</p>";
  4094.                 if ($reason) {
  4095.                     $msg .= "<p>They gave the following reason:</p>";
  4096.                     $msg .= "<p>'" $reason "'</p>";
  4097.                 }
  4098.                 $msg .= "<p>You can view the status of all of the participants for this event at</p>";
  4099.                 $msg .= "<p><a href='" $eventUrl "#participants'>$eventUrl#participants</a>" "</p>";
  4100.                 $msg .= "Regards,<br/>";
  4101.                 $msg .= "OceanExpert Team";
  4102.                 if (trim($contact['email2']) != '') {
  4103.                     $recipient $contact['email2'];
  4104.                 } else {
  4105.                     $recipient $contact['email1'];
  4106.                 }
  4107.                 $this->sendEmail(
  4108.                     $recipient,
  4109.                     "info@oceanexpert.org",
  4110.                     $subject,
  4111.                     $msg,
  4112.                     $contact['idInd']
  4113.                 );
  4114.             }
  4115.             $participants->setStatusChangedAt(new DateTime('now'));
  4116.             $em->persist($participants);
  4117.             $em->flush();
  4118.             $message = array(
  4119.                 'status' => 2,
  4120.                 'message' => $action,
  4121.             );
  4122.         }
  4123.         if (!isset($eventCountry)
  4124.             || !($eventCountry->getCountry())
  4125.         ) {
  4126.             $eventCountry 'not specified (yet)';
  4127.         } else {
  4128.             $eventCountry $eventCountry->getCountry();
  4129.         }
  4130.         $data = array(
  4131.             'event' => $event,
  4132.             'response' => $message,
  4133.             'expert' => $expert,
  4134.             'eventCountry' => $eventCountry,
  4135.         );
  4136.         return $this->render(
  4137.             'Event/eventConfirmation.html.twig',
  4138.             array(
  4139.                 'data' => $data
  4140.             )
  4141.         );
  4142.     }
  4143.     /**
  4144.      * @param $to
  4145.      * @param $from
  4146.      * @param $subject
  4147.      * @param $message
  4148.      * @param $idInd
  4149.      */
  4150.     private function sendEmail($to$from$subject$message$idInd)
  4151.     {
  4152.         $em $this->getDoctrine()->getManager();
  4153.         $headers "MIME-Version: 1.0\r\n";
  4154.         $headers .= "Content-type:text/html;charset=UTF-8\r\n";
  4155.         $now = new DateTime('now');
  4156.         if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  4157.             $ip $_SERVER['HTTP_CLIENT_IP'];
  4158.         } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  4159.             $ip $_SERVER['HTTP_X_FORWARDED_FOR'];
  4160.         } else {
  4161.             $ip $_SERVER['REMOTE_ADDR'];
  4162.         }
  4163.         $template $this->get('twig')
  4164.             ->render(
  4165.                 'Email/generalEventTemplate.html.twig',
  4166.                 array(
  4167.                     'message' => $message
  4168.                 )
  4169.             );
  4170.         $mail = new Mails();
  4171.         $mail->setCreateTime($now);
  4172.         $mail->setTimeToSend($now);
  4173.         $mail->setIdUser($idInd);
  4174.         $mail->setIp($ip);
  4175.         $mail->setSender($from);
  4176.         $mail->setRecipient($to);
  4177.         $mail->setHeaders($headers);
  4178.         $mail->setSubject($subject);
  4179.         $mail->setBody(
  4180.             trim(
  4181.                 preg_replace(
  4182.                     '/\s\s+/',
  4183.                     ' ',
  4184.                     $template
  4185.                 )
  4186.             )
  4187.         );
  4188.         $mail->setAttachments('');
  4189.         $mail->setTrySent(0);
  4190.         $mail->setDeleteAfterSend(1);
  4191.         $mail->setMandrilId(null);
  4192.         $em->persist($mail);
  4193.         $em->flush();
  4194.     }
  4195.     /**
  4196.      * @param $idEvent
  4197.      * @return Response
  4198.      */
  4199.     public function viewPrintableParticipantsAction($idEvent)
  4200.     {
  4201.         if ($idEvent == 0) {
  4202.             return new Response('No event id provided.');
  4203.         }
  4204.         $em $this->getDoctrine()->getManager();
  4205.         $event $em->getRepository('OceanExpertBundle:Events')->findOneBy(array('idEvent' => $idEvent));
  4206.         $data = array(
  4207.             'event' => $event,
  4208.             'participants' => $this->getPrintableParticipantsByRole($idEvent)
  4209.         );
  4210.         return $this->render('Event/printableParticipants.html.twig', array('data' => $data));
  4211.     }
  4212.     /**
  4213.      * get the participants by role for the given event
  4214.      *
  4215.      * @param int idEvent
  4216.      *
  4217.      * @return array | Response
  4218.      */
  4219.     private function getPrintableParticipantsByRole($idEvent)
  4220.     {
  4221.         $participants = array();
  4222.         $roles $this->getDoctrine()
  4223.             ->getRepository('OceanExpertBundle:EventParticipantroles')
  4224.             ->createQueryBuilder('e')
  4225.             ->select('e.role,e.idRole,e.pptOrdering')
  4226.             ->where('e.idEvent = :eventId')
  4227.             ->setParameter('eventId'$idEvent)
  4228.             ->orderBy('e.ordering''asc')
  4229.             ->getQuery()
  4230.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  4231.         $roles[] = array(
  4232.             "role" => "Other",
  4233.             "idRole" => '',
  4234.             "pptOrdering" => '',
  4235.         );
  4236.         $addressFormatRepository = new AddressFormatRepository();
  4237.         $countryRepository = new CountryRepository();
  4238.         $subdivisionRepository = new SubdivisionRepository();
  4239.         $formatter = new DefaultFormatter($addressFormatRepository$countryRepository$subdivisionRepository);
  4240.         $address = new Address();
  4241.         foreach ($roles as $role) {
  4242.             $repository $this->getDoctrine()->getManager()->createQueryBuilder();
  4243.             if ($this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
  4244.                 //let's check if the logged-in user has a 'real' profile
  4245.                 //the mandatory profile fields are all filled and the expert is active
  4246.                 $em $this->getDoctrine()->getManager();
  4247.                 $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  4248.                 if (!SecurityController::checkUserProfile($em$userId)) {
  4249.                     return $this->render(
  4250.                         'Exception/error.html.twig',
  4251.                         array(
  4252.                             'message' => 'seems your profile is not complete yet or has not been approved yet. Please complete your profile first.',
  4253.                         )
  4254.                     );
  4255.                 }
  4256.                 $repository->add(
  4257.                     'select',
  4258.                     'e.idRole, 
  4259.                     e.idInd, 
  4260.                     e.status, 
  4261.                     i.title, 
  4262.                     i.fname, 
  4263.                     i.sname, 
  4264.                     i.gender, 
  4265.                     i.useInstAddr, 
  4266.                     i.tel, 
  4267.                     i.fax, 
  4268.                     i.addr1, 
  4269.                     i.addr2, 
  4270.                     i.city, 
  4271.                     i.state, 
  4272.                     i.postcode, 
  4273.                     i.jobtitle, 
  4274.                     i.email1,
  4275.                     i.email2, 
  4276.                     c.countryCode, 
  4277.                     c.country, 
  4278.                     ins.instName, 
  4279.                     ins.instNameEng, 
  4280.                     i.dept, 
  4281.                     ins.instAddress insAddr1, 
  4282.                     ins.addr2 as instAddr2, 
  4283.                     ins.city as insCity, 
  4284.                     ins.state as insState, 
  4285.                     ins.postcode as insPostcode, 
  4286.                     ci.countryCode as insCountryCode, 
  4287.                     ci.country as insCountry, 
  4288.                     r.role,
  4289.                     r.ordering'
  4290.                 );
  4291.             } else {
  4292.                 $repository->add(
  4293.                     'select',
  4294.                     'e.idRole, 
  4295.                     e.idInd, 
  4296.                     e.status, 
  4297.                     i.title, 
  4298.                     i.fname, 
  4299.                     i.sname, 
  4300.                     i.useInstAddr, 
  4301.                     i.tel, 
  4302.                     i.fax, 
  4303.                     i.addr1, 
  4304.                     i.addr2, 
  4305.                     i.city, 
  4306.                     i.state, 
  4307.                     i.postcode, 
  4308.                     i.jobtitle, 
  4309.                     i.email1,
  4310.                     i.email2, 
  4311.                     c.countryCode, 
  4312.                     c.country, 
  4313.                     ins.instName, 
  4314.                     ins.instNameEng, 
  4315.                     i.dept, 
  4316.                     ins.instAddress as insAddr1, 
  4317.                     ins.addr2 as instAddr2, 
  4318.                     ins.city as insCity, 
  4319.                     ins.state as insState, 
  4320.                     ins.postcode as insPostcode, 
  4321.                     ci.countryCode as insCountryCode, 
  4322.                     ci.country as insCountry,
  4323.                     r.role,
  4324.                     r.ordering'
  4325.                 );
  4326.             }
  4327.             $repository->add(
  4328.                 'from',
  4329.                 'OceanExpertBundle:EventParticipants e'
  4330.             );
  4331.             $repository->leftJoin(
  4332.                 'OceanExpertBundle:Indiv',
  4333.                 'i',
  4334.                 'WITH',
  4335.                 'i.idInd = e.idInd'
  4336.             );
  4337.             $repository->leftJoin(
  4338.                 'OceanExpertBundle:IndivInstitution',
  4339.                 'ii',
  4340.                 'WITH',
  4341.                 'ii.idInd = i.idInd'
  4342.             );
  4343.             $repository->leftJoin(
  4344.                 'OceanExpertBundle:Countries',
  4345.                 'c',
  4346.                 'WITH',
  4347.                 'c.idCountry = i.countryCode'
  4348.             );
  4349.             $repository->leftJoin(
  4350.                 'OceanExpertBundle:Institutions',
  4351.                 'ins',
  4352.                 'WITH',
  4353.                 'ins.idInst = ii.idInst'
  4354.             );
  4355.             $repository->leftJoin(
  4356.                 'OceanExpertBundle:Countries',
  4357.                 'ci',
  4358.                 'WITH',
  4359.                 'ci.idCountry = ins.countryCode'
  4360.             );
  4361.             $repository->leftJoin(
  4362.                 'OceanExpertBundle:EventParticipantroles',
  4363.                 'r',
  4364.                 'WITH',
  4365.                 'e.idRole = r.idRole and e.idEvent = r.idEvent'
  4366.             );
  4367.             $repository->where('e.idEvent = :eventId');
  4368.             $repository->andWhere('e.status in (0,1)');
  4369.             if ($role['idRole'] != '') {
  4370.                 $repository->andwhere('e.idRole = :idRole');
  4371.                 $repository->setParameter('idRole'$role['idRole']);
  4372.             } else {
  4373.                 $repository->andwhere('e.idRole is NULL');
  4374.             }
  4375.             $repository->setParameter('eventId'$idEvent);
  4376.             $repository->addOrderBy('i.sname''asc');
  4377.             $participantsResult $repository->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY);
  4378.             foreach ($participantsResult as $key => $participant) {
  4379.                 if ($participant['useInstAddr'] == 1) {
  4380.                     $address $address
  4381.                         ->withCountryCode($participant['insCountryCode'])
  4382.                         ->withAdministrativeArea($participant['insState'])
  4383.                         ->withLocality($participant['insCity'])
  4384.                         ->withPostalCode($participant['insPostcode'])
  4385.                         ->withAddressLine1($participant['insAddr1'])
  4386.                         ->withAddressLine2($participant['instAddr2']);
  4387.                 } else {
  4388.                     $address $address
  4389.                         ->withCountryCode($participant['countryCode'])
  4390.                         ->withAdministrativeArea($participant['state'])
  4391.                         ->withLocality($participant['city'])
  4392.                         ->withPostalCode($participant['postcode'])
  4393.                         ->withAddressLine1($participant['addr1'])
  4394.                         ->withAddressLine2($participant['addr2']);
  4395.                 }
  4396.                 try {
  4397.                     $participantsResult[$key]['address'] = $formatter->format($address);
  4398.                 } catch (ErrorException $e) {
  4399.                     $participantsResult[$key]['address'] = 'incorrect address';
  4400.                 }
  4401.             }
  4402.             if (count($participantsResult) > 0) {
  4403.                 $participants[$role['role']] = $participantsResult;
  4404.             }
  4405.         }
  4406.         //remove as much as possible all stupid chars that people enter in their profiles....
  4407.         //or stuff we put there....
  4408.         //put all in one string
  4409.         $participants json_encode($participants);
  4410.         //these lines may look weird
  4411.         // but this is the only way to get rid of ALL strange things people put in their profiles
  4412.         $participants preg_replace('/&amp;/s''and '$participants);
  4413.         $participants preg_replace('/amp;/s'''$participants);
  4414.         $participants preg_replace('/&quot;/s''and '$participants);
  4415.         $participants preg_replace('/quot;/s'''$participants);
  4416.         $participants preg_replace('/&/s''and '$participants);
  4417.         $participants preg_replace('/\\\"/s'''$participants);
  4418.         $participants html_entity_decode($participants);
  4419.         $participants htmlspecialchars_decode($participants);
  4420.         $participants preg_replace('/<br\s?\/?>/s'' '$participants);
  4421.         $participants preg_replace('/<\/?p>/s'' '$participants);
  4422.         $participants strip_tags($participants);
  4423.         $participants preg_replace('/\\\\r\\\\n/s'' '$participants);
  4424.         $participants preg_replace('/\n/'''$participants);
  4425.         $participants preg_replace('/\\\\n/'''$participants);
  4426.         $participants preg_replace('/\s+/'' '$participants);
  4427.         //make it an associative array again
  4428.         return json_decode($participantstrue);
  4429.     }
  4430.     /**
  4431.      * @param $idEvent
  4432.      * @return Response
  4433.      */
  4434.     public function viewAgendaPrintableAction($idEvent)
  4435.     {
  4436.         $data $this->getPrintableAgendaData($idEvent);
  4437.         return $this->render(
  4438.             'Event/agendaPrintable.html.twig',
  4439.             array(
  4440.                 'data' => $data
  4441.             )
  4442.         );
  4443.     }
  4444.     /**
  4445.      * get the agenda items for this event
  4446.      * sort the data by agendaOrder in a natural order
  4447.      *
  4448.      * @param int $idEvent
  4449.      *
  4450.      * @return array|Response
  4451.      */
  4452.     private function getPrintableAgendaData(int $idEvent)
  4453.     {
  4454.         if ($idEvent == 0) {
  4455.             return new Response('No event id provided.');
  4456.         }
  4457.         $em $this->getDoctrine()->getManager();
  4458.         $event $em->getRepository('OceanExpertBundle:Events')
  4459.             ->findOneBy(
  4460.                 array(
  4461.                     'idEvent' => $idEvent
  4462.                 )
  4463.             );
  4464.         $country $em->getRepository('OceanExpertBundle:Countries')
  4465.             ->findOneBy(
  4466.                 array(
  4467.                     'idCountry' => $event->getIdCountry()
  4468.                 )
  4469.             );
  4470.         $tmpAgendaItems $this->getDoctrine()
  4471.             ->getRepository('OceanExpertBundle:EventAgendaitems')
  4472.             ->createQueryBuilder('e')
  4473.             ->select('e.idAgendaitem, e.id, e.title, e.parentIdevent, e.agendaOrder, e.notes, e.requiredActions')
  4474.             ->where('e.idEvent = :eventId')
  4475.             ->setParameter(':eventId'$event)
  4476.             ->addOrderBy('e.idAgendaitem + 0''ASC')
  4477.             ->addOrderBy('e.agendaOrder''ASC')
  4478.             ->getQuery()
  4479.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  4480.         foreach ($tmpAgendaItems as $key => $tmpAgendaItem) {
  4481.             $agendaItems[$tmpAgendaItem['idAgendaitem']] = $tmpAgendaItem;
  4482.         }
  4483.         ksort($agendaItemsSORT_NATURAL);
  4484.         $addressFormatRepository = new AddressFormatRepository();
  4485.         $countryRepository = new CountryRepository();
  4486.         $subdivisionRepository = new SubdivisionRepository();
  4487.         $formatter = new DefaultFormatter(
  4488.             $addressFormatRepository,
  4489.             $countryRepository,
  4490.             $subdivisionRepository
  4491.         );
  4492.         if (isset($country)) {
  4493.             $address = new Address();
  4494.             $address $address
  4495.                 ->withCountryCode($country->getCountryCode())
  4496.                 ->withAdministrativeArea($event->getState())
  4497.                 ->withLocality($event->getCity())
  4498.                 ->withPostalCode($event->getPostcode())
  4499.                 ->withAddressLine1($event->getAddress());
  4500.             $address $formatter->format($address);
  4501.         } else {
  4502.             $address 'no location specified or online event';
  4503.         }
  4504.         return array(
  4505.             'event' => $event,
  4506.             'address' => $address,
  4507.             'agenda' => $agendaItems
  4508.         );
  4509.     }
  4510.     /**
  4511.      * @param $idEvent
  4512.      * @return Response
  4513.      */
  4514.     public function viewAnnonatedAgendaPrintableAction($idEvent)
  4515.     {
  4516.         $data $this->getPrintableAgendaData($idEvent);
  4517.         return $this->render('Event/agendaAnnonated.html.twig', array('data' => $data));
  4518.     }
  4519.     /**
  4520.      * @return JsonResponse
  4521.      */
  4522.     public function updateEventStatusAction(Request $request)
  4523.     {
  4524.         $idEvent $request->request->get('idEvent');
  4525.         $eventStatus $request->request->get('eventStatus');
  4526.         $status 0;
  4527.         $message '';
  4528.         if ($this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')) {
  4529.             $author $this->get('security.token_storage')->getToken()->getUser();
  4530.             $em $this->getDoctrine()->getManager();
  4531.             $event $em->getRepository('OceanExpertBundle:Events')
  4532.                 ->findOneBy(
  4533.                     array(
  4534.                         'idEvent' => $idEvent
  4535.                     )
  4536.                 );
  4537.             if (isset($eventStatus)
  4538.                 && $eventStatus != ''
  4539.             ) {
  4540.                 $event->setStatus($eventStatus);
  4541.                 $event->setLastEditBy($author->getId());
  4542.                 $event->setUpdatedAt(new DateTime("now"));
  4543.                 $em->persist($event);
  4544.                 $em->flush();
  4545.                 $status 1;
  4546.             }
  4547.         } else {
  4548.             $status 0;
  4549.             $message 'you have no rights to do this';
  4550.         }
  4551.         return new JsonResponse(
  4552.             array(
  4553.                 'status' => $status,
  4554.                 'eStatus' => $eventStatus,
  4555.                 'message' => $message
  4556.             )
  4557.         );
  4558.     }
  4559.     /**
  4560.      * @param $idEvent
  4561.      * @param $idFile
  4562.      * @param Request $request
  4563.      *
  4564.      * @return Response|RedirectResponse
  4565.      */
  4566.     public function importParticipantsAction($idEvent$idFileRequest $request)
  4567.     {
  4568.         $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  4569.         $eventDetails $this->getDoctrine()->getRepository('OceanExpertBundle:Events')->findOneBy(array('idEvent' => $idEvent));
  4570.         $eventParticipants = array();
  4571.         $idfile '';
  4572.         $importFile = array();
  4573.         $error '';
  4574.         $em $this->getDoctrine()->getManager();
  4575.         if ($idFile != 0) {
  4576.             $eventParticipants $this->getImportdata($idFile);
  4577.             $importFile $em->getRepository('OceanExpertBundle:EventParticipantImportFiles')->findOneBy(array('id' => $idFile'idEvent' => $idEvent));
  4578.             if (!$importFile) {
  4579.                 return $this->redirect($this->generateUrl('import_event_participants', array('idEvent' => $idEvent)));
  4580.             }
  4581.         }
  4582.         if ($request->isMethod('POST')) {
  4583.             $file $request->files->get('importfile');
  4584.             $filedata = array(
  4585.                 'id' => $idEvent,
  4586.                 'file' => $file,
  4587.                 'filename' => $file->getClientOriginalName(),
  4588.                 'type' => 'image',
  4589.                 'path' => "uploads/events/" $idEvent "/uploadParticipantsFiles/",
  4590.                 'user' => $userId,
  4591.                 'dbEntry' => 0
  4592.             );
  4593.             if ($file->getClientSize() > 33554432) {
  4594.                 $error "File size is too big (> 32MB)";
  4595.             }
  4596.             $mimetypes = array(
  4597.                 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  4598.                 'application/vnd.ms-excel'
  4599.             );
  4600.             if (!in_array($file->getClientMimetype(), $mimetypes)) {
  4601.                 $error "Not an excel file. Please save the file in microsoft Excel format and upload again.";
  4602.             }
  4603.             $file $filedata['path'] . $filedata['filename'];
  4604.             $types = array('Xlsx''Xls');
  4605.             if ($error == '') {
  4606.                 $this->uploadFile($filedata);
  4607.                 $importFile = new EventParticipantImportFiles();
  4608.                 $importFile->setIdEvent($idEvent);
  4609.                 $importFile->setFilename($filedata['filename']);
  4610.                 $importFile->setPath($filedata['path']);
  4611.                 $importFile->setUploadedBy($userId);
  4612.                 $importFile->setUploadedAt(new DateTime());
  4613.                 $em->persist($importFile);
  4614.                 $em->flush();
  4615.                 $idfile $importFile->getId();
  4616.                 foreach ($types as $type) {
  4617.                     try {
  4618.                         $reader IOFactory::createReader($type);
  4619.                     } catch (Exception $e) {
  4620.                         $error "File cannot be read.";
  4621.                     }
  4622.                     if ($reader->canRead($file)) {
  4623.                         break;
  4624.                     } else {
  4625.                         $error "Not an excel file. Please save the file in microsoft Excel format and upload again.";
  4626.                     }
  4627.                 }
  4628.                 if (file_exists($file)) {
  4629.                     $objPHPExcel IOFactory::load($file);
  4630.                     foreach ($objPHPExcel->getWorksheetIterator() as $sheet) {
  4631.                         $highestRow $sheet->getHighestRow();
  4632.                         $highestColumn $sheet->getHighestColumn();
  4633.                         if ($highestRow 1) {
  4634.                             if ($highestColumn == "W") {
  4635.                                 $headings $sheet->rangeToArray('A2:' $highestColumn 2,
  4636.                                     NULL,
  4637.                                     TRUE,
  4638.                                     FALSE)[0];
  4639.                                 for ($row 2$row <= $highestRow$row++) {
  4640.                                     $rowData $sheet->rangeToArray('A' $row ':' $highestColumn $row,
  4641.                                         NULL,
  4642.                                         TRUE,
  4643.                                         FALSE)[0];
  4644.                                     $eventParticipant = new EventParticipantUpload();
  4645.                                     $eventParticipant->setEmail($rowData[0]);
  4646.                                     $eventParticipant->setFname($this->makeTitle($rowData[1]));
  4647.                                     $eventParticipant->setSname(strtoupper($rowData[2]));
  4648.                                     $eventParticipant->setGender($this->makeTitle($rowData[3]));
  4649.                                     $eventParticipant->setNationality($this->makeTitle($rowData[4]));
  4650.                                     $eventParticipant->setPhone($rowData[5]);
  4651.                                     $eventParticipant->setAddressLine1($this->makeTitle($rowData[6]));
  4652.                                     $eventParticipant->setAddressLine2($this->makeTitle($rowData[7]));
  4653.                                     $eventParticipant->setCity($this->makeTitle($rowData[8]));
  4654.                                     $eventParticipant->setState($this->makeTitle($rowData[9]));
  4655.                                     $eventParticipant->setZipCode($rowData[10]);
  4656.                                     $eventParticipant->setCountry($this->makeTitle($rowData[11]));
  4657.                                     $eventParticipant->setInstitution($this->makeTitle($rowData[12]));
  4658.                                     $eventParticipant->setInstAddrLine1($this->makeTitle($rowData[13]));
  4659.                                     $eventParticipant->setInstAddrLine2($this->makeTitle($rowData[14]));
  4660.                                     $eventParticipant->setInstCity($this->makeTitle($rowData[15]));
  4661.                                     $eventParticipant->setInstState($this->makeTitle($rowData[16]));
  4662.                                     $eventParticipant->setInstZipCode($rowData[17]);
  4663.                                     $eventParticipant->setInstCountry($this->makeTitle($rowData[18]));
  4664.                                     $eventParticipant->setInstEmail($rowData[19]);
  4665.                                     $eventParticipant->setJobTitle(ucfirst($rowData[20]));
  4666.                                     $eventParticipant->setJobDescription(ucfirst($rowData[21]));
  4667.                                     $eventParticipant->setoceanexpertProfile($rowData[22]);
  4668.                                     $eventParticipant->setIdFile($idfile);
  4669.                                     $em->persist($eventParticipant);
  4670.                                     $em->flush();
  4671.                                 }
  4672.                             } else {
  4673.                                 $error "Invalid column count in the file. The number of columns should be 23.";
  4674.                                 unlink($file);
  4675.                                 if ($importFile) {
  4676.                                     $em->remove($importFile);
  4677.                                     $em->flush();
  4678.                                 }
  4679.                             }
  4680.                         } else {
  4681.                             $error "No import data present in the file.";
  4682.                             unlink($file);
  4683.                             if ($importFile) {
  4684.                                 $em->remove($importFile);
  4685.                                 $em->flush();
  4686.                             }
  4687.                         }
  4688.                     }
  4689.                 }
  4690.                 if ($error == '') {
  4691.                     return $this->redirect(
  4692.                         $this->generateUrl(
  4693.                             'import_event_participants',
  4694.                             array(
  4695.                                 'idEvent' => $idEvent,
  4696.                                 'idFile' => $idfile
  4697.                             )
  4698.                         )
  4699.                     );
  4700.                 } else {
  4701.                     return $this->render(
  4702.                         'Event/importEventParticipants.html.twig',
  4703.                         array(
  4704.                             'data' => array(
  4705.                                 'event' => $eventDetails,
  4706.                                 'participants' => $eventParticipants,
  4707.                                 'idFile' => $idfile,
  4708.                                 'importFile' => $importFile,
  4709.                                 'fileerror' => $error
  4710.                             )
  4711.                         )
  4712.                     );
  4713.                 }
  4714.             }
  4715.         }
  4716.         return $this->render(
  4717.             'Event/importEventParticipants.html.twig',
  4718.             array(
  4719.                 'data' => array(
  4720.                     'event' => $eventDetails,
  4721.                     'participants' => $eventParticipants,
  4722.                     'idFile' => $idfile,
  4723.                     'importFile' => $importFile,
  4724.                     'fileerror' => $error
  4725.                 )
  4726.             )
  4727.         );
  4728.     }
  4729.     /**
  4730.      * @param $idFile
  4731.      *
  4732.      * @return object[]
  4733.      */
  4734.     function getImportdata($idFile)
  4735.     {
  4736.         $em $this->getDoctrine()->getManager();
  4737.         $expertdata $em->getRepository('OceanExpertBundle:EventParticipantUpload')->findBy(array('idFile' => $idFile));
  4738.         foreach ($expertdata as $expert) {
  4739.             $expertdetails $em->getRepository('OceanExpertBundle:Indiv')->findOneBy(array('email1' => $expert->getEmail()));
  4740.             if ($expertdetails) {
  4741.                 $expert->setStatus(1);
  4742.                 $expert->setIdProfile($expertdetails->getIdInd());
  4743.                 $expert->setOceanexpertProfile($this->generateUrl('view_profile', array('user' => $expertdetails->getIdInd())));
  4744.                 $em->persist($expert);
  4745.                 $em->flush();
  4746.             } else {
  4747.                 if ($expert->getoceanexpertProfile() != '') {
  4748.                     if (strpos($expert->getoceanexpertProfile(), '/expert/') !== false) {
  4749.                         $urlparts explode('/'$expert->getoceanexpertProfile());
  4750.                         $expertpart end($urlparts);
  4751.                     } elseif (strpos($expert->getoceanexpertProfile(), 'viewMemberRecord') !== false) {
  4752.                         $urlparts explode('='$expert->getoceanexpertProfile());
  4753.                         $expertpart end($urlparts);
  4754.                     }
  4755.                     if (isset($expertpart)
  4756.                         && is_numeric($expertpart)
  4757.                     ) {
  4758.                         $user $this->fosUserManager->findUserBy(array('id' => $expertpart));
  4759.                         if ($user) {
  4760.                             $expertdetails $em->getRepository('OceanExpertBundle:Indiv')->findOneBy(array('idInd' => $user->getId()));
  4761.                         } else {
  4762.                             $expert->setStatus(0);
  4763.                             $expert->setIdProfile('');
  4764.                         }
  4765.                     } else {
  4766.                         $expertdetails $em->getRepository('OceanExpertBundle:Indiv')->findOneBy(array('idInd' => $expertpart));
  4767.                     }
  4768.                     if ($expertdetails) {
  4769.                         $expert->setStatus(1);
  4770.                         $expert->setIdProfile($expertdetails->getIdInd());
  4771.                         $expert->setOceanexpertProfile($this->generateUrl('view_profile', array('user' => $expertdetails->getIdInd())));
  4772.                     } else {
  4773.                         $expert->setStatus(0);
  4774.                         $expert->setIdProfile('');
  4775.                     }
  4776.                 } else {
  4777.                     $expert->setStatus(0);
  4778.                     $expert->setIdProfile('');
  4779.                 }
  4780.                 $em->persist($expert);
  4781.                 $em->flush();
  4782.             }
  4783.         }
  4784.         return $expertdata;
  4785.     }
  4786.     /**
  4787.      * @param $title
  4788.      * @return string
  4789.      */
  4790.     function makeTitle($title)
  4791.     {
  4792.         $str ucwords(strtolower($title));
  4793.         $exclude 'a,an,the,for,and,nor,but,or,yet,so,such,as,at,around,by,after,along,for,from,of,on,to,with,without';
  4794.         $excluded explode(","$exclude);
  4795.         foreach ($excluded as $noCap) {
  4796.             $str str_replace(ucwords($noCap), strtolower($noCap), $str);
  4797.         }
  4798.         return ucfirst($str);
  4799.     }
  4800.     /**
  4801.      * change some info about the participant
  4802.      * TBC this is only used when importing event participants
  4803.      *
  4804.      * @param $idEvent
  4805.      * @param $idUser
  4806.      * @param Request $request
  4807.      * @return Response
  4808.      */
  4809.     public function editParticipantAction($idEvent$idUserRequest $request)
  4810.     {
  4811.         $em $this->getDoctrine()->getManager();
  4812.         $countries $this->getDoctrine()
  4813.             ->getRepository('OceanExpertBundle:Countries')
  4814.             ->createQueryBuilder('c')
  4815.             ->select('c.idCountry, c.country,c.countryCode')
  4816.             ->orderBy('c.country''asc')
  4817.             ->getQuery()
  4818.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  4819.         foreach ($countries as $country) {
  4820.             $countryArr[$country['idCountry']] = $country['country'];
  4821.         }
  4822.         $userInstitute = array();
  4823.         $instituteSeaRegion $this->getDoctrine()->getRepository('OceanExpertBundle:Regions')->seaRegions();
  4824.         $user $em->getRepository('OceanExpertBundle:EventParticipantUpload')->findOneBy(array('id' => $idUser));
  4825.         if ($user) {
  4826.             $user->setCountry(array_search($user->getCountry(), $countryArr));
  4827.             $user->setNationality(array_search($user->getNationality(), $countryArr));
  4828.             $user->setInstCountry(array_search($user->getInstCountry(), $countryArr));
  4829.             $connection $em->getConnection();
  4830.             $statement $connection->prepare("SELECT i.id_inst, i.inst_name, i.inst_address, i.city, i.state, i.postcode FROM institutions i
  4831.                 WHERE ( i.inst_name = :instName
  4832.                     AND i.country_code = :country
  4833.                     AND i.activated = 1
  4834.                     )
  4835.                 ");
  4836.             $statement->bindValue('instName'$user->getInstitution());
  4837.             $statement->bindValue('country'$user->getInstCountry());
  4838.             $statement->execute();
  4839.             $userInstitute $statement->fetchAll();
  4840.             if (count($userInstitute) == 0) {
  4841.                 $connection $em->getConnection();
  4842.                 $statement $connection->prepare("SELECT i.id_inst, i.inst_name, i.inst_address, i.city, i.state, i.postcode FROM institutions i
  4843.                 WHERE ( MATCH (i.inst_name) AGAINST (:instName)
  4844.                     AND i.country_code = :country
  4845.                     AND i.activated = 1
  4846.                     )
  4847.                 ");
  4848.                 $statement->bindValue('instName'$user->getInstitution());
  4849.                 $statement->bindValue('country'$user->getInstCountry());
  4850.                 $statement->execute();
  4851.                 $userInstitute $statement->fetchAll();
  4852.             }
  4853.         }
  4854.         return $this->render(
  4855.             'Event/editParticipants.html.twig',
  4856.             array(
  4857.                 'data' => array(
  4858.                     'user' => $user,
  4859.                     'idUser' => $idUser,
  4860.                     'idEvent' => $idEvent,
  4861.                     'userInstitute' => $userInstitute,
  4862.                     'countries' => $countries,
  4863.                     'availableSeaRegions' => $instituteSeaRegion
  4864.                 )
  4865.             )
  4866.         );
  4867.     }
  4868.     /**
  4869.      * @param $code
  4870.      * @return string
  4871.      */
  4872.     public function getCountryByCode($code)
  4873.     {
  4874.         $em $this->getDoctrine()->getManager();
  4875.         $product $em->getRepository('OceanExpertBundle:Countries')->find($code);
  4876.         return $product->getCountry();
  4877.     }
  4878.     /**
  4879.      * @param Request $request
  4880.      * @return Response
  4881.      */
  4882.     public function deleteImportParticipantAction(Request $request)
  4883.     {
  4884.         $idParticipant $request->request->get('idParticipant');
  4885.         $em $this->getDoctrine()->getManager();
  4886.         $participant $em->getRepository('OceanExpertBundle:EventParticipantUpload')->findOneBy(array('id' => $idParticipant));
  4887.         if ($participant) {
  4888.             $em->remove($participant);
  4889.             $em->flush();
  4890.             return new Response(1);
  4891.         } else {
  4892.             return new Response(0);
  4893.         }
  4894.     }
  4895.     /**
  4896.      * @param Request $request
  4897.      * @return Response
  4898.      */
  4899.     public function checkParticipantsDetailAction(Request $request)
  4900.     {
  4901.         $idFile $request->request->get('idfile');
  4902.         $invalidProfile $this->checkParticipants($idFile);
  4903.         if (count($invalidProfile) > 0) {
  4904.             $data = array(
  4905.                 'status' => 0,
  4906.                 'participants' => $invalidProfile
  4907.             );
  4908.         } else {
  4909.             $data = array(
  4910.                 'status' => 1,
  4911.                 'participants' => array()
  4912.             );
  4913.         }
  4914.         return new JsonResponse($data);
  4915.     }
  4916.     /**
  4917.      * @param $idFile
  4918.      * @return mixed
  4919.      */
  4920.     function checkParticipants($idFile)
  4921.     {
  4922.         $em $this->getDoctrine()->getManager();
  4923.         $invalidProfile $em->getRepository('OceanExpertBundle:EventParticipantUpload')
  4924.             ->createQueryBuilder('ep')
  4925.             ->select('ep.idProfile, ep.fname, ep.sname, ep.email')
  4926.             ->where('ep.idProfile is NULL')
  4927.             ->orWhere('ep.idProfile = 0')
  4928.             ->andWhere('ep.idFile = :idfile')
  4929.             ->setParameter('idfile'$idFile)
  4930.             ->orderBy('ep.sname''ASC')
  4931.             ->getQuery()
  4932.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  4933.         return $invalidProfile;
  4934.     }
  4935.     /**
  4936.      * Function to check and add event participant
  4937.      * @param Request $request
  4938.      * @return Response
  4939.      */
  4940.     public function addEventParticipantsAction(Request $request)
  4941.     {
  4942.         $idFile $request->request->get('idfile');
  4943.         $idEvent $request->request->get('idevent');
  4944.         $em $this->getDoctrine()->getManager();
  4945.         $invalidProfile $this->checkParticipants($idFile);
  4946.         if (count($invalidProfile) == 0) {
  4947.             $participants $em->getRepository('OceanExpertBundle:EventParticipantUpload')
  4948.                 ->createQueryBuilder('ep')
  4949.                 ->select('ep.idProfile,ep.phone')
  4950.                 ->where('ep.idProfile is not NULL')
  4951.                 ->orWhere('ep.idProfile != 0')
  4952.                 ->andWhere('ep.idFile = :idfile')
  4953.                 ->setParameter('idfile'$idFile)
  4954.                 ->getQuery()
  4955.                 ->getResult(AbstractQuery::HYDRATE_ARRAY);
  4956.             foreach ($participants as $participant) {
  4957.                 $eventparticipant $em->getRepository('OceanExpertBundle:EventParticipants')
  4958.                     ->findOneBy(
  4959.                         array(
  4960.                             'idInd' => $participant['idProfile'],
  4961.                             'idEvent' => $idEvent
  4962.                         )
  4963.                     );
  4964.                 if (!$eventparticipant) {
  4965.                     $eventparticipant = new EventParticipants();
  4966.                     $eventparticipant->setStatus(1);
  4967.                     $eventparticipant->setIdEvent($idEvent);
  4968.                     $eventparticipant->setIdInd($participant['idProfile']);
  4969.                     $eventparticipant->setContactTel($participant['phone']);
  4970.                     $em->persist($eventparticipant);
  4971.                     $em->flush();
  4972.                 }
  4973.                 $removeParticipantData $em->getRepository("OceanExpertBundle:EventParticipantUpload")
  4974.                     ->findOneBy(
  4975.                         array(
  4976.                             'idProfile' => $eventparticipant->getIdInd()
  4977.                         )
  4978.                     );
  4979.                 $em->remove($removeParticipantData);
  4980.                 $em->flush();
  4981.                 $importfile $em->getRepository('OceanExpertBundle:EventParticipantImportFiles')
  4982.                     ->findOneBy(
  4983.                         array(
  4984.                             'id' => $idFile
  4985.                         )
  4986.                     );
  4987.                 if ($importfile) {
  4988.                     unlink($importfile->getPath() . $importfile->getFilename());
  4989.                     $em->remove($importfile);
  4990.                     $em->flush();
  4991.                 }
  4992.             }
  4993.         }
  4994.         return new Response(1);
  4995.     }
  4996.     /**
  4997.      * create ICS file to download
  4998.      * this file should contain all info about an event
  4999.      *
  5000.      * @param int $idEvent id of the event
  5001.      *
  5002.      * @return Response
  5003.      */
  5004.     public function getIcsAction($idEvent)
  5005.     {
  5006.         //https://calendar.google.com/calendar/render?action=TEMPLATE&dates=20220112T000000Z%2F20220112T235959Z&details=thisAreTheDetails&location=&text=thisIsTheTitle
  5007.         $em $this->getDoctrine()->getManager();
  5008.         $event $em->getRepository('OceanExpertBundle:Events')
  5009.             ->findOneBy(
  5010.                 array(
  5011.                     'idEvent' => $idEvent
  5012.                 )
  5013.             );
  5014.         /*
  5015.         Convert times to iCalendar format.
  5016.         They require a block for 'Ymd' and then another block for the time, with format 'his'.
  5017.         Both of those blocks are separated by a "T".
  5018.         The Z is declared at the end for UTC time, but shouldn't be included in the date conversion.
  5019.         */
  5020.         //we cannot set a time in OE for events
  5021.         //$event->setStartOn($event->getStartOn()->format('Ymd\This') . 'Z');
  5022.         //$event->setEndOn($event->getEndOn()->format('Ymd\This') . 'Z');
  5023.         $event->setStartOn($event->getStartOn()->format('Ymd\T') . '000001Z');
  5024.         $event->setEndOn($event->getEndOn()->format('Ymd\This') . '235959Z');
  5025.         //get rid of any formatting we may have added
  5026.         $event->setTitle(strip_tags($event->getEventTitle()));
  5027.         $event->setShortTitle(strip_tags($event->getEventShortTitle()));
  5028.         $response $this->render(
  5029.             'Event/getIcs.html.twig',
  5030.             array(
  5031.                 'url' => $this->generateUrl(
  5032.                     'view_event',
  5033.                     array(
  5034.                         'event' => $event->getIdEvent()
  5035.                     ),
  5036.                     UrlGenerator::ABSOLUTE_URL
  5037.                 ),
  5038.                 'event' => $event
  5039.             )
  5040.         );
  5041.         $response->headers->set(
  5042.             'Content-Type',
  5043.             'text/calendar; charset=utf-8'
  5044.         );
  5045.         $response->headers->set(
  5046.             'Content-Disposition',
  5047.             'attachment; filename="OceanExpertEvent_' $idEvent '_' date('Ymd') . '.ics"'
  5048.         );
  5049.         return $response;
  5050.     }
  5051. }