DomPaginationByDanielGP   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 112
dl 0
loc 203
rs 9.92
c 0
b 0
f 0
wmc 31

6 Methods

Rating   Name   Duplication   Size   Complexity  
D setPagination() 0 86 14
A normalizeFeatureArray() 0 10 3
A setArrayToSelect() 0 16 3
A setStartingPageRecord() 0 19 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
     * Builds a <select> based on a given array
67
     *
68
     * @version 20080618
69
     * @param array|null $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)) {
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)
121
    {
122
        $sReturn             = null;
123
        $iRecPrPg            = min($inRecPrPg, $iAllRec);
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 (!is_null($this->tCmnSuperGlobals->get('page'))) {
148
                $iCrtPgNo = $this->tCmnSuperGlobals->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="'
166
            . $this->tCmnSuperGlobals->getScriptName() . '">';
167
        $sReturn .= $this->setArrayToSelect($pages2display, $this->tCmnSuperGlobals->get('page'), 'page', [
168
            'size'  => 1,
169
            'autosubmit',
170
            'id_no' => mt_rand()
171
        ]);
172
        if (isset($_GET)) {
173
            foreach ($_GET as $key => $value) {
174
                if ($key != 'page') {
175
                    if (is_array($value)) {
176
                        foreach ($value as $value2) {
177
                            $sReturn .= $this->setStringIntoShortTag('input', [
178
                                'type'  => 'hidden',
179
                                'name'  => $key . '[]',
180
                                'value' => $value2,
181
                            ]);
182
                        }
183
                    } else {
184
                        $sReturn .= $this->setStringIntoShortTag('input', [
185
                            'type'  => 'hidden',
186
                            'name'  => $key,
187
                            'value' => $value,
188
                        ]);
189
                    }
190
                }
191
            }
192
        }
193
        $sReturn .= '</form></span>';
194
        if ($iCrtPgNo != $iNumberOfPages) {
195
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Next'), 'a', [
196
                'href'  => ('?page=' . ($iCrtPgNo + 1 ) . $sAdditionalArguments ),
197
                'class' => 'pagination',
198
            ]);
199
        } else {
200
            $sReturn .= $this->setStringIntoTag($this->lclMsgCmn('i18n_Next'), 'span', [
201
                'class' => 'pagination_inactive',
202
            ]);
203
        }
204
        $sReturn .= '</span>';
205
        return $sReturn;
206
    }
207
208
    /**
209
     * Returns starting records for LIMIT clause on SQL interrogation
210
     *
211
     * @version 20080521
212
     * @param string $sDefaultPageNo
213
     * @param int $iRecordsPerPage
214
     * @param int $iAllRecords
215
     * @param boolean $bKeepFullPage
216
     * @return int
217
     */
218
    private function setStartingPageRecord($sDefaultPageNo, $iRecordsPerPage, $iAllRecords, $bKeepFullPage = true)
219
    {
220
        if (is_null($this->tCmnSuperGlobals->get('page'))) {
221
            switch ($sDefaultPageNo) {
222
                case 'last':
223
                    $iStartingPageRecord = $iAllRecords - $iRecordsPerPage;
224
                    break;
225
                case 'first':
226
                default:
227
                    $iStartingPageRecord = 0;
228
                    break;
229
            }
230
        } else {
231
            $iStartingPageRecord = ($this->tCmnSuperGlobals->get('page') - 1 ) * $iRecordsPerPage;
232
        }
233
        if (($bKeepFullPage ) && (($iStartingPageRecord + $iRecordsPerPage ) > $iAllRecords)) {
234
            $iStartingPageRecord = $iAllRecords - $iRecordsPerPage;
235
        }
236
        return max(0, $iStartingPageRecord);
237
    }
238
}
239