<?php

if ( ! function_exists( 'gp_categories' ) ) {
	function gp_categories() {
		register_widget( 'GP_Categories' );
	}
}
add_action( 'widgets_init', 'gp_categories' );

class GP_Categories extends WP_Widget {

	function GP_Categories() {
		$widget_ops = array( 'classname' => 'gp-categories', 'description' => __( 'A list or dropdown of categories.', 'gp_lang' ) );
		$this->WP_Widget( 'gp-categories-widget', __( 'GP Categories', 'gp_lang' ), $widget_ops );
	}

    function widget( $args, $instance ) {
    
        extract( $args );

        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories', 'gp_lang' ) : $instance['title'] );
        $c = $instance['count'] ? '1' : '0';
        $h = $instance['hierarchical'] ? '1' : '0';
        $d = $instance['dropdown'] ? '1' : '0';
        $exclude = $instance['exclude'];
        $taxonomy = $instance['taxonomy'];     
		
		// Unique Name
		STATIC $i = 0;
		$i++;
		$name = 'gpcats' . $i;
		
        echo html_entity_decode( $before_widget );
        
        
        ?>
        
		<?php if ( $title ) { echo html_entity_decode( $before_title . $title . $after_title ); } ?>

		<?php 
		
		$args = array(
			'taxonomy' => $taxonomy,
			'exclude' => $exclude,
			'orderby' => 'name',
			'show_count' => $c,
			'hierarchical' => $h,
		);

        if ( $d ) {

			if ( $c == '1' ) {
        
        		if ( ! function_exists ( 'get_terms_dropdown' ) ) {
						function get_terms_dropdown( $taxonomies, $args ) {					
					
						// Unique Name
						STATIC $i = 0;
						$i++;
						$name = 'gpcats' . $i;
		
						$myterms = get_terms( $taxonomies, $args );
						$output = "<select id='" . $name . "' name='" . $name . "' class='selection'><option value='-1'>" . __( 'Select Category', 'gp_lang' ) . "</option>";
				
						foreach( $myterms as $term ) {
							$term_taxonomy = $term->taxonomy;
							$term_slug = $term->slug;
							$term_name = $term->name;
							$term_count = " (" . $term->count . ")";
							$output .= "<option value='" . $term_slug . "'>" . $term_name . $term_count . "</option>";
						}
						$output .= "</select>";
						return $output;
					}
				}
			
			} else {
			
				if ( ! function_exists( 'get_terms_dropdown' ) ) {
					function get_terms_dropdown( $taxonomies, $args ) {
										
						// Unique Name
						STATIC $i = 0;
						$i++;
						$name = 'gpcats' . $i;
		
						$myterms = get_terms( $taxonomies, $args );
						$output = "<select id='" . $name . "' name='" . $name . "'><option value='-1'>" . __( 'Select Category', 'gp_lang' )."</option>";
				
						foreach( $myterms as $term ) {
							$term_taxonomy = $term->taxonomy;
							$term_slug = $term->slug;
							$term_name = $term->name;
							$output .= "<option value='" . $term_slug . "'>" . $term_name . "</option>";
						}
						$output .= "</select>";
						return $output;
					}
				}

			}
			
			$taxonomies = array( $taxonomy );
			
			echo get_terms_dropdown( $taxonomies, $args );
			
			?>

			<script type='text/javascript'>
			/* <![CDATA[ */
				var dropdown<?php echo sanitize_html_class( $name ); ?> = document.getElementById( '<?php echo sanitize_html_class( $name ); ?>' );
				function onCatChange<?php echo sanitize_html_class( $name ); ?>() {
					if (dropdown<?php echo sanitize_html_class( $name ); ?>.options[dropdown<?php echo sanitize_html_class( $name ); ?>.selectedIndex].value != '-1' ) {
						location.href = "<?php echo home_url(); ?>/?<?php echo esc_attr( $taxonomy ); ?>="+dropdown<?php echo sanitize_html_class( $name ); ?>.options[dropdown<?php echo sanitize_html_class( $name ); ?>.selectedIndex].value;
					}
				}
				dropdown<?php echo sanitize_html_class( $name ); ?>.onchange = onCatChange<?php echo sanitize_html_class( $name ); ?>;
			/* ]]> */
			</script>

		<?php } else { ?>
		
			<ul>
				<?php
				$args['title_li'] = '';
				wp_list_categories( apply_filters( 'widget_categories_args', $args ) );
				?>
			</ul>
			
		<?php
		
        }

        echo html_entity_decode( $after_widget );
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['count'] = $new_instance['count'] ? 1 : 0;
        $instance['hierarchical'] = $new_instance['hierarchical'] ? 1 : 0;
        $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
        $instance['exclude'] = $new_instance['exclude'];
        $instance['taxonomy'] = $_POST['taxonomy'];

        return $instance;
    }

    function form( $instance ) {
    
        //Defaults
        $instance = wp_parse_args( ( array ) $instance, array( 'title' => '', 'exclude' => '', 'taxonomy' => 'category' ) );
        $title = esc_attr( $instance['title'] );
        $count = isset( $instance['count'] ) ? (bool) $instance['count'] :false;
        $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
        $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
        $exclude = esc_attr( $instance['exclude'] );
        $taxonomy = $instance['taxonomy'];

	?>

        <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'gp_lang' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'taxonomy' ) ); ?>"><?php _e( 'Taxonomy:', 'gp_lang' ); ?></label>
			<select id="taxonomy" name="taxonomy">
				<option value="category" <?php if ( $instance['taxonomy'] == 'category' ) { echo 'selected="selected"'; } ?>><?php _e( 'Post Categories', 'gp_lang' ); ?></option>			
				<option value="gp_hubs" <?php if ( $instance['taxonomy'] == 'gp_hubs' ) { echo 'selected="selected"'; } ?>><?php _e( 'Hub Categories', 'gp_lang' ); ?></option> 			
                <option value="gp_videos" <?php if ( $instance['taxonomy'] == 'gp_videos' ) { echo 'selected="selected"'; } ?>><?php _e( 'Video Categories', 'gp_lang' ); ?></option>
			</select>
		</p>
		
        <p><input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'dropdown' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'dropdown' ) ); ?>"<?php checked( $dropdown ); ?> />
        <label for="<?php echo esc_attr( $this->get_field_id( 'dropdown' ) ); ?>"><?php _e( 'Show as dropdown', 'gp_lang' ); ?></label><br />

        <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>"<?php checked( $count ); ?> />
        <label for="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>"><?php _e( 'Show post counts', 'gp_lang' ); ?></label><br />

        <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'hierarchical' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hierarchical' ) ); ?>"<?php checked( $hierarchical ); ?> />
        <label for="<?php echo esc_attr( $this->get_field_id( 'hierarchical' ) ); ?>"><?php _e( 'Show hierarchy', 'gp_lang' ); ?></label></p>

        <p><label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:', 'gp_lang' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" type="text" value="<?php echo esc_attr( $exclude ); ?>" />
		<br/><small><?php _e( 'Enter the ID numbers of the categories you want to exclude, separating each with a comma (e.g. 48,142).', 'gp_lang' ); ?></small></p>
                
<?php

    }

}

?>