File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/class.paging.php
<?php
/**
* This file contains the iwp_paging class
*
* @version $Id$
*
*
* @package IWP
* @subpackage IWP_API
*/
/**
* IWP Paging Class
* This class is used by different sections to calculate paging links
*
* @package IWP
* @subpackage IWP_API
*/
class iwp_paging extends iwp_engine {
/**
* Instance
* This static variable holds the current instance of this object being loaded.
* So using the getInstance function anywhere will return the very same instance.
*
* @var iwp_paging Instance
*/
public static $Instance;
/**
* The amount of items per page
* @var integer
*/
public $ItemsPerPage = 0;
/**
* The total number of pages
* @var integer
*/
public $PageCount = 0;
/**
* The current page number
* @var integer
*/
public $CurrentPage = 0;
/**
* The URL for the previous page
* @var string
*/
public $PreviousURL = '';
/**
* The URL for the next page
* @var string
*/
public $NextURL = '';
/**
* The URL for the first page
*
* @var string
*/
public $FirstURL = '';
/**
* The URL for the last (final) page
*
* @var string
*/
public $LastURL = '';
/**
* The array of all the pages and their links
* @var array
*/
public $PageList = array();
/**
* The string to replace page numbers with in provided urls, the default is %d
* @var string
*/
public $ReplaceToken = '%d';
/**
* getInstance
* This is a static function that sets up the class instance and stores it to the static variable. It will then return that instantiation in the future.
*
* @return iwp_paging Returns the instantiated object
**/
public static function getInstance(){
if(!isset(self::$Instance)){
self::$Instance = new self();
}
return self::$Instance;
}
/**
* Constructor.
*
* For convenience, if the required parameters for SetPaging are passed, it will automatically be called.
*
* @param Integer $totalItems
* @param Integer $itemsPerPage
* @param Integer $requestPage
* @param String $url
* @param Integer $numPagesToShow
* @return Object iwp_paging
*/
public function __construct($totalItems = null, $itemsPerPage = null, $requestPage = null, $url = null, $numPagesToShow = 5)
{
parent::__construct();
if (isset($totalItems) && isset($itemsPerPage) && isset($requestPage) && isset($url)) {
$this->SetPaging($totalItems, $itemsPerPage, $requestPage, $url, $numPagesToShow);
}
}
/**
* The main paging function, taking in the arguments and making all the calculations for the paging. It sets the next & previous links. It sets the page count and the page list.
*
* @param integer $totalItems The total number of items across all pages
* @param integer $itemsPerPage The maximum number of items to have on 1 page
* @param integer $requestPage The current page number (not number of items)
* @param string $url The URL structure to add the page number to. It should have a placeholder in it for page numbers such as %d (customisable using the $ReplaceToken property)
* @param integer $numPagesToShow The number of pages that should be displayed in the list of pages
*
* @return void
*/
function SetPaging($totalItems, $itemsPerPage, $requestPage, $url, $numPagesToShow = 5) {
$this->ItemsPerPage = $itemsPerPage;
$numPages = (int) ceil($totalItems / $itemsPerPage);
if($numPagesToShow > $numPages){
$numPagesToShow = $numPages;
}
if ($numPages <= 1) {
$this->CurrentPage = 1;
$this->NextURL = '';
$this->PreviousURL = '';
$this->PageList = array();
$this->PageCount = 0;
$this->FirstURL = '';
$this->LastURL = '';
} else {
$requestPage = (int)$requestPage;
if($requestPage > $numPages){
$requestPage = $numPages;
}
if($requestPage < 1){
$requestPage = 1;
}
$this->CurrentPage = $requestPage;
$this->FirstURL = str_replace($this->ReplaceToken, 1, $url);
$this->LastURL = str_replace($this->ReplaceToken, $numPages, $url);
if(($this->CurrentPage+1) <= $numPages){
$this->NextURL = str_replace($this->ReplaceToken, ($this->CurrentPage+1), $url);
} else {
$this->NextURL = str_replace($this->ReplaceToken, $numPages, $url);
}
if ($this->CurrentPage > 1) {
$this->PreviousURL = str_replace($this->ReplaceToken, ($this->CurrentPage-1) ,$url);
} else {
$this->PreviousURL = str_replace($this->ReplaceToken, 1, $url);
}
if ($numPages > $numPagesToShow) {
$start_page = $this->CurrentPage - (floor($numPagesToShow / 2));
if ($start_page < 1){
$start_page = 1;
}
$end_page = $this->CurrentPage + (floor($numPagesToShow / 2));
if ($end_page > $numPages){
$end_page = $numPages;
}
if ($end_page < $numPagesToShow){
$end_page = $numPagesToShow;
}
$pagestoshow = ($end_page - $start_page);
if (($pagestoshow < $numPagesToShow) && ($numPages > $numPagesToShow)){
$start_page = ($end_page - $numPagesToShow + 1);
}
} else {
$start_page = 1;
$end_page = $numPages;
}
for ($pageid = $start_page; $pageid <= $end_page; $pageid++) {
if ($pageid > $numPages){
break;
}
$this->PageList[$pageid] = str_replace($this->ReplaceToken, $pageid ,$url);
}
$this->PageCount = $numPages;
}
}
/**
* Returns the starting limit as an integer. Useful so we don't have to do repeat calculations when
* manually querying the database and sending a starting limit. A iwp_paging::getEndLimit() was added
* to complement the usage of iwp_paging::getStartLimit().
*
* @return int
*/
public function getStartLimit()
{
$start = ($this->CurrentPage - 1) * $this->ItemsPerPage;
if (!$start) {
$start = 0;
}
return $start;
}
/**
* Retruns the ending limit as an integer. Somewhat redundant because it just returns the property,
* but it was added for consistency as a complement to iwp_paging::getStartLimit() as well as to
* a logical way to generate an limiter for a database query. If the API changes in iwp_paging, then
* changing the logic to generate the end limit will only have to be changed here.
*
* @return int
*/
public function getEndLimit()
{
$end = $this->ItemsPerPage;
if (!$end) {
$end = 10;
}
return $end;
}
}