|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PhpBotFramework. |
|
5
|
|
|
* |
|
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
|
8
|
|
|
* published by the Free Software Foundation, version 3. |
|
9
|
|
|
* |
|
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
|
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13
|
|
|
* Lesser General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace PhpBotFramework\Utilities; |
|
20
|
|
|
|
|
21
|
|
|
define("DELIMITER", "::::::::::::::::::::::::::::::::::::::\n"); |
|
22
|
|
|
|
|
23
|
|
|
class Paginator |
|
24
|
|
|
{ |
|
25
|
|
|
public static function paginateItems( |
|
26
|
|
|
$items, |
|
27
|
|
|
int $index, |
|
28
|
|
|
&$keyboard, |
|
29
|
|
|
$format_item, |
|
30
|
|
|
int $item_per_page = 3, |
|
31
|
|
|
$prefix = 'list', |
|
32
|
|
|
string $delimiter = DELIMITER |
|
33
|
|
|
) { |
|
34
|
|
|
|
|
35
|
|
|
// Assign the position of first item to show |
|
36
|
|
|
$item_position = ($index - 1) * $item_per_page + 1; |
|
37
|
|
|
|
|
38
|
|
|
$items_number = $items->rowCount(); |
|
39
|
|
|
|
|
40
|
|
|
$counter = 1; |
|
41
|
|
|
|
|
42
|
|
|
$items_displayed = 0; |
|
43
|
|
|
|
|
44
|
|
|
$total_pages = intval($items_number / $item_per_page); |
|
45
|
|
|
|
|
46
|
|
|
// If there an incomplete page |
|
47
|
|
|
if (($items_number % $item_per_page) != 0) { |
|
48
|
|
|
$total_pages++; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Initialize keyboard with the list |
|
52
|
|
|
$keyboard->addListKeyboard($index, $total_pages, $prefix); |
|
53
|
|
|
|
|
54
|
|
|
$message = ''; |
|
55
|
|
|
|
|
56
|
|
|
// Iterate over all results |
|
57
|
|
|
while ($item = $items->fetch()) { |
|
58
|
|
|
// If we have to display the first item of the page and we |
|
59
|
|
|
// found the item to show (using the position calculated before) |
|
60
|
|
|
if ($items_displayed === 0 && $counter === $item_position) { |
|
61
|
|
|
$message .= $format_item($item, $keyboard); |
|
62
|
|
|
$items_displayed++; |
|
63
|
|
|
// If there are space for other items |
|
64
|
|
|
} elseif ($items_displayed > 0 && $items_displayed < $item_per_page) { |
|
65
|
|
|
$message .= $delimiter; |
|
66
|
|
|
$message .= $format_item($item, $keyboard); |
|
67
|
|
|
|
|
68
|
|
|
$items_displayed++; |
|
69
|
|
|
} elseif ($items_displayed === $item_per_page) { |
|
70
|
|
|
break; |
|
71
|
|
|
} else { |
|
72
|
|
|
$counter++; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $message; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|