Added pagination (#19)
* Added initial functionality for pagination * Display of pagination now works for total page number greater than 8. Note that the actual pagination is not yet implemented * Pagination is now functional and loads the requested sites * Added cursor pointer to pagination * page parameter is now set to 1 if not a number or smaller than 1 * page parameter is now checked with isset to prevent errors if it is not supplied * Changed default number of domains per page to 15 * Added isset check to $input->page to prevent errors if it is not set
This commit is contained in:
parent
dc8fab4d42
commit
788c0d1d19
|
@ -28,7 +28,63 @@ if(!isset($input->csrfToken) || $input->csrfToken !== $_SESSION['csrfToken']) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isset($input->action) && $input->action == "getDomains") {
|
if(isset($input->action) && $input->action == "getDomains") {
|
||||||
|
// Check if the requested page is a number
|
||||||
|
if(!(isset($input->page) && is_int($input->page) && $input->page > 0)) {
|
||||||
|
echo "Requested page must be a positive number!";
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Here we get the number of matching records
|
||||||
|
$sql = "
|
||||||
|
SELECT COUNT(*) AS anzahl
|
||||||
|
FROM domains D
|
||||||
|
LEFT OUTER JOIN permissions P ON D.id = P.domain
|
||||||
|
WHERE (P.user=? OR ?) AND
|
||||||
|
(D.name LIKE ? OR ?) AND
|
||||||
|
(D.type=? OR ?)
|
||||||
|
";
|
||||||
|
|
||||||
|
$stmt = $db->prepare($sql);
|
||||||
|
|
||||||
|
if(isset($input->name)) {
|
||||||
|
$name_filter = "%" . $input->name . "%";
|
||||||
|
$name_filter_used = 0;
|
||||||
|
} else {
|
||||||
|
$name_filter = "";
|
||||||
|
$name_filter_used = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
$id_filter = $_SESSION['id'];
|
||||||
|
$id_filter_used = (int)($_SESSION['type'] == "admin" ? 1 : 0);
|
||||||
|
|
||||||
|
if(isset($input->type)) {
|
||||||
|
$type_filter = $input->type;
|
||||||
|
$type_filter_used = 0;
|
||||||
|
} else {
|
||||||
|
$type_filter = "";
|
||||||
|
$type_filter_used = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
$stmt->bind_param("sisiii",
|
||||||
|
$id_filter, $id_filter_used,
|
||||||
|
$name_filter, $name_filter_used,
|
||||||
|
$type_filter, $type_filter_used
|
||||||
|
);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
$result = $stmt->get_result();
|
||||||
|
|
||||||
|
// This is the object containing the number of rows
|
||||||
|
$obj = $result->fetch_object();
|
||||||
|
|
||||||
|
// Initialize the return value
|
||||||
|
$retval = Array();
|
||||||
|
|
||||||
|
$retval['pages']['current'] = $input->page;
|
||||||
|
$retval['pages']['total'] = ceil($obj->anzahl / $config['domain_rows']);
|
||||||
|
|
||||||
|
|
||||||
|
// Now the real search is done on the database
|
||||||
$sql = "
|
$sql = "
|
||||||
SELECT D.id,D.name,D.type,count(R.domain_id) AS records
|
SELECT D.id,D.name,D.type,count(R.domain_id) AS records
|
||||||
FROM domains D
|
FROM domains D
|
||||||
|
@ -61,6 +117,16 @@ if(isset($input->action) && $input->action == "getDomains") {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Now the number of entries gets limited to the domainRows config value.
|
||||||
|
* SQL LIMIT is used for that:
|
||||||
|
* LIMIT lower, upper
|
||||||
|
* Note that LIMIT 0,4 returns the first five rows!
|
||||||
|
*/
|
||||||
|
$lower_limit = ($config['domain_rows'] * ($input->page - 1));
|
||||||
|
|
||||||
|
$sql .= " LIMIT " . $lower_limit . ", " . $config['domain_rows'];
|
||||||
|
|
||||||
$stmt = $db->prepare($sql);
|
$stmt = $db->prepare($sql);
|
||||||
|
|
||||||
if(isset($input->name)) {
|
if(isset($input->name)) {
|
||||||
|
@ -91,10 +157,8 @@ if(isset($input->action) && $input->action == "getDomains") {
|
||||||
|
|
||||||
$result = $stmt->get_result();
|
$result = $stmt->get_result();
|
||||||
|
|
||||||
$retval = Array();
|
|
||||||
|
|
||||||
while($obj = $result->fetch_object()) {
|
while($obj = $result->fetch_object()) {
|
||||||
$retval[] = $obj;
|
$retval['data'][] = $obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,4 +26,7 @@ $config['db_name'] = "pdnsmanager";
|
||||||
//Remote update
|
//Remote update
|
||||||
$config['nonce_lifetime'] = 15;
|
$config['nonce_lifetime'] = 15;
|
||||||
|
|
||||||
|
//Number of rows in domain overview
|
||||||
|
$config['domain_rows'] = 15;
|
||||||
|
|
||||||
include 'config-user.php';
|
include 'config-user.php';
|
10
domains.php
10
domains.php
|
@ -82,12 +82,16 @@ limitations under the License.
|
||||||
</thead>
|
</thead>
|
||||||
<tbody class="cursor-pointer">
|
<tbody class="cursor-pointer">
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
||||||
</table>
|
</table>
|
||||||
|
<nav id="pagination-wrapper" class="text-center defaulthidden">
|
||||||
|
<ul id="pagination" class="pagination cursor-pointer">
|
||||||
|
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
if($_SESSION['type'] == "admin") {
|
if($_SESSION['type'] == "admin") {
|
||||||
echo '<div class="row">';
|
echo '<div class="row text-center">';
|
||||||
echo '<a class="btn btn-success" href="add-domain.php#MASTER">Add MASTER</a>';
|
echo '<a class="btn btn-success" href="add-domain.php#MASTER">Add MASTER</a>';
|
||||||
echo '<a class="btn btn-primary margin-left-20" href="add-domain.php#NATIVE">Add NATIVE</a>';
|
echo '<a class="btn btn-primary margin-left-20" href="add-domain.php#NATIVE">Add NATIVE</a>';
|
||||||
echo '</div>';
|
echo '</div>';
|
||||||
|
@ -108,6 +112,8 @@ limitations under the License.
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<?php echo '<span class="hidden" id="csrfToken">' . $_SESSION['csrfToken'] . '</span>'; ?>
|
<?php echo '<span class="hidden" id="csrfToken">' . $_SESSION['csrfToken'] . '</span>'; ?>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -53,9 +53,13 @@ $(document).ready(function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function requestData() {
|
function requestData(page) {
|
||||||
|
if(typeof(page) !== 'number' || page <= 0) {
|
||||||
|
page = 1;
|
||||||
|
}
|
||||||
|
|
||||||
var restrictions = {
|
var restrictions = {
|
||||||
csrfToken: $('#csrfToken').text()
|
csrfToken: $('#csrfToken').text(),
|
||||||
};
|
};
|
||||||
|
|
||||||
restrictions.sort = sort;
|
restrictions.sort = sort;
|
||||||
|
@ -71,17 +75,60 @@ function requestData() {
|
||||||
}
|
}
|
||||||
|
|
||||||
restrictions.action = "getDomains";
|
restrictions.action = "getDomains";
|
||||||
|
restrictions.page = page;
|
||||||
|
|
||||||
$.post(
|
$.post(
|
||||||
"api/domains.php",
|
"api/domains.php",
|
||||||
JSON.stringify(restrictions),
|
JSON.stringify(restrictions),
|
||||||
function(data) {
|
function(data) {
|
||||||
recreateTable(data);
|
recreateTable(data.data);
|
||||||
|
recreatePagination(data.pages)
|
||||||
},
|
},
|
||||||
"json"
|
"json"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function recreatePagination(data) {
|
||||||
|
$('#pagination').empty();
|
||||||
|
|
||||||
|
if(data.total === 1) {
|
||||||
|
$('#pagination-wrapper').hide();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(data.current > 1) {
|
||||||
|
$('<li><a href="#"><span class="glyphicon glyphicon-chevron-left"></span></a></li>').appendTo('#pagination').data("page", data.current - 1).click(paginationClicked);
|
||||||
|
}
|
||||||
|
|
||||||
|
$('<li><span>1</span></li>').appendTo('#pagination').data("page", 1).click(paginationClicked);
|
||||||
|
|
||||||
|
if(data.current > 4) {
|
||||||
|
$('<li class="disabled"><span>…</span></li>').appendTo('#pagination');
|
||||||
|
}
|
||||||
|
|
||||||
|
for(var i = data.current - 2; i <= data.current + 2; i++) {
|
||||||
|
if(i > 1 && i < data.total) {
|
||||||
|
if(data.current === i) {
|
||||||
|
$('<li class="active"><span>' + i + '</span></li>').appendTo('#pagination');
|
||||||
|
} else {
|
||||||
|
$('<li><span>' + i + '</span></li>').appendTo('#pagination').data("page", i).click(paginationClicked);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(data.current < data.total - 3) {
|
||||||
|
$('<li class="disabled"><span>…</span></li>').appendTo('#pagination');
|
||||||
|
}
|
||||||
|
|
||||||
|
$('<li><span>' + data.total + '</span></li>').appendTo('#pagination').data("page", data.total).click(paginationClicked);
|
||||||
|
|
||||||
|
if(data.current < data.total) {
|
||||||
|
$('<li><a href="#"><span class="glyphicon glyphicon-chevron-right"></span></a></li>').appendTo('#pagination').data("page", data.current + 1).click(paginationClicked);
|
||||||
|
}
|
||||||
|
|
||||||
|
$('#pagination-wrapper').show();
|
||||||
|
}
|
||||||
|
|
||||||
function recreateTable(data) {
|
function recreateTable(data) {
|
||||||
$('#table-domains>tbody').empty();
|
$('#table-domains>tbody').empty();
|
||||||
|
|
||||||
|
@ -145,3 +192,7 @@ function deleteDomainWithId(id, callback) {
|
||||||
"json"
|
"json"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function paginationClicked() {
|
||||||
|
requestData($(this).data("page"));
|
||||||
|
}
|
Loading…
Reference in a new issue