<?php
/**
 * Template name: Single User All Ads
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Classiera
 * @since Classiera
 */

if ( !is_user_logged_in() ) { 

	global $redux_demo; 
	$login = $redux_demo['login'];
	wp_redirect( $login ); exit;

}


global $redux_demo; 
$edit = $redux_demo['edit'];
$pagepermalink = get_permalink($post->ID);
if(isset($_GET['delete_id'])){
	$deleteUrl = $_GET['delete_id'];
	wp_delete_post($deleteUrl);
}
global $current_user, $user_id;
wp_get_current_user();
$user_info = get_userdata($user_ID);
$user_id = $current_user->ID; // You can set $user_id to any users, but this gets the current users ID.

get_header(); 


?>
<?php 
global $redux_demo; 
$featured_ads_option = $redux_demo['featured-options-on'];
$profile = $redux_demo['profile'];
$all_adds = $redux_demo['all-ads'];
$allFavourite = $redux_demo['all-favourite'];
$newPostAds = $redux_demo['new_post'];
$caticoncolor="";
$category_icon_code ="";
$category_icon="";
$category_icon_color="";
?>
<?php 
	$page = get_page($post->ID);
	$current_page_id = $page->ID;
?>
<?php $page_custom_title = get_post_meta($current_page_id, 'page_custom_title', true); ?>
<!-- page Heading -->
<?php $titleBg = $redux_demo['title-bg']['url']; ?>	
<section class="pageHeading" style="background:url(<?php echo $titleBg; ?>);">
    <div class="row">
    <h3><?php echo $user_identity; ?><?php esc_html_e("'s Profile", 'classiera') ?></h3>
	<p><?php echo $page_custom_title; ?></p>
    </div>
