<?php

class Theme_Widget_ExCategories extends WP_Widget {

	var $prefix;
	var $textdomain;
	var $option_exclude;
	var $cat_array;
	var $deep = 1;
	/**
	 * Set up the widget's unique name, ID, class, description, and other options.
	 * @since 0.6
	 */
	function Theme_Widget_ExCategories() {
		$this->textdomain   = THEME_ADMIN_DOMAIN;
		$widget_ops		 = array( 'classname' => 'excategories', 'description' => __( 'An advanced widget that gives you total control over the output of your category links.', $this->textdomain ) );
		$control_ops		= array( 'width' => 800, 'height' => 350);

		$exclude_cats = theme_get_option('blog','exclude_categorys');
		foreach ($exclude_cats as $key => $value) {
			$this->option_exclude[] = $value;
		}

		$cats = get_categories('orderby=name&hide_empty=0');
		
		foreach ($cats as $cat) {
			$this->cat_array[$cat->cat_ID] = array('name'=>$cat->cat_name,'parent' => $cat->parent);
		}
		if(!empty($cats)){
			foreach ($this->cat_array as $cat) {
				$res = $this->cat_walk($cat['parent']);
				if( $res > $this->deep) {
					$this->deep = $res;
				}
			}
		}
		$this->WP_Widget( 'excategories', THEME_SLUG.' - '.__( 'Categories',  $this->textdomain ), $widget_ops, $control_ops );
	}


