non_mesh_stations.php 13.9 KB
Newer Older
Eric - kg6wxc's avatar
Eric - kg6wxc committed
1
2
3
<?php
/*Aug 12, 2017 8:52:20 PM
*non_mesh_stations.php
4
*Eric Satterlee - KG6WXC
Eric - kg6wxc's avatar
Eric - kg6wxc committed
5
6
*/
//simple page to add non-mesh stations to the map
Eric - kg6wxc's avatar
Eric - kg6wxc committed
7
$INCLUDE_DIR = "../..";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
8
9
$USER_SETTINGS = parse_ini_file($INCLUDE_DIR . "/scripts/user-settings.ini");
require $INCLUDE_DIR . "/scripts/wxc_functions.inc";
10
@include $INCLUDE_DIR . "/custom.inc";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

$sql_connection = wxc_connectToMySQL();

?>
<script>
$("#add_non_mesh_station").submit(function(event) {
    event.preventDefault();
    
    var $form = $(this),
    	which = $form.find("input[type='hidden'][name='add_new_non_mesh_station']").val(),
        name = $form.find("input[name='station_name']").val(),
        description = $form.find("input[name='station_description']").val(),
        type = $form.find("select[name='station_type']").val(),
        lat = $form.find("input[name='station_lat']").val(),
        lon = $form.find("input[name='station_lon']").val(),
        url = $form.attr("action");
    
    var posting = $.post(url, {add_new_non_mesh_station: which,
        				station_name: name,
        				station_description: description,
        				station_type: type,
        				station_lat: lat,
        				station_lon: lon
        			});

	//posting.success: function(reponse) {
    //    $('#admin_content').html(reponse);
    //}
    //posting.done(function(data) {
    //    var content = $(data).find("#content");
    //    $("#admin_content").empty().append(content);
    //});
    posting.done(function(data) {
        //var content = $(data).html;
        $("#admin_content").html(data);
    });
});
</script>
<script>
$(".remove_non_mesh_station_form").submit(function(event) {
    event.preventDefault();
    
    var $form = $(this),
    	which = $form.find("input[type='hidden'][name='remove_non_mesh_station']").val(),
        name = $form.find("input[type='hidden'][name='station_name']").val(),
        description = $form.find("input[type='hidden'][name='station_description']").val(),
        type = $form.find("input[type='hidden'][name='station_type']").val(),
        lat = $form.find("input[type='hidden'][name='station_lat']").val(),
        lon = $form.find("input[type='hidden'][name='station_lon']").val(),
        url = $form.attr("action");
    
    var posting = $.post(url, {remove_non_mesh_station: which,
        				station_name: name,
        				station_description: description,
        				station_type: type,
        				station_lat: lat,
        				station_lon: lon
        			});

	//posting.success: function(reponse) {
    //    $('#admin_content').html(reponse);
    //}
    //posting.done(function(data) {
    //    var content = $(data).find("#content");
    //    $("#admin_content").empty().append(content);
    //});
    posting.done(function(data) {
        //var content = $(data).html;
        $("#admin_content").html(data);
    });
});
</script>
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

<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>

Eric - kg6wxc's avatar
Eric - kg6wxc committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<script>
function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  table = document.getElementById("existing_non_mesh_stations");
  switching = true;
  //Set the sorting direction to ascending:
  dir = "asc";
  /*Make a loop that will continue until
  no switching has been done:*/
  while (switching) {
    //start by saying: no switching is done:
    switching = false;
    rows = table.getElementsByTagName("TR");
    /*Loop through all table rows (except the
    first, which contains table headers):*/
    for (i = 1; i < (rows.length - 1); i++) {
      //start by saying there should be no switching:
      shouldSwitch = false;
      /*Get the two elements you want to compare,
      one from current row and one from the next:*/
      x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];
      /*check if the two rows should switch place,
      based on the direction, asc or desc:*/
      if (dir == "asc") {
        if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
          //if so, mark as a switch and break the loop:
          shouldSwitch= true;
          break;
        }
      } else if (dir == "desc") {
        if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
          //if so, mark as a switch and break the loop:
          shouldSwitch= true;
          break;
        }
      }
    }
    if (shouldSwitch) {
      /*If a switch has been marked, make the switch
      and mark that a switch has been done:*/
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      //Each time a switch is done, increase this count by 1:
      switchcount ++;
    } else {
      /*If no switching has been done AND the direction is "asc",
      set the direction to "desc" and run the while loop again.*/
      if (switchcount == 0 && dir == "asc") {
        dir = "desc";
        switching = true;
      }
    }
  }
}
</script>

<form action="non_mesh_stations.php" id="add_non_mesh_station" method="POST">
	Input a new "Non-MESH" station:<br>
	Name: <input type="text" name="station_name" value="">
	Description: <input type="text" name="station_description" value="">
	Type: <select name="station_type">
		<option value="operator">Radio Operator</option>
		<option value="firedepartment">Fire Department</option>
		<option value="police">Police Station</option>
		<option value="eoc">EOC</option>
		<option value="hospital">Hospital</option>
182
		<option value="futuremesh">Future Mesh</option>
Eric - kg6wxc's avatar
Eric - kg6wxc committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
	</select>
	Lat: <input type="text" name="station_lat" value="">
	Lon: <input type="text" name="station_lon" value="">
	<input type="hidden" name="add_new_non_mesh_station" value="add_new_non_mesh_station">
	<input type="submit" value="Add">
</form>