</section><!-- End page Heading -->
<section id="advertisement" class="noBorder">
	<div class="row">
		<div class="large-8 medium-12 columns">
			<section class="adSubmit boxPad">
				<div class="row">
					<div class="large-12 columns">
						<div class="adHeading">
							<i style="background:#e96969;" class="fa fa-user"></i>
							<h4><?php esc_html_e("My Ads", 'classiera') ?></h4>
						</div>
					</div>
				</div><!-- End ROW -->
				<div class="row">
					<div class="large-12 columns">
						<div class="authorHead">
							<h4><?php echo $user_identity; ?></h4>
						</div><!-- End authorHead -->
					</div><!-- End large-12 columns -->
				</div><!-- End ROW -->
				<div class="row">
					<div class="large-12 columns">
						<div class="authorBio">
								<?php 
									global $wp_query, $wp;
									$wp_query = new WP_Query();
									$kulPost = array(
										'post_type'  => 'post',
										'author' => $user_id,
										'posts_per_page' => 100,	
										);
									$wp_query = new WP_Query($kulPost);
									$current = -1;
									$current2 = 0;
									$count = 0;									
								?>
								<?php while ($wp_query->have_posts()) : $wp_query->the_post(); $current++; $current2++;?>
							<div class="userAds border">
								<div class="ad-image">
									<?php
									if ( has_post_thumbnail()) {
									   $imageurl = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'classiera-myads');
									   $thumb_id = get_post_thumbnail_id($post->id);
									   $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
									   echo '<a target="_blank" class="ad-image" href="' .get_permalink($post->ID). '" title="' . the_title_attribute('echo=0') . '" >';
									   //echo get_the_post_thumbnail($post->ID, 'thumbnail');
										?>
										<img src="<?php echo $imageurl[0]; ?>" alt="<?php if(empty($alt)){echo "Image";}else{ echo $alt; } ; ?>"/>
										<?php
									   echo '</a>';
									 }else{
										 ?>
										<img src="<?php echo get_template_directory_uri() . '/images/nothumb/nothumb80.png' ?>" alt="No Thumb"/> 
										 <?php
									 }
									?>
								</div><!-- End ad-image -->
								<div class="ad-description">
									<h5>
										<a target="_blank" class="my-ad-title" href="<?php the_permalink(); ?>"><?php $theTitle = get_the_title(); $theTitle = (strlen($theTitle) > 22) ? substr($theTitle,0,22).'...' : $theTitle; echo $theTitle; ?></a>
									</h5>
									<ul class="inline-list">
									<?php $classieraPstatus = get_post_status( $post->ID );?>
										<li>
											<span>
											<i class="fa fa-check-square-o"></i>
											<?php classieraPStatusTrns($classieraPstatus); ?>
											</span>
										</li>
										<li><span><i class="fa fa-eye"></i><?php echo classiera_get_post_views($post->ID); ?></span></li>
										<?php 
											global $redux_demo;
											$edit_post_page_id = $redux_demo['edit_post'];
											$postID = $post->ID;
											global $wp_rewrite;
											if ($wp_rewrite->permalink_structure == ''){
												//we are using ?page_id
												$edit_post = $edit_post_page_id."&post=".$post->ID;
												$del_post = $pagepermalink."&delete_id=".$post->ID;
											}else{
												//we are using permalinks
												$edit_post = $edit_post_page_id."?post=".$post->ID;
												$del_post = $pagepermalink."?delete_id=".$post->ID;
											}
										if(get_post_status( $post->ID ) !== 'private'){ 	
										?>
										<li><a href="<?php echo $edit_post; ?>"><i class="fa fa-pencil-square-o"></i><?php esc_html_e("Edit", 'classiera') ?></a></li>
										<?php } ?>
										<li><a class="thickbox" href="#TB_inline?height=150&amp;width=400&amp;inlineId=examplePopup<?php echo $post->ID; ?>"><i class="fa fa-trash-o"></i><?php esc_html_e("Delete", 'classiera') ?></a></li>
										<?php $dateFormat = get_option( 'date_format' );?>
										<li><span><i class="fa fa-clock-o"></i><?php echo get_the_date($dateFormat, $post_id); ?></span></li>
									</ul>
									<div class="delete-popup" id="examplePopup<?php echo $post->ID; ?>" style="display:none">
										<h4><?php esc_html_e("Are you sure you want to delete this?", 'classiera') ?></h4>
										<a class="button-ag large green" href="<?php echo $del_post; ?>">
										<span class="button-inner"><?php esc_html_e("Confirm", 'classiera') ?></span>
										</a>
									</div>
								</div><!-- End ad-description -->
							</div><!-- End userAds -->
							<?php  endwhile;	?>
							<?php wp_reset_query(); ?>						
							
						</div><!-- End authorBio -->
					</div><!-- End large-12 columns -->
				</div><!-- End ROW -->
			</section><!-- End adSubmit boxPad-->
		</div><!-- End large-8 -->
		<!-- Start Sidebar -->
		<div class="large-4 medium-12 columns">
			<section class="sidebar">
				<div class="row">
					<div class="large-12 medium-6 columns medium-centered">
						<div class="widgetBox">
							<div class="widgetTitle">
								<i class="fa fa-user"></i>
								<h4><?php esc_html_e("ACCOUNT OVERVIEW", 'classiera') ?></h4>
							</div><!-- End widgetTitle -->
							<div class="widgetContent">
							<?php $profile = $redux_demo['profile']; ?>
							<?php require_once get_template_directory() . '/inc/BFI_Thumb.php'; ?>
								<div class="authorAccount">
									<div class="large-7 medium-8 large-centered medium-centered small-centered columns">
										<div class="widgetAuthorImg">
											<?php $author_avatar_url = get_user_meta($user_ID, "classify_author_avatar_url", true); 
											if(!empty($author_avatar_url)) {
												$params = array( 'width' => 120, 'height' => 120, 'crop' => true );
												echo "<img class='author-avatar' src='" . bfi_thumb( "$author_avatar_url", $params ) . "' alt='' />";
											}else{
												$avatar_url = classiera_get_avatar_url ( get_the_author_meta('user_email', $user_ID), $size = '150' );?>
												<img class="avatar" src="<?php echo $avatar_url; ?>" alt="" /><?php
											}
											?>
											<h5><?php echo $user_identity; ?></h5>
											<?php 
												$role = $current_user->roles;
												$classieraRole = $role[0];
											?>
											<p><?php classieraRoleTrns($classieraRole); ?> </p>
										</div><!-- End widgetAuthorImg -->
									</div><!-- End large-7 -->
									<ul class="authorList clearfix">
										<li>
											<a href="<?php echo $profile; ?>">
												<i class="fa fa-user"></i>
												<?php esc_html_e("About Me", 'classiera') ?>												
											</a>
                                        </li>
										<li>
											<a href="<?php echo $all_adds; ?>">
												<i class="fa fa-briefcase"></i>
												<?php esc_html_e("My Ads", 'classiera') ?>
												<span><?php echo count_user_posts($user_ID);?></span>
											</a>
                                        </li>
										<li>										
											<a href="<?php echo $allFavourite; ?>">
												<i class="fa fa-heart"></i>
												<?php esc_html_e("Favourites", 'classiera') ?>
												<span>
													<?php 
													global $current_user;
													wp_get_current_user();
													$user_id = $current_user->ID;
													$myarray = classiera_authors_all_favorite($user_id);
													if(!empty($myarray)){
														$args = array(
														   'post_type' => 'post',
														   'post__in'      => $myarray
														);
													$wp_query = new WP_Query( $args );
													$current = -1;
													$current2 = 0;
													while ($wp_query->have_posts()) : $wp_query->the_post(); $current++; $current2++; 													
													endwhile;
													echo $current2;
													}else{
														echo "0";
													}
													?>
												</span>
											</a>
										</li>
										<li>
											<a href="<?php echo $edit; ?>">
												<i class="fa fa-gears"></i>
												<?php esc_html_e("Profile Settings", 'classiera') ?>
											</a>
										</li>
										<li>
											<a href="<?php echo wp_logout_url(get_option('siteurl')); ?>">
												<i class="fa fa-sign-out"></i>
												<?php esc_html_e("Logout", 'classiera') ?>
											</a>
										</li>
										<li>
											<a href="<?php echo $newPostAds; ?>" class="button"><?php esc_html_e("POST NEW AD", 'classiera') ?></a>
											
										</li>
									</ul><!-- End UL authorList -->
								</div><!-- End authorAccount -->
							</div><!-- End widgetContent -->
						</div><!-- End widgetBox -->
					</div><!-- End large-12 -->
					<?php get_sidebar('pages'); ?>
				</div><!-- End row -->
			</section><!-- End Section sidebar -->
		</div><!-- End large-4 medium-12 columns -->
		<!-- End Sidebar -->
	</div><!-- End ROW -->
</section><!-- End advertisement -->
<?php get_footer(); ?>