Passed
Push — 1.0.0-dev ( 83bedf...ceb5d8 )
by nguereza
02:34
created

Pagination::determinePaginationQueryStringValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 4
nop 0
dl 0
loc 27
rs 9.6
c 0
b 0
f 0
1
<?php
2
    defined('ROOT_PATH') || exit('Access denied');
3
	/**
4
	 * TNH Framework
5
	 *
6
	 * A simple PHP framework using HMVC architecture
7
	 *
8
	 * This content is released under the GNU GPL License (GPL)
9
	 *
10
	 * Copyright (C) 2017 Tony NGUEREZA
11
	 *
12
	 * This program is free software; you can redistribute it and/or
13
	 * modify it under the terms of the GNU General Public License
14
	 * as published by the Free Software Foundation; either version 3
15
	 * of the License, or (at your option) any later version.
16
	 *
17
	 * This program is distributed in the hope that it will be useful,
18
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
	 * GNU General Public License for more details.
21
	 *
22
	 * You should have received a copy of the GNU General Public License
23
	 * along with this program; if not, write to the Free Software
24
	 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
	*/
26
27
    class Pagination{
28
        
29
		/**
30
         * The list of loaded config
31
         * @var array
32
         */
33
        private $config = array();
34
35
        /**
36
         * The pagination current query string
37
         * @var string
38
         */
39
        private $paginationQueryString = null;
40
41
        /**
42
         * Create an instance of pagination
43
         * @param array $overwriteConfig the list of configuration to overwrite the defined configuration in config_pagination.php
44
         */
45
        public function __construct($overwriteConfig = array()){
46
            if (file_exists(CONFIG_PATH . 'config_pagination.php')){
47
                $config = array();
48
                require_once CONFIG_PATH . 'config_pagination.php';
49
                if (empty($config) || ! is_array($config)){
50
                    show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php');
51
                }
52
				else{
53
					if (! empty($overwriteConfig)){
54
						$config = array_merge($config, $overwriteConfig);
55
					}
56
					$this->config = $config;
57
                    //put it gobally
58
					Config::setAll($config);
59
					unset($config);
60
				}
61
            }
62
            else{
63
                show_error('Unable to find the pagination configuration file');
64
            }
65
        }
66
67
68
        /**
69
         * Set the pagination custom configuration to overwrite the default configuration in
70
         * config_pagination.php
71
         * @param array $config the configuration to overwrite
72
         */
73
        public function setConfig(array $config = array()){
74
            if (! empty($config)){
75
                $this->config = array_merge($this->config, $config);
76
                Config::setAll($config);
77
            }
78
        }
79
80
        /**
81
         * Return the value of the pagination configuration
82
         * 
83
         * @return array
84
         */
85
        public function getConfig(){
86
            return $this->config;
87
        }
88
89
        /**
90
         * Return the value of the pagination query string
91
         * @return string
92
         */
93
        public function getPaginationQueryString(){
94
            return $this->paginationQueryString;
95
        }
96
97
         /**
98
         * Set the value of the pagination query string
99
         * @param string $paginationQueryString the new value
100
         * @return object
101
         */
102
        public function setPaginationQueryString($paginationQueryString){
103
            $this->paginationQueryString = $paginationQueryString;
104
            return $this;
105
        }
106
107
108
109
        /**
110
         * Determine automatically the value of the pagination query string
111
         * Using the REQUEST URI
112
         * 
113
         * @return object
114
         */
115
        public function determinePaginationQueryStringValue(){
116
            $pageQueryName = $this->config['page_query_string_name'];
117
            $queryString = Url::queryString();
118
            $currentUrl = Url::current();
119
            $query = '';
120
             if ($queryString == ''){
121
                $query = '?' . $pageQueryName . '=';
122
            }
123
            else{
124
                $tab = explode($pageQueryName . '=', $queryString);
125
                $nb = count($tab);
126
                if ($nb == 1){
127
                    $query = '?' . $queryString . '&' . $pageQueryName . '=';
128
                }
129
                else{
130
                    if ($tab[0] == ''){
131
                        $query = '?' . $pageQueryName . '=';
132
                    }
133
                    else{
134
                        $query = '?' . $tab[0] . '' . $pageQueryName . '=';
135
                    }
136
                }
137
            }
138
            $temp = explode('?', $currentUrl);
139
            $query = $temp[0] . $query;
140
            $this->paginationQueryString = $query;
141
            return $this;
142
        }
143
144
        /**
145
         * Generate the pagination link
146
         * @param  int $totalRows the total number of data
147
         * @param  int $currentPageNumber the current page number
148
         * 
149
         * @return string the pagination link
150
         */
151
        public function getLink($totalRows, $currentPageNumber){
152
            $numberOfLink = $this->config['nb_link'];
153
			$numberOfRowPerPage = $this->config['pagination_per_page'];
154
            if (empty($this->paginationQueryString)){
155
                //determine the pagination query string value
156
                $this->determinePaginationQueryStringValue();
157
            }
158
            $query = $this->paginationQueryString;
0 ignored issues
show
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
159
            //************************************
160
            $navbar = '';
161
            $numberOfPage = ceil($totalRows / $numberOfRowPerPage);
162
            $currentPageNumber = (int) $currentPageNumber;
163
            $numberOfLink = (int) $numberOfLink;
164
            $numberOfRowPerPage = (int) $numberOfRowPerPage;
165
			
166
            if ($currentPageNumber <= 0){
167
				$currentPageNumber = 1;
168
			}
169
            if ($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0) {
170
                return $navbar;
171
            }
172
            $start = null;
173
            $end = null;
174
            $begin = null;
175
            if ($numberOfLink % 2 == 0){
176
                $start = $currentPageNumber - ($numberOfLink / 2) + 1;
177
                $end = $currentPageNumber + ($numberOfLink / 2);
178
            }
179
            else{
180
                $start = $currentPageNumber - floor($numberOfLink / 2);
181
                $end = $currentPageNumber + floor($numberOfLink / 2);
182
            }
183
            if ($start <= 1){
184
                $begin = 1;
185
                $end = $numberOfLink;
186
            }
187
            else if ($start > 1 && $end < $numberOfPage){
188
                $begin = $start;
189
                $end = $end;
190
            }
191
            else{
192
                $begin = ($numberOfPage - $numberOfLink) + 1;
193
                $end = $numberOfPage;
194
            }
195
            if ($numberOfPage <= $numberOfLink){
196
                $begin = 1;
197
                $end = $numberOfPage;
198
            }
199
200
            //**
201
            if ($currentPageNumber == 1){
202
                $navbar .= $this->buildPaginationLinkForFirstPage($begin, $end, $currentPageNumber);
0 ignored issues
show
Bug introduced by
It seems like $end can also be of type double; however, parameter $end of Pagination::buildPaginationLinkForFirstPage() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

202
                $navbar .= $this->buildPaginationLinkForFirstPage($begin, /** @scrutinizer ignore-type */ $end, $currentPageNumber);
Loading history...
Bug introduced by
It seems like $begin can also be of type double; however, parameter $begin of Pagination::buildPaginationLinkForFirstPage() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

202
                $navbar .= $this->buildPaginationLinkForFirstPage(/** @scrutinizer ignore-type */ $begin, $end, $currentPageNumber);
Loading history...
203
            }
204
            ///////////
205
            else if ($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){
206
                $navbar .= $this->buildPaginationLinkForMiddlePage($begin, $end, $currentPageNumber);
0 ignored issues
show
Bug introduced by
It seems like $end can also be of type double; however, parameter $end of Pagination::buildPaginationLinkForMiddlePage() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

206
                $navbar .= $this->buildPaginationLinkForMiddlePage($begin, /** @scrutinizer ignore-type */ $end, $currentPageNumber);
Loading history...
Bug introduced by
It seems like $begin can also be of type double; however, parameter $begin of Pagination::buildPaginationLinkForMiddlePage() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

206
                $navbar .= $this->buildPaginationLinkForMiddlePage(/** @scrutinizer ignore-type */ $begin, $end, $currentPageNumber);
Loading history...
207
            }
208
            else if ($currentPageNumber == $numberOfPage){
209
               $navbar .= $this->buildPaginationLinkForLastPage($begin, $end, $currentPageNumber);
0 ignored issues
show
Bug introduced by
It seems like $end can also be of type double; however, parameter $end of Pagination::buildPaginationLinkForLastPage() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

209
               $navbar .= $this->buildPaginationLinkForLastPage($begin, /** @scrutinizer ignore-type */ $end, $currentPageNumber);
Loading history...
Bug introduced by
It seems like $begin can also be of type double; however, parameter $begin of Pagination::buildPaginationLinkForLastPage() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

209
               $navbar .= $this->buildPaginationLinkForLastPage(/** @scrutinizer ignore-type */ $begin, $end, $currentPageNumber);
Loading history...
210
            }
211
            $navbar = $this->config['pagination_open'] . $navbar . $this->config['pagination_close'];
212
            return $navbar;
213
        }
214
215
        /**
216
         * Build the pagination link for the first page
217
         * @param  int $begin             the pagination begin number
218
         * @param  int $end               the pagination end number
219
         * @param  int $currentPageNumber the pagination current page number
220
         * @return string                    
221
         */
222
        protected function buildPaginationLinkForFirstPage($begin, $end, $currentPageNumber){
223
            $navbar = null;
224
            $query = $this->paginationQueryString;
225
            for($i = $begin; $i <= $end; $i++){
226
                if ($i == $currentPageNumber){
227
                    $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close'];
228
                }
229
                else{
230
                    $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close'];
231
                }
232
            }
233
            $navbar .= $this->config['next_open'] . '<a href="' . $query . ($currentPageNumber + 1) . '">' . $this->config['next_text'] . '</a>' . $this->config['next_close'];
234
            return $navbar;
235
        }
236
237
        /**
238
         * Build the pagination link for the page in the middle
239
         * @param  int $begin             the pagination begin number
240
         * @param  int $end               the pagination end number
241
         * @param  int $currentPageNumber the pagination current page number
242
         * @return string                    
243
         */
244
        protected function buildPaginationLinkForMiddlePage($begin, $end, $currentPageNumber){
245
            $navbar = null;
246
            $query = $this->paginationQueryString;
247
            $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close'];
248
            for($i = $begin; $i <= $end; $i++){
249
                if ($i == $currentPageNumber){
250
                    $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close'];
251
                }
252
                else{
253
                    $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close'];
254
                }
255
            }
256
            $navbar .= $this->config['next_open']."<a href='$query".($currentPageNumber + 1)."'>".$this->config['next_text']."</a>".$this->config['next_close'];
257
            return $navbar;
258
        }
259
260
        /**
261
         * Build the pagination link for the last page
262
         * @param  int $begin             the pagination begin number
263
         * @param  int $end               the pagination end number
264
         * @param  int $currentPageNumber the pagination current page number
265
         * @return string                    
266
         */
267
        protected function buildPaginationLinkForLastPage($begin, $end, $currentPageNumber){
268
            $navbar = null;
269
            $query = $this->paginationQueryString;
270
            $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close'];
271
            for($i = $begin; $i <= $end; $i++){
272
                if ($i == $currentPageNumber){
273
                    $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close'];
274
                }
275
                else{
276
                    $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close'];
277
                }
278
            }
279
            return $navbar;
280
        }
281
    }
282