Advertisement
phpaddict

Opencart custom pagination class

Jul 16th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.90 KB | None | 0 0
  1. <?php
  2. /*
  3. tested with opencart 1.5.4
  4. */
  5.  
  6. class Pagination {
  7.     public $total = 0;
  8.     public $page = 1;
  9.     public $limit = 20;
  10.     public $num_links = 10;
  11.     public $url = '';
  12.     public $text = 'Showing {start} to {end} of {total} ({pages} Pages)';
  13.     public $text_first = '|&lt;';
  14.     public $text_last = '&gt;|';
  15.     public $text_next = '&gt;';
  16.     public $text_prev = '&lt;';
  17.     public $style_links = 'links';
  18.     public $style_results = 'results';
  19.  
  20.     public $show_next_last = false;
  21.     public $dots_before = '';
  22.     public $dots_after = '';
  23.     public $current_before = '';
  24.     public $current_after = '';
  25.     public $prev_before = '';
  26.     public $prev_after = '';
  27.     public $next_before = '';
  28.     public $next_after = '';
  29.     public $link_before = '';
  30.     public $link_after = '';
  31.     public $output_before = '';
  32.     public $output_after = '';
  33.  
  34.     public function render() {
  35.         $total = $this->total;
  36.        
  37.         if ($this->page < 1) {
  38.             $page = 1;
  39.         } else {
  40.             $page = $this->page;
  41.         }
  42.        
  43.         if (!(int)$this->limit) {
  44.             $limit = 10;
  45.         } else {
  46.             $limit = $this->limit;
  47.         }
  48.        
  49.         $num_links = $this->num_links;
  50.         $num_pages = ceil($total / $limit);
  51.        
  52.         $output = '';
  53.        
  54.         if ($page > 1) {
  55.             if ($this->show_next_last) {
  56.                 $output .= ' <a href="' . str_replace('{page}', 1, $this->url) . '">' . $this->text_first . '</a> ';
  57.             }
  58.             if ($this->prev_before && $this->prev_after) {
  59.                 $output .= ' ' . str_replace('{url}', str_replace('{page}', $page - 1, $this->url), $this->prev_before);
  60.                 $output .= $this->text_prev;
  61.                 $output .= $this->prev_after . ' ';
  62.             } else {
  63.                 $output .= ' <a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</a> ';
  64.             }
  65.         }
  66.  
  67.         if ($num_pages > 1) {
  68.             if ($num_pages <= $num_links) {
  69.                 $start = 1;
  70.                 $end = $num_pages;
  71.             } else {
  72.                 $start = $page - floor($num_links / 2);
  73.                 $end = $page + floor($num_links / 2);
  74.            
  75.                 if ($start < 1) {
  76.                     $end += abs($start) + 1;
  77.                     $start = 1;
  78.                 }
  79.                        
  80.                 if ($end > $num_pages) {
  81.                     $start -= ($end - $num_pages);
  82.                     $end = $num_pages;
  83.                 }
  84.             }
  85.  
  86.             if ($start > 1) {
  87.                 $output .= $this->dots_before . ' ... ' . $this->dots_after;
  88.             }
  89.  
  90.             for ($i = $start; $i <= $end; $i++) {
  91.                 if ($page == $i) {
  92.                     $output .= $this->current_before;
  93.                     $output .= !$this->current_before ? ' <b>' : '';
  94.                     $output .= $i;
  95.                     $output .= !$this->current_after ? '</b> ' : '';
  96.                     $output .= $this->current_after;
  97.                 } else {
  98.                     if ($this->link_before && $this->link_after) {
  99.                         $output .= ' ' . str_replace('{url}', str_replace('{page}', $i, $this->url), $this->link_before);
  100.                         $output .= $i;
  101.                         $output .= $this->link_after . ' ';
  102.                     } else {
  103.                         $output .= ' <a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a> ';
  104.                     }
  105.                 }  
  106.             }
  107.  
  108.             if ($end < $num_pages) {
  109.                 $output .= $this->dots_before . ' ... ' . $this->dots_after;
  110.             }
  111.         }
  112.  
  113.         if ($page < $num_pages) {
  114.             if ($this->next_before && $this->next_after) {
  115.                 $output .= ' ' . str_replace('{url}', str_replace('{page}', $page + 1, $this->url), $this->next_before);
  116.                 $output .= $this->text_next;
  117.                 $output .= $this->next_after . ' ';
  118.             } else {
  119.                 $output .= ' <a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a> ';
  120.             }
  121.             if ($this->show_next_last) {
  122.                 $output .= ' <a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a> ';
  123.             }
  124.         }
  125.  
  126.         $find = array(
  127.             '{start}',
  128.             '{end}',
  129.             '{total}',
  130.             '{pages}'
  131.         );
  132.  
  133.         $replace = array(
  134.             ($total) ? (($page - 1) * $limit) + 1 : 0,
  135.             ((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit),
  136.             $total,
  137.             $num_pages
  138.         );
  139.  
  140.         $return = '';
  141.  
  142.         if ($output) {
  143.             if ($this->output_before && $this->output_after) {
  144.                 $return .= $this->output_before . $output . $this->output_after;
  145.             } else {
  146.                 $return .= '<div class="' . $this->style_links . '">' . $output . '</div>';
  147.             }
  148.         }
  149.  
  150.         if ($this->style_results) {
  151.             $return .= '<div class="' . $this->style_results . '">' . str_replace($find, $replace, $this->text) . '</div>';
  152.         }
  153.  
  154.         return $return;
  155.     }
  156. }
  157.  
  158. /*
  159. Usage
  160. ------
  161.  
  162. to get this:
  163.  
  164. <div class="pager skew-25">
  165.     <ul>
  166.         <li><a href="#" class="skew25"><i class="fa fa-angle-left"></i></a></li>
  167.         <li class="selected"><span class="skew25">1</span></li>
  168.         <li><a href="#" class="skew25">2</a></li>
  169.         <li><a href="#" class="skew25">3</a></li>
  170.         <li><a href="#" class="skew25">4</a></li>
  171.         <li><a href="#" class="skew25">5</a></li>
  172.         <li><span class="skew25">...</span></li>
  173.         <li><a href="#" class="skew25">10</a></li>
  174.         <li><a href="#" class="skew25"><i class="fa fa-angle-right"></i></a></li>
  175.     </ul>
  176. </div>
  177.  
  178. you would do this:
  179.  
  180. // standard opencart code
  181. $pagination = new Pagination();
  182. $pagination->total = $review_total;
  183. $pagination->page = $page;
  184. $pagination->limit = 5;
  185. $pagination->text = $this->language->get('text_pagination');
  186. $pagination->url = $this->url->link('blog/blog/review', 'blog_id=' . $this->request->get['blog_id'] . '&page={page}');
  187.  
  188. // new custom code
  189. $pagination->dots_before = '<li><span class="skew25">';
  190. $pagination->dots_after = '</span></li>';
  191. $pagination->current_before = '<li class="selected"><span class="skew25">';
  192. $pagination->current_after = '</span></li>';
  193. $pagination->text_prev = '';
  194. $pagination->text_next = '';
  195. $pagination->prev_before = '<li><a href="{url}" class="skew25"><i class="fa fa-angle-left">';
  196. $pagination->prev_after = '</i></a></li>';
  197. $pagination->next_before = '<li><a href="{url}" class="skew25"><i class="fa fa-angle-right">';
  198. $pagination->next_after = '</i></a></li>';
  199. $pagination->link_before = '<li><a href="{url}" class="skew25">';
  200. $pagination->link_after = '</a></li>';
  201. $pagination->output_before = '<div class="pager skew-25"><ul>';
  202. $pagination->output_after = '</ul></div>';
  203. $pagination->style_results = '';
  204. $pagination->num_links = 7;
  205.  
  206. */
  207.  
  208. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement