Pages   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 378
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 378
rs 9.2
c 0
b 0
f 0
wmc 34
lcom 1
cbo 4

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
B navigation() 0 36 6
B _light() 0 31 4
B _full() 0 29 4
B _middle() 0 27 5
B _group() 0 34 5
B _arrows() 0 43 4
A _build() 0 23 2
A offset() 0 6 1
A params() 0 6 1
1
<?php
2
3
/**
4
 * Generate page turn links
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Pagination
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\pagination;
17
18
/**
19
 * Generate page turn links
20
 *
21
 * @category  Core
22
 * @package   Pagination
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Pages
30
{
31
    /**
32
     * Plugin name
33
     **/
34
    private $_plugin;
35
36
    /**
37
     * Action name
38
     **/
39
    private $_action;
40
41
    /**
42
     * Total amount of entries
43
     **/
44
    private $_total;
45
46
    /**
47
     * Limit of entries on a page
48
     **/
49
    private $_limit;
50
51
    /**
52
     * Page to start from
53
     **/
54
    private $_start;
55
56
    /**
57
     * Additional parameters for url
58
     **/
59
    private $_params = [];
60
61
    /**
62
     * Start a new page generator
63
     *
64
     * @param string $plugin Plugin name
65
     * @param string $action Action name
66
     * @param string $total  Amount of entries
67
     * @param string $limit  Limit of entries per page
68
     *
69
     * @return \csphere\core\pagination\Pages
70
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
71
72
    public function __construct($plugin, $action, $total, $limit)
73
    {
74
        // Store settings
75
        $this->_plugin = $plugin;
76
        $this->_action = $action;
77
        $this->_total  = (int)$total;
78
        $this->_limit  = (int)$limit;
79
80
        // Get start page and make sure its above zero
81
        $this->_start = (int)\csphere\core\http\Input::get('get', 'page');
82
83
        if ($this->_start < 1) {
84
85
            $this->_start = 1;
86
        }
87
    }
88
89
    /**
90
     * Generate page turn links
91
     *
92
     * @param boolean $light  Light version with only last, current and next page
93
     * @param boolean $arrows Whether or not to attach arrows for page turns
94
     *
95
     * @return string
96
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
97
98
    public function navigation($light = false, $arrows = true)
99
    {
100
        $data = [];
101
102
        // Set amount of pages and build page navigation
103
        $pages = ceil($this->_total / $this->_limit);
104
105
        // Use full or light mode
106
        if ($light == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
107
108
            $data['groups'] = $this->_light($pages);
109
110
        } else {
111
112
            $end = 3;
113
114
            // If there are very few pages end earlier or skip groups
115
            if ($end > $pages || $pages < 10) {
116
117
                $end = $pages;
118
119
            } elseif ($this->_start > 2 && $this->_start < 6) {
120
121
                $end = $this->_start + 1;
122
            }
123
124
            $data['groups'] = $this->_full($pages, $end);
125
        }
126
127
        // Generate template output
128
        $data['pages'] = $pages;
129
130
        $result = $this->_build($data, $arrows);
131
132
        return $result;
133
    }
134
135
    /**
136
     * Generate light layout
137
     *
138
     * @param integer $pages Amount of pages
139
     *
140
     * @return array
141
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
142
143
    private function _light($pages)
144
    {
145
        $groups = [];
146
147
        // Use current page as default
148
        $next = $this->_start - 1;
149
        $end  = $next + 2;
150
151
        // Handle special cases
152
        if ($this->_start == 1) {
153
154
            $next = 1;
155
            $end  = $next + 2;
156
        }
157
158
        if ($pages < 3) {
159
160
            $next = 1;
161
            $end  = $pages;
162
163
        } elseif ($this->_start >= $pages) {
164
165
            $next = $pages - 2;
166
            $end  = $pages;
167
        }
168
169
        $first    = $this->_group($next, $end);
170
        $groups[] = ['links' => $first, 'space' => 'no'];
171
172
        return $groups;
173
    }
174
175
    /**
176
     * Generate full layout
177
     *
178
     * @param integer $pages Amount of pages
179
     * @param integer $end   End of first group
180
     *
181
     * @return array
182
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
183
184
    private function _full($pages, $end)
185
    {
186
        $groups = [];
187
188
        // Generate first group of links
189
        $first    = $this->_group(1, $end);
190
        $groups[] = ['links' => $first, 'space' => 'no'];
191
192
        // Generate second group of links
193
        if ($pages > $end && $end < 4) {
194
195
            $middle = $this->_middle($pages);
196
            $end    = $middle['end'];
197
198
            $second   = $this->_group($middle['next'], $end);
199
            $groups[] = ['links' => $second, 'space' => 'yes'];
200
        }
201
202
        // Generate third group of links
203
        if ($pages > $end) {
204
205
            $next = $pages - 2;
206
207
            $third    = $this->_group($next, $pages);
208
            $groups[] = ['links' => $third, 'space' => 'yes'];
209
        }
210
211
        return $groups;
212
    }
213
214
    /**
215
     * Determine page start and end for middle part of full layout
216
     *
217
     * @param integer $pages Amount of pages
218
     *
219
     * @return array
220
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
221
222
    private function _middle($pages)
223
    {
224
        // Use current page as default
225
        $next = floor($pages / 2);
226
        $end  = $next + 2;
227
228
        // Handle special cases
229
        if ($this->_start > 5 && $this->_start < ($pages - 1)) {
230
231
            $next = $this->_start - 1;
232
            $end  = $next + 2;
233
234
            if ($this->_start > ($pages - 5)) {
235
236
                $next = $this->_start - 1;
237
                $end  = $pages;
238
239
                if ($next == ($pages - 1)) {
240
241
                    $next--;
242
                }
243
            }
244
        }
245
246
        // Build array for group method
247
        return ['next' => $next, 'end' => $end];
248
    }
249
250
    /**
251
     * Generate a group of links
252
     *
253
     * @param integer $start Start page
254
     * @param integer $end   Last page
255
     *
256
     * @return array
257
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
258
259
    private function _group($start, $end)
260
    {
261
        $params = $this->_params;
262
        $links  = [];
263
264
        // Display at least one page on start
265
        if ($start == 1 && $end < 1) {
266
267
            $end = 1;
268
        }
269
270
        // Create and concat all links
271
        for ($i = $start; $i <= $end; $i++) {
272
273
            // Check if link is to current page
274
            if ($i == $this->_start) {
275
276
                $links[] = ['page' => $i, 'href' => '', 'active' => 'yes'];
277
278
            } else {
279
280
                // Build href for link
281
                $params['page'] = $i;
282
283
                $href = \csphere\core\url\Link::href(
284
                    $this->_plugin, $this->_action, $params
285
                );
286
287
                $links[] = ['page' => $i, 'href' => $href, 'active' => 'no'];
288
            }
289
        }
290
291
        return $links;
292
    }
293
294
    /**
295
     * Generate arrows for navigation
296
     *
297
     * @param integer $pages Amount of pages
298
     *
299
     * @return array
300
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
301
302
    private function _arrows($pages)
303
    {
304
        $params = $this->_params;
305
        $arrows = [];
306
307
        // Add << and < arrows
308
        $arrows['first'] = 1;
309
310
        $prev = $this->_start - 1;
311
312
        if ($this->_start < 2) {
313
314
            $prev = 1;
315
        }
316
317
        $arrows['previous'] = $prev;
318
319
        // Add > and >> arrows
320
        $next = $this->_start + 1;
321
322
        if ($next > $pages) {
323
324
            $next = $pages;
325
        }
326
327
        $arrows['next'] = $next;
328
329
        $arrows['last'] = $pages;
330
331
        // Change arrows to links
332
        foreach ($arrows AS $name => $page) {
333
334
            $params['page'] = $page;
335
336
            $href = \csphere\core\url\Link::href(
337
                $this->_plugin, $this->_action, $params
338
            );
339
340
            $arrows[$name] = $href;
341
        }
342
343
        return $arrows;
344
    }
345
346
    /**
347
     * Create content from template file
348
     *
349
     * @param array   $data   Data array for template
350
     * @param boolean $arrows Whether or not to attach arrows for page turns
351
     *
352
     * @return string
353
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
354
355
    private function _build(array $data, $arrows)
356
    {
357
        $data['arrow'] = ['show' => 'no'];
358
359
        // Add links for arrows if requested
360
        if ($arrows == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
361
362
            $data['arrow'] = $this->_arrows((int)$data['pages']);
363
364
            $data['arrow']['show'] = 'yes';
365
        }
366
367
368
        // Get view driver object
369
        $loader = \csphere\core\service\Locator::get();
370
        $view = $loader->load('view');
371
372
        // Send data to view and fetch box result
373
        $view->template('default', 'core_pages', $data, true);
374
        $result = $view->box();
375
376
        return $result;
377
    }
378
379
    /**
380
     * Get offset e.g. for database queries
381
     *
382
     * @return integer
383
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
384
385
    public function offset()
386
    {
387
        $offset = ($this->_start - 1) * $this->_limit;
388
389
        return $offset;
390
    }
391
392
    /**
393
     * Set additional parameters
394
     *
395
     * @param array $params Parameters as an array of key value pairs
396
     *
397
     * @return boolean
398
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
399
400
    public function params(array $params)
401
    {
402
        $this->_params = $params;
403
404
        return true;
405
    }
406
}
407