Passed
Push — master ( f9c2a7...616839 )
by Richard
07:25 queued 11s
created

XoopsPageNav::renderImageNav()   C

Complexity

Conditions 15
Paths 30

Size

Total Lines 62
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 52
c 0
b 0
f 0
nc 30
nop 1
dl 0
loc 62
rs 5.9166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * XOOPS page navigation
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2021 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.0.0
16
 * @author              Kazumi Ono (http://www.myweb.ne.jp/, http://jp.xoops.org/)
17
 */
18
19
use Xmf\Request;
20
21
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
22
23
/**
24
 * Class XoopsPageNav
25
 */
26
class XoopsPageNav
27
{
28
    /**
29
     * *#@+
30
     *
31
     * @access private
32
     */
33
    public $total;
34
    public $perpage;
35
    public $current;
36
    public $url;
37
    /**
38
     * *#@-
39
     */
40
41
    /**
42
     * Constructor
43
     *
44
     * @param int    $total_items   Total number of items
45
     * @param int    $items_perpage Number of items per page
46
     * @param int    $current_start First item on the current page
47
     * @param string $start_name    Name for "start" or "offset"
48
     * @param string $extra_arg     Additional arguments to pass in the URL
49
     */
50
    public function __construct($total_items, $items_perpage, $current_start, $start_name = 'start', $extra_arg = '')
51
    {
52
        $this->total   = (int)$total_items;
53
        $this->perpage = (int)$items_perpage;
54
        $this->current = (int)$current_start;
55
        $this->extra   = $extra_arg;
0 ignored issues
show
Bug Best Practice introduced by
The property extra does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
        if ($extra_arg != '' && (substr($extra_arg, -5) !== '&amp;' || substr($extra_arg, -1) !== '&')) {
57
            $this->extra = '&amp;' . $extra_arg;
58
        }
59
        $this->url = htmlspecialchars(Request::getString('PHP_SELF', '', 'SERVER'), ENT_QUOTES) . '?' . trim($start_name) . '=';
60
    }
61
62
    /**
63
     * Create text navigation
64
     *
65
     * @param  integer $offset
66
     * @return string
67
     */
68
    public function renderNav($offset = 4)
69
    {
70
        $ret = '';
71
        if ($this->total <= $this->perpage) {
72
            return $ret;
73
        }
74
        if (($this->total != 0) && ($this->perpage != 0)) {
75
			$navigation = array();
76
            $total_pages = ceil($this->total / $this->perpage);
77
            if ($total_pages > 1) {
78
				$i = 0;
79
                $prev = $this->current - $this->perpage;
80
                if ($prev >= 0) {
81
					$navigation[$i]['url'] = $this->url . $prev . $this->extra;
82
					$navigation[$i]['value'] = '';
83
					$navigation[$i]['option'] = 'first';
84
					++$i;
85
                }
86
                $counter      = 1;
87
                $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
88
                while ($counter <= $total_pages) {					
89
                    if ($counter == $current_page) {
90
						$navigation[$i]['url'] = $this->url . $prev . $this->extra;
91
						$navigation[$i]['value'] = $counter;
92
						$navigation[$i]['option'] = 'selected';
93
                    } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || $counter == 1 || $counter == $total_pages) {
94
                        if ($counter == $total_pages && $current_page < $total_pages - $offset) {
95
							$navigation[$i]['url'] = '';
96
							$navigation[$i]['value'] = '';
97
							$navigation[$i]['option'] = 'break';
98
							++$i;
99
                        }
100
						$navigation[$i]['url'] = $this->url . (($counter - 1) * $this->perpage) . $this->extra;
101
						$navigation[$i]['value'] = $counter;
102
						$navigation[$i]['option'] = 'show';
103
						++$i;
104
                        if ($counter == 1 && $current_page > 1 + $offset) {
105
							$navigation[$i]['url'] = '';
106
							$navigation[$i]['value'] = '';
107
							$navigation[$i]['option'] = 'break';
108
                        }
109
                    }
110
                    ++$counter;
111
					++$i;
112
                }
113
                $next = $this->current + $this->perpage;
114
                if ($this->total > $next) {
115
					$navigation[$i]['url'] = $this->url . $next . $this->extra;
116
					$navigation[$i]['value'] = '';
117
					$navigation[$i]['option'] = 'last';
118
                }
119
            }
120
			return $this->displayPageNav('Nav', $navigation);
121
        }
122
        return $ret;
123
    }
124
125
    /**
126
     * Create a navigational dropdown list
127
     *
128
     * @param  boolean $showbutton Show the "Go" button?
129
     * @return string
130
     */
131
    public function renderSelect($showbutton = false)
132
    {
133
        $ret = '';
134
		if ($this->total < $this->perpage) {
135
            return $ret;
136
        }
137
        $total_pages = ceil($this->total / $this->perpage);
138
        if ($total_pages > 1) {
139
            $counter      = 1;
140
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
141
			while ($counter <= $total_pages) {
142
                if ($counter == $current_page) {
143
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . $this->extra . '" selected>' . $counter . '</option>';
144
                } else {
145
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . $this->extra . '">' . $counter . '</option>';
146
                }
147
                ++$counter;
148
            }
149
            if ($showbutton) {
150
				$navigation['button'] = true;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$navigation was never initialized. Although not strictly required by PHP, it is generally a good practice to add $navigation = array(); before regardless.
Loading history...
151
            } else {
152
				$navigation['button'] = false;
153
			}
154
			$navigation['select'] = $ret;
155
			return $this->displayPageNav('Select', $navigation);
156
        }
157
        return $ret;
158
    }
159
160
    /**
161
     * Create navigation with images
162
     *
163
     * @param  integer $offset
164
     * @return string
165
     */
166
    public function renderImageNav($offset = 4)
167
    {
168
        $ret = '';
169
		if ($this->total < $this->perpage) {
170
            return $ret;
171
        }
172
        $total_pages = ceil($this->total / $this->perpage);
173
        if ($total_pages > 1) {
174
			$i = 0;
175
            $prev = $this->current - $this->perpage;
176
            if ($prev >= 0) {
177
				$navigation[$i]['url'] = $this->url . $prev . $this->extra;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$navigation was never initialized. Although not strictly required by PHP, it is generally a good practice to add $navigation = array(); before regardless.
Loading history...
178
				$navigation[$i]['value'] = '';
179
				$navigation[$i]['option'] = 'first';
180
				++$i;
181
            } else {
182
				$navigation[$i]['url'] = '';
183
				$navigation[$i]['value'] = '';
184
				$navigation[$i]['option'] = 'firstempty';
185
				++$i;
186
            }
187
            $counter      = 1;
188
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
189
            while ($counter <= $total_pages) {
190
                if ($counter == $current_page) {
191
					$navigation[$i]['url'] = '';
192
					$navigation[$i]['value'] = $counter;
193
					$navigation[$i]['option'] = 'selected';
194
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || $counter == 1 || $counter == $total_pages) {
195
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
196
						$navigation[$i]['url'] = '';
197
						$navigation[$i]['value'] = '';
198
						$navigation[$i]['option'] = 'break';
199
						++$i;
200
                    }
201
					$navigation[$i]['url'] = $this->url . (($counter - 1) * $this->perpage) . $this->extra;
202
					$navigation[$i]['value'] = $counter;
203
					$navigation[$i]['option'] = 'show';
204
					++$i;
205
                    if ($counter == 1 && $current_page > 1 + $offset) {
206
						$navigation[$i]['url'] = '';
207
						$navigation[$i]['value'] = '';
208
						$navigation[$i]['option'] = 'break';
209
                    }
210
                }
211
                ++$counter;
212
				++$i;
213
            }
214
            $next = $this->current + $this->perpage;
215
            if ($this->total > $next) {
216
				$navigation[$i]['url'] = $this->url . $next . $this->extra;
217
				$navigation[$i]['value'] = '';
218
				$navigation[$i]['option'] = 'last';
219
				++$i;
220
            } else {
221
				$navigation[$i]['url'] = '';
222
				$navigation[$i]['value'] = '';
223
				$navigation[$i]['option'] = 'lastempty';
224
            }
225
			return $this->displayPageNav('Image', $navigation);
226
        }
227
        return $ret;
228
    }
229
230
    /**
231
     * Display navigation in template
232
     *
233
     * @param  string $type
234
     * @param  array $navigation
235
     * @return string
236
     */
237
	private function displayPageNav($type = 'nav', $navigation = array()){
238
		if (!isset($GLOBALS['xoTheme']) || !is_object($GLOBALS['xoTheme'])) {
239
			include_once $GLOBALS['xoops']->path('/class/theme.php');
240
			$GLOBALS['xoTheme'] = new \xos_opal_Theme();
241
		}
242
		require_once $GLOBALS['xoops']->path('/class/template.php');
243
		$pageNavTpl = new \XoopsTpl();
244
		$pageNavTpl->assign('pageNavType', $type);
245
		$pageNavTpl->assign('pageNavigation', $navigation);
246
		
247
		return $pageNavTpl->fetch("db:system_pagenav.tpl");
248
		
249
	}
250
}
251