'error'));
        $errorEmail = self::createMensajeError($errores, 'new_email', 'span', array('class' => 'error'));
        $errorPassword = self::createMensajeError($errores, 'new_pass', 'span', array('class' => 'error'));
        $errorPassword2 = self::createMensajeError($errores, 'repass', 'span', array('class' => 'error'));
        $errorVerify = self::createMensajeError($errores, 'terms', 'span', array('class' => 'error'));
        $html = "
";
        return $html;
    }
    protected function procesaFormulario($datos){
        $result = array();
        
        $nombre = $this->test_input($datos['new_name']) ?? null;
        $nombre = strtolower($nombre);
        if ( empty($nombre) || mb_strlen($nombre) < 3 || mb_strlen($nombre) > 15 ) {
            $result['new_name'] = "El nombre tiene que tener\nuna longitud de al menos\n3 caracteres\ny menos de 15 caracteres.";
        }
        $email = $this->test_input($datos['new_email']) ?? null;
        if ( empty($email) || !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $email) ) {
            $result['new_email'] = "El email no es válido.";
        }
        
        $password = $this->test_input($datos['new_pass']) ?? null;
        if ( empty($password) || !mb_ereg_match(self::HTML5_PASS_REGEXP, $password) ) {
            $result['new_pass'] = "El password tiene que tener\nuna longitud de al menos\n 4 caracteres 1 mayúscula y 1 número.";
        }
        $password2 = $this->test_input($datos['repass']) ?? null;
        if ( empty($password2) || strcmp($password, $password2) !== 0 ) {
            $result['repass'] = "Los passwords deben coincidir";
        }
        $verify = $this->test_input($datos['terms']) ?? null;
        if ( empty($verify) ) {
            $result['terms'] = "Debe confirmar la casilla de\ntérminos y condiciones.";
        }
        
        if (count($result) === 0) {
            $bd = new UserDAO('complucine');
            if($bd){
                $this->user = $bd->selectUserName($nombre);
                if ($this->user->data_seek(0)) {
                    $result[] = "El usuario ya existe.";
                }
                else{
                    $this->user = $bd->selectUserEmail($email);
                    if ($this->user->data_seek(0)) {
                        $result[] = "El email ya está registrado.";
                    } else {
                        if($bd->createUser("", $nombre, $email, $password, "user")){
                            $this->user = $bd->selectUser($nombre, $password);
                            if ($this->user) {
                                $this->user->setPass(null);
                                $_SESSION["user"] = serialize($this->user);
                                $_SESSION["nombre"] = $this->user->getName();
                                $_SESSION["rol"] = $this->user->getRol();
                                $_SESSION["login"] = true;
                                $img = "../img/users/user.jpg"; //USER_PICS
                                $profile_img = "../img/users/".$nombre.".jpg";
                                copy($img, $profile_img);
                                $result = ROUTE_APP."register/register.php";
                            } else {
                                $result[] = "Ha ocurrido un error al iniciar la sesión\nPero el usuario se creó correctamente.";
                            }
                        } else {
                            $result[] = "Ha ocurrido un error al crear el usuario.";
                        }
                    }
                }
            } else {
                $result[] = "Error al conectar con la BD.";
            }
        }
        return $result;
    }
    //Returns validation response:
    static public function getReply() {
        
        if(isset($_SESSION["login"])){
            $name = strtoupper($_SESSION['nombre']);
            $reply = "Bienvenido {$_SESSION['nombre']}
                        {$name}, has creado tu cuenta de usuario correctamente.
                        Usa los botones para navegar
                        
                        \n";
        }   
        else if(!isset($_SESSION["login"])){
            $reply = "ERROR
                        Ha ocurrido un problema y no hemos podido completar el registro
                        Vuelve a intetarlo o inicia sesión si tienes una cuenta de usuario.
                        
                        \n";
        }
        return $reply;
    }
}
?>