<?php
class Theme_Widget_ExPages extends WP_Widget {

	var $textdomain;
	var $page_array;
	var $deep = 1;

	function Theme_Widget_ExPages() {

		$this->textdomain   = THEME_ADMIN_DOMAIN;
		$widget_ops  = array( 'classname' => 'pages', 'description' => __( 'An advanced widget that gives you total control over the output of your page links.', $this->textdomain) );
		$control_ops = array( 'width' => 800, 'height' => 350);

		$pages = get_pages();
		foreach ($pages as $page) {
			$this->page_array[$page->ID] = array('name'=>$page->post_name,'parent' => $page->post_parent);
		}
		if(!empty($this->page_array)){
			foreach ($this->page_array as $page) {
				$res = $this->page_walk($page['parent']);
				if( $res > $this->deep) {
					$this->deep = $res;
				}
			}
		}
		$this->WP_Widget( "expages", THEME_SLUG.' - '.__( 'Pages',  $this->textdomain ), $widget_ops, $control_ops );
	}

	function page_walk($parent){
		if ($parent != 0 ) {
			return $this->page_walk($this->page_array[$parent]['parent'])+1;
		} else {
			return 1;
		}
	}

	function widget( $args, $instance ) {
		$cache = wp_cache_get('theme_widget_popular_posts', 'widget');

		extract( $args );

		$title = apply_filters( 'widget_title', $instance['title'] );

		$sort_column = $instance['sort_column'];
		$sort_order = $instance['sort_order'];
		$select = !empty($instance['select']) ? implode(",",$instance['select']) : '';
		$isexclude = !empty($instance['isexclude']) ? '1' : '0';
		$depth = (int)$instance['depth'];
		$authors = $instance['authors'];
		$link_before = $instance['link_before'];
		$link_after = $instance['link_after'];
		$date_format = $instance['date_format'];
		$show_date = $instance['show_date'];
		$number = (int)$instance['number'];
		$meta_value = $instance['meta_value'];
		$meta_key = $instance['meta_key'];
		$hierarchical = isset( $instance['hierarchical'] ) ? $instance['hierarchical'] : false;
		$show_home = isset( $instance['show_home'] ) ? $instance['show_home'] : false;
		if ( !$date_format )
			$date_format = get_option( 'date_format' );
		$exclude=null;
		$include=null;
		if ( $isexclude ) {
			$exclude = $select;
		} else {
			$include = $select;
		}

		$pages = array(
			'depth' => $depth,
			'sort_column' => $sort_column,
			'sort_order' => $sort_order,
			'show_date' => $show_date,
			'date_format' => $date_format,
			'exclude' => $exclude,
			'include' => $include,
			'hierarchical' => $hierarchical,
			'authors' => $authors,
			'link_before' => $link_before,
			'link_after' => $link_after,
			'number' => $number,
			'meta_key' => $meta_key,
			'meta_value' => $meta_value,
			'title_li' => '',
			'echo' => 0,
		);
		echo $before_widget;

		if ( $title )
			echo $before_title . $title . $after_title;

		echo '<ul class="pages">';

		if ( $show_home )
			echo '<li class="page_item' . ( ( is_front_page() ) ? ' current_page_item' : '' ) . '"><a href="' . get_bloginfo( 'url' ) . '" title="' . get_bloginfo( 'name' ) . '">' . $link_before . __( 'Home', $this->textdomain) . $link_after . '</a></li>';

		echo str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages( $pages ) );

		echo '</ul>' . $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['depth'] = strip_tags( $new_instance['depth'] );
		$instance['select'] = $new_instance['select'];
		$instance['isexclude'] = !empty( $new_instance['isexclude'] ) ? 1 : 0;
		$instance['authors'] = ! empty( $new_instance['meta_key'] ) ? strip_tags( $new_instance['authors'] ) : '';
		$instance['date_format'] = strip_tags( $new_instance['date_format'] );
		$instance['number'] = strip_tags( $new_instance['number'] );
		$instance['sort_column'] = $new_instance['sort_column'];
		$instance['sort_order'] = $new_instance['sort_order'];
		$instance['show_date'] = $new_instance['show_date'];
		$instance['link_before'] = $new_instance['link_before'];
		$instance['link_after'] = $new_instance['link_after'];
		$instance['meta_value']	= ! empty( $new_instance['meta_value'] ) ? strip_tags($new_instance['meta_value']) : '';
		$instance['meta_key'] = ! empty( $new_instance['meta_key'] ) ? $new_instance['meta_key'] : '';

