Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.18 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  *
  5.  *
  6.  * Class: Pagination
  7.  *
  8.  * Description: Extended pagination class
  9.  * Version: 1.0
  10.  *
  11.  * @author: iamfake @ flashback
  12.  *
  13.  *
  14.  *
  15.  * Usage:
  16.  *
  17.  * //new pagination object
  18.  * //params ( number_of_results, current_page/offset, limited_results_per_page, (optional|defaults to true)show_numeric_links )
  19.  * $paging = new Pagination( count( $contents ), $offset, $limit, true );
  20.  *
  21.  * //setters
  22.  * //set a unique identifier for this object (optional)
  23.  * //will concatenated '_133t' to the end of the class identifier strings. (! to your own set with the chained method ->addClass())
  24.  * $paging->uniqueIdentifier( '_1337' );
  25.  *
  26.  * //set some custom quicklinks (optional)
  27.  * //addClass chains to the current array item
  28.  * $paging->first( '&larr;' )->addClass( 'first' );
  29.  * $paging->last( '&rarr;' )->addClass( 'last' );
  30.  * $paging->next( '&raquo;' )->addClass( 'next' );
  31.  * $paging->previous( '&laquo;' )->addClass( 'previous' );
  32.  *
  33.  * //args( number_to_jump, label/key (optional | defaults to int value of the jump) )
  34.  * $paging->quickJump( 10, '&hellip;' )->addClass( 'quickJump' );
  35.  *
  36.  * //optional
  37.  * $paging->limitedNumberOfLinks( 3 );
  38.  * //if set to false quicklinks will be static
  39.  * //and always visible on the page
  40.  * //remove or set to true to make it dynamic (optional | defaults to true)
  41.  * $paging->useDynamicQuickLinks( false );
  42.  *
  43.  *
  44.  * //getters
  45.  * //does what the method name suggest, returns set values
  46.  * $paging->currentOffset()
  47.  * $paging->limit()
  48.  * $paging->currentPage()
  49.  * $paging->totalNumberOfPages()
  50.  * $paging->hasResults()
  51.  *
  52.  *
  53.  * //return array of pagination links, takes 1 (optional) argument.
  54.  * a classname for all the numeric links.
  55.  * $paging->links( 'paging_default' )
  56.  *
  57.  *
  58.  *
  59.  * HTML Usage:
  60.  *
  61.  * <ul class="pagination">
  62.  *  <?php foreach( $links as $link ): ?>
  63.  *  <li class="<?php print $link['class']; ?>"><a href="?page=<?php print $link['value']; ?>"><?php print $link['key']; ?></a></li>
  64.  *  <?php endforeach; ?>
  65.  * </ul>
  66.  *
  67.  *
  68.  *
  69.  *
  70.  */
  71. final class Pagination
  72. {
  73.    
  74.     private $_totalNumberOfResults,
  75.             $_numberOfResultsPerPage,
  76.             $_totalNumberOfPages,
  77.             $_currentPage,
  78.             $_showNumeric,
  79.             $_currentItem       = null,
  80.             $_uniqueIdentifier  = '',
  81.             $_dynamicQuickLinks = true,
  82.             $_links             = array(),
  83.             $_gutter            = null,
  84.             $_quickJump         = null,
  85.             $_first             = null,
  86.             $_last              = null,
  87.             $_next              = null,
  88.             $_previous          = null,
  89.             $_iterator          = 1,
  90.             $_limit;
  91.            
  92.     /**
  93.      * construct
  94.      *
  95.      * @param $totalNumberOfResults, $currentPage, $numberOfResultsPerPage, $showNumeric
  96.      * @return void
  97.      */
  98.     public function __construct( $totalNumberOfResults, $currentPage, $numberOfResultsPerPage, $showNumeric = true )
  99.     {
  100.         //validate inputtypes
  101.         if( !is_integer( $totalNumberOfResults ) || !is_numeric( $currentPage ) || !is_integer( $numberOfResultsPerPage ) || !is_bool( $showNumeric ) )
  102.         {
  103.             throw new PaginationException( 'Please insert valid types.' );
  104.         }
  105.        
  106.         //set class properties
  107.         $this->_totalNumberOfResults    = $totalNumberOfResults;
  108.         $this->_currentPage             = (int)$currentPage;
  109.         $this->_numberOfResultsPerPage  = $numberOfResultsPerPage;
  110.         $this->_showNumeric             = $showNumeric;
  111.        
  112.         //set the total amount of pages
  113.         $this->_totalNumberOfPages      = (int)ceil( $totalNumberOfResults / $numberOfResultsPerPage );
  114.         $this->_limit                   = $this->_totalNumberOfPages;
  115.     }
  116.    
  117.     /**
  118.      * if there are results to show
  119.      *
  120.      * @param
  121.      * @return (bool) true|false
  122.      */
  123.     public function isValid()
  124.     {
  125.         if( $this->_totalNumberOfPages < $this->_currentPage )
  126.         {
  127.             return false;
  128.         }
  129.        
  130.         return true;
  131.        
  132.     }
  133.      
  134.     /**
  135.      * set unique identifier
  136.      *
  137.      * @param
  138.      * @return void
  139.      */
  140.     public function uniqueIdentifier( $id = null )
  141.     {
  142.         if( !$id )
  143.         {
  144.             return $this->_uniqueIdentifier;
  145.         }
  146.        
  147.         return $this->_uniqueIdentifier = $id;
  148.     }
  149.      
  150.     /**
  151.      * construct
  152.      *
  153.      * @param
  154.      * @return void
  155.      */
  156.     public function currentPage()
  157.     {
  158.         return $this->_currentPage;
  159.     }
  160.    
  161.     /**
  162.      * construct
  163.      *
  164.      * @param
  165.      * @return void
  166.      */
  167.     public function currentOffset()
  168.     {
  169.         return ( ( $this->_currentPage - 1 ) * $this->_numberOfResultsPerPage );
  170.     }
  171.    
  172.     /**
  173.      * construct
  174.      *
  175.      * @param
  176.      * @return void
  177.      */
  178.     public function totalNumberOfPages()
  179.     {
  180.         return $this->_totalNumberOfPages;
  181.     }
  182.    
  183.     /**
  184.      * construct
  185.      *
  186.      * @param
  187.      * @return void
  188.      */
  189.     public function limit()
  190.     {
  191.         return $this->_numberOfResultsPerPage;
  192.     }
  193.    
  194.     /**
  195.      * quickjump
  196.      *
  197.      * @param
  198.      * @return void
  199.      */
  200.     public function quickJump( $value, $key = null )
  201.     {
  202.  
  203.         if( !is_integer( $value ) )
  204.         {
  205.             throw new TemplateException( 'Please insert a valid INTEGER in method ' . __METHOD__ . ' for jump : "' . $value . '"' );
  206.         }
  207.        
  208.         //calculate jump value
  209.         $value = ( ( $this->_currentPage + $value ) <= $this->_totalNumberOfPages ) ? ( $this->_currentPage + $value ) : $this->_totalNumberOfPages;
  210.        
  211.        
  212.         //if key is set use it else use value
  213.         $key = ( $key ) ? $key : $value;
  214.        
  215.         $this->_quickJump = array( 'value' => $value, 'key' => $key, 'class' => '' );
  216.         $this->_currentItem = &$this->_quickJump;
  217.         return $this;
  218.     }
  219.    
  220.     /**
  221.      * limit pagination links
  222.      *
  223.      * @param $gutter
  224.      * @return void
  225.      */
  226.     public function limitedNumberOfLinks( $gutter )
  227.     {
  228.         //validate input param.
  229.         if( !is_integer( $gutter ) )
  230.         {
  231.             throw new PaginationException( 'Calling method : ' . __METHOD__ . ' with $gutter of type : ' . gettype( $gutter ) . ' expected INTEGER.' );
  232.         }
  233.        
  234.         $this->_gutter = $gutter;
  235.     }
  236.    
  237.     /**
  238.      * setter for next link
  239.      *
  240.      * @param
  241.      * @return self
  242.      */
  243.     public function next( $key )
  244.     {
  245.         $this->_next = array( 'key' => $key, 'value' => ( ( ($this->_currentPage + 1 ) < $this->_totalNumberOfPages ) ? ($this->_currentPage + 1 ) : $this->_totalNumberOfPages ) );
  246.         $this->_currentItem     = &$this->_next;
  247.         return $this;
  248.     }
  249.    
  250.     /**
  251.      * setter for previous link
  252.      *
  253.      * @param
  254.      * @return self
  255.      */
  256.     public function previous( $key )
  257.     {
  258.         $this->_previous        = array( 'key' => $key, 'value' => ( ( ( $this->_currentPage - 1 ) >= 1 ) ? ( $this->_currentPage - 1 ) : 1 ) );
  259.         $this->_currentItem     = &$this->_previous;
  260.         return $this;
  261.     }
  262.    
  263.     /**
  264.      * setter for first link
  265.      *
  266.      * @param
  267.      * @return self
  268.      */
  269.     public function first( $key )
  270.     {
  271.         $this->_first       = array( 'key' => $key, 'value' => 1 );
  272.         $this->_currentItem = &$this->_first;
  273.         return $this;
  274.     }
  275.    
  276.     /**
  277.      * setter for last link
  278.      *
  279.      * @param
  280.      * @return self
  281.      */
  282.     public function last( $key )
  283.     {
  284.         $this->_last        = array( 'key' => $key, 'value' => $this->_totalNumberOfPages );
  285.         $this->_currentItem = &$this->_last;
  286.         return $this;
  287.     }
  288.    
  289.     /**
  290.      * Setter method to define if quicklinks should be static or dynamic.
  291.      *
  292.      * option to set quicklinks to be static or dynamic(default).
  293.      *
  294.      * @param (bool) $bool
  295.      * @return void
  296.      */
  297.     public function useDynamicQuickLinks( $bool )
  298.     {
  299.         if( !is_bool( $bool ) )
  300.         {
  301.             throw new PaginationException( 'Value passed to method : ' . __METHOD__ . ' needs to be a boolean value.' );
  302.         }
  303.        
  304.         $this->_dynamicQuickLinks = $bool;
  305.     }
  306.    
  307.     /**
  308.      * set class for specific item.
  309.      *
  310.      * chains to first, last, next, previous, jump
  311.      *
  312.      * @param
  313.      * @return void
  314.      */
  315.     public function addClass( $classname )
  316.     {
  317.         if( !$this->_currentItem )
  318.             throw new PaginationException( 'Please chain your class methods properly.' );
  319.         $this->_currentItem[ 'class' ] = $classname . $this->_uniqueIdentifier;
  320.     }
  321.    
  322.     /**
  323.      * building the gutters
  324.      *
  325.      * set new iterator values
  326.      *
  327.      * @param
  328.      * @return void
  329.      */
  330.     private function _defineNewGutters()
  331.     {
  332.         //total number of valid links
  333.         $totalNumberOfValidLinks = ( $this->_gutter * 2 ) + 1;
  334.        
  335.         //
  336.         if( $this->_currentPage <= $this->_gutter ){
  337.             $this->_iterator = 1;
  338.         }else if( $this->_currentPage + $this->_gutter >= $this->_totalNumberOfPages ){
  339.             $this->_iterator = $this->_totalNumberOfPages - ($this->_gutter * 2);
  340.         }else{
  341.             $this->_iterator = $this->_currentPage - $this->_gutter;
  342.         }
  343.        
  344.         //build limit based on gutters
  345.         $this->_limit = ( ($this->_currentPage + $this->_gutter) <= $this->_totalNumberOfPages ) ? $this->_iterator + ($this->_gutter * 2) : ( $this->_totalNumberOfPages );
  346.     }
  347.    
  348.     /**
  349.      * build (array)$_links
  350.      *
  351.      * @param
  352.      * @return void
  353.      */
  354.     private function _buildLinks( $classname )
  355.     {  
  356.         //build dynamic links
  357.         if( $this->_gutter && ( ( $this->_gutter * 2) + 1 ) < $this->_totalNumberOfPages )
  358.         {
  359.             $this->_defineNewGutters();
  360.         }
  361.        
  362.         //set first link
  363.         //check to see if isn't set, if so check to that it is available for showing.
  364.         if( $this->_first && !$this->_dynamicQuickLinks || $this->_first && ( ( $this->_currentPage - $this->_gutter ) > 1 ) )
  365.         {
  366.             if( !$this->_dynamicQuickLinks && ( $this->_currentPage - $this->_gutter) <= 1 )
  367.                 $this->_first[ 'class' ] .= ' deactive firstDeactive' . $this->_uniqueIdentifier;
  368.                
  369.             $this->_links[] = $this->_first;
  370.         }
  371.        
  372.         //set previous link
  373.         //check to see if isn't set, if so check to that it is available for showing.
  374.         if( $this->_previous && !$this->_dynamicQuickLinks || $this->_previous && ( $this->_currentPage > 1 ) )
  375.         {
  376.             if( !$this->_dynamicQuickLinks && ( $this->_currentPage <= 1 ) )
  377.                 $this->_previous[ 'class' ] .= ' deactive previousDeactive' . $this->_uniqueIdentifier;
  378.             $this->_links[] = $this->_previous;
  379.         }
  380.                
  381.         //create numerical links
  382.         if( $this->_showNumeric )
  383.         {
  384.  
  385.             for( $this->_iterator; $this->_iterator <= $this->_limit; $this->_iterator++ )
  386.             {
  387.                 $this->_links[] = array( 'key' => $this->_iterator, 'value' => $this->_iterator ,'class' => $classname . $this->_uniqueIdentifier . ( ($this->_iterator == $this->_currentPage ) ? ' active' : '' ) );
  388.             }
  389.            
  390.             //quick jump to page
  391.             if( $this->_quickJump && !$this->_dynamicQuickLinks || $this->_quickJump && ( ( $this->_currentPage + $this->_gutter ) < $this->_totalNumberOfPages ) )
  392.             {
  393.                 if( !$this->_dynamicQuickLinks && ( ( $this->_currentPage + $this->_gutter ) >= $this->_totalNumberOfPages ) )
  394.                     $this->_quickJump[ 'class' ] .= ' deactive quickJumpDeactive' . $this->_uniqueIdentifier;
  395.                 $this->_links[] = $this->_quickJump;
  396.             }
  397.         }
  398.  
  399.         //set next page
  400.         if( $this->_next && !$this->_dynamicQuickLinks || $this->_next && ( $this->_currentPage < $this->_totalNumberOfPages ) )
  401.         {
  402.             if( !$this->_dynamicQuickLinks && ( $this->_currentPage >= $this->_totalNumberOfPages ) )
  403.                 $this->_next[ 'class' ] .= ' deactive nextDeactive' . $this->_uniqueIdentifier;
  404.             $this->_links[] = $this->_next;
  405.         }
  406.        
  407.         //set lastpage
  408.         if( $this->_last && !$this->_dynamicQuickLinks || $this->_last && ( ( $this->_currentPage + $this->_gutter ) < $this->_totalNumberOfPages ) )
  409.         {
  410.             if( !$this->_dynamicQuickLinks && ( ( $this->_currentPage + $this->_gutter ) >= $this->_totalNumberOfPages ) )
  411.                 $this->_last[ 'class' ] .= ' deactive lastDeactive' . $this->_uniqueIdentifier;
  412.             $this->_links[] = $this->_last;
  413.         }
  414.        
  415.     }
  416.    
  417.     /**
  418.      * output links
  419.      *
  420.      * @param
  421.      * @return (array) $links
  422.      */
  423.     public function links( $classname = '' )
  424.     {
  425.         //build links
  426.         $this->_buildLinks( $classname );
  427.         return $this->_links;
  428.     }
  429.    
  430. }
  431.  
  432. class PaginationException extends Exception{}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement