<?php
/*
Nazwa szablonu: Strona — Formularz kontaktowy
*/

// Pobranie wartości przekazanych za pomocą formularza
$email = sanitize_email( $_POST['email'] );
$cname = sanitize_text_field( $_POST['cname'] );
$phone = sanitize_text_field( $_POST['phone'] );
$message = sanitize_text_field( $_POST['message'] );
$sendemail = !empty( $_POST['sendemail'] );

// Czy formularz został wysłany?
if ( !empty( $sendemail )
    && !empty( $cname )
        && !empty( $email )
        && empty( $lname ) ) {

        $mailto = get_bloginfo( 'admin_email' );
        $mailsubj = "Informacje z formularza kontaktowego " . get_bloginfo( 'name' );
        $mailhead = "Od: " . $cname . " <" . $email . ">\n";
        $mailbody = "Imię i nazwisko: " . $cname . "\n\n";
        $mailbody .= "E-mail: $email\n\n";
        $mailbody .= "Telefon: $phone\n\n";
        $mailbody .= "Wiadomość:\n" . $message;

        // Wysłanie wiadomości e-mail
        wp_mail( $mailto, $mailsubj, $mailbody, $mailhead );

        // Przygotowanie komunikatu dla tej strony i usunięcie wartości zmiennych
        $msg = "Wiadomość została wysłana.";

        $email = "";
        $cname = "";
        $phone = "";
        $message = "";

}
elseif ( !empty( $sendemail ) && !is_email( $email ) )
    $msg = "Proszę podać prawidłowy adres e-mail.";
elseif ( !empty( $lname ) )
    $msg = "Czy jesteś spamerem?";
elseif ( !empty( $sendemail ) && empty( $cname ) )
    $msg = "Proszę podać imię i nazwisko.";
elseif ( !empty( $sendemail ) && !empty( $cname ) && empty( $email ) )
    $msg = "Proszę podać adres e-mail.";

// Pobranie nagłówka
get_header();
?>
<div id="wrapper">
 <div id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <h1><?php the_title(); ?></h1>
<?php if ( !empty( $msg ) ) { ?>
   <div class="message"><?php echo $msg?></div>
<?php } ?>
  <form class="general" action="<?php the_permalink(); ?>" method="post">
   <div class="form-row">
        <label for="cname">Imię</label>
        <input type="text" name="cname" value="<?php echo esc_attr($cname);?>"/>
        <small class="red">* Wymagane</small>
   </div>
   <div class="hidden">
        <label for="lname">Nazwisko</label>
        <input type="text" name="lname" value="<?php echo esc_attr($lname);?>"/>
        <small class="red">POZOSTAW TO POLE PUSTE</small>
   </div>
   <div class="form-row">
        <label for="email">E-mail</label>
        <input type="text" name="email" value="<?php echo esc_attr($email);?>"/>
        <small class="red">* Wymagane</small>
   </div>
   <div class="form-row">
        <label for="phone">Telefon</label>
        <input type="text" name="phone" value="<?php echo esc_attr($phone);?>"/>
   </div>
   <div class="form-row">
        <label for="message">Pytanie lub komentarz</label>
        <textarea class="textarea" id="message" name="message" rows="4" cols="55">
<?php echo esc_textarea( $message )?>
        </textarea>
   </div>
   <div class="form-row">
        <label for="sendemail">&nbsp;</label>
        <input type="submit" id="sendemail" name="sendemail" value="Wyślij"/>
   </div>
  </form>
<?php endwhile; endif; ?>
 </div>
</div>
<?php
// Pobranie stopki
get_footer();
?>
