diff --git a/api/domains.php b/api/domains.php index c3e23fe..fc02d87 100644 --- a/api/domains.php +++ b/api/domains.php @@ -28,39 +28,22 @@ if(!isset($input->csrfToken) || $input->csrfToken !== $_SESSION['csrfToken']) { } 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 D.id,D.name,D.type,count(R.domain_id) AS records + SELECT COUNT(*) AS anzahl FROM domains D - LEFT OUTER JOIN records R ON D.id = R.domain_id LEFT OUTER JOIN permissions P ON D.id = P.domain - WHERE (P.user=? OR ?) - GROUP BY D.id, D.name, D.type - HAVING + WHERE (P.user=? OR ?) AND (D.name LIKE ? OR ?) AND (D.type=? OR ?) "; - if(isset($input->sort->field) && $input->sort->field != "") { - if($input->sort->field == "id") { - $sql .= "ORDER BY id"; - } else if($input->sort->field == "name") { - $sql .= "ORDER BY name"; - } else if($input->sort->field == "type") { - $sql .= "ORDER BY type"; - } else if($input->sort->field == "records") { - $sql .= "ORDER BY records"; - } - - if(isset($input->sort->order)) { - if($input->sort->order == 0) { - $sql .= " DESC"; - } else if($input->sort->order == 1) { - $sql .= " ASC"; - } - } - } - $stmt = $db->prepare($sql); if(isset($input->name)) { @@ -91,10 +74,91 @@ if(isset($input->action) && $input->action == "getDomains") { $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 = " + SELECT D.id,D.name,D.type,count(R.domain_id) AS records + FROM domains D + LEFT OUTER JOIN records R ON D.id = R.domain_id + LEFT OUTER JOIN permissions P ON D.id = P.domain + WHERE (P.user=? OR ?) + GROUP BY D.id, D.name, D.type + HAVING + (D.name LIKE ? OR ?) AND + (D.type=? OR ?) + "; + + if(isset($input->sort->field) && $input->sort->field != "") { + if($input->sort->field == "id") { + $sql .= "ORDER BY id"; + } else if($input->sort->field == "name") { + $sql .= "ORDER BY name"; + } else if($input->sort->field == "type") { + $sql .= "ORDER BY type"; + } else if($input->sort->field == "records") { + $sql .= "ORDER BY records"; + } + + if(isset($input->sort->order)) { + if($input->sort->order == 0) { + $sql .= " DESC"; + } else if($input->sort->order == 1) { + $sql .= " ASC"; + } + } + } + + /* + * 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); + + 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(); while($obj = $result->fetch_object()) { - $retval[] = $obj; + $retval['data'][] = $obj; } } diff --git a/config/config-default.php b/config/config-default.php index c6eedf8..5466095 100644 --- a/config/config-default.php +++ b/config/config-default.php @@ -26,4 +26,7 @@ $config['db_name'] = "pdnsmanager"; //Remote update $config['nonce_lifetime'] = 15; -include 'config-user.php'; \ No newline at end of file +//Number of rows in domain overview +$config['domain_rows'] = 15; + +include 'config-user.php'; diff --git a/domains.php b/domains.php index 3013f77..fd60f6a 100644 --- a/domains.php +++ b/domains.php @@ -50,7 +50,7 @@ limitations under the License. - +
@@ -82,12 +82,16 @@ limitations under the License. -
+ '; + echo '
'; echo 'Add MASTER'; echo 'Add NATIVE'; echo '
'; @@ -108,6 +112,8 @@ limitations under the License.
- ' . $_SESSION['csrfToken'] . ''; ?> + + ' . $_SESSION['csrfToken'] . ''; ?> + diff --git a/js/domains.js b/js/domains.js index b487f2e..5f375a2 100644 --- a/js/domains.js +++ b/js/domains.js @@ -39,49 +39,96 @@ $(document).ready(function() { } requestData(); }); - + $('#searchName').bind("paste keyup", function() { requestData(); }); - + $('#searchType').change(function() { requestData(); }); - + $('#searchType').select2({ minimumResultsForSearch: Infinity }); }); -function requestData() { +function requestData(page) { + if(typeof(page) !== 'number' || page <= 0) { + page = 1; + } + var restrictions = { - csrfToken: $('#csrfToken').text() + csrfToken: $('#csrfToken').text(), }; - + restrictions.sort = sort; - + var searchName = $('#searchName').val(); if(searchName.length > 0) { restrictions.name = searchName; } - + var searchType = $('#searchType').val(); if(searchType != "none") { restrictions.type = searchType; } - + restrictions.action = "getDomains"; - + restrictions.page = page; + $.post( "api/domains.php", JSON.stringify(restrictions), function(data) { - recreateTable(data); + recreateTable(data.data); + recreatePagination(data.pages) }, "json" ); } +function recreatePagination(data) { + $('#pagination').empty(); + + if(data.total === 1) { + $('#pagination-wrapper').hide(); + return; + } + + if(data.current > 1) { + $('
  • ').appendTo('#pagination').data("page", data.current - 1).click(paginationClicked); + } + + $('
  • 1
  • ').appendTo('#pagination').data("page", 1).click(paginationClicked); + + if(data.current > 4) { + $('
  • ').appendTo('#pagination'); + } + + for(var i = data.current - 2; i <= data.current + 2; i++) { + if(i > 1 && i < data.total) { + if(data.current === i) { + $('
  • ' + i + '
  • ').appendTo('#pagination'); + } else { + $('
  • ' + i + '
  • ').appendTo('#pagination').data("page", i).click(paginationClicked); + } + } + } + + if(data.current < data.total - 3) { + $('
  • ').appendTo('#pagination'); + } + + $('
  • ' + data.total + '
  • ').appendTo('#pagination').data("page", data.total).click(paginationClicked); + + if(data.current < data.total) { + $('
  • ').appendTo('#pagination').data("page", data.current + 1).click(paginationClicked); + } + + $('#pagination-wrapper').show(); +} + function recreateTable(data) { $('#table-domains>tbody').empty(); @@ -144,4 +191,8 @@ function deleteDomainWithId(id, callback) { }, "json" ); +} + +function paginationClicked() { + requestData($(this).data("page")); } \ No newline at end of file