| Conditions | 30 |
| Paths | > 20000 |
| Total Lines | 148 |
| Lines | 21 |
| Ratio | 14.19 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 199 | public function create_links_ajax() { |
||
| 200 | |||
| 201 | $CI = &get_instance(); |
||
| 202 | |||
| 203 | // If our item count or per-page total is zero there is no need to continue. |
||
| 204 | if ($this->total_rows == 0 OR $this->per_page == 0) { |
||
| 205 | return ''; |
||
| 206 | } |
||
| 207 | |||
| 208 | // Calculate the total number of pages |
||
| 209 | $num_pages = ceil($this->total_rows / $this->per_page); |
||
| 210 | |||
| 211 | // Is there only one page? Hm... nothing more to do here then. |
||
| 212 | if ($num_pages == 1) { |
||
| 213 | return ''; |
||
| 214 | } |
||
| 215 | |||
| 216 | // Determine the current page number. |
||
| 217 | $CI = &get_instance(); |
||
| 218 | |||
| 219 | View Code Duplication | if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) { |
|
| 220 | if ($CI->input->get($this->query_string_segment) != 0) { |
||
| 221 | $this->cur_page = $CI->input->get($this->query_string_segment); |
||
| 222 | |||
| 223 | // Prep the current page - no funny business! |
||
| 224 | $this->cur_page = (int) $this->cur_page; |
||
| 225 | } |
||
| 226 | } else { |
||
| 227 | if ($CI->uri->segment($this->uri_segment) != 0) { |
||
| 228 | $this->cur_page = $CI->uri->segment($this->uri_segment); |
||
| 229 | |||
| 230 | // Prep the current page - no funny business! |
||
| 231 | $this->cur_page = (int) $this->cur_page; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | $this->num_links = (int) $this->num_links; |
||
| 236 | |||
| 237 | if ($this->num_links < 1) { |
||
| 238 | show_error('Your number of links must be a positive number.'); |
||
| 239 | } |
||
| 240 | |||
| 241 | if (!is_numeric($this->cur_page)) { |
||
| 242 | $this->cur_page = 0; |
||
| 243 | } |
||
| 244 | |||
| 245 | // Is the page number beyond the result range? |
||
| 246 | // If so we show the last page |
||
| 247 | if ($this->cur_page > $this->total_rows) { |
||
| 248 | $this->cur_page = ($num_pages - 1) * $this->per_page; |
||
| 249 | } |
||
| 250 | |||
| 251 | $uri_page_number = $this->cur_page; |
||
| 252 | $this->cur_page = floor(($this->cur_page / $this->per_page) + 1); |
||
| 253 | |||
| 254 | // Calculate the start and end numbers. These determine |
||
| 255 | // which number to start and end the digit links with |
||
| 256 | $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1; |
||
| 257 | $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages; |
||
| 258 | |||
| 259 | // Is pagination being used over GET or POST? If get, add a per_page query |
||
| 260 | // string. If post, add a trailing slash to the base URL if needed |
||
| 261 | if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) { |
||
| 262 | $this->base_url = rtrim($this->base_url) . '&' . $this->query_string_segment . '='; |
||
| 263 | } else { |
||
| 264 | $this->base_url = rtrim($this->base_url, '/') . '/'; |
||
| 265 | } |
||
| 266 | |||
| 267 | // And here we go... |
||
| 268 | $output = ''; |
||
| 269 | |||
| 270 | // Check for separated controls |
||
| 271 | if ($this->separate_controls) { |
||
| 272 | $controls_output = ''; |
||
| 273 | } |
||
| 274 | |||
| 275 | // Render the "First" link |
||
| 276 | View Code Duplication | if ($this->cur_page > $this->num_links) { |
|
| 277 | $output .= $this->first_tag_open . '<a href="' . $this->base_url . 'offset' . '/' . $this->suffix . '">' . $this->first_link . '</a>' . $this->first_tag_close; |
||
| 278 | } |
||
| 279 | |||
| 280 | // Render the "previous" link |
||
| 281 | if ($this->cur_page != 1) { |
||
| 282 | $i = $uri_page_number - $this->per_page; |
||
| 283 | if ($i == 0) { |
||
| 284 | $i = 'offset'; |
||
| 285 | } |
||
| 286 | |||
| 287 | if (!$this->separate_controls) { |
||
| 288 | $output .= $this->prev_tag_open . '<a href="' . $this->base_url . $i . '/' . $this->suffix . '">' . $this->prev_link . '</a>' . $this->prev_tag_close; |
||
| 289 | } else { |
||
| 290 | $controls_output .= $this->prev_tag_open . '<a href="' . $this->base_url . $i . '/' . $this->suffix . '">' . $this->prev_link . '</a>' . $this->prev_tag_close; |
||
| 291 | } |
||
| 292 | } else { |
||
| 293 | if ($this->separate_controls) { |
||
| 294 | $controls_output .= str_replace('>', ' class="disabled">', $this->prev_tag_open) . '<span>' . $this->prev_link . '</span>' . $this->prev_tag_close; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | // Write the digit links |
||
| 299 | for ($loop = $start - 1; $loop <= $end; $loop++) { |
||
| 300 | $i = ($loop * $this->per_page) - $this->per_page; |
||
| 301 | |||
| 302 | if ($i >= 0) { |
||
| 303 | if ($this->cur_page == $loop) { |
||
| 304 | $output .= $this->cur_tag_open . $loop . $this->cur_tag_close; // Current page |
||
| 305 | } else { |
||
| 306 | $n = ($i == 0) ? 'offset' : $i; |
||
| 307 | $output .= $this->num_tag_open . '<a href="' . $this->base_url . $n . '/' . $this->suffix . '">' . $loop . '</a>' . $this->num_tag_close; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | // Render the "next" link |
||
| 313 | if ($this->cur_page < $num_pages) { |
||
| 314 | if (!$this->separate_controls) { |
||
| 315 | $output .= $this->next_tag_open . '<a href="' . $this->base_url . ($this->cur_page * $this->per_page) . '/' . $this->suffix . '">' . $this->next_link . '</a>' . $this->next_tag_close; |
||
| 316 | View Code Duplication | } else { |
|
| 317 | $controls_output .= $this->next_tag_open . '<a href="' . $this->base_url . ($this->cur_page * $this->per_page) . '/' . $this->suffix . '">' . $this->next_link . '</a>' . $this->next_tag_close; |
||
| 318 | } |
||
| 319 | } else { |
||
| 320 | if ($this->separate_controls) { |
||
| 321 | $controls_output .= str_replace('>', ' class="disabled">', $this->next_tag_open) . '<span>' . $this->next_link . '</span>' . $this->next_tag_close; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | // Render the "Last" link |
||
| 326 | if (($this->cur_page + $this->num_links) < $num_pages) { |
||
| 327 | $i = (($num_pages * $this->per_page) - $this->per_page); |
||
| 328 | $output .= $this->last_tag_open . '<a href="' . $this->base_url . $i . '/' . $this->suffix . '">' . $this->last_link . '</a>' . $this->last_tag_close; |
||
| 329 | } |
||
| 330 | |||
| 331 | // Kill double slashes. Note: Sometimes we can end up with a double slash |
||
| 332 | // in the penultimate link so we'll kill all double slashes. |
||
| 333 | $output = preg_replace('#([^:])//+#', "\\1/", $output); |
||
| 334 | |||
| 335 | // Add the wrapper HTML if exists |
||
| 336 | $output = $this->full_tag_open . $output . $this->full_tag_close; |
||
| 337 | |||
| 338 | if ($this->separate_controls) { |
||
| 339 | $controls_output = preg_replace('#([^:])//+#', "\\1/", $controls_output); |
||
| 340 | $controls_output = $this->controls_tag_open . $controls_output . $this->controls_tag_close; |
||
| 341 | |||
| 342 | $output = $output . $controls_output; |
||
| 343 | } |
||
| 344 | |||
| 345 | return $output; |
||
| 346 | } |
||
| 347 | |||
| 348 | } |