Test Failed
Push — master ( d0fe65...f5edd0 )
by Daniel
02:27
created

DomPaginationByDanielGP::setStartingPageRecord()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 4
dl 0
loc 20
rs 8.9777
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2018 Daniel Popiniuc
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace danielgp\common_lib;
28
29
/**
30
 * DOM component functions
31
 *
32
 * @author Daniel Popiniuc
33
 */
34
trait DomPaginationByDanielGP
35
{
36
37
    use DomBasicComponentsByDanielGP;
38
39
    /**
40
     * Returns a pagination bar
41
     *
42
     * @param int $iCrtPgNo
43
     * @param int $iRecPrPg
44
     * @param int $iAllRec
45
     * @param boolean $bKpFlPg
46
     * returns string
47
     */
48
    protected function setPagination($iCrtPgNo, $iRecPrPg, $iAllRec, $bKpFlPg = true)
49
    {
50
        $sReturn             = null;
51
        $iRecPrPg            = min($iRecPrPg, $iAllRec);
52
        $iStartingPageRecord = $this->setStartingPageRecord(
53
            $iCrtPgNo, $iRecPrPg, $iAllRec, $bKpFlPg);
54
        if (APP_INDICATIVE == 'cs') {
0 ignored issues
show
Bug introduced by
The constant danielgp\common_lib\APP_INDICATIVE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
            $prvMsg = 'Previous';
0 ignored issues
show
Unused Code introduced by
The assignment to $prvMsg is dead and can be removed.
Loading history...
56
        } else {
57
            $prvMsg = 'Anterioara';
58
        }
59
        $sReturn .= '<span style="float:left;font-size:smaller; '
60
            . 'margin-top:1px; margin-right:1px;">'
61
            . $this->setStringIntoTag($iAllRec, 'b')
62
            . $this->lclMsgCntrl('i18n_RecordsAvailableNowDisplaying')
0 ignored issues
show
Bug introduced by
The method lclMsgCntrl() does not exist on danielgp\common_lib\DomPaginationByDanielGP. Did you maybe mean lclMsgCmn()? ( Ignorable by Annotation )

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

62
            . $this->/** @scrutinizer ignore-call */ lclMsgCntrl('i18n_RecordsAvailableNowDisplaying')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
            . $this->setStringIntoTag(($iStartingPageRecord + 1), 'b')
64
            . ' - ' . $this->setStringIntoTag(
65
                min($iAllRec, ($iStartingPageRecord + $iRecPrPg)), 'b')
66
            . ' </span>';
67
        switch ($iCrtPgNo) {
68
            case 'first':
69
                $iCrtPgNo = ceil(($iStartingPageRecord + 1 ) / $iRecPrPg);
70
                break;
71
            case 'last':
72
                $iCrtPgNo = ceil($iAllRec / $iRecPrPg);
73
                break;
74
        }
75
        $sReturn              .= '<span style="float:right;font-size:smaller; '
76
            . 'margin-top:1px; margin-right:1px;">';
77
        $iNumberOfPages       = ceil($iAllRec / $iRecPrPg);
78
        $sAdditionalArguments = '';
79
        if (isset($_GET)) {
80
            if ($_GET != ['page' => @$_GET['page']]) {
81
                $sAdditionalArguments = '&amp;'
82
                    . $this->setArrayToStringForUrl('&amp;'
0 ignored issues
show
Bug introduced by
It seems like setArrayToStringForUrl() 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

82
                    . $this->/** @scrutinizer ignore-call */ setArrayToStringForUrl('&amp;'
Loading history...
83
                        , $_GET, ['page', 'action', 'server_action']);
84
            }
85
            if (isset($_GET['page'])) {
86
                $iCrtPgNo = $_GET['page'];
87
            }
88
        }
89
        if ($iCrtPgNo != 1) {
90
            $sReturn .= $this->setStringIntoTag($this->lclMsgCntrl('i18n_Previous'), 'a', [
91
                'href'  => ('?page=' . ($iCrtPgNo - 1 ) . $sAdditionalArguments ),
92
                'class' => 'pagination'
93
            ]);
94
        } else {
95
            $sReturn .= $this->setStringIntoTag($this->lclMsgCntrl('i18n_Previous'), 'span', [
96
                'class' => 'pagination_inactive'
97
            ]);
98
        }
99
        for ($counter = 1; $counter <= $iNumberOfPages; $counter++) {
100
            $pages2display[$counter] = $counter;
101
        }
102
        $sReturn .= '<span class="pagination"><form method="get" action="'
103
            . $_SERVER['SCRIPT_NAME'] . '">';
104
        $sReturn .= $this->setArrayToSelect($pages2display, @$_REQUEST['page']
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pages2display does not seem to be defined for all execution paths leading up to this point.
Loading history...
Bug introduced by
It seems like setArrayToSelect() 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

104
        $sReturn .= $this->/** @scrutinizer ignore-call */ setArrayToSelect($pages2display, @$_REQUEST['page']
Loading history...
105
            , 'page', ['size' => 1, 'autosubmit', 'id_no' => mt_rand()]);
106
        if (isset($_GET)) {
107
            foreach ($_GET as $key => $value) {
108
                if ($key != 'page') {
109
                    if (is_array($value)) {
110
                        foreach ($value as $value2) {
111
                            $sReturn .= $this->setStringIntoShortTag('input'
112
                                , ['type'  => 'hidden'
113
                                , 'name'  => $key . '[]'
114
                                , 'value' => $value2]);
115
                        }
116
                    } else {
117
                        $sReturn .= $this->setStringIntoShortTag('input'
118
                            , ['type'  => 'hidden', 'name'  => $key
119
                            , 'value' => $value]);
120
                    }
121
                }
122
            }
123
        }
124
        $sReturn .= '</form></span>';
125
        if ($iCrtPgNo != $iNumberOfPages) {
126
            $sReturn .= $this->setStringIntoTag($this->lclMsgCntrl('i18n_Next'), 'a', [
127
                'href'  => ('?page=' . ($iCrtPgNo + 1 ) . $sAdditionalArguments ),
128
                'class' => 'pagination'
129
            ]);
130
        } else {
131
            $sReturn .= $this->setStringIntoTag($this->lclMsgCntrl('i18n_Next'), 'span', [
132
                'class' => 'pagination_inactive'
133
            ]);
134
        }
135
        $sReturn .= '</span>';
136
        return $sReturn;
137
    }
138
139
    /**
140
     * Returns starting records for LIMIT clause on SQL interrogation
141
     *
142
     * @version 20080521
143
     * @param string $sDefaultPageNo
144
     * @param int $iRecordsPerPage
145
     * @param int $iAllRecords
146
     * @param boolean $bKeepFullPage
147
     * @return int
148
     */
149
    protected function setStartingPageRecord($sDefaultPageNo, $iRecordsPerPage
150
        , $iAllRecords, $bKeepFullPage = true)
151
    {
152
        if (isset($_REQUEST['page'])) {
153
            $iStartingPageRecord = ($_REQUEST['page'] - 1 ) * $iRecordsPerPage;
154
        } else {
155
            switch ($sDefaultPageNo) {
156
                case 'last':
157
                    $iStartingPageRecord = $iAllRecords - $iRecordsPerPage;
158
                    break;
159
                case 'first':
160
                default:
161
                    $iStartingPageRecord = 0;
162
                    break;
163
            }
164
        }
165
        if (($bKeepFullPage ) && (($iStartingPageRecord + $iRecordsPerPage ) > $iAllRecords)) {
166
            $iStartingPageRecord = $iAllRecords - $iRecordsPerPage;
167
        }
168
        return max(0, $iStartingPageRecord);
169
    }
170
171
}
172