/**
  * @Security("has_role('ROLE_USER')") //TODO: apply correct role
  * @Route("/vacature/nieuw", name="create_vacancy")
  * @Route("/{organisation_urlid}/vacature/nieuw", name="create_vacancy_for_organisation")
  */
 public function createVacancyAction(Request $request, $organisation_urlid = null)
 {
     $t = $this->get('translator');
     $em = $this->getDoctrine()->getManager();
     if ($organisation_urlid) {
         $user = $this->getUser();
         $organisation = $em->getRepository("AppBundle:Organisation")->findOneByUrlid($organisation_urlid);
         if (!$user->getOrganisations()->contains($organisation)) {
             throw $this->createAccessDeniedException($t->trans('vacancy.create.noAdmin'));
         }
     }
     $vacancy = new Vacancy();
     $vacancy->setStartdate(new \DateTime("today"))->setEnddate(new \DateTime("today"));
     $form = $this->createForm(VacancyType::class, $vacancy);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $vacancy = $form->getData();
         if (!is_null($organisation_urlid)) {
             $vacancy->setOrganisation($organisation);
         }
         if ($form->get('save')->isClicked()) {
             $vacancy->setPublished(Vacancy::SAVED);
         }
         $this->setCoordinates($vacancy);
         $vacancy->setCreator($this->getUser());
         $em->persist($vacancy);
         $em->flush();
         //set a success message
         $this->addFlash('approve_message', $t->trans('vacancy.flash.createStart') . ' ' . $vacancy->getTitle() . ' ' . $t->trans('vacancy.flash.createEnd'));
         if (!is_null($organisation_urlid)) {
             //set digest / send email to all administrators
             $info = array('subject' => $t->trans('vacancy.mail.create'), 'template' => 'vacature_aangemaakt.html.twig', 'txt/plain' => 'vacature_aangemaakt.txt.twig', 'to' => $user->getEmail(), 'data' => array('user' => $user, 'vacancy' => $vacancy, 'org' => $organisation), 'event' => DigestEntry::NEWVACANCY);
             $this->digestAndMail($info, $organisation);
         }
         return $this->redirect($this->generateUrl("vacancy_by_urlid", ["urlid" => $vacancy->getUrlId()]));
     } else {
         if ($form->isSubmitted() && !$form->isValid()) {
             //set an error message
             $this->addFlash('error', $t->trans('general.flash.formError'));
         }
     }
     return $this->render("vacancy/vacature_nieuw.html.twig", ["form" => $form->createView(), "createForm" => true]);
 }
 /**
  * Create a list of all notifications
  * @param Person $user a user
  * @param Organisation $organisation an organisation
  * @param Vacancy $vacancy a vacancy
  * @return Response
  */
 public function listNotificationsAction($user, $organisation = null, $vacancy = null)
 {
     $digestNotifications = [];
     $qb = $this->getDoctrine()->getManager()->createQueryBuilder();
     $qb->select(array('dE'))->from('AppBundle:DigestEntry', 'dE')->where($qb->expr()->andX($qb->expr()->eq('dE.handled', 0), $qb->expr()->eq('dE.user', $user->getId()), $qb->expr()->neq('dE.event', 1)));
     if ($organisation) {
         $qb->where('dE.Organisation = :organisation')->setParameter('organisation', $organisation->getId());
     }
     if ($vacancy) {
         $qb->where('dE.vacancy = :vacancy')->setParameter('vacancy', $vacancy->getId());
     }
     $qb->add('orderBy', 'dE.id DESC');
     $digests = $qb->getQuery()->getResult();
     foreach ($digests as $digest) {
         $textAndActionLink = $this->getTextAndActionLinkForEvent($digest);
         array_push($digestNotifications, $textAndActionLink);
     }
     return $this->render("person/persoon_notificaties.html.twig", ["notifications" => $digestNotifications]);
 }