<?php
if(!class_exists('WP_List_Table')){
	require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}

class FLCLinksTable extends WP_List_Table {

	private $data;

	public function __construct( $data ){
		$this->data 	= $data;
		parent::__construct( array(
			'singular'  => 'Link',
			'plural'    => 'Link',
			'ajax'      => false
		) );
	}
	public function column_default( $item, $column_name ){
        	switch( $column_name ){
			case 'cb':
				return '<input type="checkbox">';

            		case 'url':	
                		return sprintf(
					'<a href="%s">%s</a>',
					$item['link'],
					$item['anchor']
				);

			case 'target':
				return $item['target'];

            		case 'do_index':
				if($item['do_index'])
					$do_index = 'doindex';
				else
					$do_index = 'noindex';	
				return $do_index;

			case 'do_follow':
				if($item['do_follow'])
					$do_follow = 'dofollow';
				else
					$do_follow = 'nofollow';
				return $do_follow;

			case 'created':
				return  date("m.d.y", $item['created']);
            		default:
                		return '';
        	}
    	}
	public function column_cb( $item ){
		return sprintf(
			'<input type="checkbox" value="%s" name="link_ids[]">',
			(string)$item['id']
		);
	}
	public function column_url($item) {
		$actions = array(
		'edit'    => sprintf('<a href="%s">Edit</a>', 
					add_query_arg( 
						array( 	
							'action' => 'edit',
							'edit' => (string)$item['id'], 
							'delete' => NULL 
						) 
					)
				),
		'delete'    => sprintf('<a href="%s">Delete</a>', 
					add_query_arg( 
						array( 
							'source' => 'links',
							'delete' => (string)$item['id'], 
							'edit' => NULL 
						) 
					)
				),
		);

		if($item['do_follow'])
			$do_follow = 'dofollow';
		else
			$do_follow = 'nofollow';

		if($item['do_index'])
			$do_index = 'doindex';
		else
			$do_index = 'noindex';

		return sprintf('%1$s %2$s', 
				sprintf(
					'<a href="%s" target="_blank" rel="%s %s">%s</a>',
					$item['link'],
					$do_follow,
					$do_index,
					$item['anchor']
				),
				$this->row_actions($actions)
		);
	}
	public function get_bulk_actions() {
        	$actions = array(
            		'delete'    => 'Delete'
        	);
        	return $actions;
	}
	public function process_bulk_action() {
	}
	public function get_columns(){
		$columns = array(
			'cb'        		=> '<input type="checkbox">',
			'url'    		=> 'Url',
			'target'    		=> 'Target',
			'do_follow'    		=> 'Follow',
			'do_index'    		=> 'Indexing',
			'created'    		=> 'Date Created',
        	);
		return $columns;
	}  
	public function get_sortable_columns() {
		$sortable_columns = array(
			'url'   => array('url', false ),
			'target'   => array('target', false ),
			'created'   => array('created', false ),
			'created'   => array('created', false ),
			'do_index'   => array('do_index', false ),
			'do_follow'   => array('do_follow', false ),
		);
		return $sortable_columns;
	}
	public function prepare_items(){
		$per_page = 20;
		$columns = $this->get_columns();
		$hidden = array();
		$sortable = $this->get_sortable_columns();
 		$this->_column_headers = array( $columns, $hidden, $sortable );
		$data = $this->data;

        	function usort_reorder($a,$b){
            		$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'user_login';
			$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc';
			$result = strcmp($a[$orderby], $b[$orderby]);
			return ($order==='asc') ? $result : -$result;
		}
		usort($data, 'usort_reorder');
		$current_page = $this->get_pagenum();
		$total_items = count($data);
		$data = array_slice($data,(($current_page-1)*$per_page),$per_page);
		$this->items = $data;

	        $this->set_pagination_args( array(
			'total_items' => $total_items,
			'per_page'    => $per_page,
			'total_pages' => ceil($total_items/$per_page)
        	) );
	}
}
?>
