XooPaginate::display()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Xooghost;
4
5
/**
6
 * XooPaginate : Page navigation manager
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @package         Xooghost
18
 * @since           2.6.0
19
 * @author          Laurent JEN (Aka DuGris)
20
 */
21
use Xoops\Core\Request;
22
23
/**
24
 * Class XooPaginate
25
 */
26
class XooPaginate
27
{
28
    private $prev;
29
    private $next;
30
    private $first;
31
    private $last;
32
33
    private $total;
34
    private $perpage;
35
    private $current;
36
    private $extra;
37
    private $url;
38
    private $offset;
39
40
    /**
41
     * @param        $total_items
42
     * @param        $items_perpage
43
     * @param        $current_start
44
     * @param string $start_name
45
     * @param string $extra_arg
46
     * @param int    $offset
47
     */
48
    public function __construct($total_items, $items_perpage, $current_start, $start_name = 'start', $extra_arg = '', $offset = 1)
49
    {
50
        $this->total = (int)$total_items;
51
        $this->perpage = (int)$items_perpage;
52
        $this->current = (int)$current_start;
53
        $this->extra = $extra_arg;
54
        if ('' != $extra_arg && ('&amp;' !== mb_substr($extra_arg, -5) || '&' !== mb_substr($extra_arg, -1))) {
55
            $this->extra = '&amp;' . $extra_arg;
56
        }
57
        $this->url = Request::getString('PHP_SELF', '', 'SERVER') . '?' . trim($start_name) . '=';
58
        $this->offset = (int)$offset;
59
60
        $this->render();
61
    }
62
63
    /**
64
     * @param $key
65
     *
66
     * @return mixed
67
     */
68
    public function getValue($key)
69
    {
70
        return $this->$key;
71
    }
72
73
    public function display()
74
    {
75
        echo $this->render();
76
    }
77
78
    /**
79
     * @return mixed|string
80
     */
81
    private function render()
82
    {
83
        $xoops = \Xoops::getInstance();
84
        $xoops->tpl()->assign('xoopaginate', $this);
85
86
        $total_pages = ceil($this->total / $this->perpage);
87
        $i = 0;
88
        if (0 != $this->total && 0 != $this->perpage) {
89
            if (($this->current - $this->perpage) >= 0) {
90
                $this->prev = $this->url . ($this->current - $this->perpage) . $this->extra;
91
                $this->first = $this->url . 0 . $this->extra;
92
            }
93
94
            $counter = 1;
95
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
96
            while ($counter <= $total_pages) {
97
                if ($counter == $current_page) {
98
                    $pages[$i]['text'] = $counter;
99
                    $pages[$i]['link'] = $this->url . (($counter - 1) * $this->perpage) . $this->extra;
100
                    $pages[$i]['value'] = (($counter - 1) * $this->perpage);
101
102
                    ++$i;
103
                } elseif (($counter > $current_page - $this->offset && $counter < $current_page + $this->offset) || 1 == $counter || $counter == $total_pages) {
104
                    if ($counter == $total_pages && $current_page < $total_pages - $this->offset) {
105
                        $pages[$i]['link'] = false;
106
                        $pages[$i]['text'] = '...';
107
                        $pages[$i]['value'] = '.';
108
                        ++$i;
109
                    }
110
                    $pages[$i]['text'] = $counter;
111
                    $pages[$i]['link'] = $this->url . (($counter - 1) * $this->perpage) . $this->extra;
112
                    $pages[$i]['value'] = (($counter - 1) * $this->perpage);
113
                    ++$i;
114
                    if (1 == $counter && $current_page > 1 + $this->offset) {
115
                        $pages[$i]['link'] = false;
116
                        $pages[$i]['text'] = '...';
117
                        $pages[$i]['value'] = '.';
118
                        ++$i;
119
                    }
120
                }
121
                ++$counter;
122
            }
123
124
            if (($this->current + $this->perpage) < $this->total) {
125
                $this->next = $this->url . ($this->current + $this->perpage) . $this->extra;
126
                $this->last = $this->url . (($counter - 2) * $this->perpage) . $this->extra;
127
            }
128
        }
129
        if ($this->total >= $this->perpage && ceil($this->total / $this->perpage) > 1) {
130
            $xoops->tpl()->assign('xoopages', $pages);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pages does not seem to be defined for all execution paths leading up to this point.
Loading history...
131
        }
132
133
        return $xoops->tpl()->fetch('module:xooghost/xoo_paginate.tpl');
134
    }
135
}
136