Commit 401acb38 authored by Eric's avatar Eric

enhancement: csv import/export of non mesh markers

importing and exporting of non mesh markers via CSV file.

also some code cleanup and a few other little changes to admin interface pages that were found during testing.

Fixes #7
parent 2933246f
......@@ -53,18 +53,22 @@ a.normalTextLink {
.img-valign {
vertical-align: middle;
}
/*
.remove_non_mesh_station_form {
/*not the droid your looking for*/
}
*/
#admin_wrapper {
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
/*
#admin_main_link a {
}
*/
#admin_header {
position: relative;
height: 110px;
......@@ -75,7 +79,7 @@ a.normalTextLink {
}
#admin_content {
/*height: 85%;*/
height: 90%;
/*height: 90%;*/
width: 100%;
float: left;
overflow: auto;
......
......@@ -34,7 +34,8 @@ if (!isset($_SESSION['userLoggedIn'])) {
fputcsv($f, array_keys($row));
$first = false;
}
$row['sysinfo_json'] = json_encode(json_decode($row['sysinfo_json']));
//there is not more sysinfo.json column anymore... maybe again in the future.
//$row['sysinfo_json'] = json_encode(json_decode($row['sysinfo_json']));
fputcsv($f, $row);
} // end while
......
<?php
session_start();
/*
* export non mesh markers to csv file
* june 2020 - kg6wxc
*/
if (!isset($_SESSION['userLoggedIn'])) {
echo "You are not logged in!<br>\n";
echo "This page should be run from within the admin interface!\n";
exit;
}else {
$INCLUDE_DIR = "../..";
$USER_SETTINGS = parse_ini_file($INCLUDE_DIR . "/scripts/user-settings.ini");
require $INCLUDE_DIR . "/scripts/wxc_functions.inc";
@include $INCLUDE_DIR . "/custom.inc";
//downloaded file name
$file_name = "non_mesh_markers_export-" . date('Y_m_d') . ".csv";
// export csv
function exportMysqlToCsv($filename = "non_mesh_marker_export.csv") {
// $conn = dbConnection();
$conn = wxc_connectToMySQL();
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql_query = "SELECT * FROM marker_info";
// Gets the data from the database
$result = $conn->query($sql_query);
$f = fopen('php://memory', 'wt');
$first = true;
while ($row = $result->fetch_assoc()) {
if ($first) {
fputcsv($f, array_keys($row));
$first = false;
}
//$row['sysinfo_json'] = json_encode(json_decode($row['sysinfo_json']));
fputcsv($f, $row);
} // end while
$conn->close();
$size = ftell($f);
rewind($f);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: $size");
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
header("Content-type: text/csv");
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
fpassthru($f);
exit;
}
// call export function
exportMysqlToCsv($file_name);
}
?>
\ No newline at end of file
......@@ -80,6 +80,38 @@ $(".remove_non_mesh_station_form").submit(function(event) {
});
});
</script>
<script>
//TODO: add stuff for the CSV upload
$("#import_non_mesh_csv").submit(function(event) {
event.preventDefault();
var $form = $(this),
which = $form.find("input[type='submit'][name='submitNonMeshCSV']").val(),
csvFile = $form.find("input[type='file'][name='csvFile']").val(),
url = $form.attr("action");
postData = new FormData(this);
postData.append("csvFile", csvFile);
postData.append("submitNonMeshCSV", which);
// var posting = $.post(url, { submitNonMeshCSV: which, csvFile: postData } );
// posting.done(function(data) { $("#admin_content").html(data); } );
var posting = $.post({
url: url,
method: "POST",
data: postData,
contentType: false,
processData: false,
success: function(data) {
$("#admin_content").html(data);
}
});
});
</script>
<script>
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
......@@ -188,9 +220,57 @@ if (isset($_POST['remove_non_mesh_station']) == "remove_non_mesh_station") {
$_POST = array();
}
}
//deal with uploaded CSV files
if ((isset($_POST['submitNonMeshCSV']) == "Upload") && (isset($_FILES['csvFile']['type']) == "text/csv")) {
$updateTable = 0;
if (($handle = fopen($_FILES['csvFile']['tmp_name'], "r")) !== FALSE) {
// fgetcsv($handle, 0, ",");
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$station_id = strip_tags($data['0']);
$station_name = strip_tags($data['1'], "<br>");
$station_description = strip_tags($data['2'], "<br>");
$station_type = strip_tags($data['3']);
$station_lat = strip_tags($data['4']);
$station_lon = strip_tags($data['5']);
if ((strtolower($station_id) == "id") &&
(strtolower($station_name)== "name") &&
(strtolower($station_description) == "description") &&
(strtolower($station_type) == "type")) {
continue;
}
$updateTable = wxc_putMySQL("INSERT INTO marker_info " .
"(id, name, description, type, lat, lon) " .
"VALUES ".
"('$station_id', '$station_name', '$station_description', '$station_type', '$station_lat', '$station_lon') " .
"ON DUPLICATE KEY UPDATE " .
"name = '$station_name', description = '$station_description', type = '$station_type', lat = '$station_lat', lon = '$station_lon'");
}
fclose($handle);
if ($updateTable = 1) {
echo "<boldText>Succesfully imported: <greenText>" .
$_FILES['csvFile']['name'] . "</greenText></boldText>.<br>\n";
//unset the post arrays
$_POST = array();
$_FILES = array();
}
}
}
?>
<?php
echo "<br>";
echo "<a href='export_non_mesh.php'>Download CSV file of the Non Mesh Markers.</a>" .
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
"<form action='non_mesh_stations.php' id='import_non_mesh_csv' enctype='multipart/form-data' method='POST'>" .
"Upload Non Mesh Marker CSV file: " .
"<input type='file' name='csvFile' id='csvFile'>&nbsp;&nbsp;<input type='submit' value='Upload' id='submitNonMeshCSV' name='submitNonMeshCSV'></form>" . "\n";
//echo "<br>\n";
echo "The uploaded CSV file <em>must</em> follow this same format: id, name, description, type, lat, lon\n";
echo "<br>\n";
echo "<br>\n";
//display what is already there
$nonMeshStationsAndMarkers = mysqli_query($GLOBALS['sql_connection'], "SELECT id, name, description, type, lat, lon FROM marker_info") or die ("Could not get list of non mesh stations" . mysqli_error($GLOBALS['sql_connection']));
if ($nonMeshStationsAndMarkers) {
......@@ -198,6 +278,7 @@ if ($nonMeshStationsAndMarkers) {
echo "These are the non-mesh stations already in the database:<br>\n";
echo "<table id=\"existing_non_mesh_stations\">\n";
echo "<tr>\n";
echo "<th class=\"pointerCursor\" onclick=\"sortTable(0)\"><boldText>Id#</boldText></th>\n";
echo "<th class=\"pointerCursor\" onclick=\"sortTable(0)\"><boldText>Name</boldText></th>\n";
echo "<th class=\"pointerCursor\" onclick=\"sortTable(1)\"><boldText>Description</boldText></th>\n";
echo "<th class=\"pointerCursor\" onclick=\"sortTable(2)\"><boldText>Type</boldText></th>\n";
......@@ -205,12 +286,17 @@ if ($nonMeshStationsAndMarkers) {
echo "<th class=\"pointerCursor\" onclick=\"sortTable(4)\"><boldText>Lon</boldText></th>\n";
echo "</tr>\n";
foreach ($nonMeshStationsAndMarkers as $value) {
echo "\n<tr><td contenteditable='true' onBlur=\"saveToDatabase(this, 'name', '" . $value['id'] ."')\" " .
echo "\n" . "<tr>" .
"<td>" . $value['id'] . "</td>" .
"<td contenteditable='true' onBlur=\"saveToDatabase(this, 'name', '" . $value['id'] ."')\" " .
"onClick=\"showEdit(this);\">" . $value['name'] . "</td>" .
"<td contenteditable='true' onBlur=''>" . $value['description'] . "</td>" .
"<td contenteditable='true' onBlur=\"saveToDatabase(this, 'description', '" . $value['id'] ."')\" " .
"onClick=\"showEdit(this);\">" . $value['description'] . "</td>" .
"<td contenteditable='true' onBlur=''>" . $value['type'] . "</td>" .
"<td contenteditable='true' onBlur=''>" . $value['lat'] . "</td>" .
"<td contenteditable='true' onBlur=''>" . $value['lon'] . "</td>" .
"<td contenteditable='true' onBlur=\"saveToDatabase(this, 'lat', '" . $value['id'] ."')\" " .
"onClick=\"showEdit(this);\">" . $value['lat'] . "</td>" .
"<td contenteditable='true' onBlur=\"saveToDatabase(this, 'lon', '" . $value['id'] ."')\" " .
"onClick=\"showEdit(this);\">" . $value['lon'] . "</td>" .
"<td class=\"BackgroundColor\">" .
"<form action=\"non_mesh_stations.php\" class=\"remove_non_mesh_station_form\" method=\"POST\">" .
"<input type=\"hidden\" name=\"station_name\" value=\"" . $value['name'] . "\">" .
......@@ -226,7 +312,7 @@ if ($nonMeshStationsAndMarkers) {
}
echo "\n\n</table>\n";
//adapted from some site called PHP Pot (yeah, it's not that... I thought that too at first... :) )
//adapted from some site called PHP Pot
$jsEditing = <<< EOD
<script>
function showEdit(editableObj) {
......
......@@ -10,7 +10,7 @@ $html = <<< EOD
<strong>Other Admin Tasks</strong>
<br>
<br>
<a href="export2csv.php">Download CSV file of the node database.</a>
<a href="export_mesh_nodes.php">Download CSV file of the node database.</a>
<br>
<br>
<script>
......
......@@ -34,7 +34,7 @@ if (!isset($_SESSION['userLoggedIn'])) {
}elseif ($_POST['promote_user'] == "demote_user") {
$query = "update users set admin = 0 where user = '" . $_POST['user'] . "' and id = '" . $_POST['id'] . "'";
if (wxc_putMySql($query)) {
echo "<strong><greenText>Succesfully demoted " . $user . " from super-user!</greenText></strong><br>\n";
echo "<strong><greenText>Succesfully demoted " . $_POST['user'] . " from super-user!</greenText></strong><br>\n";
}else {
echo "<strong><redText>There was and error demoting the user: " . $_POST['user'] . "!!";
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment