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,7 +22,9 @@ require_once '../lib/session.php';
$input = json_decode(file_get_contents('php://input')); $input = json_decode(file_get_contents('php://input'));
$sql = " if(isset($input->action) && $input->action == "getDomains") {
$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
LEFT OUTER JOIN records R ON D.id = R.domain_id LEFT OUTER JOIN records R ON D.id = R.domain_id
@ -32,9 +34,9 @@ $sql = "
HAVING HAVING
(D.name LIKE ? OR ?) AND (D.name LIKE ? OR ?) AND
(D.type=? OR ?) (D.type=? OR ?)
"; ";
if(isset($input->sort->field) && $input->sort->field != "") { if(isset($input->sort->field) && $input->sort->field != "") {
if($input->sort->field == "id") { if($input->sort->field == "id") {
$sql .= "ORDER BY id"; $sql .= "ORDER BY id";
} else if($input->sort->field == "name") { } else if($input->sort->field == "name") {
@ -52,42 +54,70 @@ if(isset($input->sort->field) && $input->sort->field != "") {
$sql .= " ASC"; $sql .= " ASC";
} }
} }
} }
$stmt = $db->prepare($sql); $stmt = $db->prepare($sql);
if(isset($input->name)) { if(isset($input->name)) {
$name_filter = "%" . $input->name . "%"; $name_filter = "%" . $input->name . "%";
$name_filter_used = 0; $name_filter_used = 0;
} else { } else {
$name_filter = ""; $name_filter = "";
$name_filter_used = 1; $name_filter_used = 1;
} }
$id_filter = $_SESSION['id']; $id_filter = $_SESSION['id'];
$id_filter_used = (int)($_SESSION['type'] == "admin" ? 1 : 0); $id_filter_used = (int)($_SESSION['type'] == "admin" ? 1 : 0);
if(isset($input->type)) { if(isset($input->type)) {
$type_filter = $input->type; $type_filter = $input->type;
$type_filter_used = 0; $type_filter_used = 0;
} else { } else {
$type_filter = ""; $type_filter = "";
$type_filter_used = 1; $type_filter_used = 1;
} }
$stmt->bind_param("sisiii", $stmt->bind_param("sisiii",
$id_filter, $id_filter_used, $id_filter, $id_filter_used,
$name_filter, $name_filter_used, $name_filter, $name_filter_used,
$type_filter, $type_filter_used $type_filter, $type_filter_used
); );
$stmt->execute(); $stmt->execute();
$result = $stmt->get_result(); $result = $stmt->get_result();
$retval = Array(); $retval = Array();
while($obj = $result->fetch_object()) { while($obj = $result->fetch_object()) {
$retval[] = $obj; $retval[] = $obj;
}
} }
echo json_encode($retval); 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($retval)) {
echo json_encode($retval);
} else {
echo "{}";
}

View file

@ -83,5 +83,18 @@ limitations under the License.
</table> </table>
</div> </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> </body>
</html> </html>

View file

@ -68,6 +68,8 @@ function requestData() {
restrictions.type = searchType; restrictions.type = searchType;
} }
restrictions.action = "getDomains";
$.post( $.post(
"api/domains.php", "api/domains.php",
JSON.stringify(restrictions), JSON.stringify(restrictions),
@ -86,7 +88,8 @@ function recreateTable(data) {
.append('<td>' + item.id + '</td>') .append('<td>' + item.id + '</td>')
.append('<td>' + item.name + '</td>') .append('<td>' + item.name + '</td>')
.append('<td>' + item.type + '</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); 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"
);
} }