<?php

class Paging {





	private $records;

	private $max_pp;

	private $numb_of_pages;

	private $numb_of_records;

	private $currents;

	private $offset = 0;

	public static $key = 'pg';

	public $url;

	

	

	

	public function __construct($rows, $max = 10) {

		$this->records = $rows;

		$this->numb_of_records = count($this->records);

		$this->max_pp = $max;

		$this->url = Url::getCurrentUrl(self::$key);

		$current = Url::getParam(self::$key);

		$this->currents = !empty($current) ? $current : 1;

		$this->numberOfPages();

		$this->getOffset();

	}

	

	

	private function numberOfPages() {

		$this->numb_of_pages = ceil($this->numb_of_records / $this->max_pp);

	}

	

	

	private function getOffset() {

		$this->offset = ($this->currents - 1) * $this->max_pp;

	}

	

	

	

	public function getRecords() {

		$out = array();

		if ($this->numb_of_pages > 1) {

			$last = ($this->offset + $this->max_pp);

			

			for($i = $this->offset; $i < $last; $i++) {

				if ($i < $this->numb_of_records) {

					$out[] = $this->records[$i];

				}

			}

		} else {

			$out = $this->records;

		}

		return $out;

	}

	

	

	private function getLinks() {

		if ($this->numb_of_pages > 1) {

			

			$out = array();

			

			// first link

			if ($this->currents > 1) {

				$out[] = "<a href=\"".$this->url."\">First</a>";

			} else {

				$out[] = "<span>First</span>";

			}

			

			

			// previous link

			if ($this->currents > 1) {

				

				// previous page number

				$id = ($this->currents - 1);

				

				$url = $id > 1 ? 

					$this->url."&amp;".self::$key."=".$id :

					$this->url;

				$out[] = "<a href=\"{$url}\">Previous</a>";

				

			} else {

				$out[] = "<span>Previous</span>";

			}

			

			

			// next link

			if ($this->currents != $this->numb_of_pages) {

				// next page number

				$id = ($this->currents + 1);

				

				$url = $this->url."&amp;".self::$key."=".$id;

				$out[] = "<a href=\"{$url}\">Next</a>";

				

			} else {

				$out[] = "<span>Next</span>";

			}

			

			

			// last link

			if ($this->currents != $this->numb_of_pages) {

				$url = $this->url."&amp;".self::$key."=".$this->numb_of_pages;

				$out[] = "<a href=\"{$url}\">Last</a>";

			} else {

				$out[] = "<span>Last</span>";

			}

			

			return "<li>".implode("</li><li>", $out)."</li>";

			

		}	

	}

	

	

	public function getPaging() {

		$links = $this->getLinks();

		if (!empty($links)) {

			$out  = '<nav align="center">

 					 	<ul class="pagination">

   						 	<li class="disabled">

								<span>

									<span aria-hidden="true">&laquo;</span>

							  	</span>';

			$out .= $links;

			$out .= ' </li>	

						</ul>

							</nav>';

			return $out;

		}

	}









      

   







}



?>