Переглянути джерело

Add ability to export projects from the test results project list page

Alan Lam 1 рік тому
батько
коміт
8e4eea3d31

+ 6 - 0
controllers/project_export.php

@@ -15,6 +15,12 @@ if(!$projects) {
 	throw new RuntimeException("No projects to export.");
 }
 
+foreach($projects as $project) {
+	if(!$user->hasPermission($project)) {
+		throw new RuntimeException("Invalid project for export requested.");
+	}
+}
+
 $template_processing = false;
 
 switch($type) {

+ 14 - 4
controllers/test_results_project_list.php

@@ -1,15 +1,23 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/**
+ * @var int $customer_id
+ */
 
 $customers = Core::$user->getCustomers();
-if(!$customers) throw new Exception(__("An unexpected error has occurred. Please try again later."));
+if(!$customers) {
+	throw new RuntimeException(__("An unexpected error has occurred. Please try again later."));
+}
 
 $customer_ids = array_column($customers, "id");
-if(!in_array($customer_id, $customer_ids)) throw new Exception(__("You do not have permission to access this page."));
+if(!in_array($customer_id, $customer_ids)) {
+	throw new RuntimeException(__("You do not have permission to access this page."));
+}
 
 $customer = Customer::get($customer_id);
 $projects = $customer->getProjects();
 
-$projects = array_filter($projects, function(Project $project) {
+$projects = array_filter($projects, static function(Project $project) {
     return $project->isFinished();
 });
 
@@ -21,6 +29,8 @@ Core::addBreadcrumb("/account", __("Account"));
 Core::addBreadcrumb("/account/test-results", __("Test Results"));
 Core::addBreadcrumb($test_results_url, $customer->name);
 
+Core::addScript("public/js/app-test-results-project-list.js");
+
 View::includeOnce("header", compact("page_title"));
 View::include("test_results_project_list", compact("projects"));
 View::includeOnce("footer");

+ 13 - 11
engine/user.php

@@ -453,31 +453,33 @@ class User extends Model {
 	}
 
 	/**
-	 * @param $object
+	 * @param Model $model
 	 * @return bool
 	 * @throws Exception
 	 */
-	public function hasPermission($object): bool {
+	public function hasPermission(Model $model): bool {
 
-		if($this->canAccessLocations()) return true;
+		if($this->canAccessLocations()) {
+			return true;
+		}
 
 		$customers = $this->getCustomers();
 		$customer_ids = array_column($customers, "id");
 
-		if($object instanceof Customer) {
-			return in_array($object->id, $customer_ids);
+		if($model instanceof Customer) {
+			return in_array($model->id, $customer_ids);
 		}
 
-		if($object instanceof Location) {
-			return in_array($object->customer_id, $customer_ids);
+		if($model instanceof Location) {
+			return in_array($model->customer_id, $customer_ids);
 		}
 
-		if($object instanceof Project) {
-			return in_array($object->customer_id, $customer_ids);
+		if($model instanceof Project) {
+			return in_array($model->customer_id, $customer_ids);
 		}
 
-		if($object instanceof Template) {
-			return in_array($object->customer_id, $customer_ids);
+		if($model instanceof Template) {
+			return in_array($model->customer_id, $customer_ids);
 		}
 
 		return false;

+ 24 - 0
public/js/app-test-results-project-list.js

@@ -0,0 +1,24 @@
+$(() => {
+	$("tbody tr").on("click", event => {
+		const checkbox = event.currentTarget.querySelector(".project-checkbox");
+		checkbox.checked = !checkbox.checked;
+	});
+
+	$("#export-csv-button").on("click", event => {
+		event.preventDefault();
+		const checkboxes = document.querySelectorAll(".project-checkbox:checked");
+
+		if(checkboxes.length === 0) {
+			return;
+		}
+
+		const params = new URLSearchParams();
+		[...checkboxes].forEach(checkbox => params.append("ids[]", checkbox.value));
+		params.append("type", "customer");
+
+		const queryString = params.toString();
+		const url = "/account/projects/export-csv?" + queryString;
+
+		window.location = url;
+	});
+});

+ 52 - 14
views/test_results_project_list.php

@@ -1,13 +1,42 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 /**
  * @var Project[] $projects
  */
 ?>
 <div class="container mt-3 py-4">
 
-    <h2><?=htmlspecialchars(__("Test Results"))?></h2>
+	<div class="row">
+		<div class="col">
+			<h2><?=htmlspecialchars(__("Test Results"))?></h2>
+		</div>
+		<div class="col-auto">
+		</div>
+	</div>
+
+	<div class="row">
+		<div class="col">
+			<h3><?=htmlspecialchars(__("Projects"))?></h3>
+		</div>
+		<div class="col-auto">
+			<?php
+			if($projects) {
+				?>
+				<div class="dropdown">
+					<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
+						<?=htmlspecialchars(__("Actions"))?>
+					</button>
+					<div class="dropdown-menu">
+						<a href="#" class="dropdown-item" id="export-csv-button">
+							<?=htmlspecialchars(__("Export CSV"))?>
+						</a>
+					</div>
+				</div>
+				<?php
+			}
+			?>
+		</div>
+	</div>
 
-    <h3><?=htmlspecialchars(__("Select Project"))?></h3>
 
     <?php
 
@@ -23,18 +52,24 @@
     else {
         ?>
 
-        <table class="table table-bordered">
-            <col style="width: 40%;">
-            <col style="width: 15%;">
-            <col style="width: 15%;">
-            <col style="width: 15%;">
-            <col style="width: 15%;">
+        <table class="table table-bordered table-hover">
+			<colgroup>
+				<col style="width: 1%;">
+				<col style="width: 40%;">
+				<col style="width: 15%;">
+				<col style="width: 15%;">
+				<col style="width: 15%;">
+				<col style="width: 15%;">
+			</colgroup>
             <thead>
-                <th><?=htmlspecialchars(__("Name"))?></th>
-                <th><?=htmlspecialchars(__("Report Month"))?></th>
-                <th><?=htmlspecialchars(__("Test Count"))?></th>
-                <th><?=htmlspecialchars(__("Pass / Fail"))?></th>
-                <th><?=htmlspecialchars(__("Pass Rate"))?></th>
+			<tr>
+				<th></th>
+				<th><?=htmlspecialchars(__("Name"))?></th>
+				<th><?=htmlspecialchars(__("Report Month"))?></th>
+				<th><?=htmlspecialchars(__("Test Count"))?></th>
+				<th><?=htmlspecialchars(__("Pass / Fail"))?></th>
+				<th><?=htmlspecialchars(__("Pass Rate"))?></th>
+			</tr>
             </thead>
             <tbody>
             <?php
@@ -42,6 +77,9 @@
                 $url = $project->getTestResultsUrl();
                 ?>
                 <tr>
+					<td>
+						<input class="project-checkbox" type="checkbox" value="<?=$project->id?>" />
+					</td>
                     <td><a href="<?=htmlspecialchars($url)?>"><?=$project->getNameHtml()?></a></td>
                     <td><?=$project->getDisplayReportMonth()?></td>
                     <td><?=$project->getTestCount()?></td>