<?php 
/* 
Template Name: Gallery :: Filterable 
*/ 
?>

<?php
$kk_content_style = get_theme_mod( 'kktfwp_content_width', 'kk-boxed' );

$kk_album_style = get_post_meta( $post->ID, '_album_layout_value', true ); 

switch( $kk_album_style ) {
	case '1-column': 
		$kk_col = 'one-col-p';
		$kk_hp = false;
		break;
	case '2-column': 
		$kk_col = 'two-col-p';
		$kk_hp = false;
		break;
	case '3-column': 
		$kk_col = 'three-col-p';
		$kk_hp = false;
		break;
	case '4-column':
		$kk_col = 'four-col-p';
		$kk_hp = false;
		break;
	case '2-column (wide)':
		$kk_col = 'two-col-p';
		$kk_hp = true;
		break;
	case '3-column (wide)':
		$kk_col = 'three-col-p';
		$kk_hp = true;
		break;
	case '4-column (wide)':
		$kk_col = 'four-col-p';
		$kk_hp = true;
		break;
	default:
		$kk_col = 'three-col-p';
		$kk_hp = false;
} 
?>

<?php $title_type = kktfwp_title_type(); ?>

<?php get_header(); ?>

<?php if ( post_password_required( $post ) ) { 

		get_template_part( 'password-form' );
		
	} else {		
		?>

   <!-- start content wrapper -->			
	<div class="content clearfix">
	
		<?php if( $title_type == 'parallax' ) : ?>
			
		<div id="kk-parallax-wrapper" class="kk-parallax-wrapper">
			
			<?php echo kktfwp_parallax(); ?>

			<div id="kk-parallax-title-wrapper" class="kk-parallax-title-wrapper">
				<div id="kk-parallax-title" class="page-title">
					<?php $source = rwmb_meta( 'kk_customTitle', false, kktfwp_postid() );						
					if ( $source ) : ?>						
						<?php echo wp_kses( rwmb_meta( 'kk_customdescription', false, kktfwp_postid()), kktfwp_kses_allowed_args()) ?>						
					<?php else: ?>					
						<?php the_title( '<h1>', '</h1>' ); ?>						
					<?php endif; ?>
				</div>	
			</div>

			<a id="kk-down" class="kk-down is-loading" href="#kk-to-me"></a>
		
		</div>				
		<?php endif; ?>
		
		<div class="content-inner-wrapper clearfix">
			<span id="kk-to-me"></span>	
		<?php if( $title_type == 'default' ) : ?>
			<div class="page-title">
				<?php the_title( '<h1>', '</h1>' ); ?>
			</div>
			
			<div class="divider clear"></div>	
		<?php endif; ?>
		
		<?php 
			while ( have_posts() ) : the_post();	
		?>
				<div id="none" class="inner-content">		
					<?php the_content(); ?>
				</div>
		<?php endwhile; ?>
			
			<?php if ( current_theme_supports( 'kk-cpt' ) ) : ?>
			
			<?php 
					$args = array(
						'post_type' => 'gallery',
						'posts_per_page' => -1
					);
					
					$filter_categories = rwmb_meta( 'kk_filter' );
					
					if ( !empty( $filter_categories ) && empty( rwmb_meta( '_kk_posts_to_display' ) ) ) {
						update_post_meta( get_the_ID(), '_kk_posts_to_display', 'category' );
						update_post_meta( get_the_ID(), '_kk_enbale_filter', 'true' );
					};
										
					if ( rwmb_meta( '_kk_posts_to_display' ) === 'category' ) {
						
						$kk_filter_terms = array();
						
						foreach ( $filter_categories as $category ) {
							$kk_filter_terms[] = $category-> term_id;
						};
						
						$args['tax_query'] = array(
								array(
									'taxonomy' 	=> 'gallery-type',
									'field'    	=> 'term_id',
									'terms' 	=> $kk_filter_terms
								)
						);							
					} elseif ( rwmb_meta( '_kk_posts_to_display' ) === 'custom' ) {
						$args['post__in'] = rwmb_meta( '_kk_defined_galleries' );					
					}		
				?>

			<?php if ( rwmb_meta( '_kk_enbale_filter' ) === 'true' ) : ?>
				<!-- start gallery filter -->
				<div id="filter">
					<a id="filter-trigger" href="#">
						<span><?php esc_html_e( 'Sort Albums', 'toppic' ) ?></span> 
						<i class="kk-icon kk-icon-angle-down"></i>
					</a>
					<ul>
						<li> 
							<a class="filter" href="#" data-filter=".all"><?php esc_html_e('All','toppic') ?></a>
						</li>
						
						<?php $categories = kktfwp_filter( $args, 'gallery-type' ); ?>
						
						<?php foreach ( $categories as $category ) : ?>		
							<li class="filter" data-filter=".<?php echo esc_attr( $category['slug'] ) ?>" >
								<a href="#" ><?php echo esc_html($category['name']) ?></a>
							</li>
						<?php endforeach; ?>	
					</ul>
				</div>
				<!-- end gallery filter -->
			<?php endif; ?>
			
			<?php if ( ( $kk_content_style == 'kk-full' ) & ( $kk_hp == true )) {
				echo '</div><div class="wide-portfolio">';
			} ?>
			
			<!-- start gallery items -->			
			<div class="gallery-wrapper">
					<ul class="isotope-container" id="<?php echo esc_attr($kk_col) ?>">
					
						<?php $query = new WP_Query( $args );

							while ( $query->have_posts() ) : $query->the_post();
						
							$terms =  get_the_terms( $post->ID, 'gallery-type' ); 
							$term_list = '';
							
							if( is_array($terms) ) {
								foreach( $terms as $term ) {
									$term_list .= $term->slug;
									$term_list .= ' ';
								}
							}
							
							switch( rwmb_meta( '_kk_linkTo' ) ) {
								case 'page':
									$postUrl = get_permalink( rwmb_meta( '_kk_linkToPage' ) );
									break;
								case 'custom':
									$postUrl = rwmb_meta( '_kk_customUrl' );									
									break;
								default:
									$postUrl = get_the_permalink();								
							}
						?>					
						
						<!-- start work -->
						
						<li id="<?php echo the_ID()?>" <?php post_class("$term_list isotope-item all"); ?> >
							
							<div class="portfolio-thumb effect-zoom linkto">	
								<a class="img-loading" href="<?php echo esc_attr( $postUrl ) ?>" <?php if ( rwmb_meta( '_kk_newTab' ) && rwmb_meta( '_kk_linkTo' ) !== 'default' ) echo 'target="_blank"'; ?> >
									<?php if( has_post_thumbnail() ): ?>								
										<?php echo get_the_post_thumbnail( get_the_id(), 'kktfwp-s', array( 'class' => 'responsively-lazy-img', 'data-lazy-load' => 1 ) ); ?>
									<?php endif; ?>
									<span class="kk-n"><?php esc_html_e('View','toppic'); ?></span>	
								</a>
							</div>	
							
							<div class="portfolio-title">
								<h2>
									<a href="<?php echo esc_attr( $postUrl ) ?>" <?php if ( rwmb_meta( '_kk_newTab' ) && rwmb_meta( '_kk_linkTo' ) !== 'default' ) echo 'target="_blank"'; ?> >
										<?php the_title(); ?>
									</a>
								</h2>
							</div>		
							<?php if (function_exists('printLikes')) { ?>
								<div class="kk-likes-portfolio">
									<?php printLikes(get_the_ID()); ?>
								</div>
							<?php } ?>
						</li>						
						
						<?php endwhile; wp_reset_postdata(); ?>
						<!-- end work -->
						<li id="kk-item-helper"></li>
						<li id="kk-item-helper"></li>
					</ul>
			</div>
			<!-- end latest work -->
			<?php endif; ?>
		</div>
		
		<?php get_template_part('extends/footer-area') ?>
		
	</div>
	<!-- end content wrapper -->
	<?php } ?>

<?php get_footer(); ?>