Explorar el Código

Repurpose Project table to exclude projects rather than include and add UI improvements

Alan Lam hace 7 años
padre
commit
2125f5eabb
Se han modificado 4 ficheros con 65 adiciones y 54 borrados
  1. 6 5
      controllers/reports.php
  2. 12 0
      engine/constraint.php
  3. 18 26
      public/js/app-reports.js
  4. 29 23
      views/reports.php

+ 6 - 5
controllers/reports.php

@@ -28,8 +28,8 @@ $date_resolution = http::value("date_resolution");
 $date_start = http::value("date_start");
 $date_end = http::value("date_end");
 
-$project_ids = http::value("project_ids", "int");
-if(!is_array($project_ids)) $project_ids = [];
+$exclude_project_ids = http::value("exclude_project_ids", "int");
+if(!is_array($exclude_project_ids)) $exclude_project_ids = [];
 
 $location_ids = http::value("location_ids");
 if(!empty($location_ids)) {
@@ -58,7 +58,6 @@ foreach($all_projects as $p) {
 	$project_table_data[] = $p->getDataTableRow();
 }
 
-
 $data = [];
 $region_data = [];
 $area_manager_data = [];
@@ -79,9 +78,11 @@ if($submit) {
 		// Get all projects
 		$projects = Project::query([
 			"customer_id" => $customer->id,
-			"id" => $project_ids
+			"id" => (new Constraint("NOT_IN"))->setValues(...$exclude_project_ids)
 		]);
 
+		$project_ids = array_column($projects, "id");
+
 		// Enforce unique template ID in selection
 		$template_ids = array_unique(array_column($projects, "template_id"));
 		if(count($template_ids) > 1) $form_exception->addError("Projects with different templates have been selected.");
@@ -383,5 +384,5 @@ if($submit) {
 }
 
 View::includeOnce("header", compact("page_title"));
-View::include("reports", compact("customer", "date_range", "date_start", "date_end", "date_resolution", "all_projects", "project_table_data", "project_ids", "location_ids", "charts", "data", "total_data", "locations_missing_region", "locations_missing_area_manager", "region_data", "area_manager_data", "key_labels", "has_pass_fail"));
+View::include("reports", compact("customer", "date_range", "date_start", "date_end", "date_resolution", "all_projects", "project_table_data", "exclude_project_ids", "location_ids", "charts", "data", "total_data", "locations_missing_region", "locations_missing_area_manager", "region_data", "area_manager_data", "key_labels", "has_pass_fail"));
 View::includeOnce("footer");

+ 12 - 0
engine/constraint.php

@@ -19,6 +19,8 @@ class Constraint {
 	const TYPE_IS_NULL = 10;
 	const TYPE_IS_NOT_NULL = 11;
 
+	const TYPE_NOT_IN = 12;
+
 	private $type;
 	private $values = [];
 
@@ -72,6 +74,16 @@ class Constraint {
 				return " IS NULL";
 			case self::TYPE_IS_NOT_NULL:
 				return " IS NOT NULL";
+
+			case self::TYPE_NOT_IN:
+
+				if(empty($this->values)) return false;
+
+				$values = array_map(function($value) {
+					return sprintf("'%s'", $value);
+				}, $this->values);
+
+				return sprintf(" NOT IN (%s)", implode(", ", $values));
 		}
 
 		return false;

+ 18 - 26
public/js/app-reports.js

@@ -11,10 +11,10 @@ $(function() {
 				data: "id",
 				class: "text-right",
 				orderable: false,
-				render: function(data, type, row) {
-					if(type == "display") {
-						var checked = (selectedProjectIds.indexOf(data) != -1) ? "checked" : ""
-						return "<input type=\"checkbox\" name=\"project_ids[]\" value=\"" + data + "\" " + checked + " /> ";
+				render: function(data, type) {
+					if(type === "display") {
+						var checked = (selectedProjectIds.indexOf(data) !== -1) ? "checked" : "";
+						return "<input type=\"checkbox\" name=\"exclude_project_ids[]\" value=\"" + data + "\" " + checked + " /> ";
 					}
 					return data;
 				}
@@ -37,7 +37,7 @@ $(function() {
 				data: "reportMonth",
 				class: "text-right",
 				render: function(data, type, row) {
-					if(type == "display" || type == "filter") {
+					if(type === "display" || type === "filter") {
 						return data;
 					}
 					return row.unixReport;
@@ -47,7 +47,7 @@ $(function() {
 	});
 
 	$(document).on("click", "#projects-table tbody tr", function(event) {
-		if(!event.target.type || event.target.type != "checkbox") {
+		if(!event.target.type || event.target.type !== "checkbox") {
 			var checkbox = $(this).find("input[type=checkbox]").get(0);
 			checkbox.checked = !checkbox.checked;
 		}
@@ -59,9 +59,7 @@ $(function() {
 
 		object.options.plugins.datalabels.display = function(context) {
 			var dataset = context.dataset;
-			var count = dataset.data.length;
-			var value = dataset.data[context.dataIndex];
-			return value;
+			return dataset.data[context.dataIndex];
 		};
 
 		if(object.datalabelFormatterCallback) {
@@ -72,7 +70,7 @@ $(function() {
 			object.options.plugins.datalabels.formatter = function(value, context) {
 				let suffix = object.options.datalabelSuffixes[context.datasetIndex];
 
-				if(suffix.length > 2 && value != 1) {
+				if(suffix.length > 2 && parseInt(value) !== 1) {
 					suffix += "s";
 				}
 
@@ -80,13 +78,12 @@ $(function() {
 			}
 		}
 
-		var chart = new Chart(this, object);
+		new Chart(this, object);
 	});
 
 	$("input[data-date-range-radio-input]").change(function() {
-		let checked = $(this).is(":checked");
 		let value = $(this).val();
-		if(value == "custom") {
+		if(value === "custom") {
 			$("#date-range-custom-fields").slideDown("fast");
 		}
 		else {
@@ -108,7 +105,7 @@ $(function() {
 		locale: {
 			format: "DD/MM/YYYY"
 		}
-	}, function(start, end, label) {
+	}, function(start, end) {
 
 		var startValue = start.format('YYYY-MM-DD');
 		var endValue = end.format('YYYY-MM-DD');
@@ -126,7 +123,7 @@ function testsByStageFormatter(value, context) {
 	let percentage = Math.round((value * 10000) / total) / 100;
 
 	let suffix = "Test";
-	if(value != 1) {
+	if(parseInt(value) !== 1) {
 		suffix += "s";
 	}
 
@@ -141,22 +138,20 @@ function exportCharts(action) {
 
 	let formId = "";
 
-	if(action == "pptx") {
+	if(action === "pptx") {
 		formId = "form-pptx";
 	}
 
-	if(action == "odp") {
+	if(action === "odp") {
 		formId = "form-odp";
 	}
 
 	let form = $("#" + formId);
-	let rendered = $(form).data("rendered");
-
-	console.log(formId, form);
-
-	if(rendered == "0") {
+	let rendered = parseInt($(form).data("rendered"));
 
+	if(rendered === 0) {
 		for(var i in Chart.instances) {
+			if(!Chart.instances.hasOwnProperty(i)) continue;
 			let chart = Chart.instances[i];
 			let base64 = chart.toBase64Image();
 
@@ -165,11 +160,8 @@ function exportCharts(action) {
 		}
 
 		$(form).data("rendered", "1");
-
 	}
 
-	console.log("done");
-
 	$(form).find("input[type=submit]").click();
 
-}
+}

+ 29 - 23
views/reports.php

@@ -1,5 +1,10 @@
 <?php
 
+/**
+ * @var Chart[] $charts
+ * @var string[] $key_labels
+ */
+
 $date_resolutions = [
 	"quarterly" => "Quarterly",
 	"monthly" => "Monthly",
@@ -26,8 +31,8 @@ if($date_start && $date_end) {
 
 ?>
 <script>
-var selectedProjectIds = <?=json_encode($project_ids)?>;
-var projectData = <?=json_encode($project_table_data)?>;
+let selectedProjectIds = <?=json_encode($exclude_project_ids)?>;
+let projectData = <?=json_encode($project_table_data)?>;
 </script>
 
 <div class="container mt-3 py-4">
@@ -61,7 +66,7 @@ var projectData = <?=json_encode($project_table_data)?>;
 			<div class="col-md">
 				<div class="form-group">
 					<label>
-						Projects
+						Exclude Projects
 					</label>
 					<table class="table table-sm table-sm-text table-hover table-pointer" id="projects-table">
 						<thead>
@@ -79,7 +84,7 @@ var projectData = <?=json_encode($project_table_data)?>;
 			</div>
 			<div class="col-md">
 				<div class="form-group">
-					<label class="form-label">
+					<label class="form-label" for="date-range">
 						Date Range
 					</label><br / >
 					<?php
@@ -134,7 +139,7 @@ var projectData = <?=json_encode($project_table_data)?>;
 	if(Core::$user->isAdmin()) {
 		if($locations_missing_region) {
 			?>
-			<div class="alert alert-warning d-print-none">
+			<div class="alert alert-warning limit-height-1 d-print-none">
 				There are some locations that are missing "region" information:
 				<ul class="mb-0">
 				<?php
@@ -160,7 +165,7 @@ var projectData = <?=json_encode($project_table_data)?>;
 		}
 		if($locations_missing_area_manager) {
 			?>
-			<div class="alert alert-warning d-print-none">
+			<div class="alert alert-warning limit-height-1 d-print-none">
 				There are some locations that are missing "area manager" information:
 				<ul class="mb-0">
 				<?php
@@ -185,10 +190,7 @@ var projectData = <?=json_encode($project_table_data)?>;
 			<?php
 		}
 	}
-	?>
-
 
-	<?php
 	if($charts) {
 		?>
 
@@ -230,7 +232,7 @@ var projectData = <?=json_encode($project_table_data)?>;
 		<div class="row mb-4">
 			<div class="col-md-4 d-print-none" style="position: relative;">
 				<h3>Chart List</h3>
-				<div class="list-group" style="position: sticky; top: 7rem;">
+				<div class="list-group limit-height-2" style="position: sticky; top: 7rem;">
 				<?php
 				foreach($charts as $index => $chart) {
 					?>
@@ -242,7 +244,7 @@ var projectData = <?=json_encode($project_table_data)?>;
 				?>
 				</div>
 			</div>
-			<div class="col-md">
+			<div class="col-md-8">
 			<?php
 			foreach($charts as $index => $chart) {
 				if($index > 0) {
@@ -284,9 +286,9 @@ var projectData = <?=json_encode($project_table_data)?>;
 				<input type="hidden" name="date_end" id="date-end" value="<?=htmlspecialchars($date_end)?>" />
 				<input type="hidden" name="date_resolution" value="<?=htmlspecialchars($date_resolution)?>" />
 				<?php
-				foreach($project_ids as $project_id) {
+				foreach($exclude_project_ids as $project_id) {
 					?>
-					<input type="hidden" name="project_ids[]" value="<?=intval($project_id)?>" />
+					<input type="hidden" name="exclude_project_ids[]" value="<?=intval($project_id)?>" />
 					<?php
 				}
 				?>
@@ -310,11 +312,13 @@ var projectData = <?=json_encode($project_table_data)?>;
 					<h4>Region: <?=htmlspecialchars($region)?></h4>
 
 					<table class="table">
-						<col style="width: 20%" />
-						<col style="width: 20%" />
-						<col style="width: 20%" />
-						<col style="width: 20%" />
-						<col style="width: 20%" />
+						<colgroup>
+							<col style="width: 20%" />
+							<col style="width: 20%" />
+							<col style="width: 20%" />
+							<col style="width: 20%" />
+							<col style="width: 20%" />
+						</colgroup>
 						<thead>
 							<tr>
 								<th>Period</th>
@@ -361,11 +365,13 @@ var projectData = <?=json_encode($project_table_data)?>;
 					<h4>Area Manager: <?=htmlspecialchars($area_manager)?></h4>
 
 					<table class="table">
-						<col style="width: 20%" />
-						<col style="width: 20%" />
-						<col style="width: 20%" />
-						<col style="width: 20%" />
-						<col style="width: 20%" />
+						<colgroup>
+							<col style="width: 20%" />
+							<col style="width: 20%" />
+							<col style="width: 20%" />
+							<col style="width: 20%" />
+							<col style="width: 20%" />
+						</colgroup>
 						<thead>
 							<tr>
 								<th>Period</th>