<?php
/**
 * Shortcode For Staff Posts
 * @package Symple Portfolio
 * @since 1.0
 * @author AJ Clarke : http://sympleplorer.com
 * @copyright Copyright (c) 2012, AJ Clarke
 * @link http://sympleplorer.com
 * @License: GNU General Public License version 3.0
 * @License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */
 


/*
 * Shortcode
 * @since v1.1
*/


if(! function_exists( 'symple_Staff_shortcode' ) ) :

	function symple_staff_shortcode( $atts ) {
		
		// Define shortcode params
		extract( shortcode_atts( array(
				'count' => '-1',
				'columns' => '3',
				'order' => 'DESC',
				'orderby' => 'date',
				'filter' => '',
				'thumbnail_crop' => 'true',
				'thumbnail_hard_crop' => 'true',
				'thumbnail_width' => '',
				'thumbnail_height' => '',
				'lightbox_only' => 'false',
				'title' => 'true',
				'meta' => 'true',
				'excerpt' => 'true',
				'read_more' => 'false',
				'read_more_text' => __('read more','wpex'),
				'excerpt_length' => '15',
				'category_id' => '',
				'tag_id' => '',
				'pagination' => 'false',
				'filter_content' => 'false',
				'offset' => 0
			), $atts ) );
		
		
		//Give staff a unique name
		$unique_classname = 'staff-'. rand(1, 100);
		
		// Start Tax Query
		$tax_query = '';
		if( $category_id !=='' && $tag_id =='' ) {
			$tax_query = array(
				array(
					'taxonomy' => 'staff_category',
					'field' => 'id',
					'terms' => $category_id
				),
			);
		} elseif( $tag_id !=='' && $category_id =='' ) {
			
			$tax_query = array(
				array(
					'taxonomy' => 'staff_tag',
					'field' => 'id',
					'terms' => $tag_id,
					'operator' => 'IN'
				)
			);
		} elseif( $category_id !=='' && $tag_id !=='' ) {
			
			$tax_query = array(
				'relation' => 'AND',
				array(
					'taxonomy' => 'staff_category',
					'field' => 'id',
					'terms' => $category_id
				),
				array(
					'taxonomy' => 'staff_tag',
					'field' => 'id',
					'terms' => $tag_id,
					'operator' => 'IN'
				)
			);
		} else {
			$tax_query = '';
		}

		// Pagination var
		if( $pagination == 'true' ) {
			global $paged;
			$paged = get_query_var('paged') ? get_query_var('paged') : 1;
		} else {
			$paged = NULL;
		}
		
		// The Query
		$wpex_staff_query = new WP_Query(
			array(
				'post_type' => 'staff',
				'posts_per_page' => $count,
				'offset' => $offset,
				'order' => $order,
				'orderby' => $orderby,
				'tax_query' => $tax_query,
				'filter_content' => $filter_content,
				'paged' => $paged
			)
		);

		$output = '';

		//Output posts
		if( $wpex_staff_query->posts ) :
		
			// Load Masonry scripts		
			$staff_isotope_class = NULL;		
			if ( $filter !== '' || $thumbnail_hard_crop == 'false') {
				wp_enqueue_script('isotope');
				wp_enqueue_script('wpex-isotope-staff');
				$staff_isotope_class = ' staff-isotope';
			}

			//Filter
			$filter_class = $staff_parent = NULL;
			if( $filter == 'false' ) $filter = NULL;
			if( $filter == 'true' ) $filter = 'category';
			if( $filter == 'category' || $filter == 'tag' ) {
				
				//Category Filter
				if ( $filter == 'category' ) $staff_parent = $category_id;
				
				//Tag filter
				if ( $filter == 'tag' ) $staff_parent = $tag_id;
				
				//get staff categories
				$terms = get_terms('staff_'.$filter.'', array( 'hide_empty' => '1', 'child_of' => $staff_parent  ) );
				
				//show filter if categories exist
				if( $terms ) {
				
					// Output filter JS into the footer like a WP Jedi Master
					$output .='
						<script type="text/javascript">
							jQuery(function($){
								$(window).load(function() {
									$(".staff-filter a").click(function(){
									var selector = $(this).attr("data-filter");
									$(".'. $unique_classname .'").isotope({ filter: selector });
									$(this).parents("ul").find("a").removeClass("active");
									$(this).addClass("active");
									return false;
									});
								});
							});
						</script>';

					// Display filter links
					$output .= '<ul class="staff-filter clearfix">';
						$output .= '<li><a href="#" class="active" data-filter="*"><span>'. __('All', 'wpex') .'</span></a></li>';
						foreach ($terms as $term ) :
							$output .= '<li><a href="#" data-filter=".staff-'. $term->slug .'"><span>'. $term->name .'</span></a></li>';
						endforeach;
					$output .= '</ul>';
				
				} // end $terms check
				
			} // end filter check
		
			// Main wrapper div
			$output .= '<div class="staff-wrap clearfix '. $unique_classname . $staff_isotope_class .'">';
		
			// Loop through posts
			$count=0;
			foreach ( $wpex_staff_query->posts as $post ) :
			$count++;
			
				// Post VARS
				$postid = $post->ID;
				$featured_img = wp_get_attachment_url( get_post_thumbnail_id( $postid ), 'full' );
				$url = get_permalink($postid);
				
				// Crop Images if wpex_img exists
				if( $thumbnail_crop !== 'false' ) {
					$featured_img = wpex_get_cropped_grid_image_url($postid, $columns, $thumbnail_width, $thumbnail_height, $thumbnail_hard_crop );
				}
				
				// Set vars to remove margin on last item in row & clear floats
				$remove_margin = $clear_floats = NULL;
				if ( $filter == '' && $thumbnail_hard_crop !== 'false') {
					$remove_margin = ( $count == $columns ) ? ' remove-margin' : NULL;
					$clear_floats = ( $count == $columns ) ? '<div class="clear-staff clear"></div>' : NULL;
				}
					
				// Reset counter
				if ( $count == $columns ) $count=0;
				
				// Get post terms
				if ( $filter !== '' ) {
					$cats = get_the_terms( $postid, 'staff_category' );
					$tags = get_the_terms( $postid, 'staff_tag' );
				} else { $cats = $tags = NULL; }
		
				// Staff Post Output
				$output .= '<article class="staff-entry entry-'. $postid .' clearfix grid-'.$columns . $remove_margin;
					if( $cats ) foreach( $cats as $cat ) $output .= ' staff-'. $cat->slug;
					if( $tags ) foreach( $tags as $tag ) $output .= ' staff-'. $tag->slug;
				$output .= '">';
						
					if( has_post_thumbnail($postid) ) {	
						$output .= '<div class="staff-entry-media"><a href="'. $url .'" title="'. get_the_title($postid) .'" class="staff-entry-img">';
							$output .= '<img src="'. $featured_img .'" alt="'. get_the_title($postid) .'" />';
						$output .= '</a><!-- .staff-entry-img --></div><!-- .staff-entry-media -->';	
					} // End thumbnail check
				
					// Staff details
					if( $title == 'true' ||  $excerpt == 'true' ) $output .=' <div class="staff-entry-details">';
					
						//Title
						if( $title == 'true' ) {
							
							//main title
							$output .= '<header class="staff-entry-title">';
								$output .= '<h3><a href="'. $url .'" title="'. get_the_title($postid) .'">'. get_the_title($postid) .'</a>';
								// Position
								if(get_post_meta( $postid, 'wpex_staff_position', true) !== '') {
                            		$output .= '<div class="staff-entry-position"><span class="icon-double-angle-right"></span>'. get_post_meta( $postid, 'wpex_staff_position', true) .'</div>';
                        		}
							$output .= '</h3>';
							$output .= '</header><!-- .staff-entry-title -->';
						}
						//Excerpt
						if( $excerpt == 'true' ) {
							$output .='<div class="staff-entry-excerpt">';
								if ( !empty($post->post_excerpt) ) {
									$output .= apply_filters('the_content', $post->post_excerpt) ;
								} else {
									$output .= wp_trim_words( strip_shortcodes( $post->post_content ), $excerpt_length);
									//Read More
									if( $read_more == 'true' ) { 
									$output .= '<a href="'. $url .'" title="'. get_the_title($postid) .'" class="staff-entry-readmore">'. $read_more_text .' &rarr;</a>';
									}
								}
							$output .=' </div><!-- /staff-entry-excerpt -->';
						}
					
					// Close details div
					if( $title == 'true' || $excerpt == 'true' ) {
						$output .='</div><!-- .staff-entry-details -->';
					}
					
				// Close main wrap	
				$output .= '</article>';
				$output .= $clear_floats; // clear floats
			
			// End foreach loop
			endforeach;
			
			// Close main wrap
			$output .= '</div>';
			
			// Paginate Posts
			if( $pagination == 'true' ) {
				$total = $wpex_staff_query->max_num_pages;
				$big = 999999999; // need an unlikely integer
				if( $total > 1 )  {
					 if( !$current_page = get_query_var('paged') )
						 $current_page = 1;
					 if( get_option('permalink_structure') ) {
						 $format = 'page/%#%/';
					 } else {
						 $format = '&paged=%#%';
					 }
					 $output .= paginate_links(array(
						'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
						'format' => $format,
						'current' => max( 1, get_query_var('paged') ),
						'total' => $total,
						'mid_size'	=> 2,
						'type' => 'list',
						'prev_text' => '&laquo;',
						'next_text' => '&raquo;',
					 ));
				}
			}
			
		
		endif; // End $wpex_staff_query->posts check
		
		
		// Set things back to normal
		$wpex_staff_query = NULL;
		wp_reset_postdata();


		// Return data
		return $output; 
		
	}
	add_shortcode("symple_staff", "symple_staff_shortcode");
	
	
	
	/*
	 * Add Editor Button
	 * @since v1.1
	*/
	
	//Creating TinyMCE buttons
	function symple_staff_editor_button() {
	   if ( current_user_can('edit_posts') &&  current_user_can('edit_pages') ) {
		 if (get_user_option('rich_editing')) {
			add_filter('mce_external_plugins', 'symple_staff_add_custom');
			add_filter('mce_buttons', 'symple_staff_register_button');
		 }
	   }
	} 
	add_action('init', 'symple_staff_editor_button');
	
	function symple_staff_add_custom($plugin_array) {
		   $plugin_array['symple_staff'] = get_template_directory_uri() .'/functions/shortcodes/mce/staff-mce-button.js';
		   return $plugin_array;
	}
	
	function symple_staff_register_button($buttons) {
	   array_push($buttons, "symple_staff");
	   return $buttons;
	} 

	
endif; // end symple_staff_shortcode check