	function cat_walk($parent){
		if ($parent != 0 ) {
			return $this->cat_walk($this->cat_array[$parent]['parent'])+1;
		} else {
			return 1;
		}
	}
	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 * @since 0.6
	 */
	function widget( $args, $instance ) {
		extract( $args );
		$exclude = null;
		$include = null;
		$title  = apply_filters( 'widget_title', $instance['title'] );

		$style = $instance['style'];
		$orderby = $instance['orderby'];
		$order = $instance['order'];
		$depth = (int)$instance['depth'];

		$select = !empty($instance['select']) ? implode(",",$instance['select']) : '';
		$isexclude = $instance['isexclude'] ? '1' : '0';
		$feed_image = $instance['feed_image'];
		$number = (int)$instance['number'];
		$show_option_all = !empty($instance['show_option_all']) ? '<a href="'.get_page_link( get_option('page_for_posts')).'">'.$instance['show_option_all'].'</a>' : false;


		$hierarchical  = $instance['hierarchical'] ? '1' : '0';
		$use_desc_for_title	= $instance['use_desc_for_title'] ? '1' : '0';
		$show_count = $instance['show_count'] ? '1' : '0';
		$hide_empty	= $instance['hide_empty'] ? '1' : '0';
		$feed = isset( $instance['feed'] ) ? $instance['feed'] : false;   

		if ( $feed ) {
			$feed = __( 'RSS', $this->textdomain );
		}
		if ( $isexclude ) {
			$exclude = $select;
		} else {
			$include = $select;
		}

		$args = array(
			'exclude' => $exclude,
			'include' => $include,
			'number' => $number,
			'depth' => $depth,
			'orderby' => $orderby,
			'order' => $order,
			'show_count' => $show_count,
			'hide_empty' => $hide_empty,
			'use_desc_for_title' => $use_desc_for_title,
			'feed' => $feed,
			'feed_image' => $feed_image,
			'hierarchical' => $hierarchical,
			'show_option_all' => $show_option_all,
			'title_li' => false,
			'echo' => 0,
		);

		echo $before_widget;
		$a = array();
		if ( $title )
			echo $before_title . $title . $after_title;
		if ( $style == 'list' ) {
			$categories = str_replace( array( "\r", "\n", "\t" ), '', wp_list_categories( $args ) );
			$categories = '<ul class="categories">' . $categories . '</ul>';
			echo $categories;
		} elseif( $style == 'dropdown'){
			$args['show_option_none'] = __('Select Category');
			$args['show_option_all']  = false;
			echo wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $args));
?>

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

<?php
		}

		echo $after_widget;
	}

	/**
	 * Updates the widget control options for the particular instance of the widget.
	 * @since 0.6
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['style'] = $new_instance['style'];
		$instance['orderby'] = $new_instance['orderby'];
		$instance['order'] = $new_instance['order'];
		$instance['depth'] = strip_tags( $new_instance['depth'] );

		$instance['select'] = $new_instance['select'];
		$instance['isexclude'] = !empty( $new_instance['isexclude'] ) ? 1 : 0;
		$instance['number'] = strip_tags( $new_instance['number'] );
		$instance['feed_image']	= strip_tags( $new_instance['feed_image'] );
		$instance['show_option_all'] = !empty( $new_instance['show_option_all'] ) ? strip_tags( $new_instance['show_option_all'] ) : 0;

		$instance['hierarchical'] = !empty( $new_instance['hierarchical'] ) ? 1 : 0;
		$instance['use_desc_for_title']	= !empty( $new_instance['use_desc_for_title'] ) ? 1 : 0;
		$instance['show_count'] = !empty( $new_instance['show_count'] ) ? 1 : 0 ;
		$instance['hide_empty'] = !empty( $new_instance['hide_empty'] ) ? 1 : 0 ;
		$instance['feed'] = !empty( $new_instance['feed'] ) ? 1 : 0 ;

		return $instance;
	}

	/**
	 * Displays the widget control options in the Widgets admin screen.
	 * @since 0.6
	 */
	function form( $instance ) {

		$title = ! empty($instance['title']) ? esc_attr( $instance['title'] ) : THEME_SLUG.'-categories';
		$style = isset($instance['style']) ? esc_attr( $instance['style'] ) : 'list';
		$orderby = isset($instance['orderby']) ? esc_attr( $instance['orderby'] ) : 'name';
		$order = isset($instance['order']) ? esc_attr( $instance['order'] ) : 'ASC';
		$depth  = isset($instance['depth']) ? esc_attr( $instance['depth'] ): 0;

		$select = ! empty($instance['select']) ? $instance['select'] : array();
		$isexclude = isset($instance['isexclude']) ? (bool) $instance['isexclude']  : false;
		$number	= ! empty($instance['number']) ? esc_attr( $instance['number'] ) : '';
		$feed_image	= ! empty($instance['feed_image']) ? esc_attr( $instance['feed_image'] ) : '';
		$show_option_all = isset($instance['show_option_all']) ? esc_attr( $instance['show_option_all'] ) : '';

		$hierarchical = isset($instance['hierarchical']) ? (bool) $instance['hierarchical']  : false;
		$use_desc_for_title = isset($instance['use_desc_for_title']) ? (bool) $instance['use_desc_for_title'] : false;
		$show_last_update = isset($instance['show_last_update']) ? (bool) $instance['show_last_update']  : false;
		$show_count	= isset($instance['show_count']) ? (bool) $instance['show_count']  : false;
		$hide_empty = isset($instance['hide_empty']) ? (bool) $instance['hide_empty']  : false;
		$feed = isset($instance['feed']) ? (bool) $instance['feed']  : false;
?>
<div style="float:left;width:31%;">
	<p>
		<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', $this->textdomain ); ?></label>
		<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" style="width:100%;" />
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'style' ); ?>"><?php _e( 'Style:', $this->textdomain ); ?> </label> 
		<select id="<?php echo $this->get_field_id( 'style' ); ?>" name="<?php echo $this->get_field_name( 'style' ); ?>" class="widefat" style="width:100%;">
			<option <?php selected($style, 'list'); ?>>list</option>
			<option <?php selected($style, 'dropdown'); ?>>dropdown</option>
		</select>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'order' ); ?>"><?php _e( 'Order:', $this->textdomain ); ?> </label> 
		<select id="<?php echo $this->get_field_id( 'order' ); ?>" name="<?php echo $this->get_field_name( 'order' ); ?>" class="widefat" style="width:100%;">
			<option <?php selected($order, 'ASC'); ?>>ASC</option>
			<option <?php selected($order, 'DESC'); ?>>DESC</option>
		</select>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e( 'Order By:', $this->textdomain ); ?> </label> 
		<select id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>" class="widefat" style="width:100%;">
			<option <?php selected($orderby, 'name');  ?>>name</option>
			<option <?php selected($orderby, 'slug');  ?>>slug</option>
			<option <?php selected($orderby, 'ID');	?>>ID</option>
			<option <?php selected($orderby, 'count'); ?>>count</option>
			<option <?php selected($orderby, 'term_group'); ?>>term_group</option>
		</select>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'depth' ); ?>"><?php _e( 'Depth:', $this->textdomain ); ?> </label>
		<select id="<?php echo $this->get_field_id( 'depth' ); ?>" name="<?php echo $this->get_field_name( 'depth' ); ?>" class="widefat" style="width:100%;">
