Browse Source

Add json_file field for templates

Alan Lam 7 years ago
parent
commit
6626764d3c

+ 1 - 2
controllers/practice.php

@@ -12,7 +12,7 @@ Core::addBreadcrumb("/account", _("Account"));
 Core::addBreadcrumb("/account/practice", $page_title);
 Core::addBreadcrumb("/account/practice", $page_title);
 Core::addBreadcrumb("/account/practice/$id", _("Form"));
 Core::addBreadcrumb("/account/practice/$id", _("Form"));
 
 
-$json = json_decode($template->json);
+$json = json_decode($template->getJSON());
 
 
 // Reset
 // Reset
 $reset = http::value("reset");
 $reset = http::value("reset");
@@ -191,7 +191,6 @@ if($submit) {
 }
 }
 
 
 if($is_unverified) {
 if($is_unverified) {
-	$json = json_decode($template->json);
 	$fields = array_merge($json->beforeTest, $json->duringTest);
 	$fields = array_merge($json->beforeTest, $json->duringTest);
 }
 }
 
 

+ 1 - 1
controllers/project_csv.php

@@ -12,7 +12,7 @@ header("Pragma: public");
 $template = $project->getTemplate();
 $template = $project->getTemplate();
 $processing_file_path = $template->getProcessingFilePath("project_csv.php");
 $processing_file_path = $template->getProcessingFilePath("project_csv.php");
 
 
-$object = json_decode($template->json);
+$object = json_decode($template->getJSON());
 
 
 $fields_1 = $object->beforeTest;
 $fields_1 = $object->beforeTest;
 $fields_2 = $object->duringTest;
 $fields_2 = $object->duringTest;

+ 1 - 1
controllers/project_customer_csv.php

@@ -12,7 +12,7 @@ header("Pragma: public");
 $template = $project->getTemplate();
 $template = $project->getTemplate();
 $processing_file_path = $template->getProcessingFilePath("project_customer_csv.php");
 $processing_file_path = $template->getProcessingFilePath("project_customer_csv.php");
 
 
-$object = json_decode($template->json);
+$object = json_decode($template->getJSON());
 
 
 $fields_1 = $object->beforeTest;
 $fields_1 = $object->beforeTest;
 $fields_2 = $object->duringTest;
 $fields_2 = $object->duringTest;

+ 12 - 7
controllers/template_add.php

