瀏覽代碼

Factorise project CSV export code

Alan Lam 7 年之前
父節點
當前提交
0c68dd0b78

+ 0 - 41
controllers/project_csv.php

@@ -1,41 +0,0 @@
-<?php
-
-$project = Project::get($id);
-if(!$project) throw new Exception("Project cannot be found.");
-if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this resource.");
-
-header("Content-Type: text/csv");
-header("Content-Disposition: attachment; filename=\"export.csv\"");
-header("Cache-Control: private, max-age=0, must-revalidate");
-header("Pragma: public");
-
-$template = $project->getTemplate();
-$processing_file_path = $template->getProcessingFilePath("project_csv.php");
-
-$object = json_decode($template->getJSON());
-
-$fields_1 = $object->beforeTest;
-$fields_2 = $object->duringTest;
-
-$fields = array_merge($fields_1, $fields_2);
-$fields = array_filter($fields, function($field) {
-	if(!isset($field->name)) return false;
-	if($field->type == "file") return false;
-	return true;
-});
-
-$tests = Test::query([
-	"project_id" => $id,
-	"status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
-]);
-
-$locations = Location::load(array_column($tests, "location_id"));
-$testers = User::load(array_column($tests, "tester_id"));
-
-$handle = fopen("php://output", "w");
-
-require $processing_file_path;
-
-fclose($handle);
-
-?>

+ 0 - 41
controllers/project_customer_csv.php

@@ -1,41 +0,0 @@
-<?php
-
-$project = Project::get($id);
-if(!$project) throw new Exception("Project cannot be found.");
-if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this resource.");
-
-header("Content-Type: text/csv");
-header("Content-Disposition: attachment; filename=\"export.csv\"");
-header("Cache-Control: private, max-age=0, must-revalidate");
-header("Pragma: public");
-
-$template = $project->getTemplate();
-$processing_file_path = $template->getProcessingFilePath("project_customer_csv.php");
-
-$object = json_decode($template->getJSON());
-
-$fields_1 = $object->beforeTest;
-$fields_2 = $object->duringTest;
-
-$fields = array_merge($fields_1, $fields_2);
-$fields = array_filter($fields, function($field) {
-	if(!isset($field->name)) return false;
-	if($field->type == "file") return false;
-	return true;
-});
-
-$tests = Test::query([
-	"project_id" => $id,
-	"status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
-]);
-
-$locations = Location::load(array_column($tests, "location_id"));
-$testers = User::load(array_column($tests, "tester_id"));
-
-$handle = fopen("php://output", "w");
-
-require $processing_file_path;
-
-fclose($handle);
-
-?>

+ 41 - 0
controllers/project_export.php

@@ -0,0 +1,41 @@
+<?php
+
+if(!isset($id, $type)) {
+	$ids = http::value("ids", "int");
+	$type = http::value("type");
+}
+else {
+	$ids = [$id];
+}
+
+$projects = Project::query(["id" => $ids]);
+
+$template_ids = array_column($projects, "template_id");
+$template_ids = array_unique($template_ids);
+if(count($template_ids) != 1) throw new Exception("Cannot create export with different templates.");
+
+$template = Template::get($template_ids[0]);
+if(!$template) throw new Exception("Template cannot be found.");
+
+$tests = Test::query([
+	"project_id" => $ids,
+	"status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
+]);
+
+switch($type) {
+
+	case "admin":
+		$template->exportTestsCSV("project_csv.php", $tests);
+		break;
+
+	case "customer":
+		$template->exportTestsCSV("project_customer_csv.php", $tests);
+		break;
+
+	case "kml":
+		// TODO
+		break;
+
+}
+
+

+ 35 - 0
engine/template.php