		$instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
		$instance['show_home'] = ! empty( $new_instance['show_home'] ) ? 1 : 0;

		return $instance;
	}

	function form( $instance ) {

		$title = ! empty($instance['title']) ? esc_attr( $instance['title'] ) : THEME_SLUG.'-pages';
		$depth = isset($instance['depth']) ? esc_attr( $instance['depth'] ) : 0;
		$select = ! empty($instance['select']) ? $instance['select'] : array();
		$isexclude = isset($instance['isexclude']) ? (bool) $instance['isexclude']  : false;
		$authors = ! empty($instance['authors']) ? esc_attr( $instance['authors'] ) : '';
		$date_format = isset($instance['date_format']) ? esc_attr( $instance['date_format'] ) : '';
		$number = ! empty($instance['number']) ? esc_attr( $instance['number'] ) : '';
		$sort_column = isset($instance['sort_column ']) ? esc_attr( $instance['sort_column '] ) : 'name';
		$sort_order = isset($instance['sort_order']) ? esc_attr( $instance['sort_order'] ) : 'ASC';
		$show_date = isset($instance['show_date']) ? esc_attr( $instance['show_date'] ) : '';
		$link_before = isset($instance['link_before']) ? esc_attr( $instance['link_before'] ) : '';
		$link_after = isset($instance['link_after']) ? esc_attr( $instance['link_after'] ) : '';
		$meta_key = ! empty($instance['meta_key']) ? esc_attr( $instance['meta_key'] ) : '';
		$meta_value = ! empty($instance['meta_value']) ? esc_attr( $instance['meta_value']) : '' ; 
		$hierarchical = isset($instance['hierarchical']) ? (bool) $instance['hierarchical']  : false;
		$show_home = isset($instance['show_home']) ? (bool) $instance['show_home']  : 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( 'sort_column' ); ?>"><?php _e( 'Order By:',$this->textdomain); ?> </label>
		<select id="<?php echo $this->get_field_id( 'sort_column' ); ?>" name="<?php echo $this->get_field_name( 'sort_column' ); ?>" class="widefat" style="width:100%;">
			<option <?php selected( $sort_column , 'post_title'); ?>>post_title</option>
			<option <?php selected( $sort_column , 'menu_order'); ?>>menu_order</option>
			<option <?php selected( $sort_column , 'post_date'); ?>>post_date</option>
			<option <?php selected( $sort_column , 'post_modified'); ?>>post_modified</option>
			<option <?php selected( $sort_column , 'ID'); ?>>ID</option>
			<option <?php selected( $sort_column , 'post_author'); ?>>post_author</option>
			<option <?php selected( $sort_column , 'post_name'); ?>>post_name</option>
		</select>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'sort_order' ); ?>"><?php _e( 'Sort Order:', $this->textdomain); ?> </label>
		<select id="<?php echo $this->get_field_id( 'sort_order' ); ?>" name="<?php echo $this->get_field_name( 'sort_order' ); ?>" class="widefat" style="width:100%;">
			<option <?php selected( $sort_order , 'ASC'); ?>>ASC</option>
			<option <?php selected( $sort_order , 'DESC'); ?>>DESC</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>
	<p>
		<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number:', $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>
</div>

<div style="float:left;width:31%;margin-left:3.5%;">
	<p>
		<label for="<?php echo $this->get_field_id('select'); ?>"><?php _e( 'Select Pages:' ,$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->page_array as $id => $page):?>
			<option value="<?php echo $id;?>" <?php if(in_array($id,$select)) echo 'selected="selected"';?>><?php echo $page['name'];?></option>
