<?php
/**
 * Template Name: Sections
 *
 * Page template with normal content area including selected sections after the post.
 * No Sidebars
 *
 */

 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_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();

      // post style attribute
      $post_style = '';

      $color_hex_pattern = '/#([a-f0-9]{3}){1,2}\b/i';
      $has_background_color = isset($post_meta['_background-color']) && preg_match($color_hex_pattern, $post_meta['_background-color']);
      $has_post_style = $has_background_color;

      if ( $has_post_style ) {
        $post_style = ' style="';
      }

      // background-color
      if ( $has_background_color ) {
        $post_style .= 'background-color:' . $post_meta['_background-color'] . ';';
        $post_class .= ' has-background-color';
      }
      if ( $has_post_style ) {
        $post_style .= '"';
      }

      // Dark or light color scheme
      if ( isset($post_meta['_color-scheme']) ) {
        $post_class .= ' color-scheme-' . $post_meta['_color-scheme'];
      }


      // Show image before or after the content
      if ( isset($post_meta['_image-position']) ) {
        $post_class .= ' image-position-' . $post_meta['_image-position'];
      }

      // Show image with shadow or not
      if ( isset($post_meta['_image-shadow']) ) {
        $post_class .= ' thumbnail-' . $post_meta['_image-shadow'];
      }

      // 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 "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());
      }
  ?>
<?php if ( $has_background_color ) { ?>
  <style type="text/css">
    #main .sections-header .title::before { border-color:<?php echo $post_meta['_background-color']; ?>; }
  </style>
<?php } ?>
  <article id="content" <?php post_class($post_class) ?><?php echo $post_style; ?>>
  <?php if ( has_post_thumbnail() ) { ?>
    <span class="post-thumbnail"><?php the_post_thumbnail('full'); ?></span>
  <?php } ?>
    <div class="content inner">
      <header class="content-header">
        <div class="inner">
        <?php if ( !empty($subheadline) ) { ?>
          <div class="subheadline content">
            <?php echo $subheadline; ?>
          </div>
        <?php } ?>
          <h1 class="title"><?php the_title(); ?></h1>
        <?php if ( !empty($download_url) ) { ?>
          <a class="download" href="<?php echo $download_url; ?>"<?php echo $download_target; ?>><?php echo __('Download', 'sandbox'); ?></a>
        <?php } ?></h1>
        <?php if ( has_excerpt() ) { ?>
          <div class="intro">
            <?php the_excerpt(); ?>
          </div>
        <?php } ?>
        <?php if ( $meta = sandbox_get_meta_data($content_meta_args) ) { ?>
          <ul class="meta">
            <?php echo $meta; ?>
          </ul>
        <?php } ?>
        </div>
      </header>
    <?php if ( !empty($content) ) { ?>
      <?php echo $content; ?>
    <?php } ?>
    </div>
  <?php if ( isset($post_taxonomies['benefit']) && !empty($post_taxonomies['benefit']) ) { ?>
    <div class="<?php echo esc_attr(sanitize_html_class(strtolower($post_taxonomies['benefit']['label']))); ?>">
    <?php foreach ( $post_taxonomies['benefit']['terms'] as $benefit ) { ?>
      <?php
        $benefit_meta = get_term_meta($benefit['term_id']);
        $benefit_thumbnail = $benefit_meta['_benefit-image'] !== false ? wp_get_attachment_image($benefit_meta['_benefit-image'][0], 'full') : false;
        $benefit_icon_name = $benefit_meta['_benefit-icon'] !== false ? $benefit_meta['_benefit-icon'][0] : false;
      ?>
      <div class="term-<?php echo esc_attr($post_taxonomies['benefit']['slug']); ?>">
        <div class="term-thumbnail content">
        <?php if ( $benefit_thumbnail ) { ?>
          <?php echo $benefit_thumbnail; ?>
        <?php } else if ( $benefit_icon_name ) { ?>
          <span class="material-icon"><?php echo $benefit_icon_name; ?></span>
        <?php } ?>
        </div>
        <div class="inner content">
          <?php echo apply_filters('the_content', $benefit['description']); ?>
        </div>
      </div>
    <?php } ?>
    </div>
  <?php } ?>
  <?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.php'); ?>
  <?php  } ?>
  </article><!-- .post -->
