Przeglądaj źródła

Add support for adding secondary address in registration form and fix form bugs

Alan Lam 7 lat temu
rodzic
commit
881fd17038
3 zmienionych plików z 66 dodań i 31 usunięć
  1. 22 24
      controllers/register.php
  2. 25 0
      engine/user_address.php
  3. 19 7
      views/register.php

+ 22 - 24
controllers/register.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 if(Core::$user) HTTP::redirect("/account");
 
@@ -7,10 +7,15 @@ $page_title = __("Register");
 Core::addBreadcrumb("/register", $page_title);
 
 $user = new User;
-$user_address = new UserAddress();
+
+$user_address = new UserAddress;
 $user_address->name = __("Current Residence");
 $user_address->country = DEFAULT_COUNTRY;
 
+$secondary_user_address = new UserAddress;
+$secondary_user_address->name = __("Secondary Residence");
+$secondary_user_address->country = DEFAULT_COUNTRY;
+
 try {
 	$submit = http::value("submit");
 
@@ -39,6 +44,9 @@ try {
 		$user_address->postal_code = http::value("postal_code");
 		$user_address->country = http::value("country");
 
+		$secondary_user_address->postal_code = http::value("secondary_postal_code");
+		$secondary_user_address->country = http::value("country");
+
 		// Referrer
 		$referrer_id = http::value("r", "int");
 		$referrer = User::get($referrer_id);
@@ -61,25 +69,12 @@ try {
 		if($email_exists) $form_exception->addError(__("Email address is already registered."));
 
 		// Geocode the postal code
-		try {
-			$results = Geolocation::geocode($user_address->postal_code . ", " . $user_address->country);
-
-			$result = $results[0];
-
-			foreach($result->address_components as $component) {
-				$types = $component->types;
-				if(in_array("postal_code", $types)) $user_address->postal_code = $component->long_name;
-				if(in_array("route", $types)) $user_address->address_1 = $component->long_name;
-				if(in_array("postal_town", $types)) $user_address->city = $component->long_name;
-				if(in_array("administrative_area_level_1", $types)) $user_address->state = $component->long_name;
-				if(in_array("country", $types)) $user_address->country = $component->long_name;
-			}
-
-			$user_address->latitude = $result->geometry->location->lat;
-			$user_address->longitude = $result->geometry->location->lng;
-		}
-		catch(Exception $e) {
-			$form_exception->addError(__("Failed to get location from postal code and country."));
+		$success = $user_address->geocode();
+		if(!$success) $form_exception->addError(__("Failed to get map location of current residence's postal code."));
+
+		if($secondary_user_address->postal_code) {
+			$success = $secondary_user_address->geocode();
+			if(!$success) $form_exception->addError(__("Failed to get map location of secondary residence's postal code."));
 		}
 
 		if($form_exception->hasErrors()) {
@@ -93,6 +88,11 @@ try {
 		$user_address->user_id = $user->id;
 		$success = $user_address->commit();
 
+		if($secondary_user_address->postal_code) {
+			$secondary_user_address->user_id = $user->id;
+			$secondary_user_address->commit();
+		}
+
 		$user->sendActivationEmail();
 
 		Core::addAlert("success", __("Registration successful. An email has been sent to verify your email address. Please follow the instructions in the email before logging in."));
@@ -107,7 +107,5 @@ catch(Exception $e) {
 }
 
 View::includeOnce("header", compact("page_title"));
-View::include("register", compact("user", "user_address"));
+View::include("register", compact("user", "user_address", "secondary_user_address"));
 View::includeOnce("footer");
-
-?>

+ 25 - 0
engine/user_address.php

@@ -60,4 +60,29 @@ class UserAddress extends Model {
 		return [$this->latitude, $this->longitude];
 	}
 
+	public function geocode(): bool {
+		try {
+			$results = Geolocation::geocode($this->postal_code . ", " . $this->country);
+
+			$result = $results[0];
+
+			foreach($result->address_components as $component) {
+				$types = $component->types;
+				if(in_array("postal_code", $types)) $this->postal_code = $component->long_name;
+				if(in_array("route", $types)) $this->address_1 = $component->long_name;
+				if(in_array("postal_town", $types)) $this->city = $component->long_name;
+				if(in_array("administrative_area_level_1", $types)) $this->state = $component->long_name;
+				if(in_array("country", $types)) $this->country = $component->long_name;
+			}
+
+			$this->latitude = $result->geometry->location->lat;
+			$this->longitude = $result->geometry->location->lng;
+
+			return true;
+		}
+		catch(Exception $e) {
+			return false;
+		}
+	}
+
 }

+ 19 - 7
views/register.php

@@ -2,6 +2,11 @@
 
 $newsletter_consent_checked = ($user->newsletter_consent) ? "checked" : "";
 
+$year = $month = $day = null;
+if($user->date_of_birth) {
+	list($year, $month, $day) = explode("-", $user->date_of_birth);
+}
+
 ?>
 <div class="container mt-3 py-4">
 	<h2><?=htmlspecialchars(__("Register"))?></h2>
@@ -54,8 +59,7 @@ $newsletter_consent_checked = ($user->newsletter_consent) ? "checked" : "";
 							<option value=""><?=htmlspecialchars(__("Select gender"))?></option>
 							<?php
 							foreach(User::genders() as $value => $display) {
-								$value = substr($value, 0, 1);
-								$selected = ($user->value == $value) ? "selected" : "";
+								$selected = ($user->gender == $value) ? "selected" : "";
 								?>
 								<option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
 								<?php
@@ -79,7 +83,7 @@ $newsletter_consent_checked = ($user->newsletter_consent) ? "checked" : "";
 				<div class="row mb-1">
 					<label class="col-sm-4 col-form-label"  for="password"><?=htmlspecialchars(__("Password"))?></label>
 					<div class="col-sm">
-						<input class="form-control" type="password" name="password" id="password" placeholder="<?=htmlspecialchars(__("Enter password"))?>" required />
+						<input class="form-control" type="password" name="password" id="password" placeholder="<?=htmlspecialchars(__("Enter password"))?>" autocomplete="new-password" required />
 						<small class="text-muted form-text"><?=sprintf(htmlspecialchars(__("At least %s6 characters%s. Must contain at least one %slowercase%s, %suppercase%s, %snumber%s and %sspecial%s character.")),
 							'<span data-length-requirement>', '</span>',
 							'<span data-lowercase-requirement>', '</span>',
@@ -92,7 +96,7 @@ $newsletter_consent_checked = ($user->newsletter_consent) ? "checked" : "";
 				<div class="row mb-1">
 					<label class="col-sm-4 col-form-label"  for="password-confirm"><?=htmlspecialchars(__("Confirm Password"))?></label>
 					<div class="col-sm">
-						<input class="form-control" type="password" name="password_confirm" id="password-confirm" placeholder="<?=htmlspecialchars(__("Confirm password"))?>" required />
+						<input class="form-control" type="password" name="password_confirm" id="password-confirm" placeholder="<?=htmlspecialchars(__("Confirm password"))?>" autocomplete="new-password" required />
 					</div>
 				</div>
 				<div class="row mb-1">
@@ -102,21 +106,21 @@ $newsletter_consent_checked = ($user->newsletter_consent) ? "checked" : "";
 						<select class="form-control w-auto d-inline-block" name="date_of_birth_day" id="date-of-birth-day" required>
 							<option value=""><?=htmlspecialchars(__("Day"))?></option>
 							<?php
-							Form::renderDayOptions();
+							Form::renderDayOptions($day);
 							?>
 						</select>
                         <label for="date-of-birth-month" class="sr-only"><?=htmlspecialchars(__("Month"))?></label>
 						<select class="form-control w-auto d-inline-block" name="date_of_birth_month" id="date-of-birth-month" required>
 							<option value=""><?=htmlspecialchars(__("Month"))?></option>
 							<?php
-							Form::renderMonthOptions();
+							Form::renderMonthOptions($month);
 							?>
 						</select>
                         <label for="date-of-birth-year" class="sr-only"><?=htmlspecialchars(__("Year"))?></label>
 						<select class="form-control w-auto d-inline-block" name="date_of_birth_year" id="date-of-birth-year" required>
 							<option value=""><?=htmlspecialchars(__("Year"))?></option>
 							<?php
-							Form::renderYearOptionsDescending();
+							Form::renderYearOptionsDescending(null, null, $year);
 							?>
 						</select>
 					</div>
@@ -127,6 +131,14 @@ $newsletter_consent_checked = ($user->newsletter_consent) ? "checked" : "";
 						<input class="form-control w-auto" type="text" name="postal_code" id="postal-code" placeholder="<?=htmlspecialchars(__("Enter postal code"))?>" value="<?=htmlspecialchars($user_address->postal_code)?>" required />
 					</div>
 				</div>
+
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label" for="secondary-postal-code"><?=htmlspecialchars(__("Secondary Postal Code"))?></label>
+					<div class="col-sm">
+						<input class="form-control w-auto" type="text" name="secondary_postal_code" id="secondary-postal-code" placeholder="<?=htmlspecialchars(__("Enter postal code"))?>" value="<?=htmlspecialchars($secondary_user_address->postal_code)?>" />
+					</div>
+				</div>
+
 				<div class="row mb-3">
 					<label class="col-sm-4 col-form-label" for="country"><?=htmlspecialchars(__("Country"))?></label>
 					<div class="col-sm">