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

OpenCart API API documentation generated by ApiGen dev-master