Passed
Pull Request — master (#9)
by Michael
03:24
created

PageNav::renderSelect()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 28
rs 8.9777
c 0
b 0
f 0
cc 6
nc 4
nop 1
1
<?php
2
3
namespace XoopsModules\Mylinks;
4
5
/**
6
 * Class MylinksPageNav
7
 */
8
class PageNav
9
{
10
    public $total;
11
    public $perpage;
12
    public $current;
13
    public $url;
14
15
    /**
16
     * MylinksPageNav constructor.
17
     * @param        $total_items
18
     * @param        $items_perpage
19
     * @param        $current_start
20
     * @param string $start_name
21
     * @param string $extra_arg
22
     */
23
    public function __construct(
24
        $total_items,
25
        $items_perpage,
26
        $current_start,
27
        $start_name = 'start',
28
        $extra_arg = ''
29
    ) {
30
        $this->total   = (int)$total_items;
31
        $this->perpage = (int)$items_perpage;
32
        $this->current = (int)$current_start;
33
        if ('' != $extra_arg && ('&amp;' !== mb_substr($extra_arg, -5) || '&' !== mb_substr($extra_arg, -1))) {
34
            $extra_arg .= '&amp;';
35
        }
36
        $this->url = xoops_getenv('SCRIPT_NAME') . '?' . $extra_arg . trim($start_name) . '=';
37
    }
38
39
    /**
40
     * @param int $offset
41
     * @return string
42
     */
43
    public function renderNav($offset = 5)
44
    {
45
        $ret = '';
46
        if ($this->total <= $this->perpage) {
47
            return $ret;
48
        }
49
        $total_pages = ceil($this->total / $this->perpage);
50
        if ($total_pages > 1) {
51
            $prev = $this->current - $this->perpage;
52
            if ($prev >= 0) {
53
                $ret .= '<a href="' . $this->url . $prev . '"><u>&laquo;</u></a> ';
54
            }
55
            $counter      = 1;
56
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
57
            while ($counter <= $total_pages) {
58
                if ($counter == $current_page) {
59
                    $ret .= '<strong>(' . $counter . ')</strong> ';
60
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || 1 == $counter
61
                          || $counter == $total_pages) {
62
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
63
                        $ret .= '... ';
64
                    }
65
                    $ret .= '<a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a> ';
66
                    if (1 == $counter && $current_page > 1 + $offset) {
67
                        $ret .= '... ';
68
                    }
69
                }
70
                ++$counter;
71
            }
72
            $next = $this->current + $this->perpage;
73
            if ($this->total > $next) {
74
                $ret .= '<a href="' . $this->url . $next . '"><u>&raquo;</u></a> ';
75
            }
76
        }
77
78
        return $ret;
79
    }
80
81
    /**
82
     * @param bool $showbutton
83
     * @return string|void
84
     */
85
    public function renderSelect($showbutton = false)
86
    {
87
        if ($this->total < $this->perpage) {
88
            return;
89
        }
90
        $total_pages = ceil($this->total / $this->perpage);
91
        $ret         = '';
92
        if ($total_pages > 1) {
93
            $ret          = '<form name="pagenavform" action="' . xoops_getenv('SCRIPT_NAME') . '">';
94
            $ret          .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
95
            $counter      = 1;
96
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
97
            while ($counter <= $total_pages) {
98
                if ($counter == $current_page) {
99
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected>' . $counter . '</option>';
100
                } else {
101
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</option>';
102
                }
103
                ++$counter;
104
            }
105
            $ret .= '</select>';
106
            if ($showbutton) {
107
                $ret .= '&nbsp;<input type="submit" value="' . _GO . '">';
108
            }
109
            $ret .= '</form>';
110
        }
111
112
        return $ret;
113
    }
114
115
    /**
116
     * @param int $offset
117
     * @return string|void
118
     */
119
    public function renderImageNav($offset = 5)
120
    {
121
        if ($this->total < $this->perpage) {
122
            return;
123
        }
124
        $total_pages = ceil($this->total / $this->perpage);
125
        $ret         = '';
126
        if ($total_pages > 1) {
127
            $ret  = '<table><tr>';
128
            $prev = $this->current - $this->perpage;
129
            if ($prev >= 0) {
130
                $ret .= '<td class="pagneutral"><a href="' . $this->url . $prev . '">&lt;</a></td><td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt=""></td>';
131
            }
132
            $counter      = 1;
133
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
134
            while ($counter <= $total_pages) {
135
                if ($counter == $current_page) {
136
                    $ret .= '<td class="pagact"><b>' . $counter . '</b></td>';
137
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || 1 == $counter
138
                          || $counter == $total_pages) {
139
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
140
                        $ret .= '<td class="paginact">...</td>';
141
                    }
142
                    $ret .= '<td class="paginact"><a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a></td>';
143
                    if (1 == $counter && $current_page > 1 + $offset) {
144
                        $ret .= '<td class="paginact">...</td>';
145
                    }
146
                }
147
                ++$counter;
148
            }
149
            $next = $this->current + $this->perpage;
150
            if ($this->total > $next) {
151
                $ret .= '<td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt=""></td><td class="pagneutral"><a href="' . $this->url . $next . '">></a></td>';
152
            }
153
            $ret .= '</tr></table>';
154
        }
155
156
        return $ret;
157
    }
158
}
159