File indexing completed on 2024-04-28 17:09:58

0001 <?php
0002 
0003 /**
0004  * Flooer Framework
0005  *
0006  * LICENSE: BSD License (2 Clause)
0007  *
0008  * @category    Flooer
0009  * @package     Flooer_Utility
0010  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0011  * @copyright   Akira Ohgaki
0012  * @license     https://opensource.org/licenses/BSD-2-Clause  BSD License (2 Clause)
0013  * @link        https://github.com/akiraohgaki/flooer
0014  */
0015 
0016 /**
0017  * Usage
0018  *
0019  * $paginationArray = Flooer_Utility_Pagination::paginate(2000, 10, 3);
0020  */
0021 
0022 /**
0023  * Paginator class
0024  *
0025  * @category    Flooer
0026  * @package     Flooer_Utility
0027  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0028  */
0029 class Flooer_Utility_Pagination
0030 {
0031 
0032     /**
0033      * Paginating
0034      *
0035      * @param   int $totalItems
0036      * @param   int $itemsPerPage
0037      * @param   int $current
0038      * @param   int $range
0039      * @param   string $style Available values: all, jumping, elastic, sliding
0040      * @return  array
0041      */
0042     public static function paginate($totalItems, $itemsPerPage, $current = 1, $range = 10, $style = 'sliding')
0043     {
0044         $pagination = array();
0045         $pagination['totalItems'] = (int) $totalItems;
0046         $pagination['itemsPerPage'] = (int) $itemsPerPage;
0047         $pagination['first'] = 1;
0048         $pagination['last'] = (int) ceil($pagination['totalItems'] / $pagination['itemsPerPage']);
0049         $pagination['current'] = (int) $current;
0050         if ($pagination['current'] >= $pagination['first']
0051             && $pagination['current'] <= $pagination['last']
0052         ) {
0053             if ($pagination['current'] > $pagination['first']) {
0054                 $pagination['previous'] = $pagination['current'] - 1;
0055             }
0056             if ($pagination['current'] < $pagination['last']) {
0057                 $pagination['next'] = $pagination['current'] + 1;
0058             }
0059             switch (strtolower($style)) {
0060                 case 'all':
0061                     // List of all pages
0062                     // 1 2 3 4 5 6 7 8 9 10 ...
0063                     $low = $pagination['first'];
0064                     $high = $pagination['last'];
0065                     break;
0066                 case 'jumping':
0067                     // The end of a range is the beginning of the new range
0068                     // Before: 1 2 3 4 5 6 7 8 [9] 10
0069                     // After:  [10] 11 12 13 14 15 16 17 18 19 20
0070                     $low = (int) floor($pagination['current'] / $range) * $range;
0071                     $high = $low + $range;
0072                     if ($low < $pagination['first']) {
0073                         $low = $pagination['first'];
0074                         $high = ($pagination['first'] + $range) - 1;
0075                         if ($high > $pagination['last']) {
0076                             $high = $pagination['last'];
0077                         }
0078                     }
0079                     if ($high > $pagination['last']) {
0080                         $high = $pagination['last'];
0081                         $low = ($pagination['last'] - $range) + 1;
0082                         if ($low < $pagination['first']) {
0083                             $low = $pagination['first'];
0084                         }
0085                     }
0086                     break;
0087                 case 'elastic':
0088                     // Elastic scrolling style
0089                     // Before: 1 2 3 4 5 6 7 8 [9] 10
0090                     // After:  1 2 3 4 5 6 7 8 9 [10] 11 12 13 14 15 16 17 18 19 20
0091                     $low = ($pagination['current'] - $range) + 1;
0092                     $high = $pagination['current'] + $range;
0093                     if ($low < $pagination['first']) {
0094                         $low = $pagination['first'];
0095                         $high = ($pagination['current'] + $range) - 1;
0096                         if ($high > $pagination['last']) {
0097                             $high = $pagination['last'];
0098                         }
0099                     }
0100                     if ($high > $pagination['last']) {
0101                         $high = $pagination['last'];
0102                         $low = ($pagination['current'] - $range) + 1;
0103                         if ($low < $pagination['first']) {
0104                             $low = $pagination['first'];
0105                         }
0106                     }
0107                     break;
0108                 case 'sliding':
0109                     // Continue to default
0110                 default:
0111                     // Sliding scrolling style
0112                     // Before: 1 2 3 4 [5] 6 7 8 9 10
0113                     // After:  2 3 4 5 [6] 7 8 9 10 11
0114                     $delta = (int) ceil($range / 2);
0115                     $low = ($pagination['current'] - $delta) + 1;
0116                     $high = $pagination['current'] + $delta;
0117                     if ($low < $pagination['first']) {
0118                         $low = $pagination['first'];
0119                         $high = ($pagination['first'] + $range) - 1;
0120                         if ($high > $pagination['last']) {
0121                             $high = $pagination['last'];
0122                         }
0123                     }
0124                     if ($high > $pagination['last']) {
0125                         $high = $pagination['last'];
0126                         $low = ($pagination['last'] - $range) + 1;
0127                         if ($low < $pagination['first']) {
0128                             $low = $pagination['first'];
0129                         }
0130                     }
0131                     break;
0132             }
0133             $pagination['pages'] = range($low, $high);
0134         }
0135         return $pagination;
0136     }
0137 
0138 }