瀏覽代碼

Add tabs and nearby locations to user page

Alan Lam 8 年之前
父節點
當前提交
96cbbf13fc
共有 4 個文件被更改,包括 440 次插入 和 173 次删除
  1. 73 0
      controllers/user_ajax.php
  2. 118 2
      public/js/app-user.js
  3. 1 1
      routes.php
  4. 248 170
      views/user.php

+ 73 - 0
controllers/user_ajax.php

@@ -24,5 +24,78 @@ switch($action) {
 			"addresses" => $output
 			"addresses" => $output
 		]);
 		]);
 
 
+		break;
+
+	case "get-locations":
+
+		$output = [];
+
+		$address_id = http::value("address_id", "int");
+		$max_distance = http::value("max_distance", "int");
+		if($max_distance <= 0) $max_distance = 25;
+
+		$address = UserAddress::queryOne([
+			"id" => $address_id,
+			"user_id" => $id,
+		]);
+
+		if(!$address) throw new Exception("Cannot find address.");
+
+		$bounds = Geolocation::getBounds($address->getGeolocation(), $max_distance);
+
+		list($min_lat, $min_lng) = $bounds->southWest;
+		list($max_lat, $max_lng) = $bounds->northEast;
+
+		$locations = Location::query([
+			"latitude" => (new Constraint("BETWEEN"))->setValues($min_lat, $max_lat),
+			"longitude" => (new Constraint("BETWEEN"))->setValues($min_lng, $max_lng),
+			"closed" => 0
+		]);
+
+		$customers = Customer::load(array_column($locations, "customer_id"));
+
+		usort($locations, function($a, $b) use ($address) {
+			return
+			Geolocation::getDistance($address->getGeolocation(), $a->getGeolocation())
+			<=>
+			Geolocation::getDistance($address->getGeolocation(), $b->getGeolocation());
+		});
+
+		foreach($locations as $location) {
+
+			$distance = Geolocation::getDistance($address->getGeolocation(), $location->getGeolocation());
+			if($distance > $max_distance) continue;
+
+			$customer = $location->getCustomer();
+
+			$customer_slug = str_replace("-", "_", Util::slug($customer->name));
+
+			$icon = null;
+			$icon_file = "{$customer_slug}.png";
+
+			if($icon_file && file_exists("public/img/markers/$icon_file")) {
+				$icon = $icon_file;
+			}
+
+			$output[] = [
+				"id" => (int) $location->id,
+				"internalId" => (int) $location->internal_id,
+				"name" => $location->name,
+				"address" => $location->getDisplayAddress(),
+				"distance" => $distance,
+				"latitude" => (double) $location->latitude,
+				"longitude" => (double) $location->longitude,
+				"icon" => $icon,
+			];
+		}
+
+		JSON::success([
+			"name" => $address->name,
+			"latitude" => (double) $address->latitude,
+			"longitude" => (double) $address->longitude,
+			"locations" => $output
+		]);
+
+
 		break;
 		break;
 }
 }

+ 118 - 2
public/js/app-user.js

@@ -1,12 +1,38 @@
+var addressesInitialised = false;
+var locationsInitialised = false;
+
+var addressMarkers = [];
+var locationMarkers = [];
+
+$(function() {
+
+	$("[data-toggle=tab]").on("shown.bs.tab", function(event) {
+
+		if(event.target.id == "addresses-tab" && addressesInitialised == false) {
+			initialiseAddresses();
+		}
+
+		if(event.target.id == "nearby-locations-tab" && locationsInitialised == false) {
+			initialiseLocations();
+		}
+
+	});
+
+	$("#address-id").change(function() {
+		initialiseLocations();
+	});
+
+});
+
 function initialiseAddresses() {
 function initialiseAddresses() {
 
 
+	let map = maps["map"];
+
 	$.get("/api/users/" + userId + "/get-addresses", {
 	$.get("/api/users/" + userId + "/get-addresses", {
 	}, function(response) {
 	}, function(response) {
 		if(!response.success) return false;
 		if(!response.success) return false;
 		if(!response.addresses) return false;
 		if(!response.addresses) return false;
 
 
-		let map = maps["map"];
-
 		let bounds = new google.maps.LatLngBounds;
 		let bounds = new google.maps.LatLngBounds;
 
 
 		for(let i in response.addresses) {
 		for(let i in response.addresses) {
@@ -33,4 +59,94 @@ function initialiseAddresses() {
 
 
 	});
 	});
 
 
+}
+
+
+
+function initialiseLocations() {
+
+	for(var i in locationMarkers) {
+		let marker = locationMarkers[i];
+		marker.setMap(null);
+		marker = null;
+	}
+
+	locationMarkers = [];
+
+	$("#nearby-locations-list-group").empty();
+
+	let map = maps["nearby-locations-map"];
+
+	$.get("/api/users/" + userId + "/get-locations", {
+		"address_id": $("#address-id").val()
+	}, function(response) {
+		if(!response.success) return false;
+		if(!response.locations) return false;
+
+		let bounds = new google.maps.LatLngBounds;
+
+		// User Address
+		let position = {
+			lat: response.latitude,
+			lng: response.longitude
+		};
+
+		let options = {
+			map: map,
+			position: position,
+			title: response.name
+		}
+
+		bounds.extend(position);
+
+		let marker = new google.maps.Marker(options);
+		locationMarkers.push(marker);
+
+		// Locations
+
+		for(let i in response.locations) {
+
+			let location = response.locations[i];
+			let position = {
+				lat: location.latitude,
+				lng: location.longitude
+			};
+
+			let options = {
+				map: map,
+				position: position,
+				title: location.name
+			}
+
+			if(location.icon) options.icon = "/img/markers/" + location.icon;
+
+			bounds.extend(position);
+
+			let marker = new google.maps.Marker(options);
+
+			locationMarkers.push(marker);
+
+			if(location.internalId == 0) {
+				location.internalId = "N/A";
+			}
+
+
+			// Add to list group
+			let listGroupItem = $("div[data-nearby-location-list-group-item-template]").clone();
+			listGroupItem.removeAttr("data-nearby-location-list-group-item-template");
+
+			listGroupItem.find("a[data-location-name]").text(location.name);
+			listGroupItem.find("span[data-location-distance]").text(location.distance + " miles");
+			listGroupItem.find("span[data-location-id]").text(location.id);
+			listGroupItem.find("span[data-location-internal-id]").text(location.internalId);
+			listGroupItem.find("small[data-location-address]").text(location.address);
+
+			listGroupItem.appendTo("#nearby-locations-list-group").show();
+
+		}
+
+		map.fitBounds(bounds);
+
+	});
+
 }
 }

+ 1 - 1
routes.php

@@ -92,7 +92,7 @@ $routes = [
 	"/^account\/users\/$id_pattern\/verify\/(id-document|photo)$/" => ["controller" => "user_verify_image", "params" => ["id", "type"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/users\/$id_pattern\/verify\/(id-document|photo)$/" => ["controller" => "user_verify_image", "params" => ["id", "type"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/users\/$id_pattern\/(id-document|photo)$/" => ["controller" => "user_verify_image", "params" => ["id", "type"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/users\/$id_pattern\/(id-document|photo)$/" => ["controller" => "user_verify_image", "params" => ["id", "type"], "require_user_level" => User::LEVEL_ADMIN],
 
 
-	"/^api\/users\/$id_pattern\/(get-addresses)$/" => ["controller" => "user_ajax", "params" => ["id", "action"], "require_user_level" => User::LEVEL_ADMIN],
+	"/^api\/users\/$id_pattern\/(get-addresses|get-locations)$/" => ["controller" => "user_ajax", "params" => ["id", "action"], "require_user_level" => User::LEVEL_ADMIN],
 
 
 	// Locations
 	// Locations
 	"/^account\/locations$/" => ["controller" => "locations", "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations$/" => ["controller" => "locations", "require_user_level" => User::LEVEL_CUSTOMER],

+ 248 - 170
views/user.php

@@ -54,198 +54,276 @@ var userId = <?=$user->id?>;
 		</div>
 		</div>
 	</div>
 	</div>
 
 
-	<div class="row">
-		<div class="col-md-8 col-lg-8 col-xl-9 mb-4">
-			<h3>General Details</h3>
+	<ul class="nav nav-tabs mb-3" id="user-tab" role="tablist">
+		<li class="nav-item">
+			<a class="nav-link active" id="general-details-tab" data-toggle="tab" href="#general-details" role="tab" aria-controls="general-details" aria-selected="true">General Details</a>
+		</li>
+		<li class="nav-item">
+			<a class="nav-link" id="addresses-tab" data-toggle="tab" href="#addresses" role="tab" aria-controls="addresses" aria-selected="true">Addresses</a>
+		</li>
+		<li class="nav-item">
+			<a class="nav-link" id="tests-tab" data-toggle="tab" href="#tests" role="tab" aria-controls="tests" aria-selected="true">Tests</a>
+		</li>
+		<li class="nav-item">
+			<a class="nav-link" id="nearby-locations-tab" data-toggle="tab" href="#nearby-locations" role="tab" aria-controls="nearby-locations" aria-selected="true">Nearby Locations</a>
+		</li>
+	</ul>
 
 
-			<div class="list-group">
-				<?php
-				foreach($details as $title => $html) {
-					?>
-					<div class="list-group-item">
-						<div class="row">
-							<div class="col-sm">
-								<?=htmlspecialchars($title)?>
-							</div>
-							<div class="col-sm-auto">
-								<?=$html?>
+	<div class="tab-content" id="user-tab-content">
+		<div class="tab-pane fade show active" id="general-details" role="tabpanel" aria-labelledby="general-details-tab">
+			<div class="row">
+				<div class="col-md-8 col-lg-8 col-xl-9 mb-4">
+					<h3>General Details</h3>
+
+					<div class="list-group">
+						<?php
+						foreach($details as $title => $html) {
+							?>
+							<div class="list-group-item">
+								<div class="row">
+									<div class="col-sm">
+										<?=htmlspecialchars($title)?>
+									</div>
+									<div class="col-sm-auto">
+										<?=$html?>
+									</div>
+								</div>
 							</div>
 							</div>
+							<?php
+						}
+						?>
+					</div>
+				</div>
+				<div class="col-md mb-4">
+					<h3>Photo / ID</h3>
+					<div class="card mb-3">
+						<div class="card-body text-center">
+							<a href="/account/users/<?=$user->id?>/photo">
+								<img class="img-fluid" src="/account/users/<?=$user->id?>/photo" alt="Photo" />
+							</a>
+						</div>
+					</div>
+					<div class="card">
+						<div class="card-body text-center">
+							<a href="/account/users/<?=$user->id?>/id-document">
+								<img class="img-fluid" src="/account/users/<?=$user->id?>/id-document" alt="ID Document" />
+							</a>
 						</div>
 						</div>
 					</div>
 					</div>
-					<?php
-				}
-				?>
-			</div>
-		</div>
-		<div class="col-md mb-4">
-			<h3>Photo / ID</h3>
-			<div class="card mb-3">
-				<div class="card-body text-center">
-					<a href="/account/users/<?=$user->id?>/photo">
-						<img class="img-fluid" src="/account/users/<?=$user->id?>/photo" alt="Photo" />
-					</a>
 				</div>
 				</div>
 			</div>
 			</div>
-			<div class="card">
-				<div class="card-body text-center">
-					<a href="/account/users/<?=$user->id?>/id-document">
-						<img class="img-fluid" src="/account/users/<?=$user->id?>/id-document" alt="ID Document" />
-					</a>
+		</div>
+		<div class="tab-pane fade" id="addresses" role="tabpanel" aria-labelledby="addresses-tab">
+			<h3>Addresses</h3>
+
+			<?php
+			if(empty($addresses)) {
+				?>
+				<div class="alert alert-info">This user has no addresses.</div>
+				<?php
+			}
+			else {
+				?>
+				<div class="row">
+					<div class="col-lg-6 mb-4">
+						<div class="list-group">
+						<?php
+						foreach($addresses as $address) {
+							?>
+							<div class="list-group-item">
+								<strong><?=htmlspecialchars($address->name)?></strong><br />
+								<small><?=htmlspecialchars($address->getDisplayAddress())?></small>
+							</div>
+							<?php
+						}
+						?>
+						</div>
+					</div>
+					<div class="col-lg-6 mb-4">
+						<div id="map" style="height: 248px;" data-maps data-lat="0" data-lng="0" data-zoom="10"></div>
+					</div>
 				</div>
 				</div>
-			</div>
+				<?php
+			}
+			?>
 		</div>
 		</div>
-	</div>
+		<div class="tab-pane fade" id="tests" role="tabpanel" aria-labelledby="tests-tab">
+			<h3>Assigned Tests</h3>
+
+			<?php
+			if(empty($pending_tests)) {
+				?>
+				<div class="alert alert-info">This user has no assigned tests.</div>
+				<?php
+			}
+			else {
+				?>
+				<table class="table table-sm table-hover">
+					<col style="width: 10%;" />
+					<col style="width: 40%;" />
+					<col style="width: 20%;" />
+					<col style="width: 10%;" />
+					<col style="width: 10%;" />
+					<thead>
+						<th>ID</th>
+						<th>Location</th>
+						<th>Project</th>
+						<th>Date Due</th>
+						<th></th>
+					</thead>
+				<?php
+				foreach($pending_tests as $test) {
+					$project = $test->getProject();
+					$location = $test->getLocation();
 
 
+					if(!is_null($test->dts_due)) {
+						$date = explode(" ", $test->dts_due)[0];
+						$unix_date = strtotime($date);
+					}
+					else {
+						$date = explode(" ", $project->dts_deadline)[0];
+						$unix_date = strtotime($date);
+					}
 
 
-	<h3>Addresses</h3>
+					$date = date("d/m/Y", $unix_date);
 
 
-	<?php
-	if(empty($addresses)) {
-		?>
-		<div class="alert alert-info">This user has no addresses.</div>
-		<?php
-	}
-	else {
-		?>
-		<div class="row">
-			<div class="col-lg-6 mb-4">
-				<div class="list-group">
+					?>
+						<tr>
+							<td><?=$test->id?></td>
+							<td>
+								<a href="/account/locations/<?=$location->id?>"><?=htmlspecialchars($location->name)?></a><br />
+								<small><?=htmlspecialchars($location->getDisplayAddress(["address_1", "address_2", "city", "postcode"]))?></small>
+							</td>
+							<td class="align-middle">
+								<a href="/account/projects/<?=$project->id?>"><?=htmlspecialchars($project->name)?></a>
+							</td>
+							<td class="align-middle">
+								<?=$date?>
+							</td>
+							<td class="align-middle text-right">
+							</td>
+						</tr>
+					<?php
+				}
+				?>
+				</table>
 				<?php
 				<?php
-				foreach($addresses as $address) {
+			}
+			?>
+
+			<h3>Completed Tests</h3>
+
+			<?php
+			if(empty($completed_tests)) {
+				?>
+				<div class="alert alert-info">This user has no completed tests.</div>
+				<?php
+			}
+			else {
+				?>
+				<table class="table table-sm table-hover">
+					<col style="width: 10%;" />
+					<col style="width: 40%;" />
+					<col style="width: 20%;" />
+					<col style="width: 10%;" />
+					<col style="width: 10%;" />
+					<thead>
+						<th>ID</th>
+						<th>Location</th>
+						<th>Project</th>
+						<th>Date</th>
+						<th></th>
+					</thead>
+				<?php
+				foreach($completed_tests as $test) {
+					$project = $test->getProject();
+					$location = $test->getLocation();
+					$date = explode(" ", $test->dts_start)[0];
+					$date = date("d/m/Y", strtotime($date));
 					?>
 					?>
-					<div class="list-group-item">
-						<strong><?=htmlspecialchars($address->name)?></strong><br />
-						<small><?=htmlspecialchars($address->getDisplayAddress())?></small>
-					</div>
+						<tr>
+							<td><?=$test->id?></td>
+							<td>
+								<a href="/account/locations/<?=$location->id?>"><?=htmlspecialchars($location->name)?></a><br />
+								<small><?=htmlspecialchars($location->getDisplayAddress(["address_1", "address_2", "city", "postcode"]))?></small>
+							</td>
+							<td class="align-middle">
+								<a href="/account/projects/<?=$project->id?>"><?=htmlspecialchars($project->name)?></a>
+							</td>
+							<td class="align-middle">
+								<?=$date?>
+							</td>
+							<td class="align-middle text-right">
+								<a class="btn btn-outline-primary" href="/account/tests/<?=$test->id?>">View</a>
+							</td>
+						</tr>
 					<?php
 					<?php
 				}
 				}
 				?>
 				?>
-				</div>
-			</div>
-			<div class="col-lg-6 mb-4">
-				<div id="map" style="height: 248px;" data-maps data-lat="0" data-lng="0" data-zoom="5" data-callback="initialiseAddresses"></div>
-			</div>
+				</table>
+				<?php
+			}
+			?>
 		</div>
 		</div>
-		<?php
-	}
-	?>
-
-	<h3>Assigned Tests</h3>
-
-	<?php
-	if(empty($pending_tests)) {
-		?>
-		<div class="alert alert-info">This user has no assigned tests.</div>
-		<?php
-	}
-	else {
-		?>
-		<table class="table table-sm table-hover">
-			<col style="width: 10%;" />
-			<col style="width: 40%;" />
-			<col style="width: 20%;" />
-			<col style="width: 10%;" />
-			<col style="width: 10%;" />
-			<thead>
-				<th>ID</th>
-				<th>Location</th>
-				<th>Project</th>
-				<th>Date Due</th>
-				<th></th>
-			</thead>
-		<?php
-		foreach($pending_tests as $test) {
-			$project = $test->getProject();
-			$location = $test->getLocation();
-
-			if(!is_null($test->dts_due)) {
-				$date = explode(" ", $test->dts_due)[0];
-				$unix_date = strtotime($date);
+		<div class="tab-pane fade" id="nearby-locations" role="tabpanel" aria-labelledby="nearby-locations-tab">
+			<h3>Nearby Locations</h3>
+
+			<?php
+			if(empty($addresses)) {
+				?>
+				<div class="alert alert-info">This user has no addresses so nearby locations cannot be displayed.</div>
+				<?php
 			}
 			}
 			else {
 			else {
-				$date = explode(" ", $project->dts_deadline)[0];
-				$unix_date = strtotime($date);
-			}
+				$address = $addresses[0];
+
+				?>
+				<div class="row">
+					<div class="col-lg-6 mb-4">
+						<select class="form-control mb-3" id="address-id">
+						<?php
+						foreach($addresses as $address) {
+							?>
+							<option value="<?=$address->id?>"><?=htmlspecialchars($address->name)?> - <?=htmlspecialchars($address->getDisplayAddress())?></option>
+							<?php
+						}
+						?>
+						</select>
 
 
-			$date = date("d/m/Y", $unix_date);
+						<div class="list-group" id="nearby-locations-list-group">
+						</div>
 
 
+						<div data-nearby-location-list-group-item-template class="list-group-item" style="display: none;" aria-hidden="true">
+							<div class="row">
+								<div class="col-md">
+									<a href="#" data-location-name>Location Name</a>
+								</div>
+								<div class="col-md-auto">
+									Distance: <span data-location-distance></span>
+								</div>
+							</div>
+							<span class="badge badge-primary">ID: <span data-location-id>123</span></span>
+							<span class="badge badge-info">Internal ID: <span data-location-internal-id>123</span></span><br />
+							<small data-location-address>Address</small>
+						</div>
+
+					</div>
+					<div class="col-lg-6 mb-4">
+						<div id="nearby-locations-map" style="height: 480px;" data-maps data-lat="<?=$address->latitude?>" data-lng="<?=$address->longitude?>" data-zoom="10">
+					</div>
+					</div>
+				</div>
+				<?php
+			}
 			?>
 			?>
-				<tr>
-					<td><?=$test->id?></td>
-					<td>
-						<a href="/account/locations/<?=$location->id?>"><?=htmlspecialchars($location->name)?></a><br />
-						<small><?=htmlspecialchars($location->getDisplayAddress(["address_1", "address_2", "city", "postcode"]))?></small>
-					</td>
-					<td class="align-middle">
-						<a href="/account/projects/<?=$project->id?>"><?=htmlspecialchars($project->name)?></a>
-					</td>
-					<td class="align-middle">
-						<?=$date?>
-					</td>
-					<td class="align-middle text-right">
-					</td>
-				</tr>
-			<?php
-		}
-		?>
-		</table>
-		<?php
-	}
-	?>
-
-	<h3>Completed Tests</h3>
-
-	<?php
-	if(empty($completed_tests)) {
-		?>
-		<div class="alert alert-info">This user has no completed tests.</div>
-		<?php
-	}
-	else {
-		?>
-		<table class="table table-sm table-hover">
-			<col style="width: 10%;" />
-			<col style="width: 40%;" />
-			<col style="width: 20%;" />
-			<col style="width: 10%;" />
-			<col style="width: 10%;" />
-			<thead>
-				<th>ID</th>
-				<th>Location</th>
-				<th>Project</th>
-				<th>Date</th>
-				<th></th>
-			</thead>
-		<?php
-		foreach($completed_tests as $test) {
-			$project = $test->getProject();
-			$location = $test->getLocation();
-			$date = explode(" ", $test->dts_start)[0];
-			$date = date("d/m/Y", strtotime($date));
-			?>
-				<tr>
-					<td><?=$test->id?></td>
-					<td>
-						<a href="/account/locations/<?=$location->id?>"><?=htmlspecialchars($location->name)?></a><br />
-						<small><?=htmlspecialchars($location->getDisplayAddress(["address_1", "address_2", "city", "postcode"]))?></small>
-					</td>
-					<td class="align-middle">
-						<a href="/account/projects/<?=$project->id?>"><?=htmlspecialchars($project->name)?></a>
-					</td>
-					<td class="align-middle">
-						<?=$date?>
-					</td>
-					<td class="align-middle text-right">
-						<a class="btn btn-outline-primary" href="/account/tests/<?=$test->id?>">View</a>
-					</td>
-				</tr>
-			<?php
-		}
-		?>
-		</table>
-		<?php
-	}
-	?>
+
+
+
+		</div>
+	</div>
+
+
+
 
 
 
 
 </div>
 </div>