Login   Register  
PHP Classes
elePHPant
Icontem

File: pagination.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Marcos Taranta  >  Holy Pagination  >  pagination.php  >  Download  
File: pagination.php
Role: Class source
Content type: text/plain
Description: The main class
Class: Holy Pagination
Display pagination links using templates
Author: By
Last change: Added the range option, that shows the range of pages from the current page.
Date: 5 years ago
Size: 13,737 bytes
 

Contents

Class file image Download
<?php

/**
 * Make a printable pagination that can be used with any type of content you want to paginate
 * 
 * @author Marcos Luiz Cassarini Taranta <marcos.taranta@gmail.com>
 * @since 2009-01-26
 */

class Pagination {
	private $pages; // Number of pages in the pagination
	private $currentPage; // Current page of the pagination system
	private $template; // Template of the whole pagination
	private $first; // Template of the link to the first page
	private $firstNoLink; // Template of the link to the first page when you are in the first page
	private $firstAlwaysLink; // Will the first always have link? Even when you are in the first page?
	private $previous; // Template of the link to the previous page
	private $previousNoLink; // Template of the link to the previous page when there's no previous page
	private $previousAlwaysLink; // Will the previous always have link? Even when there's no previous page?
	private $page; // Template of each page number
	private $pageNoLink; // Template of each page without link
	private $pageAlwaysLink; // Will the pagination always have link? Even if the page is the current one?
	private $range; // Range of the number of pages more and less to be show using the current page as point
	private $separator; // Template of the separator between number of the pages
	private $showSeparator; // Show the separator or no?
	private $next; // Template of the link to the next page
	private $nextNoLink; // Template of the link to the next page when there's no next page
	private $nextAlwaysLink; // Will the next always have link? Even when there's no next page?
	private $last; // Template of the link to the last page
	private $lastNoLink; // Template of the link to the last page when you're in the last page
	private $lastAlwaysLink; // Will the last always have link? Event when you're in the last page?
	private $link; // Default link address
	
	/**
	 * @return the number of the current page
	 */
	public function getCurrentPage() {
		return $this->currentPage;
	}
	
	/**
	 * @param integer $currentPage
	 */
	public function setCurrentPage($currentPage) {
		if($currentPage > 0)
			$this->currentPage = $currentPage;
	}
	
	/**
	 * @return the template of the link to the first page
	 */
	public function getFirst() {
		return $this->first;
	}
	
	/**
	 * @param string $first
	 */
	public function setFirst($first) {
		$this->first = $first;
	}
	
	/**
	 * @return true if the first always have link or false if dont
	 */
	public function isFirstAlwaysLink() {
		return $this->firstAlwaysLink;
	}
	
	/**
	 * @param boolean $firstAlwaysLink
	 */
	public function setFirstAlwaysLink($firstAlwaysLink) {
		$this->firstAlwaysLink = $firstAlwaysLink;
	}
	
	/**
	 * @return the template of the first without link
	 */
	public function getFirstNoLink() {
		return $this->firstNoLink;
	}
	
	/**
	 * @param string $firstNoLink
	 */
	public function setFirstNoLink($firstNoLink) {
		$this->firstNoLink = $firstNoLink;
	}
	
	/**
	 * @return the template of the link to the last page
	 */
	public function getLast() {
		return $this->last;
	}
	
	/**
	 * @param string $last
	 */
	public function setLast($last) {
		$this->last = $last;
	}
	
	/**
	 * @return true if the last always will have link or false if dont
	 */
	public function isLastAlwaysLink() {
		return $this->lastAlwaysLink;
	}
	
	/**
	 * @param boolean $lastAlwaysLink
	 */
	public function setLastAlwaysLink($lastAlwaysLink) {
		$this->lastAlwaysLink = $lastAlwaysLink;
	}
	
	/**
	 * @return template of the last when there's no link
	 */
	public function getLastNoLink() {
		return $this->lastNoLink;
	}
	
	/**
	 * @param string $lastNoLink
	 */
	public function setLastNoLink($lastNoLink) {
		$this->lastNoLink = $lastNoLink;
	}
	
	/**
	 * @return the template of the address of the link
	 */
	public function getLink() {
		return $this->link;
	}
	
	/**
	 * @param string $link
	 */
	public function setLink($link) {
		$this->link = $link;
	}
	
	/**
	 * @return template of the link to the next page
	 */
	public function getNext() {
		return $this->next;
	}
	
	/**
	 * @param string $next
	 */
	public function setNext($next) {
		$this->next = $next;
	}
	
	/**
	 * @return true if the next will always have link or false if dont
	 */
	public function isNextAlwaysLink() {
		return $this->nextAlwaysLink;
	}
	
	/**
	 * @param boolean $nextAlwaysLink
	 */
	public function setNextAlwaysLink($nextAlwaysLink) {
		$this->nextAlwaysLink = $nextAlwaysLink;
	}
	
