コード例 #1
0
ファイル: UserController.php プロジェクト: ufm040/kikala
 /**
  * Page d'inscription
  */
 public function register()
 {
     $error = array();
     $isValid = true;
     // si l'utilisateur est déjà connecté => redirige vers la page "Mon compte"
     $authentificationManager = new \W\Security\AuthentificationManager();
     if ($authentificationManager->getLoggedUser()) {
         $this->show('user/detail_account', ['user' => $this->getUser()]);
     }
     // formulaire soumis ?
     if ($_POST) {
         $username = $_POST['username'];
         $email = $_POST['email'];
         $password = $_POST['password'];
         $passwordConfirm = $_POST['passwordConfirm'];
         $lastname = $_POST['lastname'];
         $firstname = $_POST['firstname'];
         $birthyear = $_POST['birthyear'];
         $sex = $_POST['sex'];
         $job = $_POST['job'];
         $instructorDescription = $_POST['instructorDescription'];
         $studentDescription = $_POST['studentDescription'];
         // validation des données => à coder
         $isValid = true;
         // Contrôle des champs obligatoires sur la formation
         $validator = new \Utils\FormValidator();
         $validator->validateNotEmpty($username, "username", "Le pseudo est obligatoire !");
         $validator->validateNotEmpty($email, "email", "L'email est obligatoire !");
         $validator->validateNotEmpty($password, "password", "Choisir un mot de passe !");
         $validator->validateNotEmpty($passwordConfirm, "passwordConfirm", "Ressaisir le mot de passe !");
         $validator->validateNotEmpty($lastname, "lastname", "Saisir votre nom !");
         $validator->validateNotEmpty($firstname, "firstname", "Saisir votre prénom !");
         $validator->validateNotEmpty($birthyear, "birthyear", "Saisir votre année de naissance !");
         $validator->validateNotEmpty($sex, "sex", "Indiquer votre sexe !");
         $validator->validateNotEmpty($job, "job", "Saisir votre métier !");
         $validator->validateNotEmpty($instructorDescription, "instructorDescription", "Saisir votre description en tant que formateur !");
         $validator->validateNotEmpty($studentDescription, "studentDescription", "Saisir votre description en tant qu'étudiant !");
         if ($validator->isValid()) {
             $validator->validateEmail($email, "email", "L'email est incorrect !");
             $validator->validateYear($birthyear, "birthyear", "Votre année de naissance doit être comprise entre 1900-2099 !");
             $validator->validateCharacter($username, "username", "Le pseudo comporte des caractères interdits !");
         }
         if (!$validator->isValid()) {
             $error = $validator->getErrors();
             $isValid = false;
         }
         if ($isValid) {
             // 1 - on crée l'instance
             $userManager = new \Manager\UserManager();
             //  erreur pour le mail (déjà existant)
             if ($userManager->emailExists($email)) {
                 $isValid = false;
                 $error['email'] = 'Email déjà utlisé !';
             }
             if ($userManager->usernameExists($username)) {
                 $isValid = false;
                 $error['username'] = '******';
             }
             // erreur sur le mdp
             if ($password != $passwordConfirm) {
                 $isValid = false;
                 $error['passwordConfirm'] = 'Les mots de passe ne correspondent pas !';
             }
         }
         // upload du fichier
         if ($_FILES['image']['size'] != 0) {
             $file = new \Utils\ImageUpload($_FILES['image'], 'assets/img/users/');
             $file->uploadFile();
             $file->reduceImage(false);
             if (!$file->isValid()) {
                 $isValid = false;
                 $error['image'] = $file->getErrors();
             } else {
                 $error['image'] = 'img/users/' . $file->getFileName();
                 $_SESSION['image_user'] = $file->getFileName();
             }
         } else {
             $_SESSION['image_user'] = '******';
         }
         // si c'est valide
         if ($isValid) {
             // on insère en base de données
             // 2 - on appelle la méthode insert
             $user = $userManager->insert(["username" => $_POST['username'], "email" => $_POST['email'], "password" => password_hash($_POST['password'], PASSWORD_DEFAULT), "lastname" => $_POST['lastname'], "firstname" => $_POST['firstname'], "birthyear" => $_POST['birthyear'], "sex" => $_POST['sex'], "job" => $_POST['job'], "instructorDescription" => $_POST['instructorDescription'], "studentDescription" => $_POST['studentDescription'], "credit" => 2, "image" => $_SESSION['image_user'], "dateCreated" => date("Y-m-d H:i:s")]);
             // on connecte l'utilisateur
             if ($user) {
                 $userId = $userManager->lastInsertUser();
                 $userConnect = $userManager->find($userId['id']);
                 $userSecurity = new \W\Security\AuthentificationManager();
                 // Connecte l'utiliasteur
                 $userSecurity->logUserIn($userConnect);
             }
             // on redirige l'utilisateur vers une page de Bienvenue
             $this->redirectToRoute("succeedregister");
         }
     }
     $this->show('user/register', ['error' => $error]);
 }