<?php for ($i = 0; $i < $this->deep; $i++) :?>
			<option value="<?php echo $i;?>"<?php selected($depth, $i); ?>><?php echo $i==0 ?  'Show All' : 'Max depth '.$i;?></option>
<?php endfor?>
		</select>
	</p>

</div>

<div style="float:left;width:31%;margin-left:3.5%;">
	<p>
		<label for="<?php echo $this->get_field_id('select'); ?>"><?php _e( 'Select Categories:' ,$this->textdomain); ?></label>
		<select style="height:6em" name="<?php echo $this->get_field_name('select'); ?>[]" id="<?php echo $this->get_field_id('select'); ?>" class="widefat" multiple="multiple">
<?php foreach($this->cat_array as $id => $cat):?>
<?php if(!in_array($id,$this->option_exclude)):?>
			<option value="<?php echo $id;?>"<?php echo in_array($id, $select)? ' selected="selected"':'';?>><?php echo $cat['name'];?></option>
<?php endif;?>
<?php endforeach;?>
		</select>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'isexclude' ); ?>">
		<input class="checkbox" type="checkbox" <?php checked( $isexclude, true ); ?> id="<?php echo $this->get_field_id( 'isexclude' ); ?>" name="<?php echo $this->get_field_name( 'isexclude' ); ?>" /> <?php _e( 'isexclude?', $this->textdomain ); ?> </label>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Show Lines:', $this->textdomain ); ?></label>
		<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" style="width:100%;" />
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'feed_image' ); ?>"><?php _e( 'Feed Image:', $this->textdomain ); ?> </label>
		<input id="<?php echo $this->get_field_id( 'feed_image' ); ?>" name="<?php echo $this->get_field_name( 'feed_image' ); ?>" type="text" value="<?php echo $feed_image; ?>" style="width:100%;" />
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'show_option_all' ); ?>"><?php _e( 'Show All Option:', $this->textdomain ); ?> </label>
		<input id="<?php echo $this->get_field_id( 'show_option_all' ); ?>" name="<?php echo $this->get_field_name( 'show_option_all' ); ?>" type="text" value="<?php echo !empty($show_option_all) ?$show_option_all : ''; ?>" style="width:100%;" />
	</p>
</div>

<div style="float:right;width:31%;margin-left:3.5%;">

	<p>
		<label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>">
		<input class="checkbox" type="checkbox" <?php checked( $hierarchical, true ); ?> id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>" /> <?php _e( 'Hierarchical?', $this->textdomain ); ?> </label>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'use_desc_for_title' ); ?>">
		<input class="checkbox" type="checkbox" <?php checked( $use_desc_for_title, true ); ?> id="<?php echo $this->get_field_id( 'use_desc_for_title' ); ?>" name="<?php echo $this->get_field_name( 'use_desc_for_title' ); ?>" /> <?php _e( 'Use description?', $this->textdomain ); ?></label>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'show_count' ); ?>">
		<input class="checkbox" type="checkbox" <?php checked( $show_count, true ); ?> id="<?php echo $this->get_field_id( 'show_count' ); ?>" name="<?php echo $this->get_field_name( 'show_count' ); ?>" /> <?php _e( 'Show count?', $this->textdomain ); ?> </label>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'hide_empty' ); ?>">
		<input class="checkbox" type="checkbox" <?php checked( $hide_empty, true ); ?> id="<?php echo $this->get_field_id( 'hide_empty' ); ?>" name="<?php echo $this->get_field_name( 'hide_empty' ); ?>" /> <?php _e( 'Hide empty?', $this->textdomain ); ?> </label>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'feed' ); ?>">
		<input class="checkbox" type="checkbox" <?php checked( $feed, true ); ?> id="<?php echo $this->get_field_id( 'feed' ); ?>" name="<?php echo $this->get_field_name( 'feed' ); ?>" /> <?php _e( 'Show RSS feed?', $this->textdomain ); ?> </label>
	</p>
</div>
<div style="clear:both;">&nbsp;</div>
<?php
	}
}

?>
