Просмотр исходного кода

Add support for template overrides

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

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

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

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

+ 22 - 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,24 @@ class Template extends Model {
 		return "/account/templates/" . $this->id;
 	}
 
+	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 +62,7 @@ class Template extends Model {
 
 			<?php
 			foreach($fields as $field) {
+				$field = $this->applyOverridesToField($field);
 				Form::renderField($field);
 			}
 			?>
@@ -74,6 +95,7 @@ class Template extends Model {
 
 				<?php
 				foreach($fields as $field) {
+					$field = $this->applyOverridesToField($field);
 					Form::renderField($field);
 				}
 				?>

+ 18 - 21
template_processing/mystery_shop/test_pdf.php

@@ -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,7 +199,7 @@ $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);
 
@@ -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);
 

+ 38 - 0
views/project_form.php

@@ -183,6 +183,44 @@ $time_of_day_options = [
 				</div>
 			</div>
 		</div>
+
+		<hr />
+
+		<h3>Template Overrides</h3>
+
+		<?php
+		if(empty($project->template_overrides_json)) {
+			?>
+			<div class="alert alert-info">There are no template overrides configured for this project.</div>
+			<?php
+		}
+		else {
+			$overrides = $project->getTemplateOverrides();
+			?>
+			<div class="list-group">
+			<?php
+			foreach($overrides as $override) {
+				?>
+				<div class="list-group-item">
+					<div class="row">
+						<div class="col-lg-4">
+							<strong>Field:</strong> <?=htmlspecialchars($override->field)?>
+						</div>
+						<div class="col-lg-4">
+							<strong>Element:</strong> <?=htmlspecialchars($override->element)?>
+						</div>
+						<div class="col-lg-4">
+							<strong>Value:</strong> <?=htmlspecialchars($override->value)?>
+						</div>
+					</div>
+				</div>
+				<?php
+			}
+			?>
+			</div>
+			<?php
+		}
+		?>
 		<hr />
 
 		<input type="hidden" name="location_ids" id="location-ids" value="<?=implode(", ", $location_ids)?>" />

+ 2 - 0
views/test_verify_form.php

@@ -99,6 +99,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 />