Test Failed
Push — master ( 3fe29a...80c82f )
by Daniel
03:11
created

DomPaginationByDanielGP   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 172
rs 10
c 0
b 0
f 0
wmc 28

5 Methods

Rating   Name   Duplication   Size   Complexity  
D setPagination() 0 82 14
A normalizeFeatureArray() 0 10 3
B setStartingPageRecord() 0 20 6
A setArrayToStringForUrl() 0 9 2
A normalizeArrayForUrl() 0 11 3
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
        DomDynamicSelectByDanielGP;
39
40
    private function normalizeArrayForUrl($featArray)
41
    {
42
        $outArray = [];
43
        foreach ($featArray as $key => $value) {
44
            if (is_numeric($key)) {
45
                $outArray[$value] = 1;
46
            } else {
47
                $outArray[$key] = $value;
48
            }
49
        }
50
        return $outArray;
51
    }
52
53
    private function normalizeFeatureArray($featArray)
54
    {
55
        $nonAsociative = ['autosubmit', 'disabled', 'hidden', 'include_null', 'multiselect'];
56
        foreach ($featArray as $key => $value) {
57
            if (in_array($value, $nonAsociative)) {
58
                $featArray[$value] = $value;
59
                unset($featArray[$key]);
60
            }
61
        }
62
        return $featArray;
63
    }
64
65
    /**
66
     * Converts an array to string
67
     *
68
     * @param string $sSeparator
69
     * @param array $aElements
70
     * @return string
71
     */
72
    protected function setArrayToStringForUrl($sSeparator, $aElements, $aExceptedElements = [''])
73
    {
74
        $outArray = $this->normalizeArrayForUrl($aElements);
75
        if (count($outArray) < 1) {
76
            return '';
77
        }
78
        $xptArray   = $this->normalizeArrayForUrl($aExceptedElements);
79
        $finalArray = array_diff_key($outArray, $xptArray);
80
        return http_build_query($finalArray, '', $sSeparator);
81
    }
82
83
    /**
84
     * Returns a pagination bar
85
     *
86
     * @param int $iCrtPgNo
87
     * @param int $inRecPrPg
88
     * @param int $iAllRec
89
     * @param boolean $bKpFlPg
90
     * returns string
91
     */
92
    protected function setPagination($iCrtPgNo, $inRecPrPg, $iAllRec, $bKpFlPg = true)
0 ignored issues
show
Unused Code introduced by
The parameter $inRecPrPg is not used and could be removed. ( Ignorable by Annotation )

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

92
    protected function setPagination($iCrtPgNo, /** @scrutinizer ignore-unused */ $inRecPrPg, $iAllRec, $bKpFlPg = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        $sReturn             = null;
95
        $iRecPrPg            = min($iRecPrPg, $iAllRec);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $iRecPrPg seems to be never defined.
Loading history...
96
        $iStartingPageRecord = $this->setStartingPageRecord($iCrtPgNo, $iRecPrPg, $iAllRec, $bKpFlPg);
97
        $sReturn             .= '<span style="float:left;font-size:smaller;margin-top:1px; margin-right:1px;">'
98
            . $this->setStringIntoTag($iAllRec, 'b')
99
            . $this->lclMsgCmn('i18n_RecordsAvailableNowDisplaying')
100
            . $this->setStringIntoTag(($iStartingPageRecord + 1), 'b')
101
            . ' - ' . $this->setStringIntoTag(min($iAllRec, ($iStartingPageRecord + $iRecPrPg)), 'b')
102
            . ' </span>';
103
        switch ($iCrtPgNo) {
104
            case 'first':
105
                $iCrtPgNo = ceil(($iStartingPageRecord + 1 ) / $iRecPrPg);
106
                break;
107
            case 'last':
108
                $iCrtPgNo = ceil($iAllRec / $iRecPrPg);
109
                break;
110
        }
111
        $sReturn              .= '<span style="float:right;font-size:smaller;margin-top:1px; margin-right:1px;">';
112
        $iNumberOfPages       = ceil($iAllRec / $iRecPrPg);
113
        $sAdditionalArguments = '';
114
        if (isset($_GET)) {
115
            if ($_GET != ['page' => @$_GET['page']]) {
116
                $sAdditionalArguments = '&amp;'
117
                    . $this->setArrayToStringForUrl('&amp;', $_GET, ['page', 'action', 'server_action']);
118
            }
119
            if (isset($_GET['page'])) {
120
                $iCrtPgNo = $_GET['page'];
121
            }
122
        }
123
        if ($iCrtPgNo != 1) {
124
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Previous'), 'a', [
125
                'href'  => ('?page=' . ($iCrtPgNo - 1 ) . $sAdditionalArguments ),
126
                'class' => 'pagination'
127
            ]);
