Added record table to edit-master.php, working inclusive filter and data

This commit is contained in:
Lukas Metzger 2016-01-20 20:46:14 +01:00
parent c540d4a76b
commit dfab14b517
3 changed files with 287 additions and 12 deletions

127
api/edit-master.php Normal file
View file

@ -0,0 +1,127 @@
<?php
/*
* Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once '../config/config-default.php';
require_once '../lib/database.php';
require_once '../lib/session.php';
$input = json_decode(file_get_contents('php://input'));
//Permission check
if(isset($input->domain)) {
$permquery = $db->prepare("SELECT * FROM permissions WHERE user=? AND domain=?");
$permquery->bind_param("ii", $_SESSION['id'], $input->domain);
$permquery->execute();
$permquery->store_result();
if($permquery->num_rows() < 1 && $_SESSION['type'] != "admin") {
echo "Permission denied!";
exit();
}
} else {
echo "Permission denied!";
exit();
}
//Action for getting Records
if(isset($input->action) && $input->action == "getRecords") {
$sql = "
SELECT id,name,type,content,ttl,prio AS priority
FROM records
WHERE
(name LIKE ? OR ?) AND
(content LIKE ? OR ?) AND
(domain_id = ?) AND
(type != 'SOA')
";
if(isset($input->type)) {
$sql .= " AND type IN(";
foreach($input->type as $filtertype) {
$filtertype = $db->escape_string($filtertype);
$sql .= "'" . $filtertype . "'" . ",";
}
$sql = rtrim($sql, ",");
$sql .= ")";
}
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 == "content") {
$sql .= " ORDER BY content";
} else if($input->sort->field == "ttl") {
$sql .= " ORDER BY ttl";
} else if($input->sort->field == "priority") {
$sql .= " ORDER BY prio";
}
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)) {
$name_filter = "%" . $input->name . "%";
$name_filter_used = 0;
} else {
$name_filter = "";
$name_filter_used = 1;
}
if(isset($input->content)) {
$content_filter = "%" . $input->content . "%";
$content_filter_used = 0;
} else {
$content_filter = "";
$content_filter_used = 1;
}
$domainId = (int)$input->domain;
$stmt->bind_param("sisii",
$name_filter, $name_filter_used,
$content_filter, $content_filter_used,
$domainId
);
$stmt->execute();
$result = $stmt->get_result();
$retval = Array();
while($obj = $result->fetch_object()) {
$retval[] = $obj;
}
}
echo json_encode($retval);

View file

@ -25,10 +25,13 @@ limitations under the License.
<link href="include/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="include/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
<link href="include/select2/select2.min.css" rel="stylesheet">
<link href="include/select2/select2-bootstrap.min.css" rel="stylesheet">
<link href="include/custom.css" rel="stylesheet">
<script src="include/jquery.js"></script>
<script src="include/bootstrap/js/bootstrap.min.js"></script>
<script src="include/select2/select2.min.js"></script>
<script src="js/edit-master.js"></script>
</head>
@ -101,7 +104,87 @@ limitations under the License.
</row>
</div>
<row class="row vspacer-50"></row>
<div id="records" class="container">
<table class="table table-hover" id="table-records">
<thead>
<tr>
<td class="cell-vertical-middle"><strong>ID</strong> <span class="glyphicon glyphicon-sort cursor-pointer"></span></td>
<td class="cell-vertical-middle">
<form class="form-inline">
<div class="form-group">
<strong>Name</strong>
<span class="glyphicon glyphicon-sort cursor-pointer "></span>
<input type="text" class="form-control no-shadow" id="searchName" placeholder="Search" autocomplete="off">
</div>
</form>
</td>
<td class="cell-vertical-middle">
<form class="form-inline">
<div class="form-group">
<strong>Type</strong>
<span class="glyphicon glyphicon-sort cursor-pointer"></span>
<select class="form-control no-shadow" id="searchType" multiple>
<option value="A" >A</option>
<option value="AAAA" >AAAA</option>
<option value="AFSDB" >AFSDB</option>
<option value="CERT" >CERT</option>
<option value="CNAME" >CNAME</option>
<option value="DHCID" >DHCID</option>
<option value="DLV" >DLV</option>
<option value="DNSKEY" >DNSKEY</option>
<option value="DS" >DS</option>
<option value="EUI48" >EUI48</option>
<option value="EUI64" >EUI64</option>
<option value="HINFO" >HINFO</option>
<option value="IPSECKEY" >IPSECKEY</option>
<option value="KEY" >KEY</option>
<option value="KX" >KX</option>
<option value="LOC" >LOC</option>
<option value="MINFO" >MINFO</option>
<option value="MR" >MR</option>
<option value="MX" >MX</option>
<option value="NAPTR" >NAPTR</option>
<option value="NS" >NS</option>
<option value="NSEC" >NSEC</option>
<option value="NSEC3" >NSEC3</option>
<option value="NSEC3PARAM" >NSEC3PARAM</option>
<option value="OPT" >OPT</option>
<option value="PTR" >PTR</option>
<option value="RKEY" >RKEY</option>
<option value="RP" >RP</option>
<option value="RRSIG" >RRSIG</option>
<option value="SOA" >SOA</option>
<option value="SPF" >SPF</option>
<option value="SRV" >SRV</option>
<option value="SSHFP" >SSHFP</option>
<option value="TLSA" >TLSA</option>
<option value="TSIG" >TSIG</option>
<option value="TXT" >TXT</option>
<option value="WKS" >WKS</option>
</select>
</div>
</form>
</td>
<td class="cell-vertical-middle">
<form class="form-inline">
<div class="form-group">
<strong>Content</strong>
<span class="glyphicon glyphicon-sort cursor-pointer"></span>
<input type="text" class="form-control no-shadow" id="searchContent" placeholder="Search" autocomplete="off">
</div>
</form>
</td>
<td class="cell-vertical-middle"><strong>Priority</strong> <span class="glyphicon glyphicon-sort cursor-pointer"></span></td>
<td class="cell-vertical-middle"><strong>TTL</strong> <span class="glyphicon glyphicon-sort cursor-pointer"></span></td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</body>

View file

@ -14,6 +14,11 @@
* limitations under the License.
*/
var sort = {
field: "",
order: 1
}
$(document).ready(function() {
$('#soa button[type=submit]').click(function(){
@ -30,8 +35,39 @@ $(document).ready(function() {
$(this).parent().removeClass("has-error");
}
});
$('#searchType').select2({
placeholder: "Filter..."
});
$('#table-records>thead>tr>td span.glyphicon').click(function() {
var field = $(this).siblings('strong').text().toLowerCase();
if(sort.field == field) {
if(sort.order == 1) sort.order = 0;
else sort.field = "";
} else {
sort.field = field;
sort.order = 1;
}
$('#table-records>thead>tr>td span').removeClass("glyphicon-sort-by-attributes glyphicon-sort-by-attributes-alt");
if(sort.field == field) {
if(sort.order == 1) $(this).addClass("glyphicon-sort-by-attributes");
else $(this).addClass("glyphicon-sort-by-attributes-alt");
}
requestRecordData();
});
$('#searchName, #searchContent').bind("paste keyup", function() {
requestRecordData();
});
$('#searchType').change(function() {
requestRecordData();
});
requestRecordData();
});
function validateSoaData() {
@ -49,23 +85,52 @@ function validateSoaData() {
}
function recreateTable(data) {
$('#table-domains>tbody').empty();
$('#table-records>tbody').empty();
$.each(data, function(index,item) {
$('<tr></tr>').appendTo('#table-domains>tbody')
$('<tr></tr>').appendTo('#table-records>tbody')
.append('<td>' + item.id + '</td>')
.append('<td>' + item.name + '</td>')
.append('<td>' + item.type + '</td>')
.append('<td>' + item.records + '</td>');
.append('<td>' + item.content + '</td>')
.append('<td>' + item.priority + '</td>')
.append('<td>' + item.ttl + '</td>')
.append('<td><span class="glyphicon glyphicon-pencil cursor-pointer "></span></td>')
.append('<td><span class="glyphicon glyphicon-trash cursor-pointer "></span></td>');
});
}
function requestRecordData() {
var restrictions = {};
$('#table-domains>tbody>tr').click(function() {
var id = $(this).children('td').first().text();
var type = $(this).children('td').eq(2).text();
if(type == 'MASTER') {
location.assign('edit-master.php#' + id);
}
});
restrictions.sort = sort;
var searchName = $('#searchName').val();
if(searchName.length > 0) {
restrictions.name = searchName;
}
var searchType = $('#searchType').val();
if(searchType != null && searchType.length > 0) {
restrictions.type = searchType;
}
var searchContent = $('#searchContent').val();
if(searchContent.length > 0) {
restrictions.content = searchContent;
}
restrictions.action = "getRecords";
restrictions.domain = location.hash.substring(1);
$.post(
"api/edit-master.php",
JSON.stringify(restrictions),
function(data) {
recreateTable(data);
},
"json"
);
}