Alan Lam 8 лет назад
Родитель
Сommit
b5ab4377c0

+ 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/address_add.php

@@ -7,6 +7,7 @@ Core::addBreadcrumb("/account/addresses", "Addresses");
 Core::addBreadcrumb("/account/addresses/add", $page_title);
 
 $user_address = new UserAddress;
+$user_address->country = DEFAULT_COUNTRY;
 
 $submit = http::value("submit");
 if($submit) {

+ 2 - 0
controllers/customer_add.php

@@ -7,6 +7,7 @@ Core::addBreadcrumb("/account/customers", "Customers");
 Core::addBreadcrumb("/account/customers/add", $page_title);
 
 $customer = new Customer;
+$customer->country = DEFAULT_COUNTRY;
 $customer->email_after_test = 1;
 $customer->email_locations_after_test = 1;
 
@@ -23,6 +24,7 @@ if($submit) {
 		$customer->city = http::value("city", "trim");
 		$customer->county = http::value("county", "trim");
 		$customer->postal_code = http::value("postal_code", "trim");
+		$customer->country = http::value("country", "trim");
 		$customer->email = http::value("email", "email");
 		$customer->phone = http::value("phone", "trim");
 		$customer->email_after_test = http::value("email_after_test", "int");

+ 1 - 0
controllers/customer_edit.php

@@ -23,6 +23,7 @@ if($submit) {
 		$customer->city = http::value("city", "trim");
 		$customer->county = http::value("county", "trim");
 		$customer->postal_code = http::value("postal_code", "trim");
+		$customer->country = http::value("country", "trim");
 		$customer->email = http::value("email", "email");
 		$customer->phone = http::value("phone", "trim");
 		$customer->email_after_test = http::value("email_after_test", "int");

+ 1 - 0
controllers/location_add.php

@@ -12,6 +12,7 @@ Core::addBreadcrumb("/account/locations/add", $page_title);
 
 $location = new Location;
 $location->customer_id = $customer->id;
+$location->country = DEFAULT_COUNTRY;
 
 $submit = http::value("submit");
 if($submit) {

+ 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");

+ 1 - 1
engine/customer.php

@@ -4,7 +4,7 @@ class Customer extends Model {
 
 	protected static $table = "customer";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["name", "address_1", "address_2", "city", "county", "postal_code", "email", "phone", "email_after_test", "email_locations_after_test"];
+	protected static $fillable_fields = ["name", "address_1", "address_2", "city", "county", "postal_code", "country", "email", "phone", "email_after_test", "email_locations_after_test"];
 
 	// Static methods
 	public static function getForAdmin() {

+ 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">

+ 8 - 0
views/customer_form.php

@@ -38,6 +38,14 @@
 				<input class="form-control" type="text" name="postal_code" id="postal-code" placeholder="Enter postal code" value="<?=htmlspecialchars($customer->postal_code)?>" />
 			</div>
 		</div>
+		<div class="row mb-1">
+			<label class="col-sm-4 col-form-label" for="country">Country</label>
+			<div class="col-sm">
+				<select class="form-control" name="country" id="country">
+				<?=Form::renderCountryOptions($customer->country)?>
+				</select>
+			</div>
+		</div>
 		<div class="row mb-1">
 			<label class="col-sm-4 col-form-label"for="email">Email Address</label>
 			<div class="col-sm">

+ 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">