/**
  * @Route("/new", name="doctor_new")
  * @Method({"GET", "POST"})
  */
 public function newAction(Request $request)
 {
     $user = new User();
     $form = $this->createForm(UserType::class, $user);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $this->get('app_bundle.user_manager')->setUserPassword($user, $user->getPassword());
         //add role based on value off checkbox
         $user->setRoles(array('ROLE_DOCTOR'));
         $image = new ProfileImage();
         $image->setPath('defaultProfileImage.png');
         $em->persist($image);
         $em->flush();
         $user->setImage($image);
         $em->persist($user);
         $em->flush();
         $doctor = new Doctor();
         $doctor->setUser($user);
         $em->persist($doctor);
         $em->flush();
         return $this->redirectToRoute('doctor_show', array('id' => $doctor->getId()));
     }
     return $this->render('AppBundle:user:new.html.twig', array('user' => $user, 'form' => $form->createView()));
 }
 /**
  * @Route("/register")
  * @Template("AppBundle:user:register.html.twig")
  */
 public function registerAction(Request $request)
 {
     $user = new User();
     $form = $this->createForm(UserType::class, $user);
     $form->add('captcha', CaptchaType::class, array('width' => 200, 'height' => 50, 'length' => 6));
     $form->add('submit', SubmitType::class, array('label' => 'Register', 'attr' => array('class' => 'btn btn-lg btn-primary')));
     //validate the form
     $form->handleRequest($request);
     if ('POST' === $request->getMethod()) {
         if ($form->isValid()) {
             $em = $this->getDoctrine()->getManager();
             $this->get('app_bundle.user_manager')->setUserPassword($user, $user->getPassword());
             $user->setRoles(array('ROLE_PATIENT'));
             $image = new ProfileImage();
             $image->setPath('defaultProfileImage.png');
             $em->persist($image);
             $em->flush();
             $user->setImage($image);
             $em->persist($user);
             $em->flush();
             $patient = new Patient();
             $patient->setUser($user);
             $em->persist($patient);
             $em->flush();
             $request->getSession()->getFlashBag()->add('success', 'You are registered, please log in');
             $message = \Swift_Message::newInstance()->setSubject('Registration confirmation')->setFrom('*****@*****.**')->setTo("*****@*****.**")->setContentType('text/html')->setBody($this->renderView('AppBundle:emails:registration.html.twig', array('name' => $user->getUsername()), 'text/html'));
             // send email
             $this->get('mailer')->send($message);
             return $this->redirect($this->generateUrl('login_route'));
         }
         return array('user' => $user, 'form' => $form->createView());
     }
     return array('user' => $user, 'form' => $form->createView());
 }