<?php
/**
* PageNavigator_AutoScroll is a class for creating page views that
* scroll automatically while the user navigates through the PageNavigator.
* The view starts out with the minimum number of pages. When the user
* gets closer to the right edge, the view adjusts itself so that
* the current page is in the center of the view. The closer he gets to
* the right edge, the more pages he sees on the screen. The maximum amount
* of pages is settable via object's methods.
* This is used by Google.com, Yandex.ru and many other search engines.
*
* @author Stanislav Okhvat <stanis@ngs.ru>
* @version $Id: PageNavigator_AutoScroll.php,v 1.00 2002/02/20 11:40:00 stasokhvat Exp $
* @package PageNavigator_AutoScroll
* @access public
*/
class PageNavigator_AutoScroll extends PageNavigator
{
/**
* Minimum number of pages to show within set of pages
*
* @var integer
* @access private
*/
var $min_pages_per_set = 10;
/**
* Maximum pages to show within set of pages
*
* @var integer
* @access private
*/
var $max_pages_per_set = 20;
/**
* How many pages to show to either side of the active page.
* Default is to center within the set (-1).
*
* @var integer
* @access private
*/
var $show_side_pages;
/**
* Constructor. Initializes the PageNavigator_AutoScroll object with the
* most important properties.
*
* @param integer current page number
* @param integer number of records per one page
* @param integer total records. May be initialized later.
* @param integer minimum number of pages displayed per one set.
* @param integer maximum number of pages displayed per one set.
* @param integer number of pages to show to the right of
* the current page. The default is -1, i.e. center the
* current page in the view. Setting this property will
* override the default behaviour.
* @return void
* @access public
*/
function PageNavigator_AutoScroll($current_page=0, $records_per_page=15, $total_records=0, $min_pages_per_set=10, $max_pages_per_set=20, $show_side_pages=-1, $query_vars='')
{
PageNavigator::PageNavigator($current_page, $records_per_page, $total_records, $query_vars);
$this->min_pages_per_set = $min_pages_per_set;
$this->max_pages_per_set = $max_pages_per_set;
$this->setSidePageNum($show_side_pages);
} // end func
/* SidePageNum */
function setSidePageNum($show_side_pages)
{
if ($show_side_pages == -1 || $show_side_pages > ceil($this->max_pages_per_set / 2))
{
// auto-center active page within set. Calculate.
$show_side_pages = ceil($this->max_pages_per_set / 2);
}
$this->show_side_pages = $show_side_pages;
}
function getSidePageNum()
{
return $this->show_side_pages;
}
function preCalculateParameters()
{
// 1. Calculate total pages
$this->totalpages = $this->getTotalPages();
// 2. If we are on the first page, the first record is 1 and the last is min_pages_per_set (assuming it does not exceed pages returned)
if ($this->current_page == 1)
{
$this->startpage = 1;
$this->endpage =
($this->min_pages_per_set > $this->totalpages ? $this->totalpages : $this->min_pages_per_set);
}
else
{
// 3. End page if we are not on first page
$endpage = $this->current_page + $this->show_side_pages;
if ($endpage < $this->min_pages_per_set)
{
$endpage = $this->min_pages_per_set;
}
$this->endpage = ($endpage >= $this->totalpages ? $this->totalpages : $endpage);
// 4. Start page if we are not on first page
$startpage = $this->endpage - $this->max_pages_per_set + 1;
$this->startpage = ($startpage <= 0 ? 1 : $startpage);
}
return true;
}
function render()
{
// do not allow to proceed if not initialized correctly
if ($this->total_records == 0)
{
return false;
}
// HEADER
$output = $this->formatHeader();
// GET INITIAL VARS
$this->preCalculateParameters(); // calculate totalpages, startpage, endpage
// CACHE QUERY STRING STRIPPED OF VARIABLES NEEDED BY THIS CLASS
$this->setProcessedQueryString();
// first generate all cells containing links to various pages
$pages = '';
for ($int = $this->startpage; $int <= $this->endpage; $int++)
{
$pages .= ( ($int == $this->current_page) ?
$this->formatActivePage($int) :
$this->formatPage($int) );
}
// LINK TO PREVIOUS PAGE
if ($this->current_page > 1)
{
$prevpage = $this->formatMovePrevious($this->current_page - 1);
}
else
{
$prevpage = $this->empty_cell;
}
// LINK TO NEXT PAGE
if ($this->current_page < $this->totalpages)
{
$nextpage = $this->formatMoveNext($this->current_page + 1);
}
else
{
$nextpage = $this->empty_cell;
}
// RENDER PAGE NAVIGATION VIEW
$output .= $prevpage.$pages.$nextpage;
// ADD TABLE FOOTER
$output .= $this->formatFooter();
return $output;
} // end func render
} // end class PageNavigator_AutoScroll
?> |