128
        } else {
129
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Previous'), 'span', [
130
                'class' => 'pagination_inactive'
131
            ]);
132
        }
133
        $pages2display = [];
134
        for ($counter = 1; $counter <= $iNumberOfPages; $counter++) {
135
            $pages2display[$counter] = $counter;
136
        }
137
        $sReturn .= '<span class="pagination"><form method="get" action="' . $_SERVER['SCRIPT_NAME'] . '">';
138
        $sReturn .= $this->setArrayToSelect($pages2display, @$_REQUEST['page']
0 ignored issues
show
Bug introduced by
The method setArrayToSelect() does not exist on danielgp\common_lib\DomPaginationByDanielGP. Did you maybe mean setArrayToSelectNotReadOnly()? ( Ignorable by Annotation )

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

138
        $sReturn .= $this->/** @scrutinizer ignore-call */ setArrayToSelect($pages2display, @$_REQUEST['page']

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...
139
            , 'page', ['size' => 1, 'autosubmit', 'id_no' => mt_rand()]);
140
        if (isset($_GET)) {
141
            foreach ($_GET as $key => $value) {
142
                if ($key != 'page') {
143
                    if (is_array($value)) {
144
                        foreach ($value as $value2) {
145
                            $sReturn .= $this->setStringIntoShortTag('input', [
146
                                'type'  => 'hidden',
147
                                'name'  => $key . '[]',
148
                                'value' => $value2,
149
                            ]);
150
                        }
151
                    } else {
152
                        $sReturn .= $this->setStringIntoShortTag('input', [
153
                            'type'  => 'hidden',
154
                            'name'  => $key,
155
                            'value' => $value,
156
                        ]);
157
                    }
158
                }
159
            }
160
        }
161
        $sReturn .= '</form></span>';
162
        if ($iCrtPgNo != $iNumberOfPages) {
163
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Next'), 'a', [
164
                'href'  => ('?page=' . ($iCrtPgNo + 1 ) . $sAdditionalArguments ),
165
                'class' => 'pagination',
166
            ]);
167
        } else {
168
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Next'), 'span', [
169
                'class' => 'pagination_inactive',
170
            ]);
171
        }
172
        $sReturn .= '</span>';
173
        return $sReturn;
174
    }
175
176
    /**
177
     * Returns starting records for LIMIT clause on SQL interrogation
178
     *
179
     * @version 20080521
180
     * @param string $sDefaultPageNo
181
     * @param int $iRecordsPerPage
182
     * @param int $iAllRecords
183
     * @param boolean $bKeepFullPage
184
     * @return int
185
     */
186
    private function setStartingPageRecord($sDefaultPageNo, $iRecordsPerPage
187
        , $iAllRecords, $bKeepFullPage = true)
188
    {
189
        if (isset($_REQUEST['page'])) {
190
            $iStartingPageRecord = ($_REQUEST['page'] - 1 ) * $iRecordsPerPage;
191
        } else {
192
            switch ($sDefaultPageNo) {
193
                case 'last':
194
                    $iStartingPageRecord = $iAllRecords - $iRecordsPerPage;
195
                    break;
196
                case 'first':
197
                default:
198
                    $iStartingPageRecord = 0;
199
                    break;
200
            }
201
        }
202
        if (($bKeepFullPage ) && (($iStartingPageRecord + $iRecordsPerPage ) > $iAllRecords)) {
203
            $iStartingPageRecord = $iAllRecords - $iRecordsPerPage;
204
        }
205
        return max(0, $iStartingPageRecord);
206
    }
207
208
}
209