Jelajahi Sumber

Add support for cherry picking locations for export

Alan Lam 8 tahun lalu
induk
melakukan
e793296ae7
3 mengubah file dengan 95 tambahan dan 6 penghapusan
  1. 2 0
      controllers/locations.php
  2. 86 3
      public/js/app-locations.js
  3. 7 3
      views/locations.php

+ 2 - 0
controllers/locations.php

@@ -23,10 +23,12 @@ Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title)
 $nvp = ["customer_id" => $customer->id];
 
 $id = http::value("id", "int");
+$ids = http::value("ids", "int");
 $internal_id = http::value("internal_id", "trim");
 $name = http::value("name", "trim");
 
 if($id) $nvp["id"] = $id;
+if($ids) $nvp["id"] = $ids;
 if($internal_id) $nvp["internal_id"] = db::escape($internal_id);
 if($name) $nvp["name"] = (new Constraint("CONTAINS"))->setValues(db::escape($name));
 

+ 86 - 3
public/js/app-locations.js

@@ -1,5 +1,13 @@
+var infoWindow = false;
+var exportIds = [];
+var markerMap = {};
+
 function initialiseLocations() {
 
+	if(!infoWindow) {
+		infoWindow = new google.maps.InfoWindow();
+	}
+
 	$.get("/api/customers/" + customerId + "/get-locations", {
 		id: $("#id").val(),
 		internal_id: $("#internal-id").val(),
@@ -27,22 +35,97 @@ function initialiseLocations() {
 			}
 
 			if(location.icon) {
-				options.icon = "/img/markers/" + location.icon;
+				options.icon = "/img/markers/building-red.png";
 			}
 
 			bounds.extend(position);
 
+			let key = "marker-" + location.id;
+
 			let marker = new google.maps.Marker(options);
-			marker.locationId = location.id;
+			marker.location = location;
 
 			marker.addListener("click", function() {
-				window.open("/account/locations/" + this.locationId, "_blank");
+				displayInfoWindow(this);
 			});
 
+			markerMap[key] = marker;
+
 		}
 
 		map.fitBounds(bounds);
 
 	});
 
+	$(document).on("click", "a[data-export-location-id]", function(event) {
+
+		let locationId = parseInt($(this).attr("data-export-location-id"));
+		let index = exportIds.indexOf(locationId);
+		let key = "marker-" + locationId;
+		let marker = markerMap[key];
+
+		if(index == -1) {
+			exportIds.push(locationId);
+			marker.setOptions({
+				icon: "/img/markers/building-green.png"
+			});
+		}
+		else {
+			exportIds.splice(index, 1);
+			marker.setOptions({
+				icon: "/img/markers/building-red.png"
+			});
+		}
+
+		displayInfoWindow(marker);
+	});
+
+	$("#export-csv-button, #export-kml-button").click(function(event) {
+
+		if(exportIds.length > 0) {
+			event.preventDefault();
+
+			let action = $(this).attr("data-action");
+
+			$("#locations-export-form input[name='ids[]']").remove();
+			$("#locations-export-form input[name='action']").val(action);
+
+			for(let i in exportIds) {
+				let locationId = exportIds[i];
+				$("#locations-export-form").append("<input type=\"hidden\" name=\"ids[]\" value=\"" + locationId + "\" />");
+			}
+
+			$("#locations-export-form").submit();
+		}
+
+	});
+
+}
+
+function displayInfoWindow(marker) {
+
+	let locationId = parseInt(marker.location.id);
+	let index = exportIds.indexOf(locationId);
+
+	let exportText = "Select for Export";
+	let exportClass = "btn-primary";
+
+	if(index > -1) {
+		exportText = "Deselect for Export";
+		exportClass = "btn-success";
+	}
+
+	let content = "<div class=\"container-fluid\" style=\"font-size: 1rem; font-weight: normal;\">";
+	content += "<p>" + marker.location.name + "</p>\n";
+
+	content += "<div class=\"row\">";
+
+	content += "<div class=\"col\"><a class=\"btn btn-block " + exportClass + "\" href=\"#\" data-export-location-id=\"" + marker.location.id + "\">" + exportText + "</a></div>";
+	content += "<div class=\"col\"><a class=\"btn btn-block btn-primary\" href=\"/account/locations/" + marker.location.id + "\" target=\"_blank\">View Details</a></div>";
+	content += "</div>";
+	content += "</div>";
+
+	infoWindow.setContent(content);
+	infoWindow.open(map, marker);
+
 }

+ 7 - 3
views/locations.php

@@ -37,8 +37,8 @@ var customerId = <?=$customer->id?>;
 				</button>
 				<div class="dropdown-menu dropdown-menu-right" aria-labelledby="actions-button">
 					<a class="dropdown-item" href="/account/locations/add?customer_id=<?=$customer->id?>/edit">Add</a>
-					<a class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export">Export CSV</a>
-					<a class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export-kml">Export KML</a>
+					<a id="export-csv-button" class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export" data-action="export">Export CSV</a>
+					<a id="export-kml-button" class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export-kml" data-action="export-kml">Export KML</a>
 				</div>
 			</div>
 
@@ -130,4 +130,8 @@ var customerId = <?=$customer->id?>;
 	<?php
 	View::include("location_list", compact("locations"));
 	?>
-</div>
+</div>
+
+<form method="post" id="locations-export-form">
+	<input type="hidden" name="action" value="" />
+</form>