Explorar el Código

Update Country field to dropdown format.

Alan Lam hace 8 años
padre
commit
97148b549e

+ 1 - 0
.gitignore

@@ -10,3 +10,4 @@ tasks/PraesepeVenues.csv
 tasks/email_test.php
 tasks/data/
 backups/
+files/countries.json

+ 1 - 0
config.template.php

@@ -41,5 +41,6 @@ define("SALT", "");
 define("FPDF_FONTPATH", __DIR__ . "/fonts");
 
 define("TIMEZONE", "Europe/London");
+define("DEFAULT_COUNTRY", "United Kingdom");
 
 ?>

+ 1 - 0
controllers/register.php

@@ -9,6 +9,7 @@ Core::addBreadcrumb("/register", $page_title);
 $user = new User;
 $user_address = new UserAddress();
 $user_address->name = "Current Residence";
+$user_address->country = DEFAULT_COUNTRY;
 
 try {
 	$submit = http::value("submit");

+ 37 - 0
engine/form.php

@@ -103,6 +103,43 @@ class Form {
 		}
 	}
 
+	public static function renderCountryOptions($selected_value = null) {
+
+		$file = ROOT_DIR . "/files/countries.json";
+		$threshold = time() - (7 * 86400);
+
+		if(false && file_exists($file) && filemtime($file) > $threshold) {
+			$countries_json = @file_get_contents($file);
+			$countries = json_decode($countries_json);
+		}
+		else {
+			$url = "http://country.io/names.json";
+			$countries_json = @file_get_contents($url);
+			$countries = json_decode($countries_json);
+
+			if($countries) {
+				file_put_contents($file, $countries_json);
+			}
+		}
+
+		if(!$countries) return false;
+
+		$country_pairs = get_object_vars($countries);
+		sort($country_pairs);
+		$countries = array_values($country_pairs);
+
+		?>
+		<option value="">Select Country</option>
+		<?php
+
+		foreach($countries as $country) {
+			$selected = ($selected_value === $country) ? "selected" : "";
+			?>
+			<option value="<?=htmlspecialchars($country)?>" <?=$selected?>><?=htmlspecialchars($country)?></option>
+			<?php
+		}
+	}
+
 	public static function renderField($options) {
 
 		switch($options->type) {

+ 3 - 1
views/address_add_form.php

@@ -13,7 +13,9 @@
 		</div>
 		<div class="form-group">
 			<label  for="country">Country</label>
-			<input class="form-control" type="text" name="country" id="country" placeholder="Enter country" value="<?=htmlspecialchars($user_address->country)?>" required />
+			<select class="form-control" name="country" id="country" required>
+			<?=Form::renderCountryOptions($user_address->country)?>
+			</select>
 		</div>
 		<button class="btn btn-primary" type="submit" name="submit" value="<?=$_SESSION["csrf_token"]?>">Add</button>
 	</form>

+ 3 - 1
views/address_edit_form.php

@@ -31,7 +31,9 @@
 				</div>
 				<div class="form-group">
 					<label  for="country">Country</label>
-					<input class="form-control" type="text" name="country" id="country" placeholder="Enter country" value="<?=htmlspecialchars($user_address->country)?>" required />
+					<select class="form-control" name="country" id="country" required>
+					<?=Form::renderCountryOptions($user_address->country)?>
+					</select>
 				</div>
 			</div>
 			<div class="col-lg">

+ 3 - 1
views/location_form.php

@@ -78,7 +78,9 @@ $tabs = [
 						<div class="row mb-1">
 							<label class="col-sm-4 col-form-label" for="country">Country</label>
 							<div class="col-sm">
-								<input class="form-control" type="text" name="country" id="country" placeholder="Enter country" value="<?=htmlspecialchars($location->country)?>" required />
+								<select class="form-control" name="country" id="country" required>
+								<?=Form::renderCountryOptions($location->country)?>
+								</select>
 							</div>
 						</div>
 

+ 3 - 1
views/register.php

@@ -115,7 +115,9 @@
 				<div class="row mb-3">
 					<label class="col-sm-4 col-form-label" for="country">Country</label>
 					<div class="col-sm">
-						<input class="form-control" type="text" name="country" id="country" placeholder="Enter country" value="<?=htmlspecialchars($user_address->country)?>" required />
+						<select class="form-control" name="country" id="country" required>
+						<?=Form::renderCountryOptions($user_address->country)?>
+						</select>
 					</div>
 				</div>
 				<div class="row mb-1">