<?php
/*
Template Name: Write A Review
*/
global $gp;

if ( ( $gp['write_a_review_visitors'] == 'enabled' && ! is_user_logged_in() ) OR is_user_logged_in() ) {

	gp_get_hub_id( get_the_ID() );

	$user_review_title_error = '';
	$user_review_name_error = '';
	$user_review_rating_error = '';
	$user_review_content_error = '';
	$user_captcha_error = '';
	$has_error = '';

	if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty( $_POST['gp_action'] ) && $_POST['gp_action'] == 'gp_new_post' ) {
	
		if ( trim( $_POST['user_review_title'] ) === '' ) {
			$user_review_title_error = __( 'Please give your review a title.', 'gp_lang' );
			$has_error = true;
		}

		if ( trim( $_POST['user_review_name'] ) === '' && ! is_user_logged_in() ) {
			$user_review_name_error = __( 'Please add your name.', 'gp_lang' );
			$has_error = true;
		}
	
		if ( trim( $_POST['user_review_rating'] ) === '' ) {
			$user_review_rating_error = __( 'Please give your review a rating.', 'gp_lang' );
			$has_error = true;
		} elseif ( ! is_numeric( $_POST['user_review_rating'] ) ) {
			$user_review_rating_error = __( 'Please make sure your rating is a numerical value.', 'gp_lang' );
			$has_error = true;
		} elseif ( $_POST['user_review_rating'] < 1 OR $_POST['user_review_rating'] > $gp['review_rating_number'] ) {
			$user_review_rating_error = __( 'Please give a rating between 1 and ' . $gp['review_rating_number'] . '.', 'gp_lang' );
			$has_error = true;
		}
					
		if ( trim( $_POST['user_review_content'] ) === '' ) {
			$user_review_content_error = __( 'Please give your review some content.', 'gp_lang' );
			$has_error = true;
		}
		
		if ( ( function_exists( 'cptch_check_custom_form' ) && cptch_check_custom_form() !== true ) OR ( function_exists( 'cptchpr_check_custom_form' ) && cptchpr_check_custom_form() !== true ) ) {
			$user_captcha_error = __( 'Please complete the CAPTCHA.', 'gp_lang' );
			$has_error = true;		
		}
		
		if ( isset( $_POST['user_review_title'] ) ) {
			$user_review_title =  $_POST['user_review_title'];
		}

		if ( isset( $_POST['user_review_name'] ) ) {
			$user_review_name =  $_POST['user_review_name'];
		}
	
		if ( isset( $_POST['user_review_rating'] ) ) {
			$user_review_rating = $_POST['user_review_rating'];
		}
					
		if ( isset( $_POST['user_review_content'] ) ) {
			$user_review_content = $_POST['user_review_content'];
		}
				
		if ( isset( $_POST['user_review_title'] ) && isset( $_POST['user_review_name'] ) && isset( $_POST['user_review_rating'] ) && isset( $_POST['user_review_content'] ) && $has_error == false ) {

			global $wpdb;

			// Approve or pending submitted review
			if ( $gp['write_a_review_submitting'] == 'approved' ) {
				$post_status = 'publish';
			} else {
				$post_status = 'pending';
			}
			
			// Create review
			$new_post = array(
				'post_type'     => 'gp_user_review',
				'post_status'   => $post_status,
				'post_title'    => $user_review_title,
				'post_content'  => $user_review_content,
			);
			$gp_post_id = wp_insert_post( $new_post );
			
			// Upload featured image
			if ( ! empty( $_FILES ) ) {
				foreach ( $_FILES as $file => $array ) {
					$newupload = gp_insert_attachment( $file, $gp_post_id );
				}
			}
		
			// Add parent hub ID
			update_post_meta( $gp_post_id, '_hub_page_id', $GLOBALS['gp_hub_id'] );
			
			// Add name field is user not logged in
			if ( isset( $_POST['user_review_name'] ) && ! is_user_logged_in() ) {
				update_post_meta( $gp_post_id, '_user_review_name', $user_review_name );	
			}
			update_post_meta( $gp_post_id, '_user_review_rating', $user_review_rating );

			// Notify admin of review via email
			if ( $post_status == 'pending' && $gp['write_a_review_email_notification'] != 'disabled' ) {
				if ( $user_review_name != '' ) {
					$author = $user_review_name;
				} else {
					$author_id = get_post_field( 'post_author', $gp_post_id );
					$author = get_the_author_meta( 'display_name', $author_id );
				}
				$to = get_option( 'admin_email' );
				$subject = "User Review [submitted]: '" . $user_review_title . "' - " . get_bloginfo( 'name' );
				$message .= "A user review was submitted on " . get_bloginfo( 'name' ) . "\n";
				$message .= "Title: " . $user_review_title . "\n";
				$message .= "Author: " . $author . "\n";
				$message .= "You can view this post at " . get_permalink( $gp_post_id );
				$headers = '';
				wp_mail( $to, $subject, $message, $headers );				
			}
				
			// Permalink structure for success URL
			if ( get_option( 'permalink_structure' ) ) {
				wp_redirect( $_SERVER['REQUEST_URI'] . '?user_review=submitted&id=' . $gp_post_id );
			} else { 
				wp_redirect( $_SERVER['REQUEST_URI'] . '&user_review=submitted&id=' . $gp_post_id );
			}
			
			exit;
		
		}
		
	}

}

get_header();

?>

<?php gp_page_header( get_the_ID() ); ?>
				