@@ -11,7 +11,6 @@ Core::addBreadcrumb("/account/templates?customer_id=$customer->id", "$customer->
 
 
 Core::addBreadcrumb($_SERVER["REQUEST_URI"], $page_title);
 Core::addBreadcrumb($_SERVER["REQUEST_URI"], $page_title);
 
 
-
 $template = new Template;
 $template = new Template;
 $template->customer_id = $customer->id;
 $template->customer_id = $customer->id;
 $template->json = "";
 $template->json = "";
@@ -25,14 +24,22 @@ if($submit) {
 
 
 		$template->name = http::value("name", "trim");
 		$template->name = http::value("name", "trim");
 		$template->json = http::value("json", "trim");
 		$template->json = http::value("json", "trim");
+		$template->json_file = http::value("json_file", "trim");
 		$template->practice = http::value("practice", "int");
 		$template->practice = http::value("practice", "int");
 
 
 		if(empty($template->name)) $form_exception->addError("Name is required.");
 		if(empty($template->name)) $form_exception->addError("Name is required.");
-		if(empty($template->json)) $form_exception->addError("JSON is required.");
+		if(empty($template->json) && empty($template->json_file)) $form_exception->addError("JSON or JSON File is required.");
+
+		if($template->json) {
+			$json = json_decode($template->json);
+			if(!$json) {
+				$form_exception->addError("JSON error: " . json_last_error_msg());
+			}
+		}
 
 
-		$json = json_decode($template->json);
-		if(!$json) {
-			$form_exception->addError("JSON error: " . json_last_error_msg());
+		if($template->json_file) {
+			$path = ROOT_DIR . "/json/" . $template->json_file;
+			if(!file_exists($path)) $form_exception->addError("JSON file does not exist.");
 		}
 		}
 
 
 		if($form_exception->hasErrors()) throw $form_exception;
 		if($form_exception->hasErrors()) throw $form_exception;
@@ -53,5 +60,3 @@ if($submit) {
 View::includeOnce("header", compact("page_title"));
 View::includeOnce("header", compact("page_title"));
 View::include("template_form", compact("page_title", "template"));
 View::include("template_form", compact("page_title", "template"));
 View::includeOnce("footer");
 View::includeOnce("footer");
-
-?>

+ 12 - 6
controllers/template_edit.php

@@ -22,14 +22,22 @@ if($submit) {
 
 
 		$template->name = http::value("name", "trim");
 		$template->name = http::value("name", "trim");
 		$template->json = http::value("json", "trim");
 		$template->json = http::value("json", "trim");
+		$template->json_file = http::value("json_file", "trim");
 		$template->practice = http::value("practice", "int");
 		$template->practice = http::value("practice", "int");
 
 
 		if(empty($template->name)) $form_exception->addError("Name is required.");
 		if(empty($template->name)) $form_exception->addError("Name is required.");
-		if(empty($template->json)) $form_exception->addError("JSON is required.");
+		if(empty($template->json) && empty($template->json_file)) $form_exception->addError("JSON or JSON File is required.");
 
 
-		$json = json_decode($template->json);
-		if(!$json) {
-			$form_exception->addError("JSON error: " . json_last_error_msg());
+		if($template->json) {
+			$json = json_decode($template->json);
+			if(!$json) {
+				$form_exception->addError("JSON error: " . json_last_error_msg());
+			}
+		}
+
+		if($template->json_file) {
+			$path = ROOT_DIR . "/json/" . $template->json_file;
+			if(!file_exists($path)) $form_exception->addError("JSON file does not exist.");
 		}
 		}
 
 
 		if($form_exception->hasErrors()) throw $form_exception;
 		if($form_exception->hasErrors()) throw $form_exception;
@@ -50,5 +58,3 @@ if($submit) {
 View::includeOnce("header", compact("page_title"));
 View::includeOnce("header", compact("page_title"));
 View::include("template_form", compact("page_title", "template"));
 View::include("template_form", compact("page_title", "template"));
 View::includeOnce("footer");
 View::includeOnce("footer");
-
-?>

+ 2 - 2
controllers/test.php

@@ -42,7 +42,7 @@ if(Core::$user->level == User::LEVEL_TESTER) {
 		return;
 		return;
 	}
 	}
 
 
-	$json = json_decode($template->json);
+	$json = json_decode($template->getJSON());
 
 
 	$submit = http::value("submit");
 	$submit = http::value("submit");
 	if($submit) {
 	if($submit) {
@@ -228,7 +228,7 @@ if(Core::$user->level == User::LEVEL_TESTER) {
 // Verify test
 // Verify test
 else if(Core::$user->level >= User::LEVEL_ADMIN) {
 else if(Core::$user->level >= User::LEVEL_ADMIN) {
 
 
-	$json = json_decode($template->json);
+	$json = json_decode($template->getJSON());
 	$fields = array_merge($json->beforeTest, $json->duringTest);
 	$fields = array_merge($json->beforeTest, $json->duringTest);
 	$values = $test->getValues();
 	$values = $test->getValues();
 
 

+ 1 - 1
controllers/test_pdf.php

@@ -13,7 +13,7 @@ $customer = $project->getCustomer();
 $template = $project->getTemplate();
 $template = $project->getTemplate();
 $template->setOverrides($project->getTemplateOverrides());
 $template->setOverrides($project->getTemplateOverrides());
 
 
-$template_parts = json_decode($template->json);
+$template_parts = json_decode($template->getJSON());
 $fields = array_merge($template_parts->beforeTest, $template_parts->duringTest);
 $fields = array_merge($template_parts->beforeTest, $template_parts->duringTest);
 
 
 $field_name_label_map = [];
 $field_name_label_map = [];

+ 1 - 1
controllers/test_result.php

@@ -8,7 +8,7 @@ $project = $test->getProject();
 $tester = $test->getTester();
 $tester = $test->getTester();
 $template = $project->getTemplate();
 $template = $project->getTemplate();
 
 
-$json = json_decode($template->json);
+$json = json_decode($template->getJSON());
 $fields = array_merge($json->beforeTest, $json->duringTest);
 $fields = array_merge($json->beforeTest, $json->duringTest);
 $values = $test->getValues();
 $values = $test->getValues();
 
 

+ 16 - 6
engine/template.php

@@ -4,11 +4,21 @@ class Template extends Model {
 
 
 	protected static $table = "template";
 	protected static $table = "template";
 	protected static $id_field = "id";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["customer_id", "name", "json", "practice", "pass_stage"];
+	protected static $fillable_fields = ["customer_id", "name", "json", "json_file", "practice", "pass_stage"];
 
 
 	private $overrides = [];
 	private $overrides = [];
 
 
 	// Object methods
 	// Object methods
+	public function getJSON() {
+		$json_path = ROOT_DIR . "/json/" . $this->json_file;
+
+		if(!empty($this->json_file) && file_exists($json_path)) {
+			return file_get_contents($json_path);
+		}
+
+		return $this->json;
+	}
+
 	public function getCustomer() {
 	public function getCustomer() {
 		return Customer::get($this->customer_id);
 		return Customer::get($this->customer_id);
 	}
 	}
@@ -32,7 +42,7 @@ class Template extends Model {
 	public function getFields() {
 	public function getFields() {
 		$fields = [];
 		$fields = [];
 
 
-		$json = json_decode($this->json);
+		$json = json_decode($this->getJSON());
 		$before_fields = $json->beforeTest;
 		$before_fields = $json->beforeTest;
 		$during_fields = $json->duringTest;
 		$during_fields = $json->duringTest;
 		$all_fields = array_merge($before_fields, $during_fields);
 		$all_fields = array_merge($before_fields, $during_fields);
@@ -46,14 +56,14 @@ class Template extends Model {
 	}
 	}
 
 
 	public function getTesterInvoiceFields() {
 	public function getTesterInvoiceFields() {
-		$json = json_decode($this->json);
+		$json = json_decode($this->getJSON());
 		if(!$json) throw new Exception("Failed to decode template JSON.");
 		if(!$json) throw new Exception("Failed to decode template JSON.");
 
 
 		return $json->testerInvoiceFields ?? [];
 		return $json->testerInvoiceFields ?? [];
 	}
 	}
 
 
 	public function getCustomerInvoiceFields() {
 	public function getCustomerInvoiceFields() {
-		$json = json_decode($this->json);
+		$json = json_decode($this->getJSON());
 		if(!$json) throw new Exception("Failed to decode template JSON.");
 		if(!$json) throw new Exception("Failed to decode template JSON.");
 
 
 		return $json->customerInvoiceFields ?? [];
 		return $json->customerInvoiceFields ?? [];
@@ -79,7 +89,7 @@ class Template extends Model {
 
 
 	public function renderBeforeTestForm() {
 	public function renderBeforeTestForm() {
 
 
-		$json = json_decode($this->json);
+		$json = json_decode($this->getJSON());
 		$fields = $json->beforeTest;
 		$fields = $json->beforeTest;
 
 
 		?>
 		?>
@@ -111,7 +121,7 @@ class Template extends Model {
 
 
 	public function renderDuringTestForm() {
 	public function renderDuringTestForm() {
 
 
-		$json = json_decode($this->json);
+		$json = json_decode($this->getJSON());
 		$fields = $json->duringTest;
 		$fields = $json->duringTest;
 
 
 		?>
 		?>

+ 75 - 0
views/template.php

@@ -1,3 +1,9 @@
+<?php
+
+$tester_invoice_fields = $template->getTesterInvoiceFields();
+$customer_invoice_fields = $template->getCustomerInvoiceFields();
+
+?>
 <div class="container pt-3 my-4">
 <div class="container pt-3 my-4">
 	<div class="row">
 	<div class="row">
 		<div class="col align-self-center">
 		<div class="col align-self-center">
@@ -8,6 +14,75 @@
 		</div>
 		</div>
 	</div>
 	</div>
 
 
+	<div class="row">
+		<div class="col-lg-6">
+			<div class="card mb-3">
+				<div class="card-header">
+					Tester Invoice Fields
+				</div>
+				<div class="card-body">
+				<?php
+				if(empty($tester_invoice_fields)) {
+					?>
+					None configured.
+					<?php
+				}
+				else {
+					?>
+					The following costs will be included in testers' invoices:
+					<?php
+				}
+				?>
+				</div>
+				<div class="list-group list-group-flush">
+				<?php
+				foreach($tester_invoice_fields as $field) {
+					?>
+					<div class="list-group-item">
+						<code><?=htmlspecialchars($field->name)?></code> as
+						"<?=htmlspecialchars($field->label)?>"
+					</div>
+					<?php
+				}
+				?>
+				</div>
+			</div>
+		</div>
+		<div class="col-lg-6">
+			<div class="card mb-3">
+				<div class="card-header">
+					Customer Invoice Fields
+				</div>
+				<div class="card-body">
+				<?php
+				if(empty($tester_invoice_fields)) {
+					?>
+					None configured.
+					<?php
+				}
+				else {
+					?>
+					The following costs will be included in customer invoices:
+					<?php
+				}
+				?>
+				</div>
+				<div class="list-group list-group-flush">
+				<?php
+				foreach($customer_invoice_fields as $field) {
+					?>
+					<div class="list-group-item">
+						<code><?=htmlspecialchars($field->name)?></code> as
+						"<?=htmlspecialchars($field->label)?>"
+					</div>
+					<?php
+				}
+				?>
+				</div>
+			</div>
+		</div>
+	</div>
+
 	<div class="card mb-3">
 	<div class="card mb-3">
 		<div class="card-header">
 		<div class="card-header">
 			Before Test Form Preview
 			Before Test Form Preview

+ 9 - 1
views/template_form.php

@@ -15,7 +15,15 @@
 				<small class="form-text text-danger">Do not touch this if you don't know what you're doing.</small>
 				<small class="form-text text-danger">Do not touch this if you don't know what you're doing.</small>
 			</label>
 			</label>
 			<div class="col-sm">
 			<div class="col-sm">
-				<textarea class="form-control text-monospace" name="json" id="json" placeholder="Enter JSON" rows="24" required><?=htmlspecialchars($template->json)?></textarea>
+				<textarea class="form-control text-monospace" name="json" id="json" placeholder="Enter JSON" rows="24"><?=htmlspecialchars($template->json)?></textarea>
+			</div>
+		</div>
+		<div class="row mb-1">
+			<label class="col-sm-4 col-form-label" for="json">
+				JSON Filename
+			</label>
+			<div class="col-sm">
+				<input class="form-control" name="json_file" id="json_file" placeholder="Enter JSON filename" value="<?=htmlspecialchars($template->json_file)?>" />
 			</div>
 			</div>
 		</div>
 		</div>
 		<div class="row mt-3 mb-3">
 		<div class="row mt-3 mb-3">

+ 1 - 1
views/test_form.php

@@ -8,7 +8,7 @@ else if($test->status == Test::STATUS_STARTED) {
 }
 }
 else if($test->status == Test::STATUS_PENDING_VERIFICATION) {
 else if($test->status == Test::STATUS_PENDING_VERIFICATION) {
 
 
-	$json = json_decode($template->json);
+	$json = json_decode($template->getJSON());
 	$fields = $json->duringTest;
 	$fields = $json->duringTest;
 
 
 	$values = $test->getValues(true);
 	$values = $test->getValues(true);