<?php
/**
 * Created by PhpStorm.
 * User: marco
 * Date: 28/06/18
 * Time: 9.11
 */

//
//
//o66fswp_acf_fields_filtered('membro_team',
//	'team_category', '<i class="fa fa-arrow-circle-o-down"></i>');
//
// TODO cambiare ordine di print della side
//o66fswp_acf_fields_filtered_by_year('press_zone', 'media_type', 'press_date');
//$video_label = 'Video';
//$media_type = 'video';
//$year = '2018';
//if (!empty($_GET['media_type'])) {
//
//}
global $scope_media_type;
function video_media_side() {
	global $post;
	global $scope_year;
	global $scope_media_type;
	global $global_post_id;


	$this_post_id = get_the_ID();
	//vars
	$label            =  get_field('label_video',$global_post_id );
	$first_query_var  = 'mediatype';
	$media_type_value = 'video';
	$year_query_var   = 'tyear';
	$fa_tag           = '<i class="fa fa-calendar-o"></i>';

	//get model from acf
	$year_array = []; ?>
	<?php if ( have_rows( $media_type_value . '_media' ) ) : ?>
		<?php while ( have_rows( $media_type_value . '_media' ) ) : the_row(); ?>
			<?php
			$date = get_sub_field( 'press_date' ); ?>
			<?php
			$year = substr( $date, 0, 4 );
			array_push( $year_array, $year );
		endwhile; ?>
	<?php else : ?>
		<?php // no rows found ?>
	<?php endif; ?>
	<?php
	$year_array_unique = array_unique( $year_array );
	rsort( $year_array_unique );
	//var_dump( $year_array_unique );
	//view
	if ( ! empty( $year_array_unique ) ) : ?>
        <div class="aside__media-year-list hidden-sm hidden-xs">
            <div class="media-year-list__heading">
                <h3><?php echo $label; ?></h3>
            </div>
            <ul>
				<?php foreach ( $year_array_unique as $item ) :
					//submodel
					$li_class_attribute = '';
					if ( $item === $scope_year ) {

						if ( $scope_media_type === 'video' ) {
							//var_dump($scope_media_type);
							$li_class_attribute = ' class="current"';
						}
					}
					$media_query_url = get_the_permalink( $this_post_id ) . '?' . $first_query_var . '=' . $media_type_value . '&' . $year_query_var . '=' . $item;
					?>
                    <li<?php echo $li_class_attribute; ?>><a
                                href="<?php echo $media_query_url; ?>"><?php echo $fa_tag . $item; ?></a></li>
				<?php endforeach;; ?>
            </ul>
        </div>
	<?php endif;
}

function press_media_side() {
	global $post;
	global $global_post_id;
	global $scope_year;
	global $scope_media_type;
	$this_post_id = get_the_ID();
	//vars
	$label            =  get_field('label_stampa',$global_post_id );
	$first_query_var  = 'mediatype';
	$media_type_value = 'press';
	$year_query_var   = 'tyear';
	$fa_tag           = '<i class="fa fa-calendar-o"></i>';

	//get model from acf
	$year_array = []; ?>
	<?php if ( have_rows( $media_type_value . '_media' ) ) : ?>
		<?php while ( have_rows( $media_type_value . '_media' ) ) : the_row(); ?>
			<?php
			$date = get_sub_field( 'press_date' ); ?>
			<?php
			$year = substr( $date, 0, 4 );
			array_push( $year_array, $year );
		endwhile; ?>
	<?php else : ?>
		<?php // no rows found ?>
	<?php endif; ?>
	<?php
	$year_array_unique = array_unique( $year_array );
	rsort( $year_array_unique );
	//var_dump( $year_array_unique );
	//view
	if ( ! empty( $year_array_unique ) ) : ?>
        <div class="aside__media-year-list hidden-xs hidden-sm">
            <div class="media-year-list__heading">
                <h3><?php echo $label; ?></h3>
            </div>
            <ul>
				<?php foreach ( $year_array_unique as $item ) :
					//submodel
					//submodel
					$li_class_attribute = '';
					if ( $item === $scope_year ) {

						if ( $scope_media_type === 'press' ) {
							//var_dump($scope_media_type);
							$li_class_attribute = ' class="current"';
						}
					}
					$media_query_url = get_the_permalink( $this_post_id ) . '?' . $first_query_var . '=' . $media_type_value . '&' . $year_query_var . '=' . $item;
					?>
                    <li<?php echo $li_class_attribute;?>><a href="<?php echo $media_query_url; ?>"><?php echo $fa_tag . $item; ?></a></li>
				<?php endforeach; ?>
            </ul>
        </div>
	<?php endif;
}

;
if ( $scope_media_type === 'video' ) {
	video_media_side();
	press_media_side();
} else {
	press_media_side();
	video_media_side();
}