<div id="content-wrapper"<?php if ( $GLOBALS['gp_layout'] != 'gp-fullwidth' ) { ?> class="gp-container"<?php } ?>>

	<div id="content">

		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>		

			<?php if ( get_post_meta( $GLOBALS['gp_hub_id'], '_wp_page_template', true ) == 'hub-template.php' ) { ?>
				<header class="entry-header small-header">
					<h2 class="entry-title"><?php echo gp_prefix_hub_title( get_the_ID() ); ?></h2>
				</header>
			<?php } ?>	
		
			<div class="entry-content">
			
				<?php if ( ( $gp['write_a_review_visitors'] == 'enabled' && ! is_user_logged_in() ) OR is_user_logged_in() ) { ?>

					<form id="user-review-form"<?php if ( ! empty( $gp['write_a_review_rules'] ) ) { ?> class="small-form"<?php } ?> name="user-review-form" method="post" action="" enctype="multipart/form-data" role="form">	

						<?php if ( ! empty( $_GET['user_review'] ) && $_GET['user_review'] == 'submitted' ) { ?>
							<div class="user-review-success">
							<?php if ( $gp['write_a_review_submitting'] == 'approved' ) {
								_e( 'Your review has been posted. <span class="view-review-link"><a href="' . get_permalink( $_GET['id'] ) . '">View this review</a></span>', 'gp_lang' );
							} else {
								_e( 'Your review was succesfully submitted. Once it is approved by a member of staff it will show up on the site.', 'gp_lang' );
							} ?>
							</div>	
						<?php } ?>
							
						<p>
							<label for="user-review-title"><?php _e( 'Title', 'gp_lang' ); ?> <span class="required">*</span></label>
							<input type="text" name="user_review_title" id="user-review-title" value="<?php if ( isset( $_POST['user_review_title'] ) ) { echo esc_attr( $_POST['user_review_title'] ); } ?>">
							<?php if ( $user_review_title_error != '' ) { ?><span class="user-review-error"><?php echo esc_attr( $user_review_title_error ); ?></span><?php } ?>
						</p>

						<?php if ( ! is_user_logged_in() ) { ?>
							<p>
								<label for="user-review-name"><?php _e( 'Your Name', 'gp_lang' ); ?> <span class="required">*</span></label>
								<input type="text" name="user_review_name" id="user-review-name" value="<?php if ( isset( $_POST['user_review_name'] ) ) { echo esc_attr( $_POST['user_review_name'] ); } ?>">
								<?php if ( $user_review_name_error != '' ) { ?><span class="user-review-error"><?php echo esc_attr( $user_review_name_error ); ?></span><?php } ?>
							</p>
						<?php } else { ?>
							<input type="hidden" name="user_review_name" id="user-review-name" value="">
						<?php } ?>
									
						<p class="comment-form-rating">
							<label for="user-review-rating"><?php _e( 'Rating', 'gp_lang' ); ?> <span class="required">*</span></label>
							<input type="text" name="user_review_rating" id="user-review-rating" value="<?php if ( isset( $_POST['user_review_rating'] ) ) { echo floatval( $_POST['user_review_rating'] ); } ?>">												                       
							<?php if ( $user_review_rating_error != '' ) { ?><span class="user-review-error"><?php echo esc_attr( $user_review_rating_error ); ?></span><?php } ?>
						</p>
			
						<p>
							<label for="user-review-image"><?php _e( 'Image', 'gp_lang' ); ?></label>
							<input type="file" name="user_review_image" id="user-review-image">
						</p>
																
						<p>
							<label for="user-review-content"><?php _e( 'Review', 'gp_lang' ); ?> <span class="required">*</span></label>
							<?php if ( isset( $_POST['user_review_content'] ) ) {
								$parse_user_review_content = stripslashes( $_POST['user_review_content'] );
							} else {
								$parse_user_review_content = '';
							} ?>
							<?php wp_editor( $parse_user_review_content, 'user_review_content', $settings = array( true, true, 'user_review_content' ) );  ?>
							<?php if ( $user_review_content_error != '' ) { ?><span class="user-review-error"><?php echo esc_attr( $user_review_content_error ); ?></span><?php } ?> 
						</p>
				
						<?php if ( function_exists( 'cptch_display_captcha_custom' ) ) { ?>
							<input type='hidden' name='cntctfrm_contact_action' value='true' />
							<p>
								<?php echo cptch_display_captcha_custom(); ?>
								<?php if ( $user_captcha_error != '' ) { ?><span class="user-review-error"><?php echo esc_attr( $user_captcha_error ); ?></span><?php } ?>
							</p>
						<?php } ?>	
						<?php if ( function_exists( 'cptchpr_display_captcha_custom' ) ) { ?>
							<input type='hidden' name='cntctfrm_contact_action' value='true' />
							<p>
								<?php echo cptchpr_display_captcha_custom(); ?>
								<?php if ( $user_captcha_error != '' ) { ?><span class="user-review-error"><?php echo esc_attr( $user_captcha_error ); ?></span><?php } ?>
							</p>	
						<?php } ?>

						<p><input type="submit" value="<?php esc_attr_e( 'Post Review', 'gp_lang' ); ?>" tabindex="40" id="submit" name="submit" /></p>

						<input type="hidden" name="gp_action" value="gp_new_post" />
					
						<?php wp_nonce_field( 'new-post' ); ?>
				
					</form>
			
					<?php if ( ! empty( $gp['write_a_review_rules'] ) ) { ?>
						<div id="user-review-rules">
							<?php echo wp_kses_post( $gp['write_a_review_rules'] ); ?>
						</div>
					<?php } ?>
				
				<?php } else { ?>
					
					<strong class="no-items-found"><?php _e( 'You must be logged in to post a review.', 'gp_lang' ); ?></strong>
				
				<?php } ?>
											
			</div><!-- END ENTRY CONTENT -->
	
		<?php endwhile; endif; ?>			

	</div><!-- END CONTENT -->

	<?php get_sidebar(); ?>

</div><!-- END CONTENT WRAPPER -->

<?php get_footer(); ?>