<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**

 * Pagination Class

 *

 * @package		CodeIgniter

 * @link		http://codeigniter.com/user_guide/libraries/pagination.html

 * 

 * Modified by CodexWorld.com

 * @Ajax pagination functionality has added with this library. 

 * @It will helps to integrate Ajax pagination with loading image in CodeIgniter application.

 * @TutorialLink http://www.codexworld.com/ajax-pagination-in-codeigniter-framework/

 */

class Ajax_pagination{



	var $base_url           = ''; // The page we are linking to
	var $total_rows  		= ''; // Total number of items (database results)
	var $per_page	 		= 10; // Max number of items you want shown per page
	var $num_links			=  2; // Number of "digit" links to show before/after the currently viewed page
	var $cur_page	 		=  0; // The current page being viewed
	var $first_link   		= 'First';
	var $next_link			= '&#187;';
	var $prev_link			= '&#171;';
	var $last_link			= 'Last';
	var $uri_segment		= 3;
	var $full_tag_open		= '<div class="pagination">';
	var $full_tag_close		= '</div>';
	var $first_tag_open		= '';
	var $first_tag_close	= '&nbsp;';
	var $last_tag_open		= '&nbsp;';
	var $last_tag_close		= '';
	var $cur_tag_open		= '&nbsp;<b>';
	var $cur_tag_close		= '</b>';
	var $next_tag_open		= '&nbsp;';
	var $next_tag_close		= '&nbsp;';
	var $prev_tag_open		= '&nbsp;';
	var $prev_tag_close		= '';
	var $num_tag_open		= '&nbsp;';
	var $num_tag_close		= '';
	var $target             = '';
    var $anchor_class		= '';
    var $show_count         = true;
    var $link_func          = 'getData';
    var $loading            = '.loading';

	/**
	 * Constructor
	 * @access	public
	 * @param	array	initialization parameters
	 */

	function CI_Pagination($params = array())
	{
		if (count($params) > 0){
			$this->initialize($params);		
		}
		log_message('debug', "Pagination Class Initialized");
	}

	/**
	 * Initialize Preferences
	 * @access	public
	 * @param	array	initialization parameters
	 * @return	void
	 */
	function initialize($params = array()){
		if (count($params) > 0){
			foreach ($params as $key => $val){
				if (isset($this->$key)){
					$this->$key = $val;
				}
			}
		}
		// Apply class tag using anchor_class variable, if set.
		if ($this->anchor_class != ''){
			$this->anchor_class = 'class="' . $this->anchor_class . '" ';
		}
	}

	/**
	 * Generate the pagination links
	 * @access	public
	 * @return	string
	 */	
	function create_links()
	{
		$addparameterstosend = "";
		
		if($_SERVER['QUERY_STRING']!="")
		{
			$addparameterstosend = "?" . $_SERVER['QUERY_STRING'];
		}
		// If our item count or per-page total is zero there is no need to continue.
		if ($this->total_rows == 0 OR $this->per_page == 0){
		   return '';
		}

		// Calculate the total number of pages
		$num_pages = ceil($this->total_rows / $this->per_page);

		// Is there only one page? Hm... nothing more to do here then.
		if ($num_pages == 1){
            $info = '<div class="col-md-5 col-sm-5">
                    <div class="dataTables_info" id="sample_3_info" role="status" aria-live="polite">Showing 1 to '.$this->total_rows.' records</div>
                  </div>
                  ';
			return $info;
		}

		// Determine the current page number.
		$CI =& get_instance();	
		if ($this->uri_segment != 0){
			$this->cur_page = $this->uri_segment;
			// Prep the current page - no funny business!
			$this->cur_page = (int) $this->cur_page;
		}
		$this->num_links = (int)$this->num_links;
		if ($this->num_links < 1){
			show_error('Your number of links must be a positive number.');
		}
		if ( ! is_numeric($this->cur_page)){
			$this->cur_page = 0;
		}

		// Is the page number beyond the result range?
		// If so we show the last page
		if ($this->cur_page > $this->total_rows){
			$this->cur_page = ($num_pages - 1) * $this->per_page;
		}

		$uri_page_number = $this->cur_page;
		$this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
		// Calculate the start and end numbers. These determine

		// which number to start and end the digit links with
		$start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
		$end   = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
		## check
		if($num_pages>=10 && $this->cur_page<6)
		{
			$end = 2*$this->num_links;
		}
		

		// Add a trailing slash to the base URL if needed
		$this->base_url = rtrim($this->base_url, '/') .'/';
  		// And here we go...
		$output = '';

		// SHOWING LINKS

		if ($this->show_count){
			$curr_offset = $this->uri_segment;
			$info = '<div class="pt-2">Showing ' . ( $curr_offset + 1 ) . ' to ' ;
			if( ( $curr_offset + $this->per_page ) < ( $this->total_rows -1 ) )
				$info .= $curr_offset + $this->per_page;
			else
				$info .= $this->total_rows;
			$info .= ' of ' . $this->total_rows . '</div><div class="pagination">';
			$output .= $info;
		}
		 $output .= '';
		 $disabled = "";
		 $prev = 'href="'.$this->base_url.($this->cur_page -1).$addparameterstosend.'"';
		 if($this->cur_page <=1)
		 {
		 	$disabled = "disabled";
			$prev = "";
		 }
		 
		 $output .='<a '.$prev.' title="Prev" class="page gradient">Previous</a>';
		 $j=1;
//		 echo "<br>" . $start . ' => ' . $end . " = " . $this->cur_page . " ==> " . $uri_page_number;
		 for($i=$start;$i<=$end;$i++)
		 {
			 if($i==$this->cur_page)
			 {
				 $output .='<span class="page active">'.$i.'</span>';
			 }
		   else
		   {
		     $output .='<a href="'.$this->base_url.$i.$addparameterstosend.'"  class="page gradient">'.$i.'</a>';
		   }
		   $j++;
		 }
		 
		 $disabled = "";
		 $next = 'href="'.$this->base_url.($this->cur_page + 1). $addparameterstosend .'"';  
		 
		 if($this->cur_page == $num_pages)
		 {
		 	$disabled = "disabled";
			$next = "";
		 }
		 $output .='<a '.$next.' title="Next" class="page gradient">Next</a></div>';
		return $output;
	}

	function getAJAXlink($count, $text) {
        $pageCount = $count?$count:0;
		return '<a href="javascript:void(0);"' . $this->anchor_class . ' onclick="'.$this->link_func.'('.$pageCount.')">'. $text .'</a>';
	}
}

// END Pagination Class
