|
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
|
|
|
|
|
37
|
|
|
use ntentan\honam\TemplateEngine; |
|
38
|
|
|
use ntentan\utils\Input; |
|
39
|
|
|
|
|
40
|
|
|
class PaginationHelper extends Helper |
|
41
|
|
|
{ |
|
42
|
|
|
/*private $pageNumber; |
|
43
|
|
|
private $numberOfPages; |
|
44
|
|
|
private $baseRoute; |
|
45
|
|
|
private $numberOfLinks; |
|
46
|
|
|
private $query;*/ |
|
47
|
|
|
|
|
48
|
|
|
private $parameters = array( |
|
49
|
|
|
'query' => null, |
|
50
|
|
|
'number_of_links' => 21, |
|
51
|
|
|
'number_of_pages' => null, |
|
52
|
|
|
'base_url' => null, |
|
53
|
|
|
'page_number' => 1 |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
private $halfNumberOfLinks; |
|
57
|
|
|
|
|
58
|
|
|
public function help($params = array()) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->parameters = array_merge($this->parameters, $params); |
|
61
|
|
|
$this->halfNumberOfLinks = ceil($this->parameters['number_of_links'] / 2); |
|
62
|
|
|
if($this->parameters['query'] != '') |
|
63
|
|
|
{ |
|
64
|
|
|
$this->parameters['page_number'] = Input::get($this->parameters['query']) == '' ? 1 : Input::get($this->parameters['query']); |
|
65
|
|
|
} |
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
private function getLink($index) |
|
71
|
|
|
{ |
|
72
|
|
|
if($this->parameters['query'] == '') |
|
73
|
|
|
{ |
|
74
|
|
|
$link = $this->parameters['base_url'] . $index; |
|
75
|
|
|
} |
|
76
|
|
|
else |
|
77
|
|
|
{ |
|
78
|
|
|
$link = $this->parameters['base_url'] . '?'; |
|
79
|
|
|
$get = Input::get(); |
|
80
|
|
|
foreach($get as $key => $value) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
if($key == $this->parameters['query']) continue; |
|
83
|
|
|
$link .= "$key=" . urlencode($value) . '&'; |
|
84
|
|
|
} |
|
85
|
|
|
$link .= "{$this->parameters['query']}=$index"; |
|
86
|
|
|
} |
|
87
|
|
|
return $link; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function __toString() |
|
91
|
|
|
{ |
|
92
|
|
|
$pagingLinks = array(); |
|
93
|
|
View Code Duplication |
if($this->parameters['page_number']> 1) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$pagingLinks[] = array( |
|
96
|
|
|
"link" => $this->getLink($this->parameters['page_number']- 1), |
|
97
|
|
|
"label" => "< Prev", |
|
98
|
|
|
"selected" => false |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if($this->parameters['number_of_pages'] <= $this->parameters['number_of_links'] || $this->parameters['page_number']< $this->halfNumberOfLinks) |
|
103
|
|
|
{ |
|
104
|
|
|
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++) |
|
105
|
|
|
{ |
|
106
|
|
|
|
|
107
|
|
|
$pagingLinks[] = array( |
|
108
|
|
|
"link" => $this->getLink($i), |
|
109
|
|
|
"label" => "$i", |
|
110
|
|
|
"selected" => $this->parameters['page_number']== $i |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
else |
|
115
|
|
|
{ |
|
116
|
|
|
if($this->parameters['number_of_pages'] - $this->parameters['page_number']< $this->halfNumberOfLinks) |
|
117
|
|
|
{ |
|
118
|
|
|
$startOffset = $this->parameters['page_number']- (($this->parameters['number_of_links'] - 1) - ($this->parameters['number_of_pages'] - $this->parameters['page_number'])); |
|
119
|
|
|
$endOffset = $this->parameters['page_number']+ ($this->parameters['number_of_pages'] - $this->parameters['page_number']); |
|
120
|
|
|
} |
|
121
|
|
|
else |
|
122
|
|
|
{ |
|
123
|
|
|
$startOffset = $this->parameters['page_number']- ($this->halfNumberOfLinks - 1); |
|
124
|
|
|
$endOffset = $this->parameters['page_number']+ ($this->halfNumberOfLinks - 1); |
|
125
|
|
|
} |
|
126
|
|
|
for($i = $startOffset ; $i <= $endOffset; $i++) |
|
127
|
|
|
{ |
|
128
|
|
|
$pagingLinks[] = array( |
|
129
|
|
|
"link" => $this->getLink($i), |
|
130
|
|
|
"label" => "$i", |
|
131
|
|
|
"selected" => $this->parameters['page_number']== $i |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
View Code Duplication |
if($this->parameters['page_number']< $this->parameters['number_of_pages']) |
|
|
|
|
|
|
137
|
|
|
{ |
|
138
|
|
|
$pagingLinks[] = array( |
|
139
|
|
|
"link" => $this->getLink($this->parameters['page_number']+ 1), |
|
140
|
|
|
"label" => "Next >", |
|
141
|
|
|
"selected" => false |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this->templateRenderer->render("links.tpl.php", array('links' => $pagingLinks)); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.