<?php
  /**
  * Class: MSHPager
  *
  * This class can be used for pagination in CodeIgniter
  *
  * @author Mohammad Sajjad Hossain
  *         msh134@gmail.com, info@sajjadhossain.com
  */
  class MSHPager
  {
	  var $url = '';
	  var $sql = '';
	  var $pageIndex = 1;
	  var $rowPerPage = 10;
	  var $totalRecords = 0;
	  var $totalPages = 0;
	  var $startIndex = 0;
	  var $endIndex = 0;
	  var $nextLink = '';
	  var $previousLink = '';

	  var $pageIndexVar = 'pageIndex';
	  var $imagePath = 'images/';

      var $CI;

	  function MSHPager($sql = '', $pageIndex = 1)
	  {
		  $this->sql = $sql;
		  $this->pageIndex = $pageIndex;
          $this->CI = get_instance();
	  }

	  function getTotalRecords()
	  {
		  if($this->sql)
		  {
			  //counting total number of rows
			  $sql = 'SELECT COUNT(*) AS total FROM (' . $this->sql . ') abc';
			  $query = $this->CI->db->query($sql);
			  $row = $query->row_array();
			  $this->totalRecords = $row['total'];
		  }

		  return $this->totalRecords;
	  }

	  function getTotalPages()
	  {
		  if($this->sql)
		  {
			  $this->totalPages = (int) ceil($this->totalRecords/$this->rowPerPage);
		  }

		  return $this->totalPages;
	  }

	  function setPageIndex()
	  {
		  if($this->pageIndex > $this->totalPages)
		  {
			  $pageIndex = $totalPages;
		  }
	  }

	  function setStartIndex()
	  {
		  $this->startIndex = ($this->pageIndex - 1) * $this->rowPerPage;
	  }

	  function setEndIndex()
	  {
		  if($this->totalRecords > $this->rowPerPage)
		  {
			  $this->endIndex = $this->startIndex + $this->rowPerPage;
		  }
		  else
		  {
			  $endIndex = $this->totalRecords;
		  }

		  if(isset($totalPages) && $pageIndex == $totalPages)
		  {
			  if($totalRecords < $totalPages * $rowPerPage)
			  {
				  $endIndex = $totalRecords;
			  }
		  }
	  }

	  function getRangeText()
	  {
		  if($this->totalRecords > 0)
		  {
			  $rangeStart = $this->startIndex + 1;

			  if(($this->totalRecords < $this->rowPerPage) || ($this->pageIndex == $this->totalPages))
			  {
				  $rangeEnd = $this->totalRecords;
			  }
			  else
			  {
				  $rangeEnd = $this->endIndex;
			  }

			  return 'Showing '  . $rangeStart . ' - ' . $rangeEnd . ' of ' . $this->totalRecords;
		  }
	  }

	  function build()
	  {
		  $this->getTotalRecords();
		  $this->getTotalPages();
		  $this->setPageIndex();
		  $this->setStartIndex();
		  $this->setEndIndex();

		  // link for next page
          isset($url) OR $url = '';
		  $this->nextLink = $url . ($this->pageIndex + 1);

		  // link for previous page
		  $this->previousLink = $url . ($this->pageIndex - 1);

		  $this->sql .= ' LIMIT ' . $this->startIndex . ', ' .  $this->rowPerPage;
	  }

	  function getPagedData()
	  {
          $this->build();

		  $query = $this->CI->db->query($this->sql);

		  return $query->result_array();
	  }

	  function getImageLinksWithDropDown()
	  {
		  $this->getImageLinksWithDropDown();
	  }

	  function getImageLinks($withCombo = false)
	  {
		  $links = '';

		  if($this->pageIndex > 1)
		  {
              $links .= '<a href="' . $this->url . '?'  . $this->pageIndexVar . '=1"><img src="'.$this->imagePath.'first.gif" border="0" align="absmiddle" alt="First" /></a> ';
			  $links .= '<a href="' . $this->url . '?'  . $this->pageIndexVar . '=' . ($this->pageIndex - 1) . '"><img src="'.$this->imagePath.'previous.gif" border="0" align="absmiddle" alt="Previous" /></a> ';
		  }
		  else
		  {
			  $links .= '<img src="'.$this->imagePath.'first.gif" border="0" align="absmiddle" alt="First" /> ';
			  $links .= '<img src="'.$this->imagePath.'previous.gif" border="0" align="absmiddle" alt="Previous" /> ';
		  }

		  if($withCombo)
		  {
			  $links .= '<form id="pager" action="' . $this->url . '" method="get">
							<select name="' . $this->pageIndexVar . '" onchange="document.forms.pager.submit()">';

			  /*for($i = 1; $i <= $this->totalPages; $i++)
			  {
				  $selected = ($i == $this->pageIndex) ? 'selected="selected"' : '';
				  $links .= '<option value="' . $i . '" ' . $selected . '>' . $i . '</option>';
			  }*/

			  $links .= '   </select>
						</form>';
		  }

		  if($this->pageIndex < $this->totalPages)
		  {
			  $links .= '<a href="' . $this->url . '?'  . $this->pageIndexVar . '=' . ($this->pageIndex + 1) . '"><img src="'.$this->imagePath.'next.gif" border="0" align="absmiddle" alt="Next" /></a> ';
			  $links .= '<a href="' . $this->url . '?'  . $this->pageIndexVar . '=' . $this->totalPages . '"><img src="'.$this->imagePath.'last.gif" border="0" align="absmiddle" alt="Last" /></a> ';
		  }
		  else
		  {
			  $links .= '<img src="'.$this->imagePath.'next.gif" border="0" align="absmiddle" alt="Next" /> ';
			  $links .= '<img src="'.$this->imagePath.'last.gif" border="0" align="absmiddle" alt="Last" /> ';
		  }

		  return $links;
	  }

	  function getNumberLinks()
	  {
		  $links = '';

          if($this->totalPages > 1)
          {
              for($i = 1; $i <= $this->totalPages; $i++)
              {
                  if($i == $this->pageIndex)
                  {
                      $a = 'class="a_sel"';
                  }
                  else
                  {
                      $a = 'class="a_norm"';
                  }

                  $links .= anchor($this->url . $i . $this->CI->config->item('url_suffix'), $i, $a);
              }

              if($this->pageIndex > 1)
              {
                  $a = 'class="a_norm"';
                  $index = $this->pageIndex - 1;
              }
              else
              {
                  $index = 1;
                  $a = 'class="a_sel"';
              }

			  $links = anchor($this->url . $index . $this->CI->config->item('url_suffix'), '&lt;&lt;', $a) . $links;

              if($this->pageIndex < $this->totalPages)
              {
                  $index = $this->pageIndex + 1;
                  $a = 'class="a_norm"';
              }
              else
              {
                  $index = $this->totalPages;
                  $a = 'class="a_sel"';
              }
              $links .= anchor($this->url . $index . $this->CI->config->item('url_suffix'), '&gt;&gt;', $a);

	  		  if($links != '')
			  {
				  $links = '<div class="alpha_search_box">' . $links . '</div>';
			  }
          }

		  return $links;
	  }
  }
?>
