Test Failed
Push — master ( 80c82f...ec79a0 )
by Daniel
02:31
created

DomPaginationByDanielGP::setArrayToSelect()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 4
dl 0
loc 16
rs 9.7333
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
        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
     * Builds a <select> based on a given array
67
     *
68
     * @version 20080618
69
     * @param array $aElements
70
     * @param mixed $sDefaultValue
71
     * @param string $selectName
72
     * @param array $featArray
73
     * @return string
74
     */
75
    protected function setArrayToSelect($aElements, $sDefaultValue, $selectName, $featArray = null)
76
    {
77
        if (!is_array($aElements)) {
0 ignored issues
show
introduced by
The condition is_array($aElements) is always true.
Loading history...
78
            return '';
79
        }
80
        if (isset($featArray['readonly'])) {
81
            $inputFeatures = [
82
                'name'     => $selectName,
83
                'id'       => $this->buildSelectId($selectName, $featArray),
84
                'readonly' => 'readonly',
85
                'class'    => 'input_readonly',
86
                'value'    => $sDefaultValue,
87
            ];
88
            return $this->setStringIntoShortTag('input', $inputFeatures) . $aElements[$sDefaultValue];
89
        }
90
        return $this->setArrayToSelectNotReadOnly($aElements, $sDefaultValue, $selectName, $featArray);
91
    }
92
93
    /**
94
     * Converts an array to string
95
     *
96
     * @param string $sSeparator
97
     * @param array $aElements
98
     * @return string
99
     */
100
    protected function setArrayToStringForUrl($sSeparator, $aElements, $aExceptedElements = [''])
101
    {
102
        $outArray = $this->normalizeArrayForUrl($aElements);
103
        if (count($outArray) < 1) {
104
            return '';
105
        }
106
        $xptArray   = $this->normalizeArrayForUrl($aExceptedElements);
107
        $finalArray = array_diff_key($outArray, $xptArray);
108
        return http_build_query($finalArray, '', $sSeparator);
109
    }
110
111
    /**
112
     * Returns a pagination bar
113
     *
114
     * @param int $iCrtPgNo
115
     * @param int $inRecPrPg
116
     * @param int $iAllRec
117
     * @param boolean $bKpFlPg
118
     * returns string
119
     */
120
    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

120
    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...
121
    {
122
        $sReturn             = null;
123
        $iRecPrPg            = min($iRecPrPg, $iAllRec);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $iRecPrPg seems to be never defined.
Loading history...
124
        $iStartingPageRecord = $this->setStartingPageRecord($iCrtPgNo, $iRecPrPg, $iAllRec, $bKpFlPg);
125
        $sReturn             .= '<span style="float:left;font-size:smaller;margin-top:1px; margin-right:1px;">'
126
            . $this->setStringIntoTag($iAllRec, 'b')
127
            . $this->lclMsgCmn('i18n_RecordsAvailableNowDisplaying')
128
            . $this->setStringIntoTag(($iStartingPageRecord + 1), 'b')
129
            . ' - ' . $this->setStringIntoTag(min($iAllRec, ($iStartingPageRecord + $iRecPrPg)), 'b')
130
            . ' </span>';
131
        switch ($iCrtPgNo) {
132
            case 'first':
133
                $iCrtPgNo = ceil(($iStartingPageRecord + 1 ) / $iRecPrPg);
134
                break;
135
            case 'last':
136
                $iCrtPgNo = ceil($iAllRec / $iRecPrPg);
137
                break;
138
        }
139
        $sReturn              .= '<span style="float:right;font-size:smaller;margin-top:1px; margin-right:1px;">';
140
        $iNumberOfPages       = ceil($iAllRec / $iRecPrPg);
141
        $sAdditionalArguments = '';
142
        if (isset($_GET)) {
143
            if ($_GET != ['page' => @$_GET['page']]) {
144
                $sAdditionalArguments = '&amp;'
145
                    . $this->setArrayToStringForUrl('&amp;', $_GET, ['page', 'action', 'server_action']);
146
            }
147
            if (isset($_GET['page'])) {
148
                $iCrtPgNo = $_GET['page'];
149
            }
150
        }
151
        if ($iCrtPgNo != 1) {
152
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Previous'), 'a', [
153
                'href'  => ('?page=' . ($iCrtPgNo - 1 ) . $sAdditionalArguments ),
154
                'class' => 'pagination'
155
            ]);
156
        } else {
157
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Previous'), 'span', [
158
                'class' => 'pagination_inactive'
159
            ]);
160
        }
161
        $pages2display = [];
162
        for ($counter = 1; $counter <= $iNumberOfPages; $counter++) {
163
            $pages2display[$counter] = $counter;
164
        }
165
        $sReturn .= '<span class="pagination"><form method="get" action="' . $_SERVER['SCRIPT_NAME'] . '">';
166
        $sReturn .= $this->setArrayToSelect($pages2display, @$_REQUEST['page']
167
            , 'page', ['size' => 1, 'autosubmit', 'id_no' => mt_rand()]);
168
        if (isset($_GET)) {
169
            foreach ($_GET as $key => $value) {
170
                if ($key != 'page') {
171
                    if (is_array($value)) {
172
                        foreach ($value as $value2) {
173
                            $sReturn .= $this->setStringIntoShortTag('input', [
174
                                'type'  => 'hidden',
175
                                'name'  => $key . '[]',
176
                                'value' => $value2,
177
                            ]);
178
                        }
179
                    } else {
180
                        $sReturn .= $this->setStringIntoShortTag('input', [
181
                            'type'  => 'hidden',
182
                            'name'  => $key,
183
                            'value' => $value,
184
                        ]);
185
                    }
186
                }
187
            }
188
        }
189
        $sReturn .= '</form></span>';
190
        if ($iCrtPgNo != $iNumberOfPages) {
191
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Next'), 'a', [
192
                'href'  => ('?page=' . ($iCrtPgNo + 1 ) . $sAdditionalArguments ),
193
                'class' => 'pagination',
194
            ]);
195
        } else {
196
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Next'), 'span', [
197
                'class' => 'pagination_inactive',
198
            ]);
199
        }
200
        $sReturn .= '</span>';
201
        return $sReturn;
202
    }
203
204
    /**
205
     * Returns starting records for LIMIT clause on SQL interrogation
206
     *
207
     * @version 20080521
208
     * @param string $sDefaultPageNo
209
     * @param int $iRecordsPerPage
210
     * @param int $iAllRecords
211
     * @param boolean $bKeepFullPage
212
     * @return int
213
     */
214
    private function setStartingPageRecord($sDefaultPageNo, $iRecordsPerPage
215
        , $iAllRecords, $bKeepFullPage = true)
216
    {
217
        if (isset($_REQUEST['page'])) {
218
            $iStartingPageRecord = ($_REQUEST['page'] - 1 ) * $iRecordsPerPage;
219
        } else {
220
            switch ($sDefaultPageNo) {
221
                case 'last':
222
                    $iStartingPageRecord = $iAllRecords - $iRecordsPerPage;
223
                    break;
224
                case 'first':
225
                default:
226
                    $iStartingPageRecord = 0;
227
                    break;
228
            }
229
        }
230
        if (($bKeepFullPage ) && (($iStartingPageRecord + $iRecordsPerPage ) > $iAllRecords)) {
231
            $iStartingPageRecord = $iAllRecords - $iRecordsPerPage;
232
        }
233
        return max(0, $iStartingPageRecord);
234
    }
235
236
}
237