Explorar o código

Add support for batch project exports

Alan Lam %!s(int64=7) %!d(string=hai) anos
pai
achega
cfe80b5f36

+ 1 - 0
controllers/project_export.php

@@ -9,6 +9,7 @@ else {
 }
 
 $projects = Project::query(["id" => $ids]);
+if(!$projects) throw new Exception("No projects to export.");
 
 $template_ids = array_column($projects, "template_id");
 $template_ids = array_unique($template_ids);

+ 2 - 1
controllers/projects.php

@@ -1,9 +1,10 @@
 <?php
 
-
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/projects", "Projects");
 
+Core::addScript("public/js/app-projects.js");
+
 $customer = Customer::getForAdmin();
 
 $page_title = "$customer->name Projects";

+ 28 - 0
public/js/app-projects.js

@@ -0,0 +1,28 @@
+$(function() {
+	$(document).on("click", "#projects-table tbody tr", function(event) {
+
+		if(event.target.nodeName == "A") return true;
+
+		if(!event.target.type || event.target.type != "checkbox") {
+			var checkbox = $(this).find("input[type=checkbox]").get(0);
+			checkbox.checked = !checkbox.checked;
+		}
+	});
+
+	$("#batch-action").change(function() {
+		var action = $("#batch-action").val();
+
+		switch(action) {
+			case "export-admin-csv":
+				$("#projects-form").attr("action", "/account/projects/export-csv");
+				$("#type").val("admin");
+				break;
+
+			case "export-customer-csv":
+				$("#projects-form").attr("action", "/account/projects/export-csv");
+				$("#type").val("customer");
+				break;
+		}
+	});
+
+});

+ 1 - 0
routes.php

@@ -64,6 +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\/export-csv$/" => ["controller" => "project_export", "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],

+ 9 - 3
views/project_list.php

@@ -1,15 +1,18 @@
-<table class="table table-sm table-sm-text table-hover">
+<table id="projects-table" class="table table-sm table-sm-text table-hover">
+	<col style="width: 48px;" />
+
 	<thead>
 		<tr>
+			<th></th>
 			<th class="text-right pr-3">ID</th>
 			<th>Name</th>
 			<th>Tests</th>
 			<th>Period</th>
 			<th>Report Month</th>
-			<th></th>
+			<th>Actions</th>
 		</tr>
 	</thead>
-	<tbody>
+	<tbody class="table-pointer">
 	<?php
 	$project_ids = array_column($projects, "id");
 	$tests = Test::query(["project_id" => $project_ids]);
@@ -35,6 +38,9 @@
 
 		?>
 		<tr>
+			<td class="text-center align-middle">
+				<input type="checkbox" name="ids[]" value="<?=$project->id?>" />
+			</td>
 			<td class="text-right pr-3 align-middle">
 				<?=$project->id?>
 			</td>

+ 27 - 3
views/projects.php

@@ -74,7 +74,31 @@
 		</div>
 	</div>
 
-	<?php
-	View::include("project_list", compact("customer", "projects"));
-	?>
+	<form id="projects-form" method="post">
+		<input type="hidden" id="type" name="type" value="" />
+		<?php
+		View::include("project_list", compact("customer", "projects"));
+		?>
+		<div class="row">
+			<div class="col-md-6 offset-md-6">
+				<div class="input-group">
+					<div class="input-group-prepend">
+						<span class="input-group-text">
+							Batch Action
+						</span>
+					</div>
+					<select class="form-control" id="batch-action" required>
+						<option value="">Select Action</option>
+						<option value="export-admin-csv">Export CSV</option>
+						<option value="export-customer-csv">Export Customer CSV</option>
+					</select>
+					<div class="input-group-append">
+						<button type="submit" class="btn btn-primary" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">
+							Submit
+						</button>
+					</div>
+				</div>
+			</div>
+		</div>
+	</form>
 </div>