<?php get_header() ?>

<?php
  /**
   * Collect term data
   * =================
   */
  // custom term object with usefull data
  $term = sandbox_get_archive_term();
  // gets an array of all queried post types
  // instead of the post type of the first post with get_post_type()
  $post_type = $term->post_types;
  // archive related default values
  $related_taxonomies = array();
  $public_taxonomies = get_taxonomies(array('public' => true ));
  $post_ids = get_posts(array(
    'post_type' => $post_type,
    'posts_per_page' => -1,
    'fields' => 'ids', // return an array of ids
  ));
  // Default more button text
  $more_text = __('Weiterlesen', 'sandbox');
  $more_text_label = ''; // used to use with printf
  $use_tax_more_text = false;
  // archive specificas based on archive type
  if ( is_tax()  || is_tag()  || is_category() ) {
    $related_taxonomies = array($term->taxonomy['WP_Taxonomy']->name);
    $meta_args = array('date', 'author');

    if ( $term->taxonomy['WP_Taxonomy']->name === 'typ' ) {
      $meta_args = array();
    }
    // More text button, %s gets replaced by
    // $more_text_label which will be $post_type_object->labels->singular_name
    // English default
    $more_text = __('View', 'sandbox') . ' %s';
    $use_tax_more_text = true;
  } else if ( is_post_type_archive() ) {
    $related_taxonomies = array_intersect(get_object_taxonomies($post_type), $public_taxonomies);
    // dont display post thumbnails in archives
    // manual overwrite, requested by client
    // $term->has_thumbnail = false;
    // echo '<!--', var_dump($taxonomies), '-->';
    // echo 'c: ', var_dump($related_taxonomies);

  }
  $header_style = '';
  if ( $term->has_thumbnail ) {
    if ( is_post_type_archive() ) {
      $term_thumbnail = array($term->thumbnail);
    } else {
      $term_thumbnail = wp_get_attachment_image_src($term->thumbnail_id, 'sandbox-hero');
    }
    $header_style = ' style="background-image:url(' . $term_thumbnail[0] . ');"';
  }
?>
<section id="content" class="articles wrapper container<?php echo $term->classes; ?>">
  <header <?php post_class('section-header'); ?><?php echo $header_style; ?>>
    <h1 class="title"><?php echo $term->title; ?></h1>
  <?php if ( $term->has_description ) { ?>
    <div class="intro">
      <?php echo $term->description; ?>
    </div>
  <?php } ?>
  </header>
  <?php
    /*
    * @Sidebar Content before
    */
    get_sidebar('content-before'); ?>
  <div class="inner">
	<?php if ( is_date() ) { rewind_posts(); } ?>
  <?php if ( have_posts() ) { ?>
<?php /* <h2 class="title"><?php echo $term->byline ?></h2> */ ?>
    <?php while ( have_posts() ) : the_post() ?>

      <?php
        $post_id = get_the_ID();
        $post_type = get_post_type($post_id);
        $post_meta = sandbox_get_post_meta($post_id);

        if ( !isset($post_meta['_hide']) || !filter_var($post_meta['_hide'], FILTER_VALIDATE_BOOLEAN) ) {
          $post_taxonomies = sandbox_get_the_taxonomies_and_terms($post_id);
          $more_text = sandbox_get_more_text($post_type, $post_meta);
          $more_url = sandbox_get_more_url($post_type, $post_meta);

          $excerpt = '';
          $has_custom_more_url = $more_url !== get_permalink();
          $more_class = 'more';
          $more_target = '';
          $post_class = 'no-download';
          $content_meta_args = array('date');
          $subheadline = isset($post_meta['_subheadline']) ? apply_filters('the_content', $post_meta['_subheadline']) : '';

          // post thumbnail as background image
          $post_style = '';
          if ( has_post_thumbnail() ) {
            $post_style = ' style="background-image:url(' . get_the_post_thumbnail_url($post_id, 'sandbox-default') . ');"';
          }

          if ( $has_custom_more_url ) {
            if ( parse_url($more_url, PHP_URL_HOST) !== $site_host ) {
              $more_target = ' target="_blank" rel="noopener"';
              $more_class .= ' external';
            }
            if ( $post_type === 'link' ) {
              $more_text = __('go to website', 'sandbox');
            }
          }

          if ( !empty($description) ) {
            $excerpt = $description;
          }

          // Post type "page"
          if ( $post_type === 'page' ) {
            $content_meta_args = false;
          }

          // Post type "service"
          if ( $post_type === 'service' ) {
            $content_meta_args = false;
          }

          // Post type "product" fields
          $produkt_pdf = isset($post_meta['_pdf-download']) ? $post_meta['_pdf-download'] : '';
          $produkt_url = isset($post_meta['_product-url']) ? $post_meta['_product-url'] : '';

          if ( !empty($produkt_pdf) ) {
            $produkt_url = wp_get_attachment_url($produkt_pdf);
          }
          if ( $post_type === 'produkt' ) {
            $content_meta_args = false;
            $more_text = __('Details', 'sandbox');
          }

          // Download button
          $download_url = !empty($produkt_pdf) ? $produkt_pdf : '';
          $download_target = '';

          if ( !empty($download_url) ) {
            if ( parse_url($download_url, PHP_URL_HOST) !== $site_host ) {
              $download_target = ' target="_blank" rel="noopener"';
            }
            $post_class = 'has-download';
          }

          if ( empty($excerpt) ) {
            $excerpt = apply_filters('the_content', has_excerpt() ? get_the_excerpt() : wp_trim_words(get_the_content(), 40));
          }
          // more class attribute
          if ( $more_class !== '' ) {
            $more_class = ' class="' . $more_class . '"';
          }
      ?>

      <?php if ( is_tag('hoeren-sehen') ) { ?>
      <?php } else { ?>
      <?php } ?>

		  <article id="post-<?php the_ID() ?>" <?php post_class($post_class) ?>>
		    <div class="inner">
        <?php if ( has_post_thumbnail() ) { ?>
          <a href="<?php the_permalink() ?>" class="post-thumbnail"<?php echo $post_style; ?>><?php the_post_thumbnail('sandbox-default'); ?></a>
        <?php } ?>
          <div class="content">
          <?php if ( !empty($subheadline) ) { ?>
            <div class="subheadline">
              <?php echo $subheadline; ?>
            </div>
          <?php } ?>
			      <h2 class="title"><a href="<?php echo $more_url; ?>"<?php echo $more_target; ?>><?php the_title() ?></a></h2>
          <?php if ( !empty($excerpt) ) { ?>
            <?php echo $excerpt; ?>
          <?php } ?>
            <a class="button" href="<?php the_permalink() ?>"><?php printf($more_text); ?></a>
        <?php /* if ( !empty($post_taxonomies) ) { ?>
            <dl class="clearfix">
          <?php foreach ( $post_taxonomies as $post_taxonomy ) { ?>
            <?php if ( !empty($post_taxonomy) && $post_taxonomy['slug'] !== 'lern_stufe' ) { ?>
              <dt class="<?php echo $post_taxonomy['slug']; ?>"><?php echo $post_taxonomy['label']; ?></dt>
              <?php foreach ( $post_taxonomy['terms'] as $post_taxonomy_term ) { ?>
              <dd class="<?php echo $post_taxonomy_term['slug']; ?>"><?php echo $post_taxonomy_term['label']; ?></dd>
              <?php } ?>
            <?php } ?>
          <?php } ?>
            </dl>
        <?php } */ ?>
        <?php if ( $meta = sandbox_get_meta_data(array('rtime', 'date', 'author', 'terms')) ) { ?>
          <ul class="meta">
            <?php echo $meta; ?>
          </ul>
        <?php } ?>
			    </div>
			  </div>
		  </article><!-- .post -->
      <?php } ?>
    <?php endwhile; ?>

		<?php if ( sandbox_archive_is_paged($term) ) { ?>
	    <?php if ( function_exists('wp_pagenavi') ) {
	    	wp_pagenavi(); // http://wordpress.org/plugins/wp-pagenavi/
	    } else {
	    ?>
			<nav id="nav-below" class="navigation">
				<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&laquo;</span> Older results', 'sandbox' ) ) ?></div>
				<div class="nav-next"><?php previous_posts_link( __( 'Newer results <span class="meta-nav">&raquo;</span>', 'sandbox' ) ) ?></div>
			</nav>
	    <?php } ?>
    <?php } ?>

	<?php } else { ?>

		<article id="post-0" class="post no-results not-found">
		  <div class="inner">
			  <div class="content">
			    <h2 class="title"><?php printf(__( 'Not Found', 'sandbox' )); ?>  :-(</h2>
				  <p><?php

				    if ( is_tag() ) {
						  printf(__('No posts written tagged with', 'sandbox') . ' ' . single_tag_title(false));
				    } elseif ( is_category() ) {
						  printf(__('No posts written in', 'sandbox') . ' ' . single_cat_title(false));
				    } elseif ( is_author() ) {
				    	printf(get_the_author() . ' ' . __("didn't wrote a post yet.", 'sandbox')); // 'hat noch keine Artikel verfasst.'
				    } else {
				    	printf(__('Apologies, but we were unable to find what you were looking for. Perhaps  searching will help.', 'sandbox'));
				    }

				  ?></p>
  		    <a href="<?php echo esc_url( home_url() ); ?>"><?php printf(__('Go to the homepage', 'sandbox')); // 'zur Startseite gehen' ?></a></p>
			  </div>
			  <form id="searchform-no-results" class="blog-search" method="get" action="<?php echo esc_url( home_url() ); ?>">
				  <input id="s-no-results" name="s" class="text" type="text" value="<?php the_search_query() ?>" size="40" />
				  <input class="button" type="submit" value="<?php printf(__('Find', 'sandbox')); ?>" />
    		</form>
    	</div>
		</article><!-- .post -->

	<?php } ?>
  </div>
  <?php
    /*
    * @Sidebar Content after
    */
    get_sidebar('content-after'); ?>
</section>
<?php get_footer() ?>
