<?php
if($_REQUEST['post_type']==CUSTOM_POST_TYPE1)
{
	add_filter('manage_posts_columns', 'product_custom_columns');
	function product_custom_columns($defaults)
	{  //lets remove some un-needed columns if it's a post
		$defaults['image'] = __('Image');
		$defaults['features'] = __('Detail Information');
		$defaults['customtaxonomy'] = CUSTOM_TAXONOMY_LABEL;
		$defaults['customtags'] = CUSTOM_TAG_LABEL;
		return $defaults;
	}
	
	add_action('manage_posts_custom_column', 'custom_column', 10, 2);
	function custom_column($column_name, $post_id)
	{   //defines what goes in the new columns
		if( $column_name == 'image' )
		{
			$post_image = $post_images = bdw_get_images($post_id,'thumb');
			if($post_image[0])
			{
				echo '<img src="'.$post_image[0].'" width=90>';
			}else
			{
				_e('No Image');	
			}
		}
		if( $column_name == 'features' )
		{
			$is_featured = __('No');
			if(get_post_meta($post_id,'is_featured',true)=='on')
			{
				$is_featured = __('Yes');
			}
			echo __('<b>Start Dt</b> : ').get_post_meta($post_id,'st_date',true).'&nbsp;&nbsp;'.get_post_meta($post_id,'st_time',true).'<br>';
			echo __('<b>End Dt</b> : ').get_post_meta($post_id,'end_date',true).'&nbsp;&nbsp;'.get_post_meta($post_id,'end_time',true).'<br>';
			echo __('<b>Address</b> : ').get_post_meta($post_id,'address',true).'<br>';
			echo __('<b>Featured</b> : ').$is_featured.'<br>';
			echo __('<b>Organizer</b> : ').get_post_meta($post_id,'organizer_name',true).'<br>';
		}
		
		if( $column_name == 'customtaxonomy' )
		{
			$category = wp_get_post_terms( $post_id, CUSTOM_CATEGORY_TYPE1, $args = array('ID') );
			$term_id_arr = array();
			$name_arr = array();
			foreach($category as $category_obj)
			{
				$term_id_arr[]=$category_obj->term_id;	
				$name_arr[]=$category_obj->name;	
			}
			if($name_arr)
			{
				echo implode(', ',$name_arr);	
			}else
			{
				_e('None');	
			}
		}
		if( $column_name == 'customtags' )
		{
			$category = wp_get_post_terms( $post_id, CUSTOM_TAG_TYPE1, $args = array('ID') );
			$term_id_arr = array();
			$name_arr = array();
			foreach($category as $category_obj)
			{
				$term_id_arr[]=$category_obj->term_id;	
				$name_arr[]=$category_obj->name;	
			}
			if($name_arr)
			{
				echo implode(', ',$name_arr);	
			}else
			{
				_e('None');	
			}
		}
	}
}

//add_action('load-edit.php', 'filtering');

function filtering() {
	add_filter('posts_where', 'custom_filter_join');
	add_action('restrict_manage_posts', 'search_manage_posts');
}

function custom_filter_join($where) {
	global $wpdb;

	if( $_REQUEST['ptype'] == 'prd' ) {
		$blog_Cat = get_blog_sub_cats_str('string');
		if($blog_Cat){
		$where .= " and $wpdb->posts.ID not in (select $wpdb->term_relationships.object_id from $wpdb->term_relationships join $wpdb->term_taxonomy on $wpdb->term_relationships.term_taxonomy_id=$wpdb->term_taxonomy.term_taxonomy_id where term_id in ($blog_Cat))";
		}
	} 
	return $where;
}

function search_manage_posts() {
?>
<select name='ctype' id='ctype' class='postform'>
<option value="0">
<?php echo __('View all ').CUSTOM_MENU_CAT_LABEL ?>
</option>
<option value="prd" <?php if( isset($_GET['ptype']) && $_GET['ptype']=='prd'){ echo 'selected="selected"'; }?> >
<?php _e('View only products'); ?>
</option>
</select>
<?php 
} 
?>