Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- *
- * Class: Pagination
- *
- * Description: Extended pagination class
- * Version: 1.0
- *
- * @author: iamfake @ flashback
- *
- *
- *
- * Usage:
- *
- * //new pagination object
- * //params ( number_of_results, current_page/offset, limited_results_per_page, (optional|defaults to true)show_numeric_links )
- * $paging = new Pagination( count( $contents ), $offset, $limit, true );
- *
- * //setters
- * //set a unique identifier for this object (optional)
- * //will concatenated '_133t' to the end of the class identifier strings. (! to your own set with the chained method ->addClass())
- * $paging->uniqueIdentifier( '_1337' );
- *
- * //set some custom quicklinks (optional)
- * //addClass chains to the current array item
- * $paging->first( '←' )->addClass( 'first' );
- * $paging->last( '→' )->addClass( 'last' );
- * $paging->next( '»' )->addClass( 'next' );
- * $paging->previous( '«' )->addClass( 'previous' );
- *
- * //args( number_to_jump, label/key (optional | defaults to int value of the jump) )
- * $paging->quickJump( 10, '…' )->addClass( 'quickJump' );
- *
- * //optional
- * $paging->limitedNumberOfLinks( 3 );
- * //if set to false quicklinks will be static
- * //and always visible on the page
- * //remove or set to true to make it dynamic (optional | defaults to true)
- * $paging->useDynamicQuickLinks( false );
- *
- *
- * //getters
- * //does what the method name suggest, returns set values
- * $paging->currentOffset()
- * $paging->limit()
- * $paging->currentPage()
- * $paging->totalNumberOfPages()
- * $paging->hasResults()
- *
- *
- * //return array of pagination links, takes 1 (optional) argument.
- * a classname for all the numeric links.
- * $paging->links( 'paging_default' )
- *
- *
- *
- * HTML Usage:
- *
- * <ul class="pagination">
- * <?php foreach( $links as $link ): ?>
- * <li class="<?php print $link['class']; ?>"><a href="?page=<?php print $link['value']; ?>"><?php print $link['key']; ?></a></li>
- * <?php endforeach; ?>
- * </ul>
- *
- *
- *
- *
- */
- final class Pagination
- {
- private $_totalNumberOfResults,
- $_numberOfResultsPerPage,
- $_totalNumberOfPages,
- $_currentPage,
- $_showNumeric,
- $_currentItem = null,
- $_uniqueIdentifier = '',
- $_dynamicQuickLinks = true,
- $_links = array(),
- $_gutter = null,
- $_quickJump = null,
- $_first = null,
- $_last = null,
- $_next = null,
- $_previous = null,
- $_iterator = 1,
- $_limit;
- /**
- * construct
- *
- * @param $totalNumberOfResults, $currentPage, $numberOfResultsPerPage, $showNumeric
- * @return void
- */
- public function __construct( $totalNumberOfResults, $currentPage, $numberOfResultsPerPage, $showNumeric = true )
- {
- //validate inputtypes
- if( !is_integer( $totalNumberOfResults ) || !is_numeric( $currentPage ) || !is_integer( $numberOfResultsPerPage ) || !is_bool( $showNumeric ) )
- {
- throw new PaginationException( 'Please insert valid types.' );
- }
- //set class properties
- $this->_totalNumberOfResults = $totalNumberOfResults;
- $this->_currentPage = (int)$currentPage;
- $this->_numberOfResultsPerPage = $numberOfResultsPerPage;
- $this->_showNumeric = $showNumeric;
- //set the total amount of pages
- $this->_totalNumberOfPages = (int)ceil( $totalNumberOfResults / $numberOfResultsPerPage );
- $this->_limit = $this->_totalNumberOfPages;
- }
- /**
- * if there are results to show
- *
- * @param
- * @return (bool) true|false
- */
- public function isValid()
- {
- if( $this->_totalNumberOfPages < $this->_currentPage )
- {
- return false;
- }
- return true;
- }
- /**
- * set unique identifier
- *
- * @param
- * @return void
- */
- public function uniqueIdentifier( $id = null )
- {
- if( !$id )
- {
- return $this->_uniqueIdentifier;
- }
- return $this->_uniqueIdentifier = $id;
- }
- /**
- * construct
- *
- * @param
- * @return void
- */
- public function currentPage()
- {
- return $this->_currentPage;
- }
- /**
- * construct
- *
- * @param
- * @return void
- */
- public function currentOffset()
- {
- return ( ( $this->_currentPage - 1 ) * $this->_numberOfResultsPerPage );
- }
- /**
- * construct
- *
- * @param
- * @return void
- */
- public function totalNumberOfPages()
- {
- return $this->_totalNumberOfPages;
- }
- /**
- * construct
- *
- * @param
- * @return void
- */
- public function limit()
- {
- return $this->_numberOfResultsPerPage;
- }
- /**
- * quickjump
- *
- * @param
- * @return void
- */
- public function quickJump( $value, $key = null )
- {
- if( !is_integer( $value ) )
- {
- throw new TemplateException( 'Please insert a valid INTEGER in method ' . __METHOD__ . ' for jump : "' . $value . '"' );
- }
- //calculate jump value
- $value = ( ( $this->_currentPage + $value ) <= $this->_totalNumberOfPages ) ? ( $this->_currentPage + $value ) : $this->_totalNumberOfPages;
- //if key is set use it else use value
- $key = ( $key ) ? $key : $value;
- $this->_quickJump = array( 'value' => $value, 'key' => $key, 'class' => '' );
- $this->_currentItem = &$this->_quickJump;
- return $this;
- }
- /**
- * limit pagination links
- *
- * @param $gutter
- * @return void
- */
- public function limitedNumberOfLinks( $gutter )
- {
- //validate input param.
- if( !is_integer( $gutter ) )
- {
- throw new PaginationException( 'Calling method : ' . __METHOD__ . ' with $gutter of type : ' . gettype( $gutter ) . ' expected INTEGER.' );
- }
- $this->_gutter = $gutter;
- }
- /**
- * setter for next link
- *
- * @param
- * @return self
- */
- public function next( $key )
- {
- $this->_next = array( 'key' => $key, 'value' => ( ( ($this->_currentPage + 1 ) < $this->_totalNumberOfPages ) ? ($this->_currentPage + 1 ) : $this->_totalNumberOfPages ) );
- $this->_currentItem = &$this->_next;
- return $this;
- }
- /**
- * setter for previous link
- *
- * @param
- * @return self
- */
- public function previous( $key )
- {
- $this->_previous = array( 'key' => $key, 'value' => ( ( ( $this->_currentPage - 1 ) >= 1 ) ? ( $this->_currentPage - 1 ) : 1 ) );
- $this->_currentItem = &$this->_previous;
- return $this;
- }
- /**
- * setter for first link
- *
- * @param
- * @return self
- */
- public function first( $key )
- {
- $this->_first = array( 'key' => $key, 'value' => 1 );
- $this->_currentItem = &$this->_first;
- return $this;
- }
- /**
- * setter for last link
- *
- * @param
- * @return self
- */
- public function last( $key )
- {
- $this->_last = array( 'key' => $key, 'value' => $this->_totalNumberOfPages );
- $this->_currentItem = &$this->_last;
- return $this;
- }
- /**
- * Setter method to define if quicklinks should be static or dynamic.
- *
- * option to set quicklinks to be static or dynamic(default).
- *
- * @param (bool) $bool
- * @return void
- */
- public function useDynamicQuickLinks( $bool )
- {
- if( !is_bool( $bool ) )
- {
- throw new PaginationException( 'Value passed to method : ' . __METHOD__ . ' needs to be a boolean value.' );
- }
- $this->_dynamicQuickLinks = $bool;
- }
- /**
- * set class for specific item.
- *
- * chains to first, last, next, previous, jump
- *
- * @param
- * @return void
- */
- public function addClass( $classname )
- {
- if( !$this->_currentItem )
- throw new PaginationException( 'Please chain your class methods properly.' );
- $this->_currentItem[ 'class' ] = $classname . $this->_uniqueIdentifier;
- }
- /**
- * building the gutters
- *
- * set new iterator values
- *
- * @param
- * @return void
- */
- private function _defineNewGutters()
- {
- //total number of valid links
- $totalNumberOfValidLinks = ( $this->_gutter * 2 ) + 1;
- //
- if( $this->_currentPage <= $this->_gutter ){
- $this->_iterator = 1;
- }else if( $this->_currentPage + $this->_gutter >= $this->_totalNumberOfPages ){
- $this->_iterator = $this->_totalNumberOfPages - ($this->_gutter * 2);
- }else{
- $this->_iterator = $this->_currentPage - $this->_gutter;
- }
- //build limit based on gutters
- $this->_limit = ( ($this->_currentPage + $this->_gutter) <= $this->_totalNumberOfPages ) ? $this->_iterator + ($this->_gutter * 2) : ( $this->_totalNumberOfPages );
- }
- /**
- * build (array)$_links
- *
- * @param
- * @return void
- */
- private function _buildLinks( $classname )
- {
- //build dynamic links
- if( $this->_gutter && ( ( $this->_gutter * 2) + 1 ) < $this->_totalNumberOfPages )
- {
- $this->_defineNewGutters();
- }
- //set first link
- //check to see if isn't set, if so check to that it is available for showing.
- if( $this->_first && !$this->_dynamicQuickLinks || $this->_first && ( ( $this->_currentPage - $this->_gutter ) > 1 ) )
- {
- if( !$this->_dynamicQuickLinks && ( $this->_currentPage - $this->_gutter) <= 1 )
- $this->_first[ 'class' ] .= ' deactive firstDeactive' . $this->_uniqueIdentifier;
- $this->_links[] = $this->_first;
- }
- //set previous link
- //check to see if isn't set, if so check to that it is available for showing.
- if( $this->_previous && !$this->_dynamicQuickLinks || $this->_previous && ( $this->_currentPage > 1 ) )
- {
- if( !$this->_dynamicQuickLinks && ( $this->_currentPage <= 1 ) )
- $this->_previous[ 'class' ] .= ' deactive previousDeactive' . $this->_uniqueIdentifier;
- $this->_links[] = $this->_previous;
- }
- //create numerical links
- if( $this->_showNumeric )
- {
- for( $this->_iterator; $this->_iterator <= $this->_limit; $this->_iterator++ )
- {
- $this->_links[] = array( 'key' => $this->_iterator, 'value' => $this->_iterator ,'class' => $classname . $this->_uniqueIdentifier . ( ($this->_iterator == $this->_currentPage ) ? ' active' : '' ) );
- }
- //quick jump to page
- if( $this->_quickJump && !$this->_dynamicQuickLinks || $this->_quickJump && ( ( $this->_currentPage + $this->_gutter ) < $this->_totalNumberOfPages ) )
- {
- if( !$this->_dynamicQuickLinks && ( ( $this->_currentPage + $this->_gutter ) >= $this->_totalNumberOfPages ) )
- $this->_quickJump[ 'class' ] .= ' deactive quickJumpDeactive' . $this->_uniqueIdentifier;
- $this->_links[] = $this->_quickJump;
- }
- }
- //set next page
- if( $this->_next && !$this->_dynamicQuickLinks || $this->_next && ( $this->_currentPage < $this->_totalNumberOfPages ) )
- {
- if( !$this->_dynamicQuickLinks && ( $this->_currentPage >= $this->_totalNumberOfPages ) )
- $this->_next[ 'class' ] .= ' deactive nextDeactive' . $this->_uniqueIdentifier;
- $this->_links[] = $this->_next;
- }
- //set lastpage
- if( $this->_last && !$this->_dynamicQuickLinks || $this->_last && ( ( $this->_currentPage + $this->_gutter ) < $this->_totalNumberOfPages ) )
- {
- if( !$this->_dynamicQuickLinks && ( ( $this->_currentPage + $this->_gutter ) >= $this->_totalNumberOfPages ) )
- $this->_last[ 'class' ] .= ' deactive lastDeactive' . $this->_uniqueIdentifier;
- $this->_links[] = $this->_last;
- }
- }
- /**
- * output links
- *
- * @param
- * @return (array) $links
- */
- public function links( $classname = '' )
- {
- //build links
- $this->_buildLinks( $classname );
- return $this->_links;
- }
- }
- class PaginationException extends Exception{}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement