123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- <?php
- /**
- * @author Ironpilot
- * @copyright Copywrite (c) 2011, STAPLE CODE
- *
- * This file is part of the STAPLE Framework.
- *
- * The STAPLE Framework is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your option)
- * any later version.
- *
- * The STAPLE Framework is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- class Staple_Pager
- {
- /**
- * Total number of items in the paged set.
- * @var int
- */
- protected $total;
- /**
- * The current page of the item set.
- * @var int
- */
- protected $page = 1;
- /**
- * Number of items listed on each page.
- * @var int
- */
- protected $itemsPerPage = 10;
- /**
- * Number of pages to list before the current page.
- * @var int
- */
- protected $pagesBeforeCurrent;
- /**
- * Number of pages to list after the current page.
- * @var int
- */
- protected $pagesAfterCurrent;
- /**
- * An array that contains additional variables for the paging function.
- * @var array[string]
- */
- protected $pageVariables = array();
- /**
- * A switch to disable the item amount selector field.
- * @var bool
- */
- protected $displayItemAmountSelector = true;
- /**
- * The default item amount selections.
- * @var array[int]
- */
- protected $itemAmountSelections = array(5,10,20,50,100);
- /**
- * The constructor loads values into the pager.
- * @param int $itemsPerPage
- * @param int $total
- * @param int $currentPage
- * @param int $pageBuffer
- */
- function __construct($itemsPerPage = NULL, $total = NULL, $currentPage = NULL, $pageBuffer = NULL)
- {
- if(isset($itemsPerPage))
- {
- $this->setItemsPerPage($itemsPerPage);
- }
- if(isset($total))
- {
- $this->setTotal($total);
- }
- if(isset($currentPage))
- {
- $this->setPage($currentPage);
- }
- if(isset($pageBuffer))
- {
- $this->setPageBuffer($pageBuffer);
- }
- }
- /**
- * Returns the displayPaging() function.
- */
- public function __toString()
- {
- return $this->displayPaging();
- }
-
- /**
- * Returns the item number for the starting item in the result set. Useful for filling in the LIMIT clause in MySQL.
- */
- public function getStartingItem()
- {
- $start = (($this->page - 1) * $this->itemsPerPage);
- if($start > 0)
- {
- return $start;
- }
- else
- {
- return 0;
- }
- }
-
- /**
- * An Alias of self::getLastPage()
- */
- public function getNumberOfPages()
- {
- return $this->getLastPage();
- }
-
- /**
- * Returns the ending page of the set.
- * @return int
- */
- public function getLastPage()
- {
- if(($this->total % $this->itemsPerPage) == 0)
- {
- return (int)($this->total / $this->itemsPerPage);
- }
- else
- {
- return (int)ceil($this->total / $this->itemsPerPage);
- }
- }
-
- /**
- * Returns an array with the page numbers.
- */
- public function getPages()
- {
- $start = 1;
- if(isset($this->pagesBeforeCurrent))
- {
- if(($this->page - $this->pagesBeforeCurrent) > 0)
- {
- $start = $this->page - $this->pagesBeforeCurrent;
- }
- }
- $end = $this->getNumberOfPages();
- if(isset($this->pagesAfterCurrent))
- {
- if(($this->page + $this->pagesAfterCurrent) < $end)
- {
- $end = $this->page + $this->pagesAfterCurrent;
- }
- }
-
- $pages = array();
- for($i = $start; $i <= $end; $i++)
- {
- $pages[] = $i;
- }
-
- return $pages;
- }
-
- //---------------------------------------------Getters and Setters---------------------------------------------
- /**
- * @return the $total
- */
- public function getTotal()
- {
- return $this->total;
- }
- /**
- * @param int $total
- */
- public function setTotal($total)
- {
- $this->total = $total;
- if($this->getStartingItem() > $total)
- {
- $this->setPage($this->getLastPage());
- }
- return $this;
- }
- /**
- * Alias of getPage()
- * @see Staple_Pager::getPage()
- */
- public function getCurrentPage()
- {
- return $this->getPage();
- }
-
- /**
- * @return the $page
- */
- public function getPage()
- {
- return $this->page;
- }
- /**
- * @param int $page
- */
- public function setPage($page)
- {
- if(isset($this->total) && ($page*$this->itemsPerPage) > $this->total)
- {
- $this->page = $this->getLastPage();
- }
- elseif ($page <= 0)
- {
- $this->page = 1;
- }
- else
- {
- $this->page = $page;
- }
- return $this;
- }
- /**
- * @return the $itemsPerPage
- */
- public function getItemsPerPage()
- {
- return (int)$this->itemsPerPage;
- }
- /**
- * @param int $itemsPerPage
- */
- public function setItemsPerPage($itemsPerPage)
- {
- //@todo recalculate the current page.
- $this->itemsPerPage = $itemsPerPage;
- return $this;
- }
- /**
- * @return bool $displayItemAmountSelector
- */
- public function getDisplayItemAmountSelector()
- {
- return $this->displayItemAmountSelector;
- }
- /**
- * @return array[int] $itemAmountSelections
- */
- public function getItemAmountSelections()
- {
- return $this->itemAmountSelections;
- }
- /**
- * @param boolean $displayItemAmountSelector
- */
- public function setDisplayItemAmountSelector($displayItemAmountSelector)
- {
- $this->displayItemAmountSelector = (bool)$displayItemAmountSelector;
- return $this;
- }
- /**
- * @param array[int] $itemAmountSelections
- */
- public function setItemAmountSelections(array $itemAmountSelections)
- {
- $this->itemAmountSelections = array();
- foreach ($itemAmountSelections as $value)
- {
- if(is_int($value))
- {
- $this->itemAmountSelections[] = $value;
- }
- }
- return $this;
- }
- /**
- * Add a single entry to the selection list.
- * @param int $amount
- * @return Staple_Pager
- */
- public function addItemAmountSelection($amount)
- {
- $this->itemAmountSelections[] = (int)$amount;
- sort($this->itemAmountSelections);
- return $this;
- }
- /**
- * @return the $pagesBeforeCurrent
- */
- public function getPagesBeforeCurrent()
- {
- return $this->pagesBeforeCurrent;
- }
- /**
- * @return the $pagesAfterCurrent
- */
- public function getPagesAfterCurrent()
- {
- return $this->pagesAfterCurrent;
- }
- /**
- * @param int $pagesBeforeCurrent
- */
- public function setPagesBeforeCurrent($pagesBeforeCurrent)
- {
- $this->pagesBeforeCurrent = $pagesBeforeCurrent;
- return $this;
- }
- /**
- * @param int $pagesAfterCurrent
- */
- public function setPagesAfterCurrent($pagesAfterCurrent)
- {
- $this->pagesAfterCurrent = $pagesAfterCurrent;
- return $this;
- }
-
- /**
- * Sets the $pagesBeforeCurrent and $pagesAfterCurrent to the same value.
- * @param int $pages
- */
- public function setPageBuffer($pages)
- {
- $this->pagesAfterCurrent = $pages;
- $this->pagesBeforeCurrent = $pages;
- return $this;
- }
-
- /**
- * This function allows the user to add additional variables to the GET string of the page links.
- * The array should be associative with it formated such that variabelName=>value.
- * @param array $vars
- * @return Staple_Pager
- */
- public function setVariables(array $vars)
- {
- if(array_key_exists('items', $vars))
- {
- unset($vars['items']);
- }
- $this->pageVariables = $vars;
- return $this;
- }
- /**
- * Add a single variable to the page GET string
- * @param string $varname
- * @param string $value
- */
- public function addVariable($varname,$value)
- {
- $this->pageVariables[$varname] = $value;
- return $this;
- }
- /**
- * Remove a single variable from the GET string.
- * @param string $varname
- */
- public function removeVariable($varname)
- {
- if(array_key_exists($varname, $this->pageVariables))
- {
- unset($this->pageVariables[$varname]);
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /**
- * This function is a temporary fix until the changes are completed to Staple_Route.
- * @param string $action
- * @deprecated
- */
- public function displayPaging($action = NULL)
- {
- //Setup Link Variables
- $linkVars = array_merge($_GET,$this->pageVariables);
-
- //These two variables would be overridden anyway.
- if(array_key_exists('items', $linkVars)) unset($linkVars['items']);
- if(array_key_exists('page', $linkVars)) unset($linkVars['page']);
-
- //Set a default link action location if none is submitted.
- if($action == NULL)
- {
- $action = Staple_Main::getRoute();
- }
- $buffer = "<div class=\"staple_pager\">\n<div class=\"staple_pager_pages\">\nPage: ";
- $pages = $this->getPages();
- if(count($pages) > 1)
- {
- if($this->getCurrentPage() == 1)
- {
- $buffer .= ' << - < ';
- }
- elseif($this->getCurrentPage() > 1)
- {
- $buffer .= '<a href="'.Staple_Link::get($action,array_merge($linkVars,array('page'=>1))).'"><<</a> - ';
- $buffer .= '<a href="'.Staple_Link::get($action,array_merge($linkVars,array('page'=>($this->getCurrentPage()-1)))).'"><</a> ';
- }
- if($pages[0] != 1)
- {
- $buffer .= '... ';
- }
- foreach($pages as $page)
- {
- if($this->getCurrentPage() == $page)
- {
- $buffer .= '<span class="currentpage">'.((int)$page).'</span> ';
- }
- else
- {
- $buffer .= '<a href="'.Staple_Link::get($action,array_merge($linkVars,array('page'=>(int)$page))).'">'.((int)$page).'</a> ';
- }
- }
- if($pages[count($pages)-1] != $this->getNumberOfPages())
- {
- $buffer .= '... ';
- }
- if($this->getCurrentPage() == $this->getNumberOfPages())
- {
- $buffer .= ' > - >> ';
- }
- else
- {
- $buffer .= '<a href="'.Staple_Link::get($action,array_merge($linkVars,array('page'=>($this->getCurrentPage()+1)))).'">></a> - ';
- $buffer .= '<a href="'.Staple_Link::get($action,array_merge($linkVars,array('page'=>$this->getNumberOfPages()))).'">>></a> ';
- }
- }
- else
- {
- $buffer .= '<< - < 1 > - >>';
- }
- $buffer .= "</div>\n";
- if($this->getDisplayItemAmountSelector() === true)
- {
-
- $buffer .= "<div class=\"staple_pager_items\">\n";
- $buffer .= 'Items Per Page: <select onChange="window.location=\''.Staple_Link::get($action,array_merge($linkVars,array('page'=>1)))."&items='+this.value\">\n";
- foreach($this->getItemAmountSelections() as $value)
- {
- $selected = '';
- if($this->getItemsPerPage() == $value)
- {
- $selected = ' selected';
- }
- $buffer .= '<option value="'.$value.'"'.$selected.'>'.$value."</option>\n";
- }
- $buffer .= "</select>\n";
- $buffer .= "</div>\n";
- }
- $buffer .= "</div>\n";
- return $buffer;
- }
- }
- ?>
|