<?php
/*
Template Name: Edit A Review
*/

global $gp;

if ( ! is_user_logged_in() OR $gp['write_a_review_editing'] == 'disabled' OR ( empty( $_POST['user_review_id'] ) && empty( $_GET['user_review'] ) ) ) {

	wp_redirect( home_url() );	
	
} else {
  
	$user_review_title_error = '';
	$user_review_rating_error = '';
	$user_review_content_error = '';
	$has_error = '';
	
	 if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty( $_POST['gp_action'] ) && $_POST['gp_action'] == 'gp_edit_post' && isset( $_POST['user_review_id'] ) ) {
		 
		$post_to_edit = array();
		$post_to_edit = get_post( $_POST['user_review_id'] );

		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_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 ( isset( $_POST['user_review_title'] ) ) {
			$user_review_title = $_POST['user_review_title'];
		}
	
		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_rating'] ) && isset( $_POST['user_review_content'] ) && $has_error == false ) {
	
			if ( $post_to_edit->post_status == 'publish' && $gp['write_a_review_editing'] == 'approved' ) {
				$post_to_edit->post_status = 'publish';
			} else {
				$post_to_edit->post_status = 'pending';
			}
			$post_to_edit->post_title = $user_review_title;
			$post_to_edit->post_content = $user_review_content;
			
			$gp_post_id = wp_update_post( $post_to_edit );
			
			update_post_meta( $gp_post_id, '_user_review_rating', $user_review_rating );

			// Notify admin of review via email
			if ( $post_to_edit->post_status == 'pending' && $gp['write_a_review_email_notification'] == 'email_always' ) {
				$to = get_option( 'admin_email' );
				$subject = "User Review [edit]: '" . $user_review_title . "' - " . get_bloginfo( 'name' );
				$message .= "A user review was edited on " . get_bloginfo( 'name' ) . "\n";
				$message .= "Title: " . $user_review_title . "\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=edited&id=' . $_POST['user_review_id'] );
			} else { 
				wp_redirect( $_SERVER['REQUEST_URI'] . '&user_review=edited&id=' . $_POST['user_review_id'] );	
			}

			// Upload featured image
			if ( ! empty( $_FILES ) ) {
				foreach ( $_FILES as $file => $array ) { 
					$newupload = gp_insert_attachment( $file, $gp_post_id );
				}
			}
			
		}
		
	}
	 
	get_header();

	?>

	<?php echo 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(); ?>
		
				<div class="entry-content">
												
					<?php if ( ! empty( $_GET['user_review'] ) && $_GET['user_review'] == 'edited' ) { ?>
				
						<div class="user-review-success">
							<?php if ( $gp['write_a_review_editing'] == 'approved' ) { ?>
								<?php _e( 'Your review was succesfully edited. <span class="view-review-link"><a href="' . get_permalink( $_GET['id'] ) . '">View this review</a></span>', 'gp_lang' ); ?>
							<?php } else { ?>
								<?php _e( 'Your review was succesfully edited but will need to be approved before it is shown on the site again.', 'gp_lang' ); ?>
							<?php } ?>	
						</div>	
				
					<?php } else { ?>	
				
						<?php $post_to_edit = get_post( $_POST['user_review_id'] ); ?>
	
						<form id="edit_post" name="edit_post" method="post" action="" enctype="multipart/form-data">						
							
							<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 echo esc_attr( $post_to_edit->post_title ); ?>">
								<?php if ( $user_review_title_error != '' ) { ?><span class="user-review-error"><?php echo esc_attr( $user_review_title_error ); ?></span><?php } ?>
							</p>

							<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 echo get_post_meta( $post_to_edit->ID, '_user_review_rating', true ); ?>">													                       
								<?php if ( $user_review_rating_error != '' ) { ?><span class="user-review-error"><?php echo esc_attr( $user_review_rating_error ); ?></span><?php } ?>
							</p>
							<p>							
								<?php if ( has_post_thumbnail( $post_to_edit->ID ) ) { ?>
									<?php echo get_the_post_thumbnail( $post_to_edit->ID, array( 150, 150 ) ); ?><br />	
								<?php } ?>								
								<label for="user-review-image"><?php _e( 'Image', 'gp_lang' ); ?></label>
								<input type="file" name="user_review_image" id="user-review-image" tabindex="30" value="" />	
							</p>
						
							<p>
								<label for="user-review-content"><?php _e( 'Review', 'gp_lang' ); ?> <span class="required">*</span></label>
								<?php if ( $post_to_edit->post_content ) {
									$parse_user_review_content = stripslashes( $post_to_edit->post_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>
		
							<p><input type="submit" value="<?php esc_attr_e( 'Edit Review', 'gp_lang' ); ?>" tabindex="40" id="submit" name="submit" /></p>

							<input type="hidden" name="user_review_id" value="<?php echo absint( $post_to_edit->ID ); ?>" />
							<input type="hidden" name="gp_action" value="gp_edit_post" />
		
							<?php // wp_nonce_field( 'new-post' ); ?>
		
						</form>
				
					<?php } ?>
															
				</div><!-- END ENTRY CONTENT -->
	
			<?php endwhile; endif; ?>		
					
		</div><!-- END CONTENT -->

		<?php get_sidebar(); ?>
	
	</div><!-- END CONTENT WRAPPER -->

	<?php get_footer(); ?>
	
<?php } ?>	