Passed
Pull Request — master (#6)
by James Ekow Abaka
02:52
created

PaginationHelper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 87
ccs 32
cts 44
cp 0.7272
rs 10
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __toString() 0 46 9
A help() 0 9 3
A getLink() 0 14 4
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\engines\php\helpers;
34
35
use ntentan\honam\engines\php\Helper;
36
use ntentan\utils\Input;
37
38
class PaginationHelper extends Helper
39
{
40
    private $parameters = [
41
        'query' => null,
42
        'number_of_links' => 21,
43
        'number_of_items' => null,
44
        'items_per_page' => 10,
45
        'base_url' => null,
46
        'page_number' => 1
47
    ];
48
49
    private $halfNumberOfLinks;
50
51 5
    public function help($params = [])
52
    {
53 5
        $this->parameters = array_merge($this->parameters, $params);
54 5
        $this->parameters['number_of_pages'] = ceil($this->parameters['number_of_items'] / $this->parameters['items_per_page']);
55 5
        $this->halfNumberOfLinks = ceil($this->parameters['number_of_links'] / 2);
56 5
        if ($this->parameters['query'] != '') {
57 1
            $this->parameters['page_number'] = Input::get($this->parameters['query']) == '' ? 1 : Input::get($this->parameters['query']);
58
        }
59 5
        return $this;
60
    }
61
62
63 4
    private function getLink($index)
64
    {
65 4
        if ($this->parameters['query'] == '') {
66 3
            $link = $this->parameters['base_url'] . $index;
67
        } else {
68 1
            $link = $this->parameters['base_url'] . '?';
69 1
            $get = Input::get();
70 1
            foreach ($get as $key => $value) {
71
                if ($key == $this->parameters['query']) continue;
72
                $link .= "$key=" . urlencode($value) . '&';
73
            }
74 1
            $link .= "{$this->parameters['query']}=$index";
75
        }
76 4
        return $link;
77
    }
78
79 5
    public function __toString()
80
    {
81 5
        $pagingLinks = array();
82 5
        if ($this->parameters['page_number'] > 1) {
83 2
            $pagingLinks[] = array(
84 2
                "link" => $this->getLink($this->parameters['page_number'] - 1),
85 2
                "label" => "Prev",
86
                "selected" => false
87
            );
88
        }
89
90 5
        if ($this->parameters['number_of_pages'] <= $this->parameters['number_of_links'] || $this->parameters['page_number'] < $this->halfNumberOfLinks) {
91 5
            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++) {
92
93 4
                $pagingLinks[] = array(
94 4
                    "link" => $this->getLink($i),
95 4
                    "label" => "$i",
96 4
                    "selected" => $this->parameters['page_number'] == $i
97
                );
98
            }
99
        } else {
100
            if ($this->parameters['number_of_pages'] - $this->parameters['page_number'] < $this->halfNumberOfLinks) {
101
                $startOffset = $this->parameters['page_number'] - (($this->parameters['number_of_links'] - 1) - ($this->parameters['number_of_pages'] - $this->parameters['page_number']));
102
                $endOffset = $this->parameters['page_number'] + ($this->parameters['number_of_pages'] - $this->parameters['page_number']);
103
            } else {
104
                $startOffset = $this->parameters['page_number'] - ($this->halfNumberOfLinks - 1);
105
                $endOffset = $this->parameters['page_number'] + ($this->halfNumberOfLinks - 1);
106
            }
107
            for ($i = $startOffset; $i <= $endOffset; $i++) {
108
                $pagingLinks[] = array(
109
                    "link" => $this->getLink($i),
110
                    "label" => "$i",
111
                    "selected" => $this->parameters['page_number'] == $i
112
                );
113
            }
114
        }
115
116 5
        if ($this->parameters['page_number'] < $this->parameters['number_of_pages']) {
117 2
            $pagingLinks[] = array(
118 2
                "link" => $this->getLink($this->parameters['page_number'] + 1),
119 2
                "label" => "Next",
120
                "selected" => false
121
            );
122
        }
123
124 5
        return $this->templateRenderer->render("links.tpl.php", array('links' => $pagingLinks));
125
    }
126
}
127