Completed
Pull Request — develop (#716)
by Agel_Nash
06:39
created

Paginate   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getPagingArray() 0 27 4
A getPagingRowArray() 0 16 3
A getNumberOfPage() 0 4 1
A getCurrentPage() 0 6 1
1
<?php namespace EvolutionCMS\Support;
2
3
use EvolutionCMS\Interfaces\PaginginateInterface;
4
5
// =====================================================
6
// FILE: Paging.php
7
//
8
// =====================================================
9
// Description: This class handles the paging from a query to be print
10
//              to the browser. You can customize it to your needs.
11
//
12
// This is distribute as is. Free of use and free to do anything you want.
13
//
14
// PLEASE REPORT ANY BUG TO ME BY EMAIL :)
15
//
16
// =========================
17
// Programmer:	  Pierre-Yves Lemaire
18
//											[email protected]
19
// =========================
20
// Date:			2001-03-25
21
// Version: 2.0
22
//
23
// Modif:
24
// Version 1.1 (2001-04-09) Remove 3 lines in getNumberOfPage() that were forgot after debugging
25
// Version 1.1 (2001-04-09) Modification to the exemple
26
// Version 1.1 (2001-04-10) Added more argv to the previous and next link. ( by: [email protected] )
27
28
// Version 2.0 (2001-11-22) Complete re-write of the script
29
// Summary: The class will be make it easier to play with results...
30
// * The class now only returns 2 arrays. All HTML, except href, tag were remove.
31
// * Function printPaging() broken in two: getPagingArray() and getPagingRowArray()
32
// * Function openTable() and closeTable() removed.
33
// =====================================================
34
35
class Paginate implements PaginginateInterface
0 ignored issues
show
Coding Style introduced by
The property $int_num_result is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $int_nbr_row is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $int_cur_position is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $str_ext_argv is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
36
{
37
38
    /**
39
     * Number of result to show per page (decided by user)
40
     *
41
     * @var int
42
     */
43
    public $int_num_result;
44
45
    /**
46
     * Total number of items (SQL count from db)
47
     *
48
     * @var int
49
     */
50
    public $int_nbr_row;
51
52
    /**
53
     * Current position in recordset
54
     *
55
     * @var int
56
     */
57
    public $int_cur_position;
58
59
    /**
60
     * Extra argv of query string
61
     *
62
     * @var string
63
     */
64
    public $str_ext_argv;
65
66
    /**
67
     * Paging constructor.
68
     * @param int $int_nbr_row
69
     * @param int $int_cur_position
70
     * @param int $int_num_result
71
     * @param string $str_ext_argv
72
     */
73
    public function __construct($int_nbr_row, $int_cur_position, $int_num_result, $str_ext_argv = "")
0 ignored issues
show
Coding Style Naming introduced by
The parameter $int_nbr_row is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $int_cur_position is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $int_num_result is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $str_ext_argv is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
74
    {
75
        $this->int_nbr_row = $int_nbr_row;
76
        $this->int_num_result = $int_num_result;
77
        $this->int_cur_position = $int_cur_position;
78
        $this->str_ext_argv = urldecode($str_ext_argv);
79
    }
80
81
    /**
82
     * This function print the paging to the screen.
83
     * This function returns an array:
84
     *      $array_paging['lower'] lower limit of where we are in result set
85
     *      $array_paging['upper'] upper limit of where we are in result set
86
     *      $array_paging['total'] total number of result
87
     *      $array_paging['previous_link'] href tag for previous link
88
     *      $array_paging['next_link'] href tag for next link
89
     *
90
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,integer|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
91
     */
92
    public function getPagingArray()
93
    {
94
        global $PHP_SELF;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
95
96
        $array_paging['lower'] = ($this->int_cur_position + 1);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$array_paging was never initialized. Although not strictly required by PHP, it is generally a good practice to add $array_paging = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
97
98
        if ($this->int_cur_position + $this->int_num_result >= $this->int_nbr_row) {
99
            $array_paging['upper'] = $this->int_nbr_row;
100
        } else {
101
            $array_paging['upper'] = ($this->int_cur_position + $this->int_num_result);
102
        }
103
104
        $array_paging['total'] = $this->int_nbr_row;
105
106
        if ($this->int_cur_position != 0) {
107
            $array_paging['first_link'] = "<a href=\"$PHP_SELF?int_cur_position=0" . $this->str_ext_argv . "\">";
108
            $array_paging['previous_link'] = "<a href=\"$PHP_SELF?int_cur_position=" . ($this->int_cur_position - $this->int_num_result) . $this->str_ext_argv . "\">";
109
        }
110
111
        if (($this->int_nbr_row - $this->int_cur_position) > $this->int_num_result) {
112
            $int_new_position = $this->int_cur_position + $this->int_num_result;
113
            $array_paging['last_link'] = "<a href=\"$PHP_SELF?int_cur_position=" . $this->int_nbr_row . $this->str_ext_argv . "\">";
114
            $array_paging['next_link'] = "<a href=\"$PHP_SELF?int_cur_position=$int_new_position" . $this->str_ext_argv . "\">";
115
        }
116
117
        return $array_paging;
118
    }
119
120
    /**
121
     * This function returns an array of string (href link with the page number)
122
     *
123
     * @return array
124
     */
125
    public function getPagingRowArray()
126
    {
127
        global $PHP_SELF;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
128
        $array_all_page = array();
129
        for ($i = 0; $i < $this->getNumberOfPage(); $i++) {
130
            // if current page, do not make a link
131
            if ($i == $this->getCurrentPage()) {
132
                $array_all_page[$i] = "<b>" . ($i + 1) . "</b>&nbsp;";
133
            } else {
134
                $int_new_position = ($i * $this->int_num_result);
135
                $array_all_page[$i] = "<a href=\"" . $PHP_SELF . "?int_cur_position=$int_new_position$this->str_ext_argv\">" . ($i + 1) . "</a>&nbsp;";
136
            }
137
        }
138
139
        return $array_all_page;
140
    }
141
142
    /**
143
     * This function returns the total number of page to display.
144
     *
145
     * @return float|int
146
     */
147
    public function getNumberOfPage()
148
    {
149
        return $this->int_nbr_row / $this->int_num_result;
150
    }
151
152
    /**
153
     * This function returns the current page number.
154
     *
155
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
156
     */
157
    public function getCurrentPage()
158
    {
159
        $int_cur_page = ($this->int_cur_position * $this->getNumberOfPage()) / $this->int_nbr_row;
160
161
        return number_format($int_cur_page, 0);
162
    }
163
}
164