	/**
	 * @return the template of the next when there's no link
	 */
	public function getNextNoLink() {
		return $this->nextNoLink;
	}
	
	/**
	 * @param string $nextNoLink
	 */
	public function setNextNoLink($nextNoLink) {
		$this->nextNoLink = $nextNoLink;
	}
	
	/**
	 * @return the template of each page with link
	 */
	public function getPage() {
		return $this->page;
	}
	
	/**
	 * @param string $page
	 */
	public function setPage($page) {
		$this->page = $page;
	}
	
	/**
	 * @return true if the pages will always have link or false if dont
	 */
	public function isPageAlwaysLink() {
		return $this->pageAlwaysLink;
	}
	
	/**
	 * @param boolean $pageAlwaysLink
	 */
	public function setPageAlwaysLink($pageAlwaysLink) {
		$this->pageAlwaysLink = $pageAlwaysLink;
	}
	
	/**
	 * @return integer the number of the range
	 */
	public function getRange() {
		return $this->range;
	}
	
	/**
	 * @param integer
	 */
	public function setRange($range) {
		if(intval($range) >= 0) {
			$this->range = intval($range);
		}
		else {
			throw new ErrorException("The range can't be less than zero!");
		}
	}
	
	
	/**
	 * @return the template of the pages when there's no link
	 */
	public function getPageNoLink() {
		return $this->pageNoLink;
	}
	
	/**
	 * @param string $pageNoLink
	 */
	public function setPageNoLink($pageNoLink) {
		$this->pageNoLink = $pageNoLink;
	}
	
	/**
	 * @return the number of pages
	 */
	public function getPages() {
		return $this->pages;
	}
	
	/**
	 * @param integer $pages
	 */
	public function setPages($pages) {
		if($pages > 0)
			$this->pages = $pages;
	}
	
	/**
	 * @return the template of the link to the previous page
	 */
	public function getPrevious() {
		return $this->previous;
	}
	
	/**
	 * @param string $previous
	 */
	public function setPrevious($previous) {
		$this->previous = $previous;
	}
	
	/**
	 * @return true of the previous will always have link or false if dont
	 */
	public function isPreviousAlwaysLink() {
		return $this->previousAlwaysLink;
	}
	
	/**
	 * @param boolean $previousAlwaysLink
	 */
	public function setPreviousAlwaysLink($previousAlwaysLink) {
		$this->previousAlwaysLink = $previousAlwaysLink;
	}
	
	/**
	 * @return the template to the previous when there's no link
	 */
	public function getPreviousNoLink() {
		return $this->previousNoLink;
	}
	
	/**
	 * @param string $previousNoLink
	 */
	public function setPreviousNoLink($previousNoLink) {
		$this->previousNoLink = $previousNoLink;
	}
	
	/**
	 * @return the template of the separator between pages
	 */
	public function getSeparator() {
		return $this->separator;
	}
	
	/**
	 * @param string $separator
	 */
	public function setSeparator($separator) {
		$this->separator = $separator;
	}
	
	/**
	 * @return true of separator will be show or false if dont
	 */
	public function isShowSeparator() {
		return $this->showSeparator;
	}
	
	/**
	 * @param boolean $showSeparator
	 */
	public function setShowSeparator($showSeparator) {
		$this->showSeparator = $showSeparator;
	}
	
	/**
	 * @return the template of the whole pagination
	 */
	public function getTemplate() {
		return $this->template;
	}
	
	/**
	 * @param string $template
	 */
	public function setTemplate($template) {
		$this->template = $template;
	}
	
	/**
	 * Sets the default properties of the object
	 */
	function __construct() {
		$this->template = "%page%";
		$this->page = "<a href=\"%link%\">%pageNumber%</a>";
		$this->pageNoLink = "%pageNumber%";
		$this->link = $_SERVER["PHP_SELF"] . (preg_match("/\?/", $_SERVER["PHP_SELF"]) == 0 ? "?page=%pageNumber%" : "&page=%pageNumber%");
		$this->separator = " | ";
		$this->showSeparator = true;
	}
	
	/**
	 * Automaticaly generate the number of pages that the pagination will have
	 * based on the ocurrencies and ocurrencies per page
	 *
	 * @param integer $totalOcurrencies
	 * @param integer $ocurrenciesPerPage
	 */
	public function generateNumberPages($totalOcurrencies, $ocurrenciesPerPage) {
		if(empty($totalOcurrencies) || $totalOcurrencies <= 0 || gettype($totalOcurrencies) != "integer") {
			throw new ErrorException("Invalid number of total ocurrencies");
		}
		else if(empty($ocurrenciesPerPage) || $ocurrenciesPerPage <= 0 || gettype($ocurrenciesPerPage) != "integer") {
			throw new ErrorException("Invalid number of ocurrencies per page");
		}
		
		$this->pages = intval(ceil($totalOcurrencies / $ocurrenciesPerPage));
	}
	
