1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBotFramework\Utilities; |
4
|
|
|
|
5
|
|
|
define("DELIMITER", ':::::::::::::::::::::::::::::::::::::: |
6
|
|
|
'); |
7
|
|
|
|
8
|
|
|
class Paginator { |
9
|
|
|
|
10
|
|
|
static public function paginateItems($items, int $index, &$keyboard, $format_item, int $item_per_page = 3, $prefix = 'list', string $delimiter = DELIMITER) { |
|
|
|
|
11
|
|
|
|
12
|
|
|
// Calc the position of the first item to show |
13
|
|
|
$item_position = ($index - 1) * $item_per_page + 1; |
14
|
|
|
|
15
|
|
|
// Counter variable |
16
|
|
|
$cont = 1; |
17
|
|
|
|
18
|
|
|
// How many items did we display? |
19
|
|
|
$items_displayed = 0; |
20
|
|
|
|
21
|
|
|
// Get how many items did the database return |
22
|
|
|
$items_number = $items->rowCount(); |
23
|
|
|
|
24
|
|
|
// Get how many complete pages there are |
25
|
|
|
$total_pages = intval($items_number / $item_per_page); |
26
|
|
|
|
27
|
|
|
// If there an incomplete page |
28
|
|
|
if (($items_number % $item_per_page) != 0) { |
29
|
|
|
|
30
|
|
|
$total_pages++; |
31
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Initialize keyboard with the list |
35
|
|
|
$keyboard->addListKeyboard($index, $total_pages, $prefix); |
36
|
|
|
|
37
|
|
|
// Initialize empty string |
38
|
|
|
$message = ''; |
39
|
|
|
|
40
|
|
|
// Iterate over all results |
41
|
|
|
while ($item = $items->fetch()) { |
42
|
|
|
|
43
|
|
|
// If we have to display the first item of the page and we found the item to show (using the position |
44
|
|
|
// calculated before) |
45
|
|
|
if ($items_displayed === 0 && $cont === $item_position) { |
46
|
|
|
|
47
|
|
|
// Format the item using closure |
48
|
|
|
$message .= $format_item($item, $keyboard); |
49
|
|
|
|
50
|
|
|
// We displayed an item |
51
|
|
|
$items_displayed++; |
52
|
|
|
|
53
|
|
|
// If we displayed at least an item but still not how much we want |
54
|
|
|
} elseif ($items_displayed > 0 && $items_displayed < $item_per_page) { |
55
|
|
|
|
56
|
|
|
// Add delimiter to the message |
57
|
|
|
$message .= $delimiter; |
58
|
|
|
|
59
|
|
|
// Format the item using closure |
60
|
|
|
$message .= $format_item($item, $keyboard); |
61
|
|
|
|
62
|
|
|
// We displayed an item |
63
|
|
|
$items_displayed++; |
64
|
|
|
|
65
|
|
|
// If we displayed all the item we wanted |
66
|
|
|
} elseif ($items_displayed === $item_per_page) { |
67
|
|
|
|
68
|
|
|
// Exit the cycle |
69
|
|
|
break; |
70
|
|
|
|
71
|
|
|
// We are just iterating over an unwanted result |
72
|
|
|
} else { |
73
|
|
|
|
74
|
|
|
$cont++; |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Return the created string |
81
|
|
|
return $message; |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|