1: | <?php
2: | namespace Opencart\Catalog\Controller\Common;
3: | /**
4: | * Class Pagination
5: | *
6: | * @package Opencart\Catalog\Controller\Common
7: | */
8: | class Pagination extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @param array<string, mixed> $setting
11: | *
12: | * @return string
13: | */
14: | public function index(array $setting): string {
15: | if (isset($setting['total'])) {
16: | $total = $setting['total'];
17: | } else {
18: | $total = 0;
19: | }
20: |
21: | if (isset($setting['page']) && $setting['page'] > 0) {
22: | $page = (int)$setting['page'];
23: | } else {
24: | $page = 1;
25: | }
26: |
27: | if (isset($setting['limit']) && (int)$setting['limit']) {
28: | $limit = (int)$setting['limit'];
29: | } else {
30: | $limit = 10;
31: | }
32: |
33: | if (isset($setting['url'])) {
34: | $url = str_replace('%7Bpage%7D', '{page}', (string)$setting['url']);
35: | } else {
36: | $url = '';
37: | }
38: |
39: | $num_links = 8;
40: | $num_pages = ceil($total / $limit);
41: |
42: | if ($url && $page > 1 && $num_pages < $page) {
43: | $back = true;
44: | } else {
45: | $back = false;
46: | }
47: |
48: | $data['page'] = $page;
49: |
50: | if ($page > 1) {
51: | $data['first'] = str_replace(['&amp;page={page}', '?page={page}', '&page={page}'], '', $url);
52: |
53: | if ($page - 1 === 1) {
54: | $data['prev'] = str_replace(['&amp;page={page}', '?page={page}', '&page={page}'], '', $url);
55: | } else {
56: | $data['prev'] = str_replace('{page}', $page - 1, $url);
57: | }
58: | } else {
59: | $data['first'] = '';
60: | $data['prev'] = '';
61: | }
62: |
63: | $data['links'] = [];
64: |
65: | if ($num_pages > 1) {
66: | if ($num_pages <= $num_links) {
67: | $start = 1;
68: | $end = $num_pages;
69: | } else {
70: | $start = $page - floor($num_links / 2);
71: | $end = $page + floor($num_links / 2);
72: |
73: | if ($start < 1) {
74: | $end += abs($start) + 1;
75: | $start = 1;
76: | }
77: |
78: | if ($end > $num_pages) {
79: | $start -= ($end - $num_pages);
80: | $end = $num_pages;
81: | }
82: | }
83: |
84: | for ($i = $start; $i <= $end; $i++) {
85: | $data['links'][] = [
86: | 'page' => $i,
87: | 'href' => str_replace('{page}', $i, $url)
88: | ];
89: | }
90: | }
91: |
92: | if ($num_pages > $page) {
93: | $data['next'] = str_replace('{page}', $page + 1, $url);
94: | $data['last'] = str_replace('{page}', $num_pages, $url);
95: | } else {
96: | $data['next'] = '';
97: | $data['last'] = '';
98: | }
99: |
100: | if ($num_pages > 1 || $back) {
101: | return $this->load->view('common/pagination', $data);
102: | } else {
103: | return '';
104: | }
105: | }
106: | }
107: |

OpenCart API API documentation generated by ApiGen dev-master