	/**
	 * Get the number of the first item to be show in the page
	 *
	 * @param integer $page number of the page to get the number of the first item
	 * @param integer $itensPerPage number of itens per page
	 * @return the number of the first item in that page
	 */
	public static function getNumberFirstItem($page, $itensPerPage) {
		if(empty($page) || $page <= 0 || gettype($page) != "integer") {
			throw new ErrorException("Invalid number of the current page");
		}
		else if(empty($itensPerPage) || $itensPerPage <= 0 || gettype($itensPerPage) != "integer") {
			throw new ErrorException("Invalid number of itens per page");
		}
		
		return ($page - 1) * $itensPerPage;
	}
	
	/**
	 * Get the output of the pagination
	 *
	 * @return the pagination formated using the templates and variables as a string
	 */
	public function getPagination() {
		if(empty($this->pages) || $this->pages <= 0 || gettype($this->pages) != "integer") {
			throw new ErrorException("Invalid number of pages in the object");
		}
		else if(empty($this->currentPage) || $this->currentPage <= 0 || gettype($this->currentPage) != "integer") {
			throw new ErrorException("Invalid current page property");
		}
		else if(empty($this->template) || gettype($this->template) != "string") {
			throw new ErrorException("Invalid template for the pagination");
		}
		
		$template = $this->template;
		
		$first = "";
		if(preg_match("/\%first\%/", $this->template) > 0) {
			if(!$this->firstAlwaysLink && $this->currentPage == 1) {
				$first = $this->firstNoLink;
			}
			else {
				$first = $this->first;
			}
			
			$first = $this->replaceTags($first, "1");
		}
		
		$previous = "";
		if(preg_match("/\%previous\%/", $this->template) > 0) {
			if(!$this->previousAlwaysLink && $this->currentPage == 1) {
				$previous = $this->previousNoLink;
			}
			else {
				$previous = $this->previous;
			}
			
			$previous = $this->replaceTags($previous, ($this->currentPage - 1));
		}
		
		$page = "";
		if(preg_match("/\%page\%/", $this->template) > 0) {
			if(!empty($this->range) && $this->range >= 0) {
				if(($this->currentPage - $this->range) < 1) {
					$i = 1;
				}
				else {
					$i = $this->currentPage - $this->range;
				}

				if(($this->currentPage + $this->range) > $this->pages) {
					$lastPage = $this->pages;
				}
				else {
					$lastPage = $this->currentPage + $this->range;
				}
			}
			else {
				$i = 1;
				$lastPage = $this->pages;
			}

			for(; $i <= $lastPage; $i++) {
				if(!$this->pageAlwaysLink && $this->currentPage == $i) {
					$pageTemp = $this->pageNoLink;
				}
				else {
					$pageTemp = $this->page;
				}
				
				if(!empty($page) && $this->showSeparator && !empty($this->separator)) {
					$page .= $this->replaceTags($this->separator, $i) . $this->replaceTags($pageTemp, $i);
				}
				else {
					$page .= $this->replaceTags($pageTemp, $i);
				}
			}
		}
		
		$next = "";
		if(preg_match("/\%next\%/", $this->template) > 0) {
			if(!$this->nextAlwaysLink && $this->currentPage == $this->pages) {
				$next = $this->nextNoLink;
			}
			else {
				$next = $this->next;
			}
			
			$next = $this->replaceTags($next, ($this->currentPage + 1));
		}
		
		$last = "";
		if(preg_match("/\%last\%/", $this->template) > 0) {
			if(!$this->lastAlwaysLink && $this->currentPage == $this->pages) {
				$last = $this->lastNoLink;
			}
			else {
				$last = $this->last;
			}
			
			$last = $this->replaceTags($last, $this->pages);
		}
		
		$template = str_replace("%first%", $first, $template);
		$template = str_replace("%previous%", $previous, $template);
		$template = str_replace("%page%", $page, $template);
		$template = str_replace("%next%", $next, $template);
		$template = str_replace("%last%", $last, $template);
		
		return $template;
	}
	
	/**
	 * Process all the tags replacement and return the string replaced
	 *
	 * @param string $template string for tag replacement
	 * @param string $pageNumber number to be used in the %pageNumber% tag
	 * @return the string with all tags replaced
	 */
	private function replaceTags($template, $pageNumber) {
		$link = $this->link;
		$link = str_replace("%currentPage%", $this->currentPage, $link);
		$link = str_replace("%pageNumber%", $pageNumber, $link);
		$template = str_replace("%link%", $link, $template);
		$template = str_replace("%currentPage%", $this->currentPage, $template);
		$template = str_replace("%pageNumber%", $pageNumber, $template);
		
		return $template;
	}
}

?>