<?php get_header() ?>

  <?php the_post() ?>

  <?php
    $site_host = parse_url(get_site_url(), PHP_URL_HOST);
    $post_id = get_the_ID();
    $post_type = get_post_type($post_id);
    $post_type_object = get_post_type_object($post_type);
    $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);

      $content = '';
      $post_class = 'no-download';
      $content_meta_args = array('rtime', 'author', 'date');

      // Post type "page" fields
      $subheadline = isset($post_meta['_subheadline']) ? apply_filters('the_content', $post_meta['_subheadline']) : '';

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

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

      // Post type "aktivitaet"
      if ( $post_type === 'aktivitaet' ) {
        $content_meta_args = array('period');
        // echo var_dump($post_meta);
        $can_book = isset($post_meta['_can-book']) ? filter_var($post_meta['_can-book'], FILTER_VALIDATE_BOOLEAN) : false;
        $booking_form_slug = isset($post_meta['_booking-form']) ? $post_meta['_booking-form'] : '';
        $conditions_table = isset($post_meta['_conditions-table']) ? $post_meta['_conditions-table'] : '';
        $terms = isset($post_meta['_terms']) ? $post_meta['_terms'] : '';
        $links_internal = isset($post_meta['_links-internal']) ? $post_meta['_links-internal'] : '';
        $links_external = isset($post_meta['_links-external']) ? $post_meta['_links-external'] : '';
        $pdf_download = isset($post_meta['_pdf-download']) ? $post_meta['_pdf-download'] : '';
      }

      // Post type "news"
      if ( $post_type === 'news' ) {
        $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;
      }

      // 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($content) ) {
        $content = apply_filters('the_content', get_the_content());
      }

      $related_options = array(
        'orderby' => 'menu_order',
        'order' => 'DESC',
        'posts_per_page' => 3,
      );

      $has_related = false;
      $post_tag_ids = array();

      if ( !empty($post_taxonomies) ) {
        foreach( $post_taxonomies['post_tag']['terms'] as $post_tag ) {
          $post_tag_ids[] = $post_tag['term_id'];
        }
	  }
      if ( !empty($post_tag_ids) ) {
        $related_options = array_merge($related_options, array(
          'post_type' => $post_type,
          'tax_query' => array(array(
            'taxonomy' => 'post_tag',
            'terms' => $post_tag_ids,
          )),
          'post__not_in' => array($post_id),
        ));
        $has_related = true;
      }
  ?>
  <article id="content" <?php post_class($post_class) ?>>
    <?php
      /*
      * @Sidebar Content before
      */
      get_sidebar('content-before'); ?>
    <div class="content inner">
      <header class="content-header">
        <div class="inner">
          <h1 class="title"><?php the_title(); ?></h1>
        <?php if ( !empty($subheadline) ) { ?>
          <div class="subheadline">
            <?php echo $subheadline; ?>
          </div>
        <?php } ?>
        <?php if ( $meta = sandbox_get_meta_data($content_meta_args) ) { ?>
          <ul class="meta">
            <?php echo $meta; ?>
          </ul>
        <?php } ?>
        <?php if ( has_excerpt() ) { ?>
          <div class="intro">
            <?php the_excerpt(); ?>
          </div>
        <?php } ?>
        </div>
      </header>
    <?php if ( $post_type === 'aktivitaet' ) { ?>
      <nav class="<?php echo $post_type; ?>-tabs">
        <ul class="menu tabs">
        <?php if ( !empty($content) ) { ?>
          <li class="<?php echo $post_type; ?>-description"><a href="#<?php echo $post_type; ?>-beschreibung"><?php echo __('Beschreibung', 'sandbox'); ?></a></li>
        <?php } ?>
        <?php if ( !empty($conditions) ) { ?>
          <li class="<?php echo $post_type; ?>-conditions"><a href="#<?php echo $post_type; ?>-konditionen"><?php echo __('Konditionen', 'sandbox'); ?></a></li>
        <?php } ?>
        <?php if ( !empty($terms) ) { ?>
          <li class="<?php echo $post_type; ?>-terms"><a href="#<?php echo $post_type; ?>-bedingungen"><?php echo __('Bedingungen', 'sandbox'); ?></a></li>
        <?php } ?>
        <?php if ( $can_book && !empty($booking_form_slug) ) { ?>
          <li class="<?php echo $post_type; ?>-booking"><a href="#<?php echo $post_type; ?>-anmeldung"><?php echo __('Anmeldung', 'sandbox'); ?></a></li>
        <?php } ?>
          <li class="<?php echo $post_type; ?>-downloads"><a href="#<?php echo $post_type; ?>-downloads"><?php echo __('Links & Downloads', 'sandbox'); ?></a></li>
        </ul>
      </nav>
    <?php } ?>
    <?php if ( has_post_thumbnail() ) { ?>
      <span class="post-thumbnail"><?php the_post_thumbnail('blog-hero'); ?></span>
    <?php } ?>
    <?php if ( !empty($content) ) { ?>
      <section id="<?php echo $post_type; ?>-beschreibung">
        <?php echo $content; ?>
      </section>
    <?php } ?>
  <?php if ( $post_type === 'aktivitaet' ) { ?>
    <?php if ( !empty($conditions_table) ) { ?>
      <?php
        $table_content = '';

        foreach ( $conditions_table as $condition_row ) {
          if ( !empty($condition_row) ) {
            $table_content .= '<tr>';
            foreach ( $condition_row as $condition_cell ) {
              $table_content .= '<td>';
              $table_content .= empty($condition_cell) ? '&nbsp;' : $condition_cell;
              $table_content .= '</td>';
            }
            $table_content .= '</tr>';
          }
        }
      ?>
      <section id="<?php echo $post_type; ?>-konditionen" class="conditions">
        <h3><?php echo __('Konditionen', 'sandbox'); ?></h3>
        <table>
          <colgroup>
            <col width="auto">
            <col width="80px">
            <col width="80px">
          </colgroup>
          <tbody>
            <?php echo $table_content; ?>
          </tbody>
        </table>
      </section>
    <?php } ?>
    <?php if ( !empty($terms) ) { ?>
      <section id="<?php echo $post_type; ?>-bedingungen" class="terms">
        <h3><?php echo __('Teilnahmebedingungen ', 'sandbox'); ?></h3>
        <?php echo apply_filters('the_content', $terms); ?>
      </section>
    <?php } ?>
    <?php if ( $can_book && !empty($booking_form_slug) ) { ?>
      <section id="<?php echo $post_type; ?>-anmeldung" class="booking">
        <h3><?php echo __('Anmeldung', 'sandbox'); ?> <?php the_title(); ?></h3>
        <?php echo do_shortcode('[cf7form cf7key="' . $booking_form_slug . '"]'); ?>
      </section>
    <?php } ?>
    <?php if ( !empty($links_internal) || !empty($links_external) || !empty($pdf_download) ) { ?>
      <section id="<?php echo $post_type; ?>-downloads" class="downloads">
        <h3><?php echo __('Links & Downloads', 'sandbox'); ?></h3>
        <ul class="links-downloads">
      <?php if ( !empty($links_internal) ) { ?>
        <?php foreach ( $links_internal as $link_id ) { ?>
          <li class="internal"><a href="<?php echo get_permalink(intval($link_id)); ?>"><?php echo get_the_title(intval($link_id)); ?></a></li>
        <?php } ?>
      <?php } ?>
      <?php if ( !empty($links_external) ) { ?>
        <?php foreach ( $links_external as $link ) { ?>
          <li class="external"><a href="<?php echo $link['url']; ?>" target="_blank" rel="nofollow,noopener"><?php echo $link['text']; ?></a></li>
          <?php } ?>
      <?php } ?>
        <?php if ( !empty($pdf_download) ) { ?>
          <li class="download"><a href="<?php echo wp_get_attachment_url($pdf_download); ?>" target="_blank"><?php echo __('Infos im PDF herunterladen', 'sandbox'); ?></a></li>
        <?php } ?>
        </ul>
      </section>
    <?php } ?>
  <?php } ?>
    </div>
    <?php
      /*
      * @Sidebar Content after
      */
      get_sidebar('content-after'); ?>
  <?php  // If comments are open or we have at least one comment, load up the comment template. ?>
  <?php  if ( comments_open() || get_comments_number() ) { ?>
    <?php comments_template('/comments-alpha_download.php'); ?>
  <?php  } ?>
  </article><!-- .post -->
  <?php  } ?>
<?php if ( $post_type === 'post' ) { ?>
  <h2 class="title author-title"><?php echo __('About the author', 'sandbox'); ?></h2>
  <div class="author-information">
    <?php
      $author_name = get_the_author();
      $author_position = get_the_author_meta('_position');
      $author_description = get_the_author_meta('description');
      $author_image = get_wp_user_avatar(get_the_author_ID(), 'thumbnail-semi-large');
      $author_linkedin_name = get_the_author_meta('_linkedin');
      $author_linkedin_url = 'https://www.linkedin.com/in/' . $author_linkedin_name;
    ?>

  <?php if ( $author_image ) { ?>
    <span class="author-image"><?php echo $author_image; ?></span>
  <?php } ?>
    <div class="content">
      <h3><?php echo $author_name; ?></h3>
    <?php if ( $author_position ) { ?>
      <p class="position"><?php echo $author_position; ?></p>
    <?php } ?>
    <?php if ( $author_description ) { ?>
      <p><?php echo $author_description; ?></p>
    <?php } ?>
      <ul class="contacts">
        <li class="linkedin"><a href="<?php echo $author_linkedin_url; ?>" target="_blank" rel="noopener, noreferrer"><?php echo $author_name; ?></a></li>
      </ul>
    </div>
  </div>
<?php } ?>
  <div class="blog-navigation">
  <?php if ( $post_type === 'post' ) { ?>
    <?php
      $blog_posts_page_id = intval(get_option('page_for_posts'));
    ?>
    <a class="button back-to-blog" href="<?php echo get_permalink($blog_posts_page_id); ?>"><?php echo __('Back to blog', 'sandbox'); ?></a>
  <?php } ?>
    <nav class="navigation post-navigation" aria-label="<?php echo __('Posts', 'sandbox'); ?>">
      <h2 class="screen-reader-text"><?php echo __('Post navigation', 'sandbox'); ?></h2>
      <div class="nav-links">
        <div class="nav-previous">
          <?php previous_post_link('%link', '<span class="meta-nav" aria-hidden="true">' . __('Vorige ' . $post_type_object->labels->singular_name, 'sandbox') . '</span> ' . '<span class="screen-reader-text">' . __('Previous post:', 'sandbox') . '</span> ' . '<span class="post-title">%title</span>'); ?>
        </div>
        <div class="nav-next">
          <?php next_post_link('%link', '<span class="meta-nav" aria-hidden="true">' . __('Nächste ' . $post_type_object->labels->singular_name, 'sandbox') . '</span> ' . '<span class="screen-reader-text">' . __('Next post:', 'sandbox') . '</span> ' . '<span class="post-title">%title</span>'); ?>
        </div>
      </div>
    </nav>
  </div>
  <?php
    if ( $has_related ) {
      $related = new WP_Query($related_options);
    }
  ?>
  <?php if ( $has_related && $related->have_posts() ) { ?>
    <h2 class="title related-title"><?php echo __('Related posts', 'sandbox'); ?></h2>
    <div class="related-posts">
      <div class="inner">
      <?php while ( $related->have_posts() ) {
          $related->the_post(); ?>
        <?php

          $post_type = get_post_type();
          $post_meta = get_post_custom();
          $more_text = sandbox_get_more_text($post_type, $post_meta);
          $excerpt = has_excerpt() ? get_the_excerpt() : wp_trim_words(get_the_content(), 40);
          $excerpt .= ' <span class="more">' . $more_text . '</span>';

        ?>
        <a <?php post_class('related-post') ?> href="<?php the_permalink(); ?>">
          <figure class="related-thumbnail"><?php the_post_thumbnail('sandbox-default'); ?></figure>
          <div class="inner content">
            <h3><?php the_title(); ?></h3>
            <?php echo apply_filters('the_content', $excerpt); ?>
          <?php if ( $meta = sandbox_get_meta_data(array('rtime', 'author', 'date')) ) { ?>
            <ul class="meta">
              <?php echo $meta; ?>
            </ul>
          <?php } ?>
          </div>
        </a>
      <?php } ?>
      </div>
    </div>
  <?php } ?>
  <?php wp_reset_postdata(); ?>
<?php get_footer() ?>
