Passed
Push — master ( 286eca...ea0496 )
by Plamen
01:27
created

trait_paging::pagingJumps()   A

Complexity

Conditions 6
Paths 18

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 18
nop 2
dl 0
loc 21
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
trait trait_paging
4
{
5
    protected static function paging(&$v)
6
    {
7
        if (self::$t['pages'] > 1) {
8
            $v['pages'] = self::$t['pages'];
9
            $v['page'] = self::$t['page'];
10
            $v['jumpL'] = self::pagingJumps();
11
            $v['jumpR'] = self::pagingJumps();
12
            $v['arrowL'] = self::config('UTF8_LEFT_SYMBOL');
13
            $v['arrowR'] = self::config('UTF8_RIGHT_SYMBOL');
14
15
            $limit = 10;
16
17
            $v['start'] = $v['page'] > ($limit / 2) ?
18
                    ($v['page'] - $limit / 2) :
19
                    1;
20
21
            if ($v['page'] > ($v['pages'] - ($limit / 2))) {
22
                $v['final'] = $v['pages'];
23
            } else if ($v['page'] > ($limit / 2)) {
24
                $v['final'] = $v['start'] + $limit;
25
            } else {
26
                $v['final'] = $limit;
27
            }
28
        }
29
    }
30
31
    /** Jump links -1M|-100K|-10K|-1K|100|{paging}|100|+1K|+10K|+100K|+1M
32
     * @param string $html Code to show
33
     * @param null|int $m multiplier
34
     * @return string */
35
    protected static function pagingJumps($html = '', $m = null)
36
    {
37
        static $direction;
38
39
        if (is_null($m)) { //on self::createPaginationLinks() calls only
40
            $direction = !isset($direction) ? '-' : '+';
41
            $m = 100;
42
        }
43
        
44
        $add = '<li class="jump"><a>' . self::jumps($m, $direction) . '</a></li>';
45
46
        $jump_exists = $direction === '-' ?
47
                        (self::$t['page'] - $m) > 0 :
48
                        (self::$t['page'] + $m) <= self::$t['pages'];
49
50
        if ($jump_exists) {
51
            $html = $direction === '-' ? $add . $html : $html . $add;
52
            return self::pagingJumps($html, ($m * 10));
53
        }
54
55
        return $html;
56
    }
57
    
58
    private static function jumps($m, $direction)
59
    {
60
        if ($m >= 1000000) {
61
            return ($direction . ($m / 1000000) . "M");
62
        } else if ($m >= 1000) {
63
            return ($direction . ($m / 1000) . "K");
64
        } else {
65
            return ($direction . $m);
66
        }
67
    }
68
}
69