Completed
Push — master ( 02b122...2a8558 )
by James Ekow Abaka
02:43
created

PaginationHelper::getLink()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 1
dl 0
loc 19
ccs 8
cts 8
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Common utilities file for the Ntentan framework. This file contains a 
4
 * collection of utility static methods which are used accross the framework.
5
 * 
6
 * Ntentan Framework
7
 * Copyright (c) 2008-2014 James Ekow Abaka Ainooson
8
 * 
9
 * Permission is hereby granted, free of charge, to any person obtaining
10
 * a copy of this software and associated documentation files (the
11
 * "Software"), to deal in the Software without restriction, including
12
 * without limitation the rights to use, copy, modify, merge, publish,
13
 * distribute, sublicense, and/or sell copies of the Software, and to
14
 * permit persons to whom the Software is furnished to do so, subject to
15
 * the following conditions:
16
 * 
17
 * The above copyright notice and this permission notice shall be
18
 * included in all copies or substantial portions of the Software.
19
 * 
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
27
 * 
28
 * @author James Ainooson <[email protected]>
29
 * @copyright Copyright 2014 James Ekow Abaka Ainooson
30
 * @license MIT
31
 */
32
33
namespace ntentan\honam\helpers;
34
35
use ntentan\honam\Helper;
36
use ntentan\honam\TemplateEngine;
37
use ntentan\utils\Input;
38
39
class PaginationHelper extends Helper
40
{
41
    /*private $pageNumber;
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
    private $numberOfPages;
43
    private $baseRoute;
44
    private $numberOfLinks;
45
    private $query;*/
46
    
47
    private $parameters = array(
48
        'query' => null,
49
        'number_of_links' => 21,
50
        'number_of_pages' => null,
51
        'base_url' => null,
52
        'page_number' => 1
53
    );
54
    private $halfNumberOfLinks;
55
    
56 5
    public function __construct()
57
    {
58 5
        TemplateEngine::appendPath(
59 5
            __DIR__ . "/../../templates/pagination"
60
        );
61 5
    } 
62
63 5
    public function help($params = array())
64
    {
65 5
        $this->parameters = array_merge($this->parameters, $params);
66 5
        $this->halfNumberOfLinks = ceil($this->parameters['number_of_links'] / 2);
67 5
        if($this->parameters['query'] != '')
68
        {
69 1
            $this->parameters['page_number'] = Input::get($this->parameters['query']) == '' ? 1 : Input::get($this->parameters['query']);
70
        }
71 5
        return $this;
72
    }
73
    
74
    
75 4
    private function getLink($index)
76
    {
77 4
        if($this->parameters['query'] == '')
78
        {
79 3
            $link = $this->parameters['base_url'] . $index;
80
        }
81
        else
82
        {
83 1
            $link = $this->parameters['base_url'] . '?';
84 1
            $get = Input::get();
85 1
            foreach($get as $key => $value)
86
            {
87
                if($key == $this->parameters['query']) continue;
88
                $link .= "$key=" . urlencode($value) . '&';
89
            }
90 1
            $link .= "{$this->parameters['query']}=$index";
91
        }
92 4
        return $link;
93
    }
94
95 5
    public function __toString()
96
    {
97 5
        $pagingLinks = array();
98 5 View Code Duplication
        if($this->parameters['page_number']> 1)
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...
99
        {
100 2
            $pagingLinks[] = array(
101 2
                "link" => $this->getLink($this->parameters['page_number']- 1),
102 2
                "label" => "< Prev",
103
                "selected" => false
104
            );
105
        }
106
107 5
        if($this->parameters['number_of_pages'] <= $this->parameters['number_of_links'] || $this->parameters['page_number']< $this->halfNumberOfLinks)
108
        {
109 3
            for($i = 1; $i <= ($this->parameters['number_of_pages'] > $this->parameters['number_of_links'] ? $this->parameters['number_of_links'] : $this->parameters['number_of_pages']) ; $i++)
110
            {
111
                
112 2
                $pagingLinks[] = array(
113 2
                    "link" => $this->getLink($i),
114 2
                    "label" => "$i",
115 2
                    "selected" => $this->parameters['page_number']== $i
116
                );
117
            }
118
        }
119
        else
120
        {
121 2
            if($this->parameters['number_of_pages'] - $this->parameters['page_number']< $this->halfNumberOfLinks)
122
            {
123 1
                $startOffset = $this->parameters['page_number']- (($this->parameters['number_of_links'] - 1) - ($this->parameters['number_of_pages'] - $this->parameters['page_number']));
124 1
                $endOffset = $this->parameters['page_number']+ ($this->parameters['number_of_pages'] - $this->parameters['page_number']);
125
            }
126
            else
127
            {
128 1
                $startOffset = $this->parameters['page_number']- ($this->halfNumberOfLinks - 1);
129 1
                $endOffset = $this->parameters['page_number']+ ($this->halfNumberOfLinks - 1);
130
            }
131 2
            for($i = $startOffset ; $i <= $endOffset; $i++)
132
            {
133 2
                $pagingLinks[] = array(
134 2
                    "link" => $this->getLink($i),
135 2
                    "label" => "$i",
136 2
                    "selected" => $this->parameters['page_number']== $i
137
                );
138
            }
139
        }
140
141 5 View Code Duplication
        if($this->parameters['page_number']< $this->parameters['number_of_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...
142
        {
143 4
            $pagingLinks[] = array(
144 4
                "link" => $this->getLink($this->parameters['page_number']+ 1),
145 4
                "label" => "Next >",
146
                "selected" => false
147
            );
148
        }
149
        
150 5
        return TemplateEngine::render("links.tpl.php", array('links' => $pagingLinks));
151
    }
152
}
153