<?php 
	
if ( ! function_exists( 'gp_filters' ) ) {

	function gp_filters( $atts, $content = null ) {	
		
		extract( shortcode_atts( array(
			'widget_title' => '',
			'parent_cat' => '',
			'fields' => '',
			'date_posted' => 'enabled',
			'date_modified' => 'disabled',
			'parent_cat_text' => 'Categories',
			'date_posted_text' => 'Release Date',
			'date_modified_text' => 'Last Updated',
			'submit_button_text' => 'Filter Items',
			'classes' => '',
			'bg_color' => '',
			'border_color' => '',
		), $atts ) );	
		
		global $gp, $post;
		
		if ( ! is_archive() )
			return;
		
		// Detect shortcode
		$GLOBALS['gp_shortcode'] = 'filters';
				
		// Unique Name	
		STATIC $i = 0;
		$i++;
		$name = 'gp_filters_wrapper_' . $i;

		// Get parent cat ID and/or slug
		if ( $parent_cat ) {
			if ( preg_match( '/^[a-zA-Z\-]+/', $parent_cat ) ) {
				$parent_cat_slug = $parent_cat;
				$parent_cat_id = get_term_by( 'slug', $parent_cat, 'gp_hubs' );
				$parent_cat_id = $parent_cat_id->term_id;
			} else {
				$parent_cat_slug = get_term_by( 'id', $parent_cat, 'gp_hubs' );
				$parent_cat_slug = $parent_cat_slug->term_id;	
				$parent_cat_id = $parent_cat;	
			}
		}
	
		// Fields to add to filter
		if ( $fields ) {
			$filters = explode( ',', $fields );
		}
		
		ob_start();
		
		//global $query_string;
		//var_dump( $query_string );
		
		?>

		<div id="<?php echo sanitize_html_class( $name ); ?>" class="filters-shortcode <?php echo sanitize_html_class( $classes ); ?>"<?php if ( $bg_color OR $border_color ) { ?> style="background: <?php echo esc_attr( $bg_color ); ?>; border-color: <?php echo esc_attr( $border_color ); ?>"<?php } ?>>

			<?php if ( $widget_title ) { ?><h3 class="widgettitle"><?php echo esc_attr( $widget_title ); ?></h3><?php } ?>
	
			<form method="post" name="filter_form" action="" enctype="multipart/form-data" role="form">

				<?php if ( $date_posted == 'enabled' ) { ?>
					<p>
						<span class="filter-title"><?php echo esc_attr( $date_posted_text ); ?></span>
						<select id="date_posted" name="date_posted">
							<option value="all"<?php if ( isset( $_POST['date_posted'] ) && $_POST['date_posted'] == 'all' ) { echo ' selected="selected"'; } ?>><?php _e( 'Any date', 'gp_lang' ); ?></option>
							<option value="year"<?php if ( isset( $_POST['date_posted'] ) && $_POST['date_posted'] == 'year' ) { echo ' selected="selected"'; } ?>><?php _e( 'In the last year', 'gp_lang' ); ?></option>
							<option value="month"<?php if ( isset( $_POST['date_posted'] ) && $_POST['date_posted'] == 'month' ) { echo ' selected="selected"'; } ?>><?php _e( 'In the last month', 'gp_lang' ); ?></option>
							<option value="week"<?php if ( isset( $_POST['date_posted'] ) && $_POST['date_posted'] == 'week' ) { echo ' selected="selected"'; } ?>><?php _e( 'In the last week', 'gp_lang' ); ?></option>
							<option value="day"<?php if ( isset( $_POST['date_posted'] ) && $_POST['date_posted'] == 'day' ) { echo ' selected="selected"'; } ?>><?php _e( 'In the last day', 'gp_lang' ); ?></option>
						</select>
					</p>
				<?php } ?>
			
				<?php if ( $date_modified == 'enabled' ) { ?>
					<p>
						<span class="filter-title"><?php echo esc_attr( $date_modified_text ); ?></span>
						<select id="date_modified" name="date_modified">
							<option value="all"<?php if ( isset( $_POST['date_modified'] ) && $_POST['date_modified'] == 'all' ) { echo ' selected="selected"'; } ?>><?php _e( 'Any date', 'gp_lang' ); ?></option>
							<option value="year"<?php if ( isset( $_POST['date_modified'] ) && $_POST['date_modified'] == 'year' ) { echo ' selected="selected"'; } ?>><?php _e( 'In the last year', 'gp_lang' ); ?></option>
							<option value="month"<?php if ( isset( $_POST['date_modified'] ) && $_POST['date_modified'] == 'month' ) { echo ' selected="selected"'; } ?>><?php _e( 'In the last month', 'gp_lang' ); ?></option>
							<option value="week"<?php if ( isset( $_POST['date_modified'] ) && $_POST['date_modified'] == 'week' ) { echo ' selected="selected"'; } ?>><?php _e( 'In the last week', 'gp_lang' ); ?></option>
							<option value="day"<?php if ( isset( $_POST['date_modified'] ) && $_POST['date_modified'] == 'day' ) { echo ' selected="selected"'; } ?>><?php _e( 'In the last day', 'gp_lang' ); ?></option>
						</select>
					</p>
				<?php } ?>
				
				<?php if ( $parent_cat ) { ?>						
					<p>
						<span class="filter-title"><?php echo esc_attr( $parent_cat_text ); ?></span>
						<select id="gp_hubs" name="gp_hubs">
							<option value="<?php echo sanitize_html_class( $parent_cat_slug ); ?>" selected="selected"><?php _e( 'All', 'gp_lang' ); ?></option>
							<?php 
							$args = array(
								'parent' => (int) $parent_cat_id,
							);
							$cats = get_terms( 'gp_hubs', $args );				
							foreach( $cats as $cat ) {
								$cat_slug = $cat->slug;
								$cat_name = $cat->name; ?>
								<option value="<?php echo esc_attr( $cat_slug ); ?>"<?php if ( isset( $_POST['gp_hubs'] ) && $_POST['gp_hubs'] == $cat_slug ) { echo ' selected="selected"'; } ?>><?php echo esc_attr( $cat_name ); ?></option>
							<?php } ?>
						</select>
					</p>
				<?php } ?>
							
				<?php foreach( $filters as $filter ) {

					// Get terms for given taxonomy
					$terms = get_terms( $filter );
	
				?>
					<p>
						<span class="filter-title"><?php echo esc_attr( ucwords( str_replace( '-', ' ', $filter ) ) ); ?></span>
						<select id="<?php echo esc_attr( $filter ); ?>" name="<?php echo esc_attr( $filter ); ?>">
							<option value=""><?php _e( 'All', 'gp_lang' ); ?></option>
							<?php foreach( $terms as $term ) {
								$term_slug = $term->slug;
								$term_name = $term->name; ?>
								<option value="<?php echo esc_attr( $term_slug ); ?>"<?php if ( isset( $_POST[ $filter ] ) && $_POST[ $filter ] == $term_slug ) { echo ' selected="selected"'; } ?>><?php echo esc_attr( $term_name ); ?></option>
							<?php } ?>
						</select>
					</p>
				<?php } ?>
				
				<input type="submit" value="<?php echo esc_attr( $submit_button_text ); ?>" tabindex="40" id="filter-submit" name="submit" class="button" />
			
				<input type="hidden" name="action" value="gp_filter" />
		
			</form>
		
		</div>
		
		<?php

		$output_string = ob_get_contents();
		ob_end_clean();
		$GLOBALS['gp_shortcode'] = null;
		return $output_string;

	}

}

add_shortcode( 'filters', 'gp_filters' );
	
?>