<?php
class GestionPymesTaxonomies {

	public static function init() {
	    self::register_tax();
	    
	    // Admin table list filter
	    add_action('restrict_manage_posts', array(__CLASS__, 'restrict_manage_posts'));
	    add_action('parse_query', array(__CLASS__, 'parse_query'));
	    
	}
	
	public static function register_tax() {
	    // States
	    $labels = array(
	        'name' => _x( 'Payment status', 'gestion-pymes' ),
	        'singular_name' => _x( 'Payment status', 'gestion-pymes' ),
	        'search_items' =>  __( 'Search Payment status', 'gestion-pymes' ),
	        'popular_items' => __( 'Popular Payment status', 'gestion-pymes' ),
	        'all_items' => __( 'All Payment status', 'gestion-pymes' ),
	        'parent_item' => null,
	        'parent_item_colon' => null,
	        'edit_item' => __( 'Edit Payment status', 'gestion-pymes' ),
	        'update_item' => __( 'Update Payment status', 'gestion-pymes' ),
	        'add_new_item' => __( 'Add New Payment status', 'gestion-pymes' ),
	        'new_item_name' => __( 'New Payment status Name', 'gestion-pymes' ),
	        'separate_items_with_commas' => __( 'Separate Payment status with commas', 'gestion-pymes' ),
	        'add_or_remove_items' => __( 'Add or remove Payment status', 'gestion-pymes' ),
	        'choose_from_most_used' => __( 'Choose from the most used Payment status', 'gestion-pymes' ),
	        'menu_name' => __( 'Payment status', 'gestion-pymes' ),
	    );
	    register_taxonomy( 'gp_invoice_payment_status', 'gp_invoice', array(
	        'hierarchical'      => true,
	        'labels'            => $labels,
	        'show_ui'           => true,
	        'show_admin_column' => true,
	        'query_var'         => true,
	        'public'        => false,
	        'rewrite'           => array( 'slug' => '/' ),
	        'meta_box_cb'       => array( __CLASS__, 'render_meta_box' )
	    ));
	    
	}
	
	public static function render_meta_box( $post ) {
	    $state_selected = wp_get_object_terms( $post->ID, 'gp_invoice_payment_status' );
	    
	    if ( sizeof( $state_selected ) ) {
	        $state_selected = $state_selected[0]->term_id;
	    } else {
	        $state_selected = '';
	    }
	    $states = get_terms(array( 'taxonomy' => 'gp_invoice_payment_status', 'hide_empty' => false) );
	    if ( !is_wp_error( $states ) ) {
	        wp_dropdown_categories(array(
	            'taxonomy' => 'gp_invoice_payment_status',
	            'class' => 'widefat',
	            'hide_empty' => 0,
	            'name' => "tax_input[gp_invoice_payment_status][]",
	            'selected' => $state_selected,
	            'orderby' => 'name',
	            'hierarchical' => 1,
	            'show_option_all' => " "
	        ));
	    }
	    
	}
	
	public static function restrict_manage_posts() {
	    global $typenow;
	    $post_type = 'gp_invoice';
	    $taxonomy  = 'gp_invoice_payment_status';
	    if ($typenow == $post_type) {
	        $selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
	        $info_taxonomy = get_taxonomy($taxonomy);
	        wp_dropdown_categories(array(
	            'show_option_all' => sprintf( __( 'Show all %s', 'gestion-pymes' ), $info_taxonomy->label ),
	            'taxonomy'        => $taxonomy,
	            'name'            => $taxonomy,
	            'orderby'         => 'name',
	            'selected'        => $selected,
	            'show_count'      => true,
	            'hide_empty'      => true,
	        ));
	    };
	}
	
	public static function parse_query($query) {
	    global $pagenow;
	    $post_type = 'gp_invoice';
	    $taxonomy  = 'gp_invoice_payment_status';
	    $q_vars    = &$query->query_vars;
	    if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
	        $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
	        $q_vars[$taxonomy] = $term->slug;
	    }
	}
}

GestionPymesTaxonomies::init();
?>