Passed
Push — master ( cb2842...994f5c )
by Patrick
02:04
created

TrapDirectorTablePaging::curPagingQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Icinga\Module\Trapdirector\Tables;
4
5
6
trait TrapDirectorTablePaging
7
{
8
   
9
    /*************** Paging *************/
10
    protected $maxPerPage = 25;
11
    
12
    protected $currentPage = 0;
13
        
14
 
15
    /*****************  Paging and counting *********/
16
    
17
    public function countQuery()
18
    {
19
        $this->query = $this->dbConn->select();
0 ignored issues
show
Bug Best Practice introduced by
The property query does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
        $this->query = $this->query
21
            ->from(
22
                $this->table,
23
                array('COUNT(*)')
24
                );
25
        $this->applyFilter();                   
0 ignored issues
show
Bug introduced by
It seems like applyFilter() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

25
        $this->/** @scrutinizer ignore-call */ 
26
               applyFilter();                   
Loading history...
26
    }
27
    
28
    public function count()
29
    {
30
        $this->countQuery();
31
        return $this->dbConn->fetchOne($this->query);
32
    }
33
    
34
    public function setMaxPerPage(int $max)
35
    {
36
        $this->maxPerPage = $max;
37
    }
38
    
39
    protected function getPagingQuery(array $getVars)
40
    {
41
        if (isset($getVars['page']))
42
        {
43
            $this->currentPage = $getVars['page'];
44
        }
45
    }
46
47
    protected function curPagingQuery()
48
    {
49
        if ($this->currentPage == '') return '';
50
        return 'page='.$this->currentPage;
51
    }
52
    
53
    public function renderPagingHeader()
54
    {
55
        $count = $this->count();
56
        if ($count <= $this->maxPerPage )
57
        {
58
            return  'count : ' . $this->count() . '<br>';
59
        }
60
        
61
        if ($this->currentPage == 0) $this->currentPage = 1;
62
        
63
        $numPages = intdiv($count , $this->maxPerPage);
64
        if ($count % $this->maxPerPage != 0 ) $numPages++;
65
        
66
        $html = '<div class="pagination-control" role="navigation">';
67
        $html .= '<ul class="nav tab-nav">';
68
        if ($this->currentPage <=1)
69
        {
70
            $html .= '
71
                <li class="nav-item disabled" aria-hidden="true">
72
                    <span class="previous-page">
73
                            <span class="sr-only">Previous page</span>
74
                            <i aria-hidden="true" class="icon-angle-double-left"></i>            
75
                     </span>
76
                </li>
77
               ';
78
        }
79
        else 
80
        {
81
            $html .= '
82
                <li class="nav-item">
83
                    <a href="'. $this->getCurrentURLAndQS('paging') .'&page='. ($this->currentPage - 1 ).'" class="previous-page" >
0 ignored issues
show
Bug introduced by
It seems like getCurrentURLAndQS() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

83
                    <a href="'. $this->/** @scrutinizer ignore-call */ getCurrentURLAndQS('paging') .'&page='. ($this->currentPage - 1 ).'" class="previous-page" >
Loading history...
84
                            <i aria-hidden="true" class="icon-angle-double-left"></i>            
85
                     </a>
86
                </li>
87
            ';
88
        }
89
        
90
        for ($i=1; $i <= $numPages ; $i++)
91
        {
92
            $active = ($this->currentPage == $i) ? 'active' : '';
93
            $first = ($i-1)*$this->maxPerPage+1;
94
            $last = $i * $this->maxPerPage;
95
            if ($last > $count) $last = $count;
96
            $display = 'Show rows '. $first . ' to '. $last .' of '. $count;
97
            $html .= '<li class="' . $active . ' nav-item">
98
                    <a href="'. $this->getCurrentURLAndQS('paging') .'&page='. $i .'" title="' . $display . '" aria-label="' . $display . '">
99
                    '.$i.'                
100
                    </a>
101
                </li>';
102
        }
103
        
104
        if ($this->currentPage == $numPages)
105
        {
106
            $html .= '
107
                <li class="nav-item disabled" aria-hidden="true">
108
                    <span class="previous-page">
109
                            <span class="sr-only">Previous page</span>
110
                            <i aria-hidden="true" class="icon-angle-double-right"></i>
111
                     </span>
112
                </li>
113
               ';
114
        }
115
        else
116
        {
117
            $html .= '
118
                <li class="nav-item">
119
                    <a href="'. $this->getCurrentURLAndQS('paging') .'&page='. ($this->currentPage + 1 ).'" class="next-page">
120
                            <i aria-hidden="true" class="icon-angle-double-right"></i>
121
                     </a>
122
                </li>
123
            ';
124
        }
125
        
126
        $html .= '</ul> </div>';
127
        
128
        return $html;
129
    }
130
    
131
    public function applyPaging()
132
    {
133
        $this->query->limitPage($this->currentPage,$this->maxPerPage);
134
        return $this;
135
    }
136
    
137
    
138
}