/**
  * @Route("/admin/user/invite")
  * @Method("POST")
  */
 public function inviteUserAction(Request $request)
 {
     $this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
     $response = new Response();
     $data = $request->request->all();
     $em = $this->getDoctrine()->getManager();
     $checkForExisting = $em->getRepository('AppBundle:Invitation')->findOneByEmail($data['email']);
     if ($checkForExisting !== null) {
         throw new \Exception('invitation.exists');
     }
     $user = $this->get('fos_user.user_manager')->findUserBy(['email' => $data['email']]);
     if ($user !== null) {
         throw new \Exception('user.exists');
     }
     $form = $this->createForm(InvitationType::class, null, []);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $invitation = new Invitation();
         $invitation->setEmail($data['email']);
         $invitation->send();
         $em->persist($invitation);
         $em->flush();
         $response->setStatusCode(204);
         return $response;
     }
     return $form;
 }
 /**
  * @Route("/budget/{budget_id}/invitation/new", name="sb_invitation_new", requirements={"budget_id": "\d+"})
  */
 public function newAction(Request $request, $budget_id)
 {
     $budget = $this->get('app.checker')->budget($this->getUser(), $budget_id, false);
     $invitation = new Invitation();
     $invitation->setBudget($budget);
     $form = $this->createForm(InvitationType::class, $invitation);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $invitation->setUser($this->getUser());
         $action = Action::newInvitation($budget, $this->getUser(), $invitation->toArray());
         $em = $this->getDoctrine()->getManager();
         $em->persist($invitation);
         $em->persist($action);
         $em->flush();
         $this->addFlash('notice', $this->get('translator')->trans('invitation.createsuccessful'));
         return $this->redirectToRoute('sb_budget_show', array('id' => $budget->getId()));
     }
     return $this->render('invitation/form.html.twig', array('form' => $form->createView(), 'budget' => $budget));
 }
 /**
  * @Route("/budget/new", name="sb_budget_new")
  */
 public function newAction(Request $request)
 {
     $budget = new Budget();
     $form = $this->createForm(BudgetType::class, $budget);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $budget->setUser($this->getUser());
         $invitation = new Invitation();
         $invitation->setBudget($budget);
         $invitation->setUser($budget->getUser());
         $invitation->setTarget($budget->getUser());
         $invitation->setStatus('manager');
         $action = Action::newBudget($budget, $this->getUser(), $budget->toArray());
         $em = $this->getDoctrine()->getManager();
         $em->persist($budget);
         $em->persist($invitation);
         $em->persist($action);
         $em->flush();
         $this->addFlash('notice', $this->get('translator')->trans('budget.createsuccessful'));
         return $this->redirectToRoute('sb_budgets');
     }
     return $this->render('budget/form.html.twig', array('form' => $form->createView()));
 }
 /**
  * Turn this item object into a generic array
  *
  * @return array
  */
 public function transform(Invitation $invitation)
 {
     return ['id' => (int) $invitation->getId(), 'code' => $invitation->getCode(), 'isSent' => $invitation->isSent(), 'email' => $invitation->getEmail()];
 }