public function tieraddedAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $tier = new Tier();
     $tier->setSiren($request->get('_SIREN'));
     $tier->setRaisonsoc($request->get('_RaisonSoc'));
     $tier->setEcole(intval($request->get('_Ecole')));
     if ($_FILES && $_FILES['_Logo']['size'] > 0) {
         $tier->setLogo(file_get_contents($_FILES['_Logo']['tmp_name']));
     }
     if ($_FILES && $_FILES['_image']['size'] > 0) {
         $tier->setFondecran(file_get_contents($_FILES['_image']['tmp_name']));
     }
     $em->persist($tier);
     for ($i = 0; $i < count($request->get('_SIRET')); $i++) {
         $etablissement = new Etablissement();
         $etablissement->setSiret($request->get('_SIRET')[$i]);
         $etablissement->setAdresse($request->get('_Adresse')[$i]);
         $etablissement->setGeocode($request->get('_Geocode')[$i]);
         $etablissement->setCodepostal($request->get('_CodeP')[$i]);
         $etablissement->setTelephone($request->get('_Tel')[$i]);
         $etablissement->setFax($request->get('_Fax')[$i]);
         $etablissement->setVille($request->get('_Ville')[$i]);
         $etablissement->setResponsable($request->get('_Resp')[$i]);
         $etablissement->setTelResponsable($request->get('_TelResp')[$i]);
         $etablissement->setMailResponsable($request->get('_MailResp')[$i]);
         $etablissement->setSite($request->get('_Site')[$i]);
         $etablissement->setTier($tier);
         $em->persist($etablissement);
         $em->flush();
         $admins = $this->getDoctrine()->getRepository('GenericBundle:User')->findByRoles(array('ROLE_SUPER_ADMIN', 'ROLE_ADMINECOLE', 'ROLE_ADMINSOC'));
         foreach ($admins as $admin) {
             $notif = new Notification();
             $notif->setEntite($etablissement->getId());
             if ($etablissement->getTier()->getEcole() && $admin . $this->isGranted('ROLE_ADMINSOC')) {
                 $notif->setType('Ecole');
                 $notif->setUser($admin);
                 $em->persist($notif);
                 $em->flush();
             }
             if (!$etablissement->getTier()->getEcole() && $admin . $this->isGranted('ROLE_ADMINECOLE')) {
                 $notif->setType('Societe');
                 $notif->setUser($admin);
                 $em->persist($notif);
                 $em->flush();
             }
         }
     }
     $em->flush();
     return $this->redirect($this->generateUrl('societe_admin', array('societe' => $this->get('security.token_storage')->getToken()->getUser()->getTier()->getRaisonsoc())));
 }
 public function UserAddedAction(Request $request)
 {
     $userManager = $this->get('fos_user.user_manager');
     $newuser = $userManager->createUser();
     $newuser->setUsername($request->get('_Username'));
     $newuser->setEmail($request->get('_mail'));
     $newuser->addRole($request->get('_role'));
     $newuser->setCivilite($request->get('civilite'));
     $newuser->setTelephone($request->get('_Tel'));
     $newuser->setPrenom($request->get('_Prenom'));
     $newuser->setNom($request->get('_Nom'));
     //generate a password
     $tokenGenerator = $this->get('fos_user.util.token_generator');
     $password = substr($tokenGenerator->generateToken(), 0, 8);
     // 8 chars
     $hash = $this->get('security.password_encoder')->encodePassword($newuser, $password);
     $newuser->setPassword($hash);
     if ($request->get('_role') == 'ROLE_ADMINSOC' || $request->get('_role') == 'ROLE_ADMINECOLE') {
         $etab = $this->getDoctrine()->getRepository('GenericBundle:Etablissement')->find($request->get('_id'));
         $newuser->setTier($etab->getTier());
     }
     if ($request->get('_role') == 'ROLE_RECRUTE' || $request->get('_role') == 'ROLE_TUTEUR') {
         $etab = $this->getDoctrine()->getRepository('GenericBundle:Etablissement')->find($request->get('_id'));
         $newuser->setEtablissement($etab);
     }
     $em = $this->getDoctrine()->getManager();
     $em->persist($newuser);
     $em->flush();
     $superadmins = $this->getDoctrine()->getRepository('GenericBundle:User')->findByRole('ROLE_SUPER_ADMIN');
     foreach ($superadmins as $admin) {
         $notif = new Notification();
         $notif->setEntite($newuser->getId());
         $notif->setType('Utilisateur');
         $notif->setUser($admin);
         $em->persist($notif);
         $em->flush();
     }
     //send password
     if ($request->get('_modele')) {
         $modele = 'GenericBundle:Mail/templates:' . $request->get('_modele') . '_' . $this->get('security.token_storage')->getToken()->getUser()->getUsername() . '.html.twig';
     } else {
         $modele = 'GenericBundle:Mail:NewUser.html.twig';
     }
     $message = \Swift_Message::newInstance()->setSubject('Email')->setFrom(array('*****@*****.**' => "HUB3E"))->setTo($request->get('_mail'))->setBody($this->renderView($modele, array('username' => $request->get('_Username'), 'password' => $password)), 'text/html');
     $this->get('mailer')->send($message);
     if ($request->get('_id')) {
         return $this->redirect($this->generateUrl('metier_user_affiche', array('id' => $request->get('_id'))));
     } else {
         return $this->redirect($this->generateUrl('metier_user_admin'));
     }
 }
 public function ImportCandidatAction($id)
 {
     $em = $this->getDoctrine()->getEntityManager();
     $import = $em->getRepository('GenericBundle:ImportCandidat')->find($id);
     $user = $em->getRepository('GenericBundle:User')->findApprenantDuplicata($id);
     $response = new JsonResponse();
     if ($user) {
         return $response->setData(array('Ajout' => '0'));
     } else {
         //$userManager = $this->get('fos_user.user_manager');
         //$newuser = $userManager->createUser();
         $newuser = new User();
         $newuser->setCivilite($import->getCivilite());
         $newuser->setNom($import->getNom());
         $newuser->setEmail($import->getEmail());
         $newuser->setPrenom($import->getPrenom());
         $newuser->setTelephone($import->getTelephone());
         $newuser->setEtablissement($import->getEtablissement());
         $newuser->addRole('ROLE_APPRENANT');
         $newuser->setUsername($import->getPrenom() . '.' . $import->getNom());
         $newuser->setInfo($import->getInfo());
         //generate a password
         $tokenGenerator = $this->get('fos_user.util.token_generator');
         $password = substr($tokenGenerator->generateToken(), 0, 8);
         // 8 chars
         $hash = $this->get('security.password_encoder')->encodePassword($newuser, $password);
         $newuser->setPassword($hash);
         $em = $this->getDoctrine()->getManager();
         $em->persist($newuser);
         $em->flush();
         $date = new \DateTime();
         $newuser->getInfo()->setDaterecup($date);
         foreach ($import->getHobbies() as $hobby) {
             $newuser->addHobby($hobby);
             $em->flush();
         }
         foreach ($import->getLangue() as $langue) {
             $newuser->addLangue($langue);
             $em->flush();
         }
         foreach ($em->getRepository('GenericBundle:Experience')->findBy(array('importCandidat' => $import)) as $experience) {
             $experience->setUser($newuser);
             $experience->setImportCandidat(null);
         }
         foreach ($em->getRepository('GenericBundle:Diplome')->findBy(array('importCandidat' => $import)) as $diplome) {
             $diplome->setUser($newuser);
             $diplome->setImportCandidat(null);
         }
         foreach ($em->getRepository('GenericBundle:Document')->findBy(array('importCandidat' => $import)) as $document) {
             $document->setUser($newuser);
             $document->setImportCandidat(null);
         }
         foreach ($em->getRepository('GenericBundle:Parents')->findBy(array('importCandidat' => $import)) as $parents) {
             $parents->setUser($newuser);
             $parents->setImportCandidat(null);
         }
         foreach ($em->getRepository('GenericBundle:Recommandation')->findBy(array('importCandidat' => $import)) as $recommandation) {
             $recommandation->setUser($newuser);
             $recommandation->setImportCandidat(null);
         }
         $em->flush();
         $superadmins = $this->getDoctrine()->getRepository('GenericBundle:User')->findByRole('ROLE_SUPER_ADMIN');
         $usercon = $this->get('security.token_storage')->getToken()->getUser();
         $superadmins = array_merge($superadmins, $this->getDoctrine()->getRepository('GenericBundle:User')->findBy(array('tier' => $usercon->getTier())));
         foreach ($superadmins as $admin) {
             if (!$this->getDoctrine()->getRepository('GenericBundle:Notification')->findOneBy(array('entite' => $newuser->getId(), 'type' => 'Utilisateur', 'user' => $admin))) {
                 $notif = new Notification();
                 $notif->setEntite($newuser->getId());
                 $notif->setType('Utilisateur');
                 $notif->setUser($admin);
                 $em->persist($notif);
                 $em->flush();
             }
         }
         //send password
         if ($usercon->getTier()) {
             if ($this->get('templating')->exists('GenericBundle:Mail/templates:' . $usercon->getTier()->getSiren() . '_NewUser.html.twig')) {
                 $modele = 'GenericBundle:Mail/templates:' . $usercon->getTier()->getSiren() . '_NewUser.html.twig';
             } else {
                 $modele = 'GenericBundle:Mail:NewUser.html.twig';
             }
         } elseif ($usercon->getEtablissement()) {
             if ($this->get('templating')->exists('GenericBundle:Mail/templates:' . $usercon->getEtablissement()->getTier()->getSiren() . '_NewUser.html.twig')) {
                 $modele = 'GenericBundle:Mail/templates:' . $usercon->getEtablissement()->getTier()->getSiren() . '_NewUser.html.twig';
             } else {
                 $modele = 'GenericBundle:Mail:NewUser.html.twig';
             }
         } else {
             $modele = 'GenericBundle:Mail:NewUser.html.twig';
         }
         $message = \Swift_Message::newInstance()->setSubject('Email')->setFrom(array('*****@*****.**' => "HUB3E"))->setTo($import->getEmail())->setBody($this->renderView($modele, array('username' => $import->getPrenom() . '.' . $import->getNom(), 'password' => $password)), 'text/html');
         $this->get('mailer')->send($message);
         $em->remove($import);
         $em->flush();
         return $response->setData(array('Ajout' => '1'));
     }
 }