<?php
/*
Template Name: Rejestracja
*/
 get_header();
the_post();

?>
<div class="container-fluid">
	<div class="container">
		<div class="row">
			<div class="col-xs-12 col-sm-6 col-sm-push-3 col-md-6 col-md-push-3">
				<div class="wrapper">

					<?php
					$err = '';
					$success = '';

					global $wpdb, $PasswordHash, $current_user, $user_ID;

					if(isset($_POST['task']) && $_POST['task'] == 'register' ) {


						$pwd1 = $wpdb->escape(trim($_POST['pwd1']));
						$pwd2 = $wpdb->escape(trim($_POST['pwd2']));
						$first_name = $wpdb->escape(trim($_POST['first_name']));
						$last_name = $wpdb->escape(trim($_POST['last_name']));
						$email = $wpdb->escape(trim($_POST['email']));
						$username = $wpdb->escape(trim($_POST['username']));

						if( $email == "" || $pwd1 == "" || $pwd2 == "" || $username == "" || $first_name == "") {
							$err = 'Uzupełnij wszystkie pola';
						} else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
							$err = 'Niepoprawny adres e-mail.';
						} else if(email_exists($email) ) {
							$err = 'Użytkownik o podanym e-mail już istnieje.';
						} else if($pwd1 <> $pwd2 ){
							$err = 'Hasło się nie zgadza.';
						} else {

							$user_id = wp_insert_user( array ('first_name' => apply_filters('pre_user_first_name', $first_name), 'user_pass' => apply_filters('pre_user_user_pass', $pwd1), 'user_login' => apply_filters('pre_user_user_login', $username), 'user_email' => apply_filters('pre_user_user_email', $email), 'role' => 'subscriber' ) );
							if( is_wp_error($user_id) ) {
								$err = 'Błąd, użytkownik o padanym loginie już istnieje.';
							} else {
								do_action('user_register', $user_id);
								$success = 'Rejestracja przebiegła pomyślnie';
							}

						}

					}
					?>

				        <!--display error/success message-->
					<div id="message">
						<?php
							if(! empty($err) ) :
								echo '<p class="error">'.$err.'';
							endif;
						?>

						<?php
							if(! empty($success) ) :
								echo '<p class="error">'.$success.'';
							endif;
						?>
					</div>

					<form method="post">
						<h3>Utworz darmowe konto</h3>
						
						<p><label>Imię</label></p>
						<p><input class="form-control input-lg required"  type="text" value="" name="first_name" id="first_name" /></p>
						<p><label>Email</label></p>
						<p><input class="form-control input-lg required"  type="text" value="" name="email" id="email" /></p>
						<p><label>Login</label></p>
						<p><input class="form-control input-lg required"   type="text" value="" name="username" id="username" /></p>
						<p><label>Hasło</label></p>
						<p><input class="form-control input-lg required"  type="password" value="" name="pwd1" id="pwd1" /></p>
						<p><label>Powtórz hasło</label></p>
						<p><input class="form-control input-lg required"  type="password" value="" name="pwd2" id="pwd2" /></p>
						<div class=""><p><?php if($sucess != "") { echo $sucess; } ?> <?php if($err != "") { echo $err; } ?></p></div>
						<button type="submit" name="btnregister" class="btn btn-theme btn-lg" >Wyślij</button>
						<input type="hidden" name="task" value="register" />
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<?php get_footer() ?>