<?php
/**
 * Template Name: Team
 *
 * Page template with normal content area including an events section after the post.
 *
 */

 get_header() ?>

  <?php the_post() ?>

  <section id="content" class="articles wrapper container">
    <header id="post-<?php the_ID(); ?>" <?php post_class('section-header'); ?>>
      <h1 class="title"><?php the_title() ?></h1>
      <div class="intro content">
        <?php the_content() ?>
      </div>
    </header>
    <?php
      /*
      * @Sidebar Content before
      */
      get_sidebar('content-before'); ?>
    <div class="inner">
      <?php
        /**
         * Team members with "_on_top" option
         * ordered by menu_order      *
         */
        $team_on_top_options = array(
          'post_type' => 'team',
          'meta_query' => array(
            array(
              'key' => '_on_top',
              'value' => 1,
              'compare' => 'LIKE',
            ),
          ),
          'posts_per_page' => -1,
          'orderby' => 'menu_order',
          'order' => 'ASC',
        );
        $team_on_top_query = new WP_Query($team_on_top_options);
      ?>
  <?php if ( $team_on_top_query->have_posts() ) { ?>

    <?php while ( $team_on_top_query->have_posts() ) {
      $team_on_top_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) ) {
            $excerpt = apply_filters('the_content', has_excerpt() ? get_the_excerpt() : '');
            $team_position = $post_meta['_team-position'];
            $team_phone = $post_meta['_team-phone'];
            $team_phone_display = sandbox_sanitize_phone($team_phone, true);
            $team_email = $post_meta['_team-email'];
            $team_linkedin = $post_meta['_team-linkedin'];

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

        ?>
        <article id="post-<?php the_ID() ?>" <?php post_class(); ?>>
          <div class="inner">
          <?php if ( has_post_thumbnail() ) { ?>
            <span class="post-thumbnail"<?php echo $thumbnail_style; ?>><?php the_post_thumbnail('sandbox-default'); ?></span>
          <?php } ?>
            <div class="content">
              <h2><?php the_title() ?></h2>
            <?php if ( !empty($team_position) ) { ?>
              <p class="position"><strong><?php echo $team_position; ?></strong></p>
            <?php } ?>
            <?php if ( !empty($excerpt) ) { ?>
              <?php echo $excerpt; ?>
            <?php } ?>
              <ul class="contacts">
              <?php if ( !empty($team_phone) ) { ?>
                <li class="phone"><a href="tel:<?php echo $team_phone; ?>"><?php echo $team_phone_display; ?></a></li>
              <?php } ?>
              <?php if ( !empty($team_email) ) { ?>
                <li class="mail"><a href="mailto:<?php echo $team_email; ?>"><?php echo $team_email; ?></a></li>
              <?php } ?>
              <?php if ( !empty($team_linkedin) ) { ?>
                <li class="linkedin"><a href="<?php echo $team_linkedin; ?>" target="_blank" rel="noreferrer nofollow"><?php echo __('Get in touch', 'sandbox'); ?></a></li>
              <?php } ?>
              </ul>
            </div>
          </div>
        </article><!-- .post -->
      <?php } ?>
    <?php } ?>
    <?php wp_reset_postdata(); ?>
  <?php } ?>

      <?php
        /**
         * Team members without "_on_top" option
         * ordered by Name (title)      *
         */
        $team_options = array(
          'post_type' => 'team',
          'order' => 'ASC',
          'orderby' => 'title',
          'posts_per_page' => 100,
          'meta_query' => array(
            'relation' => 'OR',
            array(
              'key' => '_on_top',
              'value' => 1,
              'compare' => 'NOT LIKE',
            ),
            array(
              'key' => '_on_top',
              'value' => 'bug #23268',
              'compare' => 'NOT EXISTS',
            )
          ),
        );
        $team_query = new WP_Query($team_options);
      ?>
  <?php if ( $team_query->have_posts() ) { ?>

    <?php while ( $team_query->have_posts() ) {
      $team_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) ) {
            $excerpt = apply_filters('the_content', has_excerpt() ? get_the_excerpt() : '');
            $team_position = $post_meta['_team-position'];
            $team_phone = $post_meta['_team-phone'];
            $team_phone_display = sandbox_sanitize_phone($team_phone, true);
            $team_email = $post_meta['_team-email'];
            $team_linkedin = $post_meta['_team-linkedin'];

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

        ?>
        <article id="post-<?php the_ID() ?>" <?php post_class(); ?>>
          <div class="inner">
          <?php if ( has_post_thumbnail() ) { ?>
            <span class="post-thumbnail" <?php echo $thumbnail_style; ?>><?php the_post_thumbnail('sandbox-default'); ?></span>
          <?php } ?>
            <div class="content">
              <h2><?php the_title() ?></h2>
            <?php if ( !empty($team_position) ) { ?>
              <p class="position"><strong><?php echo $team_position; ?></strong></p>
            <?php } ?>
            <?php if ( !empty($excerpt) ) { ?>
              <?php echo $excerpt; ?>
            <?php } ?>
              <ul class="contacts">
              <?php if ( !empty($team_phone) ) { ?>
                <li class="phone"><a href="tel:<?php echo $team_phone; ?>"><?php echo $team_phone_display; ?></a></li>
              <?php } ?>
              <?php if ( !empty($team_email) ) { ?>
                <li class="mail"><a href="mailto:<?php echo $team_email; ?>"><?php echo $team_email; ?></a></li>
              <?php } ?>
              <?php if ( !empty($team_linkedin) ) { ?>
                <li class="linkedin"><a href="<?php echo $team_linkedin; ?>" target="_blank" rel="noreferrer nofollow"><?php echo __('Get in touch', 'sandbox'); ?></a></li>
              <?php } ?>
              </ul>
            </div>
          </div>
        </article><!-- .post -->
        <?php } ?>
      <?php } ?>
      <?php wp_reset_postdata(); ?>
    <?php } ?>
    </div>
    <?php
      /*
      * @Sidebar Content after
      */
      get_sidebar('content-after'); ?>
  </section>

<?php get_footer() ?>