@@ -9,6 +9,41 @@ class Template extends Model {
 	private $overrides = [];
 
 	// Object methods
+	public function exportTestsCSV($processing_file, $tests = []) {
+
+		$template = $this;
+
+		// Preload
+		Project::load(array_column($tests, "project_id"));
+		Location::load(array_column($tests, "location_id"));
+		User::load(array_column($tests, "tester_id"));
+
+		// Get processing file
+		$processing_file_path = $this->getProcessingFilePath($processing_file);
+
+		// Get fields
+		$config = json_decode($this->getJSON());
+
+		$fields = array_merge($config->beforeTest, $config->duringTest);
+		$fields = array_filter($fields, function($field) {
+			if(!isset($field->name)) return false;
+			if($field->type == "file") return false;
+			return true;
+		});
+
+		// Output
+		header("Content-Type: text/plain");
+		header("Content-Disposition: attachment; filename=\"export.csv\"");
+		header("Cache-Control: private, max-age=0, must-revalidate");
+		header("Pragma: public");
+
+		$handle = fopen("php://output", "w");
+
+		require $processing_file_path;
+
+		fclose($handle);
+	}
+
 	public function getJSON() {
 		$json_path = ROOT_DIR . "/json/" . $this->json_file;
 

+ 1 - 2
routes.php

@@ -64,8 +64,7 @@ $routes = [
 	"/^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-(admin|customer)-csv$/" => ["controller" => "project_export", "params" => ["id", "type"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/export-kml$/" => ["controller" => "project_kml", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/invoice$/" => ["controller" => "project_invoice", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/(mark-finished|mark-unfinished)$/" => ["controller" => "project_mark_finished", "params" => ["id", "action"], "require_user_level" => User::LEVEL_ADMIN],

+ 2 - 1
template_processing/age_verification/project_csv.php

@@ -19,6 +19,7 @@ fputcsv($handle, $field_names);
 
 foreach($tests as $test) {
 
+	$project = $test->getProject();
 	$location = $test->getLocation();
 	$tester = $test->getTester();
 	$values = $test->getValues(true);
@@ -35,7 +36,7 @@ foreach($tests as $test) {
 	$travel_costs = "";
 
 	$output = [];
-	$output[] = $id;
+	$output[] = $project->id;
 	$output[] = $project->name;
 	$output[] = $template->name;
 	$output[] = $test->id;

+ 1 - 0
template_processing/age_verification/project_customer_csv.php

@@ -18,6 +18,7 @@ fputcsv($handle, $field_names);
 
 foreach($tests as $test) {
 
+	$project = $test->getProject();
 	$location = $test->getLocation();
 	$tester = $test->getTester();
 	$values = $test->getValues(true);

+ 2 - 1
template_processing/age_verification_mitchells_butlers/project_csv.php

@@ -19,6 +19,7 @@ fputcsv($handle, $field_names);
 
 foreach($tests as $test) {
 
+	$project = $test->getProject();
 	$location = $test->getLocation();
 	$tester = $test->getTester();
 	$values = $test->getValues(true);
@@ -35,7 +36,7 @@ foreach($tests as $test) {
 	$travel_costs = "";
 
 	$output = [];
-	$output[] = $id;
+	$output[] = $project->id;
 	$output[] = $project->name;
 	$output[] = $template->name;
 	$output[] = $test->id;

+ 1 - 0
template_processing/age_verification_mitchells_butlers/project_customer_csv.php

@@ -18,6 +18,7 @@ fputcsv($handle, $field_names);
 
 foreach($tests as $test) {
 
+	$project = $test->getProject();
 	$location = $test->getLocation();
 	$tester = $test->getTester();
 	$values = $test->getValues(true);

+ 2 - 1
template_processing/mystery_shop/project_csv.php

@@ -18,6 +18,7 @@ fputcsv($handle, $field_names);
 
 foreach($tests as $test) {
 
+	$project = $test->getProject();
 	$location = $test->getLocation();
 	$tester = $test->getTester();
 	$values = $test->getValues(true);
@@ -32,7 +33,7 @@ foreach($tests as $test) {
 	$travel_costs = "";
 
 	$output = [];
-	$output[] = $id;
+	$output[] = $project->id;
 	$output[] = $project->name;
 	$output[] = $template->name;
 	$output[] = $test->id;

+ 1 - 0
template_processing/mystery_shop/project_customer_csv.php

@@ -16,6 +16,7 @@ fputcsv($handle, $field_names);
 
 foreach($tests as $test) {
 
+	$project = $test->getProject();
 	$location = $test->getLocation();
 	$tester = $test->getTester();
 	$values = $test->getValues(true);

+ 1 - 1
views/project.php

@@ -29,7 +29,7 @@ var projectID = <?=$project->id?>;
 				</button>
 				<div class="dropdown-menu dropdown-menu-right" aria-labelledby="actions-button">
 					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/edit">Edit</a>
-					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-csv">Export CSV</a>
+					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-admin-csv">Export CSV</a>
 					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-customer-csv">Export Customer CSV</a>
 					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-kml">Export KML</a>
 					<?php