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

+ 2 - 1
controllers/locations.php

@@ -65,11 +65,12 @@ if(!empty($action)) {
 			$handle = fopen("php://output", "w");
 
 			fputcsv($handle, [
-				"Internal ID", "City", "Legal Entity", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude"
+				"ID", "Internal ID", "City", "Legal Entity", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude"
 			]);
 
 			foreach($locations as $location) {
 				fputcsv($handle, [
+					$location->id,
 					$location->internal_id,
 					$location->city,
 					$location->legal_entity_name,

+ 1 - 1
controllers/project_edit.php

@@ -88,7 +88,7 @@ if($submit) {
 		if($form_exception->hasErrors()) throw $form_exception;
 
 		$success = $project->commit();
-		if(!$success) throw new Exception("Project could not be added at this time. Please try again later.");
+		if(!$success) throw new Exception("Project could not be edited at this time. Please try again later.");
 
 		// Create test for new location IDs
 		foreach($location_ids_to_add as $location_id) {

+ 121 - 0
controllers/project_edit_template_overrides.php

@@ -0,0 +1,121 @@
+<?php
+
+$project = Project::get($id);
+if(!$project) throw new Exception("Project not found.");
+
+$template = $project->getTemplate();
+if(!$template) throw new Exception("Template not found.");
+
+$fields = $template->getFields();
+
+Core::addScript("public/js/app-project-form.js");
+
+$page_title = "Template Overrides";
+
+$customer = $project->getCustomer();
+
+Core::addBreadcrumb("/account", "Account");
+Core::addBreadcrumb("/account/projects", "Projects");
+Core::addBreadcrumb("/account/projects?customer_id=$customer->id", "$customer->name Projects");
+Core::addBreadcrumb("/account/projects/$id", $project->name);
+Core::addBreadcrumb("/account/projects/$id/edit", "Edit Project");
+Core::addBreadcrumb("/account/projects/$id/edit/template-overrides", $page_title);
+
+$template_overrides = $project->getTemplateOverrides();
+
+$submit = http::value("submit");
+if($submit) {
+
+	$add = http::value("add");
+	$delete = http::value("delete");
+
+	$field = http::value("field");
+	$element = http::value("element");
+	$value = http::value("value");
+
+	try {
+		Core::validateCSRFToken($submit);
+
+		$form_exception = new FormException;
+
+
+		if($delete) {
+
+			$delete_index = -1;
+
+			$parts = explode("-", $delete);
+			if(count($parts) == 2) {
+				list($field, $element) = $parts;
+
+				foreach($template_overrides as $index => $override) {
+					if($field == $override->field && $element == $override->element) {
+						// Found override
+						$delete_index = $index;
+						break;
+					}
+				}
+
+			}
+		}
+		if($add) {
+
+			if(empty($field)) $form_exception->addError("Field cannot be empty.");
+			if(empty($element)) $form_exception->addError("Element cannot be empty.");
+			if(empty($value)) $form_exception->addError("Value cannot be empty.");
+
+			if(!isset($fields[$field])) $form_exception->addError("Invalid field.");
+			switch($element) {
+				case "label":
+					break;
+				default:
+					$form_exception->addError("Invalid element.");
+			}
+
+			foreach($template_overrides as $index => $override) {
+				if($field == $override->field && $element == $override->element) {
+					$form_exception->addError("An override already exists for this field/element combo. Please remove the existing override and try again.");
+				}
+			}
+
+		}
+
+		if($form_exception->hasErrors()) throw $form_exception;
+
+		// OK at this point
+
+		if($delete) {
+			// Remove override from array
+			array_splice($template_overrides, $delete_index, 1);
+		}
+
+		if($add) {
+			// Add override to array
+			$template_overrides[] = [
+				"field" => $field,
+				"element" => $element,
+				"value" => $value,
+			];
+		}
+
+		$project->template_overrides_json = json_encode($template_overrides);
+
+
+		$success = $project->commit();
+		if(!$success) throw new Exception("Project Template Overrides could not be edited at this time. Please try again later.");
+
+		Core::addAlert("success", "Project Template Overrides updated successfully.");
+		HTTP::redirect("/account/projects/$project->id/edit/template-overrides");
+	}
+	catch(FormException $e) {
+		$e->generateAlerts();
+	}
+	catch(Exception $e) {
+		Core::addAlert("danger", $e->getMessage());
+	}
+}
+
+View::includeOnce("header", compact("page_title"));
+View::include("project_template_overrides_form", compact("project", "template", "fields", "template_overrides"));
+View::includeOnce("footer");
+
+?>

+ 1 - 0
controllers/register.php

@@ -33,6 +33,7 @@ try {
 		$user->date_of_birth = http::getDateFromFields("date_of_birth");
 		$user->phone = http::value("phone", "trim");
 		$user->terms_accepted = http::value("terms_accepted", "int");
+		$user->newsletter_consent = http::value("newsletter_content", "int");
 		$user->dts_registered = gmdate("Y-m-d H:i:s");
 
 		$user_address->postal_code = http::value("postal_code");

+ 2 - 0
controllers/settings.php

@@ -28,6 +28,8 @@ if($submit) {
 		$user->last_name = http::value("last_name", "trim");
 		$user->phone = http::value("phone", "trim");
 
+		$user->newsletter_consent = http::value("newsletter_content", "int");
+
 		$user->preferred_payment_method = http::value("preferred_payment_method", "int");
 		$user->bank_name = http::value("bank_name", "trim");
 		$user->bank_sort_code = http::value("bank_sort_code", "trim");

+ 6 - 1
controllers/test.php

@@ -15,6 +15,11 @@ $customer = $project->getCustomer();
 $tester = $test->getTester();
 $template = $project->getTemplate();
 
+$template_overrides = $project->getTemplateOverrides();
+if($template_overrides) {
+	$template->setOverrides($template_overrides);
+}
+
 $page_title = $location->name;
 
 Core::addBreadcrumb("/account", "Account");
@@ -328,7 +333,7 @@ else if(Core::$user->level >= User::LEVEL_ADMIN) {
 
 			if($form_exception->hasErrors()) throw $form_exception;
 
-			$test->retest |= http::value("retest", "int");
+			$test->retest = http::value("retest", "int");
 
 			$test->dts_updated = Util::getDateTime();
 			$success = $test->commit();

+ 11 - 0
controllers/test_pdf.php

@@ -7,6 +7,17 @@ if(!$test) throw new Exception("Test not found.");
 
 $project = $test->getProject();
 $template = $project->getTemplate();
+$template->setOverrides($project->getTemplateOverrides());
+
+$template_parts = json_decode($template->json);
+$fields = array_merge($template_parts->beforeTest, $template_parts->duringTest);
+
+$field_name_label_map = [];
+foreach($fields as $field) {
+	if(empty($field->name)) continue;
+	$field = $template->applyOverridesToField($field);
+	$field_name_label_map[$field->name] = $field->label;
+}
 
 if(!isset($output)) $output = true;
 

+ 14 - 0
controllers/tests_tester_postal_codes.php

@@ -0,0 +1,14 @@
+<?php
+
+$user = Core::$user;
+$tests = $user->getPendingTests();
+$locations = Location::load(array_column($tests, "location_id"));
+
+header("Content-Type: text/plain; charset=utf-8");
+
+foreach($locations as $location) {
+	echo $location->postal_code . "\n";
+}
+
+
+?>

+ 113 - 3
controllers/user_ajax.php

@@ -41,6 +41,7 @@ switch($action) {
 
 		if(!$address) throw new Exception("Cannot find address.");
 
+		// Get locations according to bounds
 		$bounds = Geolocation::getBounds($address->getGeolocation(), $max_distance);
 
 		list($min_lat, $min_lng) = $bounds->southWest;
@@ -52,8 +53,10 @@ switch($action) {
 			"closed" => 0
 		]);
 
+		// Get customers
 		$customers = Customer::load(array_column($locations, "customer_id"));
 
+		// Sort locations by distance
 		usort($locations, function($a, $b) use ($address) {
 			return
 			Geolocation::getDistance($address->getGeolocation(), $a->getGeolocation())
@@ -61,23 +64,95 @@ switch($action) {
 			Geolocation::getDistance($address->getGeolocation(), $b->getGeolocation());
 		});
 
+		// Get tests
+		$tests = Test::query([
+			"status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
+			"location_id" => array_column($locations, "id")
+		]);
+
+		$location_test_array = [];
+		foreach($tests as $test) {
+			$exists = isset($location_test_array[$test->location_id]);
+			if(!$exists) {
+				$location_test_array[$test->location_id] = $test;
+			}
+			else {
+				$existing = $location_test_array[$test->location_id];
+
+				$unix_start_existing = is_null($existing->dts_start) ? 0 : strtotime($existing->dts_start . " UTC");
+				$unix_start_current = is_null($test->dts_start) ? 0 : strtotime($test->dts_start . " UTC");
+
+				if($unix_start_current > $unix_start_existing) {
+					$location_test_array[$test->location_id] = $test;
+				}
+			}
+		}
+
+		// Get projects
+		$projects = Project::load(array_column($location_test_array, "project_id"));
+
+		// Start output
 		foreach($locations as $location) {
 
+			// Filter by distance
 			$distance = Geolocation::getDistance($address->getGeolocation(), $location->getGeolocation());
 			if($distance > $max_distance) continue;
 
-			$customer = $location->getCustomer();
+			// Get test
+			$test = $location_test_array[$location->id] ?? false;
 
+			// Get customer
+			$customer = $location->getCustomer();
 			$customer_slug = str_replace("-", "_", Util::slug($customer->name));
 
+			// Test
+			$tester = null;
+			$formatted_start_dts = null;
+			$formatted_due_dts = null;
+			$past_due = false;
+
+			// Icon
 			$icon = null;
-			$icon_file = "{$customer_slug}.png";
+			$icon_file = "{$customer_slug}_none.png";
+
+			// Process test
+			if($test) {
+				$tester = $test->getTester();
+				$project = $test->getProject();
+
+				if(!is_null($test->dts_start)) {
+					$unix_start = strtotime($test->dts_start);
+					$formatted_start_dts = date("d/m/Y H:i", $unix_start);
+				}
+
+				if(is_null($test->pass)) {
+					$icon_file = "{$customer_slug}_pending.png";
+
+					// Has due date
+					if(!is_null($test->dts_due)) {
+						$unix_due = strtotime($test->dts_due);
+						$formatted_due_dts = date("d/m/Y H:i", $unix_due);
+
+						if($tester && time() > $unix_due) {
+							$icon_file = "{$customer_slug}_past_due.png";
+							$past_due = true;
+						}
+					}
+
+				}
+				else if($test->pass) {
+					$icon_file = "{$customer_slug}_pass.png";
+				}
+				else if(!$test->pass) {
+					$icon_file = "{$customer_slug}_fail.png";
+				}
+			}
 
 			if($icon_file && file_exists("public/img/markers/$icon_file")) {
 				$icon = $icon_file;
 			}
 
-			$output[] = [
+			$data = [
 				"id" => (int) $location->id,
 				"internalId" => (int) $location->internal_id,
 				"name" => $location->name,
@@ -86,7 +161,42 @@ switch($action) {
 				"latitude" => (double) $location->latitude,
 				"longitude" => (double) $location->longitude,
 				"icon" => $icon,
+				"customer" => [
+					"id" => $customer->id,
+					"name" => $customer->name,
+				],
+				"test" => null,
 			];
+
+			if($test) {
+
+				$pass = is_null($test->pass) ? null : intval($test->pass);
+
+				$data["test"] = [
+					"id" => (int) $test->id,
+					"pass" => $pass,
+					"formattedStartDts" => $formatted_start_dts,
+					"formattedDueDts" => $formatted_due_dts,
+					"tester" => null,
+					"pastDue" => $past_due,
+				];
+
+				if($project) {
+					$data["test"]["project"] = [
+						"id" => (int) $project->id,
+						"name" => $project->name,
+					];
+				}
+
+				if($tester) {
+					$data["test"]["tester"] = [
+						"id" => (int) $tester->id,
+						"name" => $tester->getDisplayName(),
+					];
+				}
+			}
+
+			$output[] = $data;
 		}
 
 		JSON::success([

+ 33 - 0
controllers/users_export.php

@@ -0,0 +1,33 @@
+<?php
+
+switch($type) {
+
+	case "email":
+		header("Content-Type: text/csv; charset=utf-8");
+		http::setDownloadHeaders("email_addresses.csv");
+
+		$handle = fopen("php://output", "w");
+
+		fputcsv($handle, ["first_name", "last_name", "email"]);
+
+		$nvp = [
+			"email" => (new Constraint("IS_NOT_NULL")),
+			"newsletter_consent" => 1,
+		];
+
+		$level = http::value("level", "int");
+		if($level) $nvp["level"] = $level;
+
+		$users = User::query($nvp);
+
+		foreach($users as $user) {
+			fputcsv($handle, [$user->first_name, $user->last_name, $user->email]);
+		}
+
+		break;
+
+	default:
+		throw new Exception("Invalid type.");
+}
+
+?>

+ 1 - 1
engine/constraint.php

@@ -17,7 +17,7 @@ class Constraint {
 	const TYPE_NOT_EQUALS = 9;
 
 	const TYPE_IS_NULL = 10;
-	const TYPE_NOT_NULL = 11;
+	const TYPE_IS_NOT_NULL = 11;
 
 	private $type;
 	private $values = [];

+ 6 - 0
engine/http.php

@@ -32,6 +32,12 @@ class HTTP {
 		return $result;
 	}
 
+	public static function setDownloadHeaders($filename) {
+		header("Content-Disposition: attachment; filename=\"$filename\"");
+		header("Cache-Control: private, max-age=0, must-revalidate");
+		header("Pragma: public");
+	}
+
 	public static function getDateFromFields($prefix = "") {
 		$date = false;
 

+ 6 - 1
engine/project.php

@@ -4,7 +4,7 @@ class Project extends Model {
 
 	protected static $table = "project";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["customer_id", "template_id", "name", "test_count", "min_age", "max_age", "default_tester_fee", "time_of_day", "tester_can_claim", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report"];
+	protected static $fillable_fields = ["customer_id", "template_id", "name", "test_count", "min_age", "max_age", "default_tester_fee", "time_of_day", "tester_can_claim", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report"];
 
 	protected static $field_map = [
 	];
@@ -62,6 +62,11 @@ class Project extends Model {
 		return Template::get($this->template_id);
 	}
 
+	public function getTemplateOverrides() {
+		$overrides = json_decode($this->template_overrides_json) ?: [];
+		return $overrides;
+	}
+
 	public function getTimeOfDayText() {
 		switch($this->time_of_day) {
 			case "A": return "Anytime";

+ 38 - 0
engine/template.php

@@ -6,6 +6,8 @@ class Template extends Model {
 	protected static $id_field = "id";
 	protected static $fillable_fields = ["customer_id", "name", "json", "practice"];
 
+	private $overrides = [];
+
 	// Object methods
 	public function getCustomer() {
 		return Customer::get($this->customer_id);
@@ -27,6 +29,40 @@ class Template extends Model {
 		return "/account/templates/" . $this->id;
 	}
 
+	public function getFields() {
+		$fields = [];
+
+		$json = json_decode($this->json);
+		$before_fields = $json->beforeTest;
+		$during_fields = $json->duringTest;
+		$all_fields = array_merge($before_fields, $during_fields);
+
+		foreach($all_fields as $field) {
+			if(empty($field->name)) continue;
+			$fields[$field->name] = $field;
+		}
+
+		return $fields;
+	}
+
+	public function setOverrides($overrides) {
+		$this->overrides = $overrides;
+	}
+
+	public function applyOverridesToField($field) {
+		foreach($this->overrides as $override) {
+			if(empty($field->name)) continue;
+			if($override->field == $field->name) {
+				switch($override->element) {
+					case "label":
+						$field->label = $override->value;
+						break;
+				}
+			}
+		}
+		return $field;
+	}
+
 	public function renderBeforeTestForm() {
 
 		$json = json_decode($this->json);
@@ -42,6 +78,7 @@ class Template extends Model {
 
 			<?php
 			foreach($fields as $field) {
+				$field = $this->applyOverridesToField($field);
 				Form::renderField($field);
 			}
 			?>
@@ -74,6 +111,7 @@ class Template extends Model {
 
 				<?php
 				foreach($fields as $field) {
+					$field = $this->applyOverridesToField($field);
 					Form::renderField($field);
 				}
 				?>

+ 4 - 2
engine/test.php

@@ -366,6 +366,8 @@ class Test extends Model {
 		return $success;
 	}
 
+	public function isPendingVerification() {
+		return $this->status == Test::STATUS_PENDING_VERIFICATION;
+	}
 }
-
-?>
+?>

+ 3 - 3
engine/user.php

@@ -4,7 +4,7 @@ class User extends Model {
 
 	protected static $table = "user";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["email", "old_id", "referrer_id", "password", "activated", "level", "score", "title", "first_name", "last_name", "gender", "date_of_birth", "reported", "reported_comment", "can_claim", "phone", "verify_token", "reset_token", "terms_accepted", "preferred_payment_method", "bank_name", "bank_sort_code", "bank_account_number", "paypal_email", "dts_registered", "dts_last_seen", "dts_verified", "dts_deleted", "verifier_id", "verifier_notes"];
+	protected static $fillable_fields = ["email", "old_id", "referrer_id", "password", "activated", "level", "score", "title", "first_name", "last_name", "gender", "date_of_birth", "reported", "reported_comment", "can_claim", "phone", "verify_token", "reset_token", "terms_accepted", "newsletter_consent", "preferred_payment_method", "bank_name", "bank_sort_code", "bank_account_number", "paypal_email", "dts_registered", "dts_last_seen", "dts_verified", "dts_deleted", "verifier_id", "verifier_notes"];
 
 	// Constants
 
@@ -445,7 +445,7 @@ class User extends Model {
 			"tester_id" => $this->id,
 			"status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_STARTED, Test::STATUS_PENDING_VERIFICATION],
 		], [
-			"sort" => "id",
+			"sort" => "dts_assigned",
 			"order" => "desc"
 		]);
 	}
@@ -455,7 +455,7 @@ class User extends Model {
 			"tester_id" => $this->id,
 			"status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
 		], [
-			"sort" => "id",
+			"sort" => "dts_start",
 			"order" => "desc"
 		]);
 	}

BIN
public/img/retest.png


+ 105 - 11
public/js/app-user.js

@@ -4,6 +4,8 @@ var locationsInitialised = false;
 var addressMarkers = [];
 var locationMarkers = [];
 
+var infoWindow = false;
+
 $(function() {
 
 	$("[data-toggle=tab]").on("shown.bs.tab", function(event) {
@@ -62,10 +64,12 @@ function initialiseAddresses() {
 
 }
 
-
-
 function initialiseLocations() {
 
+	if(infoWindow == false) {
+		infoWindow = new google.maps.InfoWindow();
+	}
+
 	for(var i in locationMarkers) {
 		let marker = locationMarkers[i];
 		marker.setMap(null);
@@ -135,16 +139,63 @@ function initialiseLocations() {
 
 
 			// Add to list group
-			let listGroupItem = $("div[data-nearby-location-list-group-item-template]").clone();
-			listGroupItem.removeAttr("data-nearby-location-list-group-item-template");
+			let lgi = $("div[data-nearby-location-list-group-item-template]").clone();
+			lgi.removeAttr("data-nearby-location-list-group-item-template");
+
+			lgi.find("a[data-location-name]").text(location.name).attr("href", "/account/locations/" + location.id);
+			lgi.find("a[data-customer-name]").text(location.customer.name).attr("href", "/account/customers/" + location.customer.id);
+			lgi.find("span[data-location-distance]").text(location.distance + " miles");
+			lgi.find("span[data-location-id]").text(location.id);
+			lgi.find("span[data-location-internal-id]").text(location.internalId);
+			lgi.find("small[data-location-address]").text(location.address);
+
+			if(location.test) {
+				let test = location.test;
+				let tester = test.tester;
+				let project = test.project;
+
+				lgi.find("div[data-test-container]").removeClass("d-none");
+				lgi.find("div[data-test-id-container] a").text(test.id).attr("href", "/account/tests/" + test.id);
+				lgi.find("div[data-test-project-name-container] a").text(project.name).attr("href", "/account/projects/" + project.id);
+
+				if(tester) {
+					lgi.find("div[data-test-user-name-container] a").text(tester.name).attr("href", "/account/users/" + tester.id);
+
+					let passText = "";
+					let passClass = "";
+
+					if(test.pass === null) {
+						passText = "Pending";
+						passClass = "warning";
+						lgi.find("div[data-test-due-dts-container]").removeClass("d-none").find("span").text(test.formattedDueDts);
+
+						if(test.pastDue) {
+							lgi.find("div[data-test-due-dts-container]").append(" <span class=\"badge badge-danger\">Overdue</span>");
+						}
+					}
+					else if(test.pass === 0) {
+						passText = "Fail";
+						passClass = "danger";
+					}
+					else if(test.pass === 1) {
+						passText = "Pass";
+						passClass = "success";
+					}
+
+					if(test.pass !== null) {
+						lgi.find("div[data-test-start-dts-container]").removeClass("d-none").find("span").text(test.formattedStartDts);
+					}
+
+					lgi.find("div[data-test-pass-container] span").addClass("text-" + passClass).text(passText);
+
+				}
+
+
+			}
 
-			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);
+			lgi.appendTo("#nearby-locations-list-group").show();
 
-			listGroupItem.appendTo("#nearby-locations-list-group").show();
+			addMarkerClickListenerForLocation(marker, location);
 
 		}
 
@@ -152,4 +203,47 @@ function initialiseLocations() {
 
 	});
 
-}
+}
+
+function addMarkerClickListenerForLocation(m, l) {
+
+	let map = maps["map"];
+
+	m.addListener("click", function() {
+		let content = "<strong><a href=\"/account/locations/" + l.id + "\" target=\"_blank\">" + l.name + "</a></strong><br />";
+		if(l.internalId) {
+			content += "<div class=\"small\"><strong>Internal ID:</strong> " + l.internalId + "</div>";
+		}
+		content += "<div class=\"small\"><strong>Location ID:</strong> " + l.id + "</div>";
+
+		if(l.test) {
+
+			content += '<hr />';
+
+			content += '<div class="small"><strong>Test ID:</strong> <a href="/account/tests/' + l.test.id + '" target="_blank">' + l.test.id + '</a></div>';
+
+			if(l.test.formattedStartDts) {
+				content += '<div class="small"><strong>Date/Time:</strong> ' + l.test.formattedStartDts + '</div>';
+			}
+
+			if(l.test.tester) {
+				content += '<div class="small"><strong>Tester ID:</strong> ' + l.test.tester.id + '</div>';
+				content += '<div class="small"><strong>Tester Name:</strong> <a href="/account/users/' + l.test.tester.id + '" target="_blank">' + l.test.tester.name + '</a></div>';
+			}
+
+			let pastDueBadge = '';
+			if(l.test.pastDue) {
+				pastDueBadge = '<span class="badge badge-lg badge-primary">Overdue</span>';
+			}
+
+			if(l.test.formattedDueDts) {
+				content += '<div class="small"><strong>Due:</strong> ' + l.test.formattedDueDts + ' ' + pastDueBadge + '</div>';
+			}
+
+		}
+
+		infoWindow.setContent(content);
+		infoWindow.open(map, m);
+	});
+
+}

+ 3 - 0
routes.php

@@ -63,6 +63,7 @@ $routes = [
 	"/^account\/projects\/add$/" => ["controller" => "project_add", "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern$/" => ["controller" => "project", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/projects\/$id_pattern\/edit$/" => ["controller" => "project_edit", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
+	"/^account\/projects\/$id_pattern\/edit\/template-overrides$/" => ["controller" => "project_edit_template_overrides", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/export-csv$/" => ["controller" => "project_csv", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/export-customer-csv$/" => ["controller" => "project_customer_csv", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/export-kml$/" => ["controller" => "project_kml", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
@@ -70,6 +71,7 @@ $routes = [
 
 	// Tests
 	"/^account\/tests$/" => ["controller" => "tests", "require_user_level" => User::LEVEL_TESTER],
+	"/^account\/tests\/postal\-codes$/" => ["controller" => "tests_tester_postal_codes", "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/kml$/" => ["controller" => "tests_tester_kml", "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/available$/" => ["controller" => "tests_available", "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/completed$/" => ["controller" => "tests_completed", "require_user_level" => User::LEVEL_TESTER],
@@ -89,6 +91,7 @@ $routes = [
 
 	// Users
 	"/^account\/users$/" => ["controller" => "users", "require_admin" => true],
+	"/^account\/users\/export\/(email)$/" => ["controller" => "users_export", "params" => ["type"], "require_admin" => true],
 	"/^account\/users\/$id_pattern$/" => ["controller" => "user", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/users\/$id_pattern\/edit$/" => ["controller" => "user_edit", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/users\/$id_pattern\/verify$/" => ["controller" => "user_verify", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],

+ 2 - 23
template_processing/age_verification/test_pdf.php

@@ -7,28 +7,7 @@ if($test->retest) {
 	$pdf->SetLeftMargin(83);
 	$pdf->SetY(10);
 
-	$pdf->SetFont("Arial", "B", $header_font_size);
-	$pdf->SetTextColor(0, 0, 255);
-	$pdf->Cell(43, 4, "RETEST", 0, 1);
-
-	$pdf->SetY($pdf->GetY() + 2);
-
-	// Find previous test
-	$previous_test = $test->getPreviousTest();
-	if($previous_test) {
-
-		$previous_test_pass_text = $previous_test->pass ? "PASS" : "FAIL";
-		$previous_test_unix_start = Util::getUnixTimeFromUTC($previous_test->dts_start);
-
-		$previous_test_date = date("d/m/Y", $previous_test_unix_start);
-
-		$pdf->SetFont("Arial", "", $normal_font_size);
-		$pdf->SetTextColor(0, 0, 0);
-		$pdf->Cell(43, 4, "Prev. Test Date: $previous_test_date", 0, 1);
-		$pdf->Cell(43, 4, "Prev. Test Result: $previous_test_pass_text", 0, 1);
-
-	}
-
+	$pdf->Image("public/img/retest.png", 83, $pdf->GetY(), 63);
 }
 
 $pdf->SetLeftMargin(10);
@@ -69,7 +48,7 @@ $pdf->Line($pdf->GetX(), $pdf->GetY(), $pdf->GetX() + 135, $pdf->GetY());
 $pdf->Cell(30, $cell_height, "Region / Manager:", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
 $pdf->Write($cell_height, $location->region);
-$pdf->Write($cell_height, "/ ");
+$pdf->Write($cell_height, " / ");
 $pdf->Write($cell_height, $location->area_manager);
 $pdf->SetY($pdf->GetY() + $cell_height);
 $pdf->Line($pdf->GetX(), $pdf->GetY(), $pdf->GetX() + 135, $pdf->GetY());

+ 20 - 23
template_processing/mystery_shop/test_pdf.php

@@ -16,7 +16,7 @@ $pdf->SetTextColor(0, 0, 0);
 $pdf->Cell(30, $cell_height, "Region / Manager:", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
 $pdf->Write($cell_height, $location->region);
-$pdf->Write($cell_height, "/ ");
+$pdf->Write($cell_height, " / ");
 $pdf->Write($cell_height, $location->area_manager);
 $pdf->SetY($pdf->GetY() + $cell_height);
 
@@ -60,7 +60,6 @@ $pdf->SetX(150);
 $pdf->SetFont("Arial", "", $small_font_size);
 $pdf->MultiCell(50, 4, $test->attire, 0, 1);
 
-
 // Test Details
 $pdf->SetLeftMargin(10);
 
@@ -112,7 +111,7 @@ switch($cleanliness) {
 	case 5: $cleanliness = "Very clean"; break;
 }
 
-$pdf->Cell(30, $cell_height, "Cleanliness:", 0, 0);
+$pdf->Cell(30, $cell_height, $field_name_label_map["cleanliness"] . ":", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
 $pdf->Cell(57.5, $cell_height, $cleanliness, 0, 1);
 
@@ -146,10 +145,8 @@ else {
 	$width = 70;
 }
 
-
 $pdf->Image($location_photo_path, $pdf->GetX(), $pdf->GetY(), $width, $height);
 
-
 // Staff Details
 $pdf->SetLeftMargin(107.5);
 $pdf->SetY($id_requests_y);
@@ -161,40 +158,40 @@ $pdf->SetFont("Arial");
 $value = $values["staff_badge_worn"] ?? "";
 if(strlen($value) == 0) $value = "Don't know/Not approached";
 else $value = ($value == 0) ? "No" : "Yes";
-$pdf->Cell(30, $cell_height, "Staff Badge Worn:", 0, 0);
+$pdf->Cell(45, $cell_height, $field_name_label_map["staff_badge_worn"] . ":", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
-$pdf->Cell(60, $cell_height, $value, 0, 1);
+$pdf->Cell(45, $cell_height, $value, 0, 1);
 
 $value = $values["staff_name"] ?? "";
-$pdf->Cell(30, $cell_height, "Staff Name:", 0, 0);
+$pdf->Cell(45, $cell_height, $field_name_label_map["staff_name"] . ":", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
-$pdf->Cell(60, $cell_height, $value, 0, 1);
+$pdf->Cell(45, $cell_height, $value, 0, 1);
 
 $pdf->SetY($pdf->GetY() + $cell_height);
 
-$value = $values["drink_offered"] ?? "";
+$value = $values["staff_offered_drink"] ?? "";
 $value = $value ? "Yes" : "No";
-$pdf->Cell(30, $cell_height, "Drink Offered:", 0, 0);
+$pdf->Cell(45, $cell_height, $field_name_label_map["staff_offered_drink"] . ":", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
-$pdf->Cell(60, $cell_height, $value, 0, 1);
+$pdf->Cell(45, $cell_height, $value, 0, 1);
 
 $value = $values["staff_offered_drink_name"] ?? "";
-$value = $value ?: "No drink offered";
-$pdf->Cell(30, $cell_height, "Drink Name:", 0, 0);
+$value = $value ?: "N/A";
+$pdf->Cell(45, $cell_height, $field_name_label_map["staff_offered_drink_name"] . ":", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
-$pdf->Cell(60, $cell_height, $value, 0, 1);
+$pdf->Cell(45, $cell_height, $value, 0, 1);
 
 $pdf->SetY($pdf->GetY() + $cell_height);
 
 $value = $values["staff_offered_promotion"] ?? "";
 $value = $value ? "Yes" : "No";
-$pdf->Cell(30, $cell_height, "Promotion Offered:", 0, 0);
+$pdf->Cell(45, $cell_height, $field_name_label_map["staff_offered_promotion"] . ":", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
-$pdf->Cell(60, $cell_height, $value, 0, 1);
+$pdf->Cell(45, $cell_height, $value, 0, 1);
 
 $value = $values["staff_offered_promotion_name"] ?? "";
 $value = $value ?: "No promotion offered";
-$pdf->Cell(95, $cell_height, "Promotion Name:", 0, 1);
+$pdf->Cell(95, $cell_height, $field_name_label_map["staff_offered_promotion_name"] . ":", 0, 1);
 $pdf->SetFont("Arial", "", $small_font_size);
 $pdf->MultiCell(90, 4, $value, 0, 1);
 
@@ -202,13 +199,13 @@ $pdf->SetY($pdf->GetY() + $cell_height);
 
 $value = $values["staff_help"] ?? "";
 $value = $value ? "Yes" : "No";
-$pdf->Cell(30, $cell_height, "Machine Explanation:", 0, 0);
+$pdf->Cell(30, $cell_height, $field_name_label_map["staff_help"] . ":", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
 $pdf->Cell(60, $cell_height, $value, 0, 1);
 
 $pdf->SetY($pdf->GetY() + $cell_height);
 
-$value = $values["staff_behaviour"];
+$value = $values["staff_behaviour"] ?? 0;
 switch($value) {
 	case 1: $value = "Rude"; break;
 	case 2: $value = "Slightly rude"; break;
@@ -218,7 +215,7 @@ switch($value) {
 	default: $value = "N/A"; break;
 }
 
-$pdf->Cell(30, $cell_height, "Staff Behaviour:", 0, 0);
+$pdf->Cell(30, $cell_height, $field_name_label_map["staff_behaviour"] . ":", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
 $pdf->Cell(60, $cell_height, $value, 0, 1);
 
@@ -226,13 +223,13 @@ $pdf->SetY($pdf->GetY() + $cell_height);
 
 $value = $values["staff_uniform"] ?? "";
 $value = $value ? "Yes" : "No";
-$pdf->Cell(30, $cell_height, "Staff Uniform:", 0, 0);
+$pdf->Cell(30, $cell_height, $field_name_label_map["staff_uniform"] . ":", 0, 0);
 $pdf->SetX($pdf->GetX() + 5);
 $pdf->Cell(60, $cell_height, $value, 0, 1);
 
 $value = $values["staff_uniform_description"] ?? "";
 $value = $value ?: "N/A";
-$pdf->Cell(95, $cell_height, "Staff Uniform Description:", 0, 1);
+$pdf->Cell(95, $cell_height, $field_name_label_map["staff_uniform_description"] . ":", 0, 1);
 $pdf->SetFont("Arial", "", $small_font_size);
 $pdf->MultiCell(90, 4, $value, 0, 1);
 

+ 58 - 0
views/project_form.php

@@ -183,6 +183,64 @@ $time_of_day_options = [
 				</div>
 			</div>
 		</div>
+
+		<?php
+		if($project->id) {
+			?>
+			<hr />
+
+			<div class="row">
+				<div class="col">
+					<h3>Template Overrides</h3>
+				</div>
+				<div class="col-auto">
+					<a class="btn btn-outline-primary" href="/account/projects/<?=$project->id?>/edit/template-overrides">Edit</a>
+				</div>
+			</div>
+
+			<?php
+			$overrides = $project->getTemplateOverrides();
+			if(empty($overrides)) {
+				?>
+				<div class="alert alert-info">There are no template overrides configured for this project.</div>
+				<?php
+			}
+			else {
+				$fields = $template->getFields();
+				?>
+				<table class="table table-bordered table-hover">
+					<thead>
+						<tr>
+							<th>Field</th>
+							<th>Element</th>
+							<th>Value</th>
+						</tr>
+					</thead>
+					<tbody>
+					<?php
+					foreach($overrides as $override) {
+						?>
+						<tr>
+							<td>
+								<?=htmlspecialchars($fields[$override->field]->label)?>
+							</td>
+							<td>
+								<?=ucfirst($override->element)?>
+							</td>
+							<td>
+								<?=htmlspecialchars($override->value)?>
+							</td>
+						</tr>
+						<?php
+					}
+					?>
+					</tbody>
+				</table>
+				<?php
+			}
+		}
+		?>
+
 		<hr />
 
 		<input type="hidden" name="location_ids" id="location-ids" value="<?=implode(", ", $location_ids)?>" />

+ 106 - 0
views/project_template_overrides_form.php

@@ -0,0 +1,106 @@
+
+<div class="container mt-3 py-4">
+
+	<form method="post">
+
+		<h3>Active Template Overrides</h3>
+
+		<?php
+		if(empty($template_overrides)) {
+			?>
+			<div class="alert alert-info">There are no active template overrides for this project.</div>
+			<?php
+		}
+		else {
+			$fields = $template->getFields();
+			?>
+			<table class="table table-bordered table-hover">
+				<thead>
+					<tr>
+						<th>Field</th>
+						<th>Element</th>
+						<th>Value</th>
+						<th>Action</th>
+					</tr>
+				</thead>
+				<tbody>
+				<?php
+				foreach($template_overrides as $override) {
+					?>
+					<tr>
+						<td class="align-middle">
+							<?=htmlspecialchars($fields[$override->field]->label)?>
+						</td>
+						<td class="align-middle">
+							<?=ucfirst($override->element)?>
+						</td>
+						<td class="align-middle">
+							<?=htmlspecialchars($override->value)?>
+						</td>
+						<td class="text-right">
+							<button type="submit" name="delete" value="<?=htmlspecialchars($override->field)?>-<?=htmlspecialchars($override->element)?>" class="btn btn-sm btn-danger">Delete</button>
+						</td>
+					</tr>
+					<?php
+				}
+				?>
+				</tbody>
+			</table>
+			<input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
+			<?php
+		}
+		?>
+	</form>
+
+	<form method="post">
+
+		<h3>Add Template Override</h3>
+
+		<input type="hidden" name="add" value="1" />
+
+		<div class="row mb-1">
+			<label class="col-sm-4 col-form-label">
+				Field
+			</label>
+			<div class="col-sm">
+				<select class="form-control" name="field" required>
+					<option value="">Select Field</option>
+					<?php
+					foreach($fields as $value => $field) {
+						?>
+						<option value="<?=htmlspecialchars($field->name)?>"><?=htmlspecialchars($field->label)?></option>
+						<?php
+					}
+					?>
+				</select>
+			</div>
+		</div>
+
+		<div class="row mb-1">
+			<label class="col-sm-4 col-form-label">
+				Element
+			</label>
+			<div class="col-sm">
+				<select class="form-control" name="element" required>
+					<option value="label">Label</option>
+				</select>
+			</div>
+		</div>
+
+		<div class="row mb-1">
+			<label class="col-sm-4 col-form-label">
+				Value
+			</label>
+			<div class="col-sm">
+				<input class="form-control" type="text" name="value" placeholder="Type override value" required />
+			</div>
+		</div>
+
+		<div class="row">
+			<div class="col-sm offset-sm-4">
+				<button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">Add Override</button>
+			</div>
+		</div>
+
+	</form>
+</div>

+ 10 - 1
views/register.php

@@ -1,3 +1,8 @@
+<?php
+
+$newsletter_consent_checked = ($user->newsletter_consent) ? "checked" : "";
+
+?>
 <div class="container mt-3 py-4">
 	<h2>Register</h2>
 
@@ -125,7 +130,11 @@
 						<label>
 							<input type="checkbox" value="1" name="terms_accepted" /> I agree to and have read the <a href="/terms-and-conditions" target="_blank">Terms and Conditions</a> and <a href="/privacy-policy" target="_blank">Privacy Policy</a>
 						</label>
-
+					</div>
+				</div>
+				<div class="row mb-1">
+					<div class="col-sm offset-sm-4 align-self-center">
+						<label><input type="checkbox" name="newsletter_content" id="newsletter-consent" value="1" <?=$newsletter_consent_checked?> /> Opt-in to newsletters about <?=htmlspecialchars(APP_NAME)?></label>
 					</div>
 				</div>
 				<div class="row">

+ 10 - 0
views/settings.php

@@ -1,3 +1,8 @@
+<?php
+
+$newsletter_consent_checked = ($user->newsletter_consent) ? "checked" : "";
+
+?>
 <div class="container mt-3 py-4">
 	<h2>Settings</h2>
 
@@ -47,6 +52,11 @@
 							<input class="form-control" type="email" name="email" id="email" placeholder="Enter email address" value="<?=htmlspecialchars($user->email)?>" required />
 						</div>
 					</div>
+					<div class="row mb-1">
+						<div class="col-sm offset-sm-4 align-self-center">
+							<label><input type="checkbox" name="newsletter_content" id="newsletter-consent" value="1" <?=$newsletter_consent_checked?> /> Opt-in to newsletters about <?=htmlspecialchars(APP_NAME)?></label>
+						</div>
+					</div>
 					<div class="row mb-1">
 						<label class="col-sm-4 col-form-label"  for="phone">Mobile Number</label>
 						<div class="col-sm">

+ 1 - 0
views/test_list_assigned.php

@@ -22,6 +22,7 @@
 
 				<div class="dropdown-menu" aria-labelledby="actions-button">
 					<a class="dropdown-item" href="/account/tests/kml">Export KML</a>
+					<a class="dropdown-item" href="/account/tests/postal-codes">Export Postcodes</a>
 				</div>
 			</div>
 			<?php

+ 13 - 13
views/test_verify_form.php

@@ -17,6 +17,7 @@ if(!is_null($test->dts_end)) {
 
 if(in_array($test->status, [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED])) {
 	$test_details["Verified"] = Util::getDisplayDateTime(strtotime($test->dts_verified . " UTC"));
+	$test_details["Retest"] = $test->retest ? "Yes" : "No";
 }
 
 $tester_details = [
@@ -99,6 +100,8 @@ function render_card_list_group($details) {
 					$header_count = 0;
 					foreach($fields as $field) {
 
+						$field = $template->applyOverridesToField($field);
+
 						if($header_count > 0 && $field->type == "header") {
 							?>
 							<hr />
@@ -288,6 +291,16 @@ function render_card_list_group($details) {
 							</div>
 						</div>
 
+						<div class="row mb-1">
+							<div class="col-sm-4">Retest</div>
+							<div class="col-sm">
+								<div class="form-check">
+									<input class="form-check-input" type="checkbox" name="retest" id="retest" value="1" <?=Form::checked($test->retest)?> />
+									<label class="form-check-label" for="retest">Retest</label>
+								</div>
+							</div>
+						</div>
+
 						<?php
 						if($test->status == Test::STATUS_PENDING_VERIFICATION) {
 							?>
@@ -339,19 +352,6 @@ function render_card_list_group($details) {
 							}
 							?>
 
-							<div class="row mb-1">
-								<div class="col-sm-4">Retest</div>
-								<div class="col-sm">
-									<div class="form-check">
-										<input class="form-check-input" type="checkbox" name="retest" id="retest" value="1" <?=Form::checked($test->retest)?> />
-										<label class="form-check-label" for="retest">Retest</label>
-									</div>
-									<small class="form-text text-muted">
-										If this test was a retest, please check the box above. Once the test is accepted or rejected, this flag cannot be changed.
-									</small>
-								</div>
-							</div>
-
 							<div class="row mb-1">
 								<label class="col-sm-4 col-form-label" for="tester-message">Message to Tester (if rejecting)</label>
 								<div class="col-sm">

+ 21 - 1
views/user.php

@@ -302,14 +302,34 @@ var userId = <?=$user->id?>;
 							<div class="row">
 								<div class="col-md">
 									<a href="#" data-location-name>Location Name</a>
+									<small><a class="text-danger" href="#" data-customer-name>Customer Name</a></small>
 								</div>
 								<div class="col-md-auto">
-									Distance: <span data-location-distance></span>
+									<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 class="d-none mt-2 small" data-test-container>
+								<div class="card card-body p-2">
+									<div><strong>Latest Test Details</strong></div>
+									<div class="row">
+										<div class="col-md">
+											<div data-test-id-container><strong>ID:</strong> <a data-test-id></a></div>
+											<div data-test-project-name-container><strong>Project:</strong> <a data-project-name></a></div>
+											<div class="d-none" data-test-user-name-container><strong>Tester:</strong> <a></a></div>
+										</div>
+										<div class="col-md">
+											<div class="d-none" data-test-start-dts-container><strong>Tested:</strong> <span></span></div>
+											<div class="d-none" data-test-due-dts-container><strong>Due:</strong> <span></span></div>
+											<div data-test-pass-container><strong>Result:</strong> <span></span></div>
+										</div>
+									</div>
+
+
+								</div>
+							</div>
 						</div>
 
 					</div>

+ 18 - 3
views/users.php

@@ -11,9 +11,24 @@ $query["sort"] = "SORT";
 			<h2>Users</h2>
 		</div>
 		<div class="col-auto">
-			<div class="row">
-				<div class="col-auto">
-
+			<div class="row no-gutters">
+				<?php
+				if(Core::$user->isAdmin()) {
+					?>
+					<div class="col-auto mr-3">
+						<div class="dropdown">
+							<button class="btn btn-outline-primary dropdown-toggle" type="button" id="actions-button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Actions</button>
+							<div class="dropdown-menu" aria-labelledby="actions-button">
+								<a class="dropdown-item" href="/account/users/export/email?level=<?=User::LEVEL_REGISTERED?>">Export Emails (Registered)</a>
+								<a class="dropdown-item" href="/account/users/export/email?level=<?=User::LEVEL_UNVERIFIED?>">Export Emails (Pending Verification)</a>
+								<a class="dropdown-item" href="/account/users/export/email?level=<?=User::LEVEL_TESTER?>">Export Emails (Tester)</a>
+							</div>
+						</div>
+					</div>
+					<?php
+				}
+				?>
+				<div class="col-auto mr-3">
 					<a class="btn btn-outline-primary" href="#filters" data-toggle="collapse">
 						Toggle Filters <span class="badge badge-pill badge-danger"><?=$filter_count?></span>
 					</a>