Completed
Pull Request — master (#8)
by Michael
02:48
created

MylinksPageNav::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 2
nop 5
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class MylinksPageNav
5
 */
6
class MylinksPageNav
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    public $total;
9
    public $perpage;
10
    public $current;
11
    public $url;
12
13
    /**
14
     * mylinkspagenav constructor.
15
     * @param        $total_items
16
     * @param        $items_perpage
17
     * @param        $current_start
18
     * @param string $start_name
19
     * @param string $extra_arg
20
     */
21
    public function __construct($total_items, $items_perpage, $current_start, $start_name = 'start', $extra_arg = '')
22
    {
23
        $this->total   = (int)$total_items;
24
        $this->perpage = (int)$items_perpage;
25
        $this->current = (int)$current_start;
26
        if ($extra_arg != '' && (substr($extra_arg, -5) != '&amp;' || substr($extra_arg, -1) != '&')) {
27
            $extra_arg .= '&amp;';
28
        }
29
        $this->url = xoops_getenv('PHP_SELF') . '?' . $extra_arg . trim($start_name) . '=';
30
    }
31
32
    /**
33
     * @param int $offset
34
     * @return string
35
     */
36
    public function renderNav($offset = 5)
37
    {
38
        $ret = '';
39
        if ($this->total <= $this->perpage) {
40
            return $ret;
41
        }
42
        $total_pages = ceil($this->total / $this->perpage);
43
        if ($total_pages > 1) {
44
            $prev = $this->current - $this->perpage;
45
            if ($prev >= 0) {
46
                $ret .= '<a href="' . $this->url . $prev . '"><u>&laquo;</u></a> ';
47
            }
48
            $counter      = 1;
49
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
50 View Code Duplication
            while ($counter <= $total_pages) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
                if ($counter == $current_page) {
52
                    $ret .= '<strong>(' . $counter . ')</strong> ';
53
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || $counter == 1
54
                          || $counter == $total_pages
55
                ) {
56
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
57
                        $ret .= '... ';
58
                    }
59
                    $ret .= '<a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a> ';
60
                    if ($counter == 1 && $current_page > 1 + $offset) {
61
                        $ret .= '... ';
62
                    }
63
                }
64
                $counter++;
65
            }
66
            $next = $this->current + $this->perpage;
67
            if ($this->total > $next) {
68
                $ret .= '<a href="' . $this->url . $next . '"><u>&raquo;</u></a> ';
69
            }
70
        }
71
72
        return $ret;
73
    }
74
75
    /**
76
     * @param bool $showbutton
77
     * @return string|void
78
     */
79
    public function renderSelect($showbutton = false)
80
    {
81
        if ($this->total < $this->perpage) {
82
            return;
83
        }
84
        $total_pages = ceil($this->total / $this->perpage);
85
        $ret         = '';
86
        if ($total_pages > 1) {
87
            $ret = '<form name="pagenavform" action="' . xoops_getenv('PHP_SELF') . '">';
88
            $ret .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
89
            $counter      = 1;
90
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
91
            while ($counter <= $total_pages) {
92
                if ($counter == $current_page) {
93
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected="selected">' . $counter . '</option>';
94
                } else {
95
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</option>';
96
                }
97
                $counter++;
98
            }
99
            $ret .= '</select>';
100
            if ($showbutton) {
101
                $ret .= '&nbsp;<input type="submit" value="' . _GO . '">';
102
            }
103
            $ret .= '</form>';
104
        }
105
106
        return $ret;
107
    }
108
109
    /**
110
     * @param int $offset
111
     * @return string|void
112
     */
113
    public function renderImageNav($offset = 5)
114
    {
115
        if ($this->total < $this->perpage) {
116
            return;
117
        }
118
        $total_pages = ceil($this->total / $this->perpage);
119
        $ret         = '';
120
        if ($total_pages > 1) {
121
            $ret  = '<table><tr>';
122
            $prev = $this->current - $this->perpage;
123
            if ($prev >= 0) {
124
                $ret .= '<td class="pagneutral"><a href="' . $this->url . $prev . '">&lt;</a></td><td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt=""></td>';
125
            }
126
            $counter      = 1;
127
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
128 View Code Duplication
            while ($counter <= $total_pages) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
                if ($counter == $current_page) {
130
                    $ret .= '<td class="pagact"><b>' . $counter . '</b></td>';
131
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || $counter == 1
132
                          || $counter == $total_pages
133
                ) {
134
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
135
                        $ret .= '<td class="paginact">...</td>';
136
                    }
137
                    $ret .= '<td class="paginact"><a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a></td>';
138
                    if ($counter == 1 && $current_page > 1 + $offset) {
139
                        $ret .= '<td class="paginact">...</td>';
140
                    }
141
                }
142
                $counter++;
143
            }
144
            $next = $this->current + $this->perpage;
145
            if ($this->total > $next) {
146
                $ret .= '<td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt=""></td><td class="pagneutral"><a href="' . $this->url . $next . '">&gt;</a></td>';
147
            }
148
            $ret .= '</tr></table>';
149
        }
150
151
        return $ret;
152
    }
153
}
154