From dfab14b517d48eb5613920b8249faee05a1f996c Mon Sep 17 00:00:00 2001 From: Lukas Metzger Date: Wed, 20 Jan 2016 20:46:14 +0100 Subject: [PATCH] Added record table to edit-master.php, working inclusive filter and data --- api/edit-master.php | 127 ++++++++++++++++++++++++++++++++++++++++++++ edit-master.php | 83 +++++++++++++++++++++++++++++ js/edit-master.js | 89 ++++++++++++++++++++++++++----- 3 files changed, 287 insertions(+), 12 deletions(-) create mode 100644 api/edit-master.php diff --git a/api/edit-master.php b/api/edit-master.php new file mode 100644 index 0000000..ebaaadf --- /dev/null +++ b/api/edit-master.php @@ -0,0 +1,127 @@ +. + * + * 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); diff --git a/edit-master.php b/edit-master.php index 5d03ef8..7df3371 100644 --- a/edit-master.php +++ b/edit-master.php @@ -25,10 +25,13 @@ limitations under the License. + + + @@ -101,7 +104,87 @@ limitations under the License. + +
+ + + + + + + + + + + + + + +
ID +
+
+ Name + + +
+
+
+
+
+ Type + + +
+
+
+
+
+ Content + + +
+
+
Priority TTL
+
diff --git a/js/edit-master.js b/js/edit-master.js index 89f5f80..865cff6 100644 --- a/js/edit-master.js +++ b/js/edit-master.js @@ -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) { - $('').appendTo('#table-domains>tbody') + $('').appendTo('#table-records>tbody') .append('' + item.id + '') .append('' + item.name + '') .append('' + item.type + '') - .append('' + item.records + ''); + .append('' + item.content + '') + .append('' + item.priority + '') + .append('' + item.ttl + '') + .append('') + .append(''); }); +} + +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" + ); } \ No newline at end of file