<?php
  /**
   * Sectiongroups
   */
  if ( isset($post_taxonomies['sectiongroup']) ) {
    $sections_terms = $post_taxonomies['sectiongroup']['terms'];
?>
  <?php foreach ( $sections_terms as $sections_term ) { ?>
    <?php
    /**
     * Sections
     */
    $sections_options = array(
      'post_type' => 'section',
      'tax_query' => array(array(
        'taxonomy' => $post_taxonomies['sectiongroup']['slug'],
        'field' => 'slug',
        'terms' => $sections_term['slug'],
      )),
      'orderby' => 'menu_order',
      'order' => 'ASC',
    );
    $post_class .= ' sections-header';
    $sections_query = new WP_Query($sections_options);
    ?>
    <?php if ( $sections_query->have_posts() ) { ?>
      <?php if ( $post_meta['_sectiongroup-title-display'] === 'show' ) { ?>
      <header class="<?php echo $post_class; ?>"<?php echo $post_style; ?>>
        <div class="inner">
          <h2 class="title"><span><?php echo $sections_term['label']; ?></span></h2>
        </div>
      </header>
      <?php } ?>
      <?php while ( $sections_query->have_posts() ) {
        $sections_query->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);

            $content = '';
            $post_class = 'no-download';
            $content_meta_args = array();

            // post style attribute
            $post_style = '';

            $color_hex_pattern = '/#([a-f0-9]{3}){1,2}\b/i';
            $has_background_color = isset($post_meta['_background-color']) && preg_match($color_hex_pattern, $post_meta['_background-color']);
            $has_post_style = $has_background_color;

            if ( $has_post_style ) {
              $post_style = ' style="';
            }

            // background-color
            if ( $has_background_color ) {
              $post_style .= 'background-color:' . $post_meta['_background-color'] . ';';
              $post_class .= ' has-background-color';
            }
            if ( $has_post_style ) {
              $post_style .= '"';
            }

            // Dark or light color scheme
            if ( isset($post_meta['_color-scheme']) ) {
              $post_class .= ' color-scheme-' . $post_meta['_color-scheme'];
            }

            // Show image before or after the content
            if ( isset($post_meta['_image-position']) ) {
              $post_class .= ' image-position-' . $post_meta['_image-position'];
            }

            // Show image with shadow or not
            if ( isset($post_meta['_image-shadow']) ) {
              $post_class .= ' thumbnail-' . $post_meta['_image-shadow'];
            }

            // 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 "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());
            }
        ?>
          <article id="section-<?php sandbox_the_slug(); ?>" <?php post_class($post_class) ?><?php echo $post_style; ?>>
          <?php if ( has_post_thumbnail() ) { ?>
            <span class="post-thumbnail"><?php the_post_thumbnail('full'); ?></span>
          <?php } ?>
            <div class="content inner">
            <?php if ( !has_term('testimonials', 'sectiongroup') ) { ?>
              <header class="content-header">
                <div class="inner">
                <?php if ( !empty($subheadline) ) { ?>
                  <div class="subheadline">
                    <?php echo $subheadline; ?>
                  </div>
                <?php } ?>
                  <h2 class="title"><?php the_title(); ?></h2>
                <?php if ( !empty($download_url) ) { ?>
                  <a class="download" href="<?php echo $download_url; ?>"<?php echo $download_target; ?>><?php echo __('Download', 'sandbox'); ?></a>
                <?php } ?></h1>
                <?php if ( has_excerpt() ) { ?>
                  <div class="intro">
                    <?php the_excerpt(); ?>
                  </div>
                <?php } ?>
                <?php if ( $meta = sandbox_get_meta_data($content_meta_args) ) { ?>
                  <ul class="meta">
                    <?php echo $meta; ?>
                  </ul>
                <?php } ?>
                </div>
              </header>
            <?php } ?>
            <?php if ( !empty($content) ) { ?>
              <?php echo $content; ?>
            <?php } ?>
            </div>
          <?php if ( isset($post_taxonomies['benefit']) && !empty($post_taxonomies['benefit']) ) { ?>
            <div class="<?php echo esc_attr(sanitize_html_class(strtolower($post_taxonomies['benefit']['label']))); ?>">
            <?php foreach ( $post_taxonomies['benefit']['terms'] as $benefit ) { ?>
              <?php
                $benefit_meta = get_term_meta($benefit['term_id']);
                $benefit_thumbnail = $benefit_meta['_benefit-image'] !== false ? wp_get_attachment_image($benefit_meta['_benefit-image'][0], 'full') : false;
                $benefit_icon_name = $benefit_meta['_benefit-icon'] !== false ? $benefit_meta['_benefit-icon'][0] : false;
              ?>
              <div class="term-<?php echo esc_attr($post_taxonomies['benefit']['slug']); ?>">
                <div class="term-thumbnail content">
                <?php if ( $benefit_thumbnail ) { ?>
                  <?php echo $benefit_thumbnail; ?>
                <?php } else if ( $benefit_icon_name ) { ?>
                  <span class="material-icon"><?php echo $benefit_icon_name; ?></span>
                <?php } ?>
                </div>
                <div class="inner content">
                  <?php echo apply_filters('the_content', $benefit['description']); ?>
                </div>
              </div>
            <?php } ?>
            </div>
          <?php } ?>
          <?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.php'); ?>
          <?php  } ?>
          </article><!-- .post -->
        <?php  } ?>
      <?php  } ?>
      <?php wp_reset_postdata(); ?>
    <?php  } ?>
  <?php  } ?>
<?php  } ?>
  <?php  } ?>

<?php get_footer() ?>