<?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( 'meta_key' ); ?>"><?php _e( 'Meta_key:', $this->textdomain); ?> </label>
		<select id="<?php echo $this->get_field_id( 'meta_key' ); ?>" name="<?php echo $this->get_field_name( 'meta_key' ); ?>" class="widefat" style="width:100%;">
			<option value='' <?php selected($meta_key, '' ); ?>>No selected</option>
<?php
		global $wpdb;
		$keys = $wpdb->get_col( "
			SELECT meta_key
			FROM $wpdb->postmeta
			GROUP BY meta_key
			HAVING meta_key NOT LIKE '\_%'
			ORDER BY meta_key");
		if( $keys ):
			foreach ($keys as $key) :
?>			
			<option value="<?php echo esc_attr($key);?>" <?php if( $meta_key == esc_attr($key)) echo 'selected="selected"'; ?>><?php echo esc_attr($key);?></option>
<?php  
				endforeach; 
endif;

?>	   
		</select>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'meta_value' ); ?>"><?php _e( 'Meta value:', $this->textdomain); ?> </label>
		<input id="<?php echo $this->get_field_id( 'meta_value' ); ?>" name="<?php echo $this->get_field_name( 'meta_value' ); ?>" type="text" value="<?php echo $meta_value; ?>" style="width:100%;" />
	</p>

</div>

<div style="float:right;width:31%;margin-left:3.5%;">
	<p>
		<label for="<?php echo $this->get_field_id( 'authors' ); ?>"><?php _e( 'Authors:', $this->textdomain); ?> </label>
		<select id="<?php echo $this->get_field_id( 'authors' ); ?>" name="<?php echo $this->get_field_name( 'authors' ); ?>" class="widefat" style="width:100%;">
			<option value='' <?php selected($authors, '' ); ?>>All</option>
<?php
$users = get_users_of_blog();
foreach ($users as $user) :
?>			
			<option value="<?php echo $user->user_id;?>"<?php if( $authors == $user->user_id) echo 'selected="selected"' ?>><?php echo $user->display_name;?></option>
<?php  endforeach; ?>	   
		</select>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'link_before' ); ?>"><?php _e( 'Before Link:', $this->textdomain); ?> </label>
		<input id="<?php echo $this->get_field_id( 'link_before' ); ?>" name="<?php echo $this->get_field_name( 'link_before' ); ?>" type="text" value="<?php echo $link_before; ?>" style="width:100%;" />
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'link_after' ); ?>"><?php _e( 'After Link:', $this->textdomain); ?> </label>
		<input id="<?php echo $this->get_field_id( 'link_after' ); ?>" name="<?php echo $this->get_field_name( 'link_after' ); ?>" type="text" value="<?php echo $link_after; ?>" style="width:100%;" />
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Show date?:', $this->textdomain); ?> </label>
		<select id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" class="widefat" style="width:100%;">
			<option value='' <?php selected($show_date, '' ); ?>>no date</option>
			<option <?php selected($show_date, 'created' ); ?>>created</option>
			<option <?php selected($show_date, 'modified' ); ?>>modified</option>
		</select>
	</p>
	<p>
		<label for="<?php echo $this->get_field_id( 'date_format' ); ?>"><?php _e( 'Date Format:', $this->textdomain); ?> </label>
		<input id="<?php echo $this->get_field_id( 'date_format' ); ?>" name="<?php echo $this->get_field_name( 'date_format' ); ?>" type="text" value="<?php echo $date_format; ?>" style="width:100%;" />
	</p>
	<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>
		<input class="checkbox" type="checkbox" <?php checked( $show_home, true ); ?> id="<?php echo $this->get_field_id( 'show_home' ); ?>" name="<?php echo $this->get_field_name( 'show_home' ); ?>" />
		<label for="<?php echo $this->get_field_id( 'show_home' ); ?>"><?php _e( 'Show home?', $this->textdomain); ?> </label>
	</p>
</div>
<div style="clear:both;">&nbsp;</div>
<?php
	}
}

?>
