Explorar el Código

Merged location forms.

Alan Lam hace 8 años
padre
commit
8cc1f023bc
Se han modificado 3 ficheros con 63 adiciones y 41 borrados
  1. 30 27
      controllers/location_add.php
  2. 1 1
      controllers/location_edit.php
  3. 32 13
      views/location_form.php

+ 30 - 27
controllers/location_add.php

@@ -21,34 +21,37 @@ if($submit) {
 		$form_exception = new FormException;
 
 		$location->name = http::value("name", "trim");
-		$location->address_1 = http::value("address", "trim");
+		$location->address_1 = http::value("address_1", "trim");
+		$location->address_2 = http::value("address_2", "trim");
+		$location->city = http::value("city", "trim");
+		$location->county = http::value("county", "trim");
+		$location->postal_code = http::value("postal_code", "trim");
+
+		$location->shop_left = http::value("shop_left", "trim");
+		$location->shop_right = http::value("shop_right", "trim");
 
 		if(empty($location->name)) $form_exception->addError("Name is required.");
-		if(empty($location->address_1)) {
-			$form_exception->addError("Full address is required.");
-		}
-		else {
-			// Geocode the full address
-			try {
-				$results = Geolocation::geocode($location->address_1);
-
-				$result = $results[0];
-
-				foreach($result->address_components as $component) {
-					$types = $component->types;
-					if(in_array("postal_code", $types)) $location->postal_code = $component->long_name;
-					if(in_array("route", $types)) $location->address_1 = $component->long_name;
-					if(in_array("postal_town", $types)) $location->city = $component->long_name;
-					if(in_array("administrative_area_level_1", $types)) $location->state = $component->long_name;
-					if(in_array("country", $types)) $location->country = $component->long_name;
-				}
-
-				$location->latitude = $result->geometry->location->lat;
-				$location->longitude = $result->geometry->location->lng;
-			}
-			catch(Exception $e) {
-				$form_exception->addError("Failed to get location from full address: " . $e->getMessage());
+		if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");
+		if(empty($location->city)) $form_exception->addError("City is required.");
+		if(empty($location->postal_code)) $form_exception->addError("Postal code is required.");
+
+		// Geocode
+		try {
+			$results = Geolocation::geocode($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]));
+
+			$result = $results[0];
+
+			foreach($result->address_components as $component) {
+				$types = $component->types;
+				if(in_array("administrative_area_level_1", $types)) $location->state = $component->long_name;
+				if(in_array("country", $types)) $location->country = $component->long_name;
 			}
+
+			$location->latitude = $result->geometry->location->lat;
+			$location->longitude = $result->geometry->location->lng;
+		}
+		catch(Exception $e) {
+			$form_exception->addError("Failed to get latitude/longitude from address.");
 		}
 
 		if($form_exception->hasErrors()) throw $form_exception;
@@ -57,7 +60,7 @@ if($submit) {
 		if(!$success) throw new Exception("Failed to add location at this time. Please try again later.");
 
 		Core::addAlert("success", "Location added successfully.");
-		HTTP::redirect("/account/locations/$location->id");
+		HTTP::redirect("/account/locations/$location->id/edit");
 	}
 	catch(FormException $e) {
 		$e->generateAlerts();
@@ -68,7 +71,7 @@ if($submit) {
 }
 
 View::includeOnce("header", compact("page_title"));
-View::include("location_add_form", compact("location"));
+View::include("location_form", compact("location"));
 View::includeOnce("footer");
 
 ?>

+ 1 - 1
controllers/location_edit.php

@@ -111,7 +111,7 @@ Core::addBreadcrumb("/account/locations/$id", $location->name);
 Core::addBreadcrumb("/account/locations/$id/edit", $page_title);
 
 View::includeOnce("header", compact("page_title"));
-View::include("location_edit_form", compact("location", "photos"));
+View::include("location_form", compact("location", "photos"));
 View::includeOnce("footer");
 
 ?>

+ 32 - 13
views/location_edit_form.php → views/location_form.php

@@ -1,5 +1,5 @@
 <div class="container mt-3 py-4">
-	<h2>Edit Location</h2>
+	<h2><?=($location->id ? "Edit" : "Add")?> Location</h2>
 
 	<form method="post" enctype="multipart/form-data">
 		<div class="row">
@@ -57,14 +57,20 @@
 					</div>
 				</div>
 
-				<h3>Add Photo</h3>
+				<?php
+				if($location->id) {
+					?>
+					<h3>Add Photo</h3>
 
-				<div class="row mb-3">
-					<label class="col-sm-4 col-form-label" for="photo">JPEG File</label>
-					<div class="col-sm">
-						<input class="form-control-file" type="file" name="photo" id="photo" />
+					<div class="row mb-3">
+						<label class="col-sm-4 col-form-label" for="photo">JPEG File</label>
+						<div class="col-sm">
+							<input class="form-control-file" type="file" name="photo" id="photo" />
+						</div>
 					</div>
-				</div>
+					<?php
+				}
+				?>
 
 				<?php
 				if(!empty($photos)) {
@@ -98,16 +104,29 @@
 
 				<h3>Map</h3>
 
-				<div class="mb-3" id="map" style="height: 330px;" data-maps data-lat="<?=$location->latitude?>" data-lng="<?=$location->longitude?>" data-zoom="14" data-marker="true" data-marker-draggable="true" data-marker-dragend-target="#map-data"></div>
+				<?php
+				if(empty($location->id)) {
+					?>
+					<div class="alert alert-info">
+						<i class="fal fa-fw fa-info-circle"></i> Map will become available when location is added.
+					</div>
+					<?php
+				}
+				else {
+					?>
+					<div class="mb-3" id="map" style="height: 330px;" data-maps data-lat="<?=$location->latitude?>" data-lng="<?=$location->longitude?>" data-zoom="14" data-marker="true" data-marker-draggable="true" data-marker-dragend-target="#map-data"></div>
 
-				<div id="map-data">
-					<input type="hidden" name="latitude" data-latitude value="<?=$location->latitude?>" />
-					<input type="hidden" name="longitude" data-longitude value="<?=$location->longitude?>" />
-				</div>
+					<div id="map-data">
+						<input type="hidden" name="latitude" data-latitude value="<?=$location->latitude?>" />
+						<input type="hidden" name="longitude" data-longitude value="<?=$location->longitude?>" />
+					</div>
+					<?php
+				}
+				?>
 
 			</div>
 		</div>
 
-		<button class="btn btn-primary" type="submit" name="submit" value="<?=$_SESSION["csrf_token"]?>">Save</button>
+		<button class="btn btn-primary" type="submit" name="submit" value="<?=$_SESSION["csrf_token"]?>">Submit</button>
 	</form>
 </div>