<?php
//add something new
if ((isset($_POST['add_new_non_mesh_station']) == "add_new_non_mesh_station") && (isset($_POST['station_name'])) && (isset($_POST['station_description'])) && (isset($_POST['station_type'])) && (isset($_POST['station_lat'])) && (isset($_POST['station_lon']))) {
    $newStationName = $_POST['station_name'];
    $newStationDescription = $_POST['station_description'];
    $newStationType = $_POST['station_type'];
    $newStationLat = $_POST['station_lat'];
    $newStationLon = $_POST['station_lon'];
    
    
    $addedToSql = wxc_putMySQL("INSERT INTO marker_info (name, description, type, lat, lon) VALUES ('$newStationName', '$newStationDescription', '$newStationType', '$newStationLat', '$newStationLon')");
    if ($addedToSql = 1) {
        echo "<boldText>Sucessfully added <greenText>" . $newStationName . "</greenText> to the database</boldText>.<br>";
        //UNSET THE $_POST array
        $_POST = array();
    }
}
//remove something already there
if (isset($_POST['remove_non_mesh_station']) == "remove_non_mesh_station") {
    $removedName = $_POST['station_name'];
    $removedDesc = $_POST['station_description'];
    $removedType = $_POST['station_type'];
    $removedLat = $_POST['station_lat'];
    $removedLon = $_POST['station_lon'];
    
    $removeFromSql = wxc_putMySQL("DELETE FROM marker_info WHERE name = '$removedName' AND description = '$removedDesc' AND type = '$removedType' AND lat = '$removedLat' AND lon = '$removedLon'");
    if ($removeFromSql = 1) {
        echo "<boldText>Removed <redText>" . $removedName . "</redText> from the database</boldText>.<br>";
        //UNSET THE $_POST array
        $_POST = array();
    }
}
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

//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();
			}
		}
}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
261
262
263
?>

<?php
264
265
266
267
268
269
270
271
272
273
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";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
274
//display what is already there
Eric - kg6wxc's avatar
Eric - kg6wxc committed
275
$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']));
Eric - kg6wxc's avatar
Eric - kg6wxc committed
276
277
278
279
280
if ($nonMeshStationsAndMarkers) {
    $nonMeshStationsAndMarkers = mysqli_fetch_all($nonMeshStationsAndMarkers, MYSQLI_ASSOC);
    echo "These are the non-mesh stations already in the database:<br>\n";
    echo "<table id=\"existing_non_mesh_stations\">\n";
    echo "<tr>\n";
281
    echo "<th class=\"pointerCursor\" onclick=\"sortTable(0)\"><boldText>Id#</boldText></th>\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
282
283
284
285
286
287
288
    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";
    echo "<th class=\"pointerCursor\" onclick=\"sortTable(3)\"><boldText>Lat</boldText></th>\n";
    echo "<th class=\"pointerCursor\" onclick=\"sortTable(4)\"><boldText>Lon</boldText></th>\n";
    echo "</tr>\n";
    foreach ($nonMeshStationsAndMarkers as $value) {
289
290
291
292
293
294
        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=\"saveToDatabase(this, 'description', '" . $value['id'] ."')\" " .
	        "onClick=\"showEdit(this);\">" . $value['description'] . "</td>" .
Eric - kg6wxc's avatar
Eric - kg6wxc committed
295
            "<td contenteditable='true' onBlur=''>" . $value['type'] . "</td>" .
296
297
298
299
            "<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>" .
Eric - kg6wxc's avatar
Eric - kg6wxc committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
            "<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'] . "\">" .
            "<input type=\"hidden\" name=\"station_description\" value=\"" . $value['description'] . "\">" .
            "<input type=\"hidden\" name=\"station_type\" value=\"" . $value['type'] . "\">" .
            "<input type=\"hidden\" name=\"station_lat\" value=\"" . $value['lat'] . "\">" .
            "<input type=\"hidden\" name=\"station_lon\" value=\"" . $value['lon'] . "\">" .
            "<input type=\"hidden\" name=\"remove_non_mesh_station\" value=\"remove_non_mesh_station\">" .
            "<input type=\"submit\" value=\"Remove\">" .
            "</form>" .
            "</td>" .
            "</tr>";
    }
    echo "\n\n</table>\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
314
    
315
    //adapted from some site called PHP Pot
Eric - kg6wxc's avatar
Eric - kg6wxc committed
316
317
318
319
320
321
322
323
324
325
    $jsEditing = <<< EOD
<script>
	function showEdit(editableObj) {
		$(editableObj).css("background", "#FFF");
	}
	function saveToDatabase(editableObj, column, id) {
		$(editableObj).css("background", "#FFF url(loaderIcon.gif) no-repeater right");
		
		//quick fix for a funny <br> that keeps showing up in the post data
		var edit = editableObj.innerHTML.replace(/<br>/, "");
326

Eric - kg6wxc's avatar
Eric - kg6wxc committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
		$.ajax({
			url: "update_nonmesh_marker.php",
			type: "POST",
			
			//part of the quick fix
			//data: 'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
			
			data: 'nonmesh=nonmesh&column='+column+'&editval='+edit+'&id='+id,
			success: function(data) {
				//$(editableObj).css("background", "#FDFDFD");
	            $(editableObj).css("background-color", "inherit");
	            $(editableObj).html(data);
				$("#admin_content").load("non_mesh_stations.php");
			}
			
		});
	}
</script>
EOD;
    
    echo $jsEditing . "\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
348
}
349
?>