forked from fusionpbx/fusionpbx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paging.php
161 lines (135 loc) · 5.22 KB
/
paging.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <[email protected]>
Portions created by the Initial Developer are Copyright (C) 2008-2018
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <[email protected]>
*/
function paging($num_rows, $param, $rows_per_page, $mini = false, $result_count = 0) {
//add multi-lingual support
$language = new text;
$text = $language->get();
//default number of rows per page
if (strlen($rows_per_page) == 0) {
$rows_per_page = 50;
}
// show the first page by default
$page_number = 0;
// if $_get['page'] defined, use it as page number
if(isset($_GET['page']) && is_numeric($_GET['page'])) {
$page_number = $_GET['page'];
}
// counting the offset
$offset = ($page_number - 1) * $rows_per_page;
// how many pages we have when using paging
if ($num_rows > 0) {
$max_page = ceil($num_rows/$rows_per_page);
}
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $max_page; $page++){
if ($page == $page_number) {
$nav .= " $page "; // no need to create a link to current page
}
else {
$nav .= " <a href=\"$self?page=$page\">$page</a> \n";
}
}
if ($page_number > 0) {
$page = $page_number - 1;
$prev = "<input class='btn' type='button' value='".$text['button-back']."' alt='".($page+1)."' title='".($page+1)."' onClick=\"window.location = '".$self."?page=$page".$param."';\">\n"; //◀
$first = "<input class='btn' type='button' value='".$text['button-next']."' onClick=\"window.location = '".$self."?page=1".$param."';\">\n"; //▲
}
else {
$prev = "<input class='btn' type='button' disabled value='".$text['button-back']."' style='opacity: 0.4; -moz-opacity: 0.4; cursor: default;'>\n"; //◀
}
if (($page_number + 1) < $max_page) {
$page = $page_number + 1;
$next = "<input class='btn' type='button' value='".$text['button-next']."' alt='".($page+1)."' title='".($page+1)."' onClick=\"window.location = '".$self."?page=$page".$param."';\">\n"; //▶
$last = "<input class='btn' type='button' value='".$text['button-back']."' onClick=\"window.location = '".$self."?page=$max_page".$param."';\">\n"; //▼
}
else {
$last = "<input class='btn' type='button' value='".$text['button-next']."' onClick=\"window.location = '".$self."?page=$max_page".$param."';\">\n"; //▼
$next = "<input class='btn' type='button' disabled value='".$text['button-next']."' style='opacity: 0.4; -moz-opacity: 0.4; cursor: default;'>\n"; //▶
}
//if the result count is less than the rows per page then this is the last page of results
if ($result_count > 0 and $result_count < $rows_per_page) {
$next = "<input class='btn' type='button' disabled value='".$text['button-next']."' style='opacity: 0.4; -moz-opacity: 0.4; cursor: default;'>\n"; //▶
}
$array = array();
$code = '';
if ($max_page > 1) {
//define javascript to include
$script = "<script>\n".
"function go(e) {\n".
"var page_num;\n".
"page_num = document.getElementById('paging_page_num').value;\n".
"do_action = false;\n".
"if (e != null) {\n".
"// called from a form field keypress event\n".
"var keyevent;\n".
"var keychar;\n".
"if (window.event) { keyevent = e.keyCode; }\n".
"else if (e.which) { keyevent = e.which; }\n".
"keychar = keyevent;\n".
"if (keychar == 13) {\n".
"do_action = true;\n".
"}\n".
"else {\n".
"keychar;\n".
"return true;\n".
"}\n".
"}\n".
"else {\n".
"// called from something else (non-keypress)\n".
"do_action = true;\n".
"}\n".
"if (do_action) {\n".
"// action to peform when enter is hit\n".
"if (page_num < 1) { page_num = 1; }\n".
"if (page_num > ".$max_page.") { page_num = ".$max_page."; }\n".
"document.location.href = '".$self."?page='+(--page_num)+'".$param."';\n".
"}\n".
"}\n".
"</script>\n";
//determine size
if ($mini) {
$code = $prev.$next."\n".$script;
}
else {
$code .= "<center nowrap=\"nowrap\">";
$code .= " ".$prev;
$code .= " ";
$code .= " <input id='paging_page_num' class='formfld' style='max-width: 50px; min-width: 50px; text-align: center;' type='text' value='".($page_number+1)."' onfocus='this.select();' onkeypress='return go(event);'>";
if ($result_count == 0) {
$code .= " <strong>".$max_page."</strong>";
}
$code .= " ";
$code .= " ".$next;
$code .= "</center>\n".$script;
}
//add to array
$array[] = $code;
}
else {
$array[] = "";
}
$array[] = $rows_per_page;
$array[] = $offset;
return $array;
}
?>