Added function to delete a complete domain

This commit is contained in:
Lukas Metzger 2016-01-23 20:38:01 +01:00
parent 91a201c783
commit 3f8156939e
3 changed files with 146 additions and 62 deletions

View file

@ -22,72 +22,102 @@ require_once '../lib/session.php';
$input = json_decode(file_get_contents('php://input'));
$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
HAVING
(D.name LIKE ? OR ?) AND
(D.type=? OR ?)
";
if(isset($input->action) && $input->action == "getDomains") {
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";
$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
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";
}
}
}
$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();
$retval = Array();
while($obj = $result->fetch_object()) {
$retval[] = $obj;
}
}
$stmt = $db->prepare($sql);
if(isset($input->action) && $input->action == "deleteDomain") {
$domainId = $input->id;
$db->autocommit(false);
$stmt = $db->prepare("DELETE FROM permissions WHERE domain=?");
$stmt->bind_param("i", $domainId);
$stmt->execute();
$stmt->close();
$stmt = $db->prepare("DELETE FROM records WHERE domain_id=?");
$stmt->bind_param("i", $domainId);
$stmt->execute();
$stmt->close();
$stmt = $db->prepare("DELETE FROM domains WHERE id=?");
$stmt->bind_param("i", $domainId);
$stmt->execute();
$stmt->close();
$db->commit();
}
if(isset($input->name)) {
$name_filter = "%" . $input->name . "%";
$name_filter_used = 0;
if(isset($retval)) {
echo json_encode($retval);
} else {
$name_filter = "";
$name_filter_used = 1;
echo "{}";
}
$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();
$retval = Array();
while($obj = $result->fetch_object()) {
$retval[] = $obj;
}
echo json_encode($retval);

View file

@ -83,5 +83,18 @@ limitations under the License.
</table>
</div>
<div class="modal fade" id="deleteConfirm" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
Are you shure to <strong>delete</strong> the zone <strong><span id="zoneToDelete"></span></strong>?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button id="buttonDelete" type="button" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</div>
</body>
</html>

View file

@ -68,6 +68,8 @@ function requestData() {
restrictions.type = searchType;
}
restrictions.action = "getDomains";
$.post(
"api/domains.php",
JSON.stringify(restrictions),
@ -86,7 +88,8 @@ function recreateTable(data) {
.append('<td>' + item.id + '</td>')
.append('<td>' + item.name + '</td>')
.append('<td>' + item.type + '</td>')
.append('<td>' + item.records + '</td>');
.append('<td>' + item.records + '</td>')
.append('<td><span class="glyphicon glyphicon-trash cursor-pointer"></span></td>');
});
@ -98,4 +101,42 @@ function recreateTable(data) {
location.assign('edit-master.php#' + id);
}
});
$('#table-domains>tbody>tr>td>span.glyphicon-trash').click(function() {
$(this).parent().parent().unbind();
deleteDomain.call(this);
});
}
function deleteDomain() {
var deleteId = $(this).parent().parent().children('td').eq(0).text();
var deleteZone = $(this).parent().parent().children('td').eq(1).text();
var rowToRemove = $(this).parent().parent();
$('#zoneToDelete').text(deleteZone);
$('#deleteConfirm #buttonDelete').click(function() {
deleteDomainWithId(deleteId, function() {
$('#deleteConfirm').modal("hide");
$(rowToRemove).remove();
});
});
$('#deleteConfirm').modal();
}
function deleteDomainWithId(id, callback) {
var data = {
action: "deleteDomain",
id: id
};
$.post(
"api/domains.php",
JSON.stringify(data),
function() {
callback();
},
"json"
);
}