Răsfoiți Sursa

Add support for exporting PPTX on Performance page

Alan Lam 8 ani în urmă
părinte
comite
09f7f6f916

+ 1 - 0
.gitignore

@@ -11,3 +11,4 @@ tasks/email_test.php
 tasks/data/
 backups/
 files/countries.json
+scrap/

+ 2 - 1
composer.json

@@ -1,5 +1,6 @@
 {
     "require": {
-        "phpmailer/phpmailer": "^6.0"
+        "phpmailer/phpmailer": "^6.0",
+		"phpoffice/phppresentation": "dev-master"
     }
 }

+ 170 - 7
composer.lock

@@ -4,20 +4,57 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "content-hash": "6d67203b6e9fc952ae681c538683a497",
+    "content-hash": "d3cdf144ec8301681fb8f12546af92b8",
     "packages": [
+        {
+            "name": "pclzip/pclzip",
+            "version": "2.8.2",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/ivanlanin/pclzip.git",
+                "reference": "19dd1de9d3f5fc4d7d70175b4c344dee329f45fd"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/ivanlanin/pclzip/zipball/19dd1de9d3f5fc4d7d70175b4c344dee329f45fd",
+                "reference": "19dd1de9d3f5fc4d7d70175b4c344dee329f45fd",
+                "shasum": ""
+            },
+            "type": "library",
+            "autoload": {
+                "classmap": [
+                    "pclzip.lib.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "LGPL-2.1"
+            ],
+            "authors": [
+                {
+                    "name": "Vincent Blavet"
+                }
+            ],
+            "description": "A PHP library that offers compression and extraction functions for Zip formatted archives",
+            "homepage": "http://www.phpconcept.net/pclzip",
+            "keywords": [
+                "php",
+                "zip"
+            ],
+            "time": "2014-06-05T11:42:24+00:00"
+        },
         {
             "name": "phpmailer/phpmailer",
-            "version": "v6.0.3",
+            "version": "v6.0.5",
             "source": {
                 "type": "git",
                 "url": "https://github.com/PHPMailer/PHPMailer.git",
-                "reference": "44d49bab5ab1fef721d3ee07e75dc0865ddf4cc6"
+                "reference": "cb3ea134d4d3729e7857737d5f320cce9caf4d32"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/44d49bab5ab1fef721d3ee07e75dc0865ddf4cc6",
-                "reference": "44d49bab5ab1fef721d3ee07e75dc0865ddf4cc6",
+                "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/cb3ea134d4d3729e7857737d5f320cce9caf4d32",
+                "reference": "cb3ea134d4d3729e7857737d5f320cce9caf4d32",
                 "shasum": ""
             },
             "require": {
@@ -70,13 +107,139 @@
                 }
             ],
             "description": "PHPMailer is a full-featured email creation and transfer class for PHP",
-            "time": "2018-01-05T13:19:58+00:00"
+            "time": "2018-03-27T13:49:45+00:00"
+        },
+        {
+            "name": "phpoffice/common",
+            "version": "0.2.9",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/PHPOffice/Common.git",
+                "reference": "edb5d32b1e3400a35a5c91e2539ed6f6ce925e4d"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/PHPOffice/Common/zipball/edb5d32b1e3400a35a5c91e2539ed6f6ce925e4d",
+                "reference": "edb5d32b1e3400a35a5c91e2539ed6f6ce925e4d",
+                "shasum": ""
+            },
+            "require": {
+                "pclzip/pclzip": "^2.8",
+                "php": ">=5.3.0"
+            },
+            "require-dev": {
+                "phpdocumentor/phpdocumentor": "2.*",
+                "phploc/phploc": "2.*",
+                "phpmd/phpmd": "2.*",
+                "phpunit/phpunit": "^4.8.36 || ^7.0",
+                "sebastian/phpcpd": "2.*",
+                "squizlabs/php_codesniffer": "2.*"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "PhpOffice\\Common\\": "src/Common/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "LGPL"
+            ],
+            "authors": [
+                {
+                    "name": "Mark Baker"
+                },
+                {
+                    "name": "Franck Lefevre",
+                    "homepage": "http://rootslabs.net"
+                }
+            ],
+            "description": "PHPOffice Common",
+            "homepage": "http://phpoffice.github.io",
+            "keywords": [
+                "common",
+                "component",
+                "office",
+                "php"
+            ],
+            "time": "2018-07-13T14:12:34+00:00"
+        },
+        {
+            "name": "phpoffice/phppresentation",
+            "version": "dev-master",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/PHPOffice/PHPPresentation.git",
+                "reference": "1fcc84072ee0061f8991b8f83050a99e706fe5ce"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/PHPOffice/PHPPresentation/zipball/1fcc84072ee0061f8991b8f83050a99e706fe5ce",
+                "reference": "1fcc84072ee0061f8991b8f83050a99e706fe5ce",
+                "shasum": ""
+            },
+            "require": {
+                "ext-xml": "*",
+                "ext-zip": "*",
+                "php": ">=5.3.0",
+                "phpoffice/common": "0.2.*"
+            },
+            "require-dev": {
+                "codeclimate/php-test-reporter": "dev-master",
+                "phpdocumentor/phpdocumentor": "2.*",
+                "phploc/phploc": "2.*",
+                "phpmd/phpmd": "2.*",
+                "phpunit/phpunit": "3.7.*",
+                "sebastian/phpcpd": "2.*",
+                "squizlabs/php_codesniffer": "2.*",
+                "twig/twig": "1.27"
+            },
+            "suggest": {
+                "ext-gd2": "Required to add images"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "PhpOffice\\PhpPresentation\\": "src/PhpPresentation/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "LGPL"
+            ],
+            "authors": [
+                {
+                    "name": "Mark Baker"
+                },
+                {
+                    "name": "Ivan Lanin",
+                    "homepage": "http://ivan.lanin.org"
+                },
+                {
+                    "name": "Franck Lefevre",
+                    "homepage": "http://rootslabs.net"
+                }
+            ],
+            "description": "PHPPresentation - Read, Create and Write Presentations documents in PHP",
+            "homepage": "http://phpoffice.github.io",
+            "keywords": [
+                "LibreOffice",
+                "PowerPoint",
+                "odp",
+                "php",
+                "ppt",
+                "pptx",
+                "presentations"
+            ],
+            "time": "2017-07-05 12:13:02"
         }
     ],
     "packages-dev": [],
     "aliases": [],
     "minimum-stability": "stable",
-    "stability-flags": [],
+    "stability-flags": {
+        "phpoffice/phppresentation": 20
+    },
     "prefer-stable": false,
     "prefer-lowest": false,
     "platform": [],

+ 19 - 60
controllers/performance.php

@@ -444,66 +444,10 @@ if($submit) {
 			]
 		]);
 		$chart->setDatalabelSuffixes(" Test");
-
-		// Generate table
-		ob_start();
-		?>
-
-		<table class="table table-sm small table-bordered mt-3">
-			<thead>
-				<tr>
-					<th></th>
-					<th>Stage 1</th>
-					<th>Stage 2</th>
-					<th>Stage 3</th>
-					<th>Stage 4</th>
-					<th>Stage 5</th>
-				</tr>
-			</thead>
-			<tbody>
-			<?php
-			foreach(["value", "percent"] as $field) {
-
-				switch($field) {
-					case "value": $label = "Test Count"; break;
-					case "percent": $label = "Percentage"; break;
-				}
-
-				?>
-				<tr>
-					<td><?=htmlspecialchars($label)?></td>
-					<?php
-					foreach(range(1, 5) as $stage) {
-
-						$key = "stage{$stage}Count";
-						$count = $total_data->{$key};
-
-						switch($field) {
-							case "value":
-								$string = $count;
-								break;
-							case "percent":
-								$string = (floor(($count * 10000) / $test_count) / 100) . "%";
-								break;
-						}
-
-						?>
-						<td class="text-right"><?=$string?></td>
-						<?php
-					}
-					?>
-				</tr>
-				<?php
-
-			}
-			?>
-			</tbody>
-		</table>
-
-		<?php
-		$html = ob_get_clean();
-
-		$chart->setAfterHtml($html);
+		$chart->setPrintSuffixes("");
+		$chart->print = true;
+		$chart->printPercentages = true;
+		$chart->setPrintLabels(["Stage 1", "Stage 2", "Stage 3", "Stage 4", "Stage 5"]);
 
 		$charts[] = $chart;
 
@@ -827,6 +771,21 @@ if($submit) {
 		Core::addAlert("danger", $e->getMessage());
 	}
 
+
+	// Export hook
+	if(!empty($action)) {
+		switch($action) {
+			case "export-pptx":
+			case "export-odp":
+
+				require "performance_presentation.php";
+
+				break;
+			default:
+				break;
+		}
+	}
+
 }
 
 

+ 183 - 0
controllers/performance_presentation.php

@@ -0,0 +1,183 @@
+<?php
+
+use PhpOffice\PhpPresentation\PhpPresentation;
+use PhpOffice\PhpPresentation\IOFactory;
+use PhpOffice\PhpPresentation\Style\Color;
+use PhpOffice\PhpPresentation\Style\Alignment;
+use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
+
+$presentation = new PhpPresentation(); // 960x720
+
+// Draw title slide
+{
+	// Create slide
+	$slide = $presentation->getActiveSlide();
+
+	// Create a shape (drawing)
+	$shape = $slide->createDrawingShape();
+	$shape->setName("Check Policy logo")
+		  ->setDescription("Check Policy logo")
+		  ->setPath("./public/img/logo.png")
+		  ->setHeight(40)
+		  ->setOffsetX(10)
+		  ->setOffsetY(10);
+
+	// Title
+	$shape = $slide->createRichTextShape()
+		  ->setHeight(120)
+		  ->setWidth(940)
+		  ->setOffsetX(10)
+		  ->setOffsetY(315);
+	$shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER );
+	$run = $shape->createTextRun($customer->name . " " . date("F Y") . " Tests");
+	$run->getFont()->setBold(true)
+					   ->setSize(45);
+}
+
+$names = [];
+
+$charts_b64 = http::value("charts_b64");
+foreach($charts_b64 as $index => $b64) {
+
+	$chart = $charts[$index];
+
+	$labels = $chart->getLabels();
+	$datasets = $chart->getDatasets();
+
+	$column_count = 1 + count($labels);
+
+	$slide = $presentation->createSlide();
+
+	list($ignore, $png_b64) = explode("base64,", $b64);
+
+	$name = tempnam("/tmp", "ppt");
+	$handle = fopen($name, "w");
+	fwrite($handle, base64_decode($png_b64));
+
+	fclose($handle);
+
+	$chart_offset_y = 100;
+	if($chart->print) {
+		$chart_offset_y = 10;
+	}
+
+
+	$shape = $slide->createDrawingShape();
+	$shape->setName("Chart")
+		->setDescription("Chart")
+		->setPath($name)
+		->setHeight(520)
+		->setOffsetX(133)
+		->setOffsetY($chart_offset_y);
+
+
+	if($chart->print) {
+		// Table
+
+		$shape = $slide->createTableShape($column_count)
+			->setWidth(900)
+			->setOffsetX(30)
+			->setOffsetY(540);
+		// Create header row
+		$row = $shape->createRow();
+		$row->setHeight(20);
+		$cell = $row->nextCell();
+
+		$labels = $chart->getPrintLabels() ?? $chart->getLabels();
+
+		foreach($labels as $label) {
+			$cell = $row->nextCell();
+			$cell->getActiveParagraph()->getAlignment()
+				->setMarginBottom(4)
+				->setMarginLeft(4)
+				->setMarginRight(4)
+				->setMarginTop(4);
+			$cell->createTextRun($label)
+				->getFont()->setBold(true);
+		}
+
+		$suffixes = $chart->getPrintSuffixes() ?? $chart->getDatalabelSuffixes();
+
+		// Datasets
+		foreach($datasets as $index => $dataset) {
+			$label = $dataset->label;
+			$data = $dataset->data;
+
+			$suffix = $suffixes[$index] ?? "";
+
+			$row = $shape->createRow();
+			$row->setHeight(20);
+			$cell = $row->nextCell();
+			$cell->getActiveParagraph()->getAlignment()
+				->setMarginBottom(4)
+				->setMarginLeft(4)
+				->setMarginRight(4)
+				->setMarginTop(4);
+			$cell->createTextRun($label)
+				->getFont()->setBold(true);
+			foreach($data as $value) {
+				$cell = $row->nextCell();
+				$cell->getActiveParagraph()->getAlignment()
+					->setHorizontal(Alignment::HORIZONTAL_RIGHT)
+					->setMarginBottom(4)
+					->setMarginLeft(4)
+					->setMarginRight(4)
+					->setMarginTop(4);
+				$cell->createTextRun($value . $suffix);
+			}
+
+			if($chart->printPercentages) {
+				$total = array_sum($data);
+
+				$percentages = [];
+				foreach($data as $value) {
+					$percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
+				}
+
+				$row = $shape->createRow();
+				$row->setHeight(20);
+				$cell = $row->nextCell();
+				$cell->getActiveParagraph()->getAlignment()
+					->setMarginBottom(4)
+					->setMarginLeft(4)
+					->setMarginRight(4)
+					->setMarginTop(4);
+				$cell->createTextRun("Percentage")
+					->getFont()->setBold(true);
+				foreach($percentages as $string) {
+					$cell = $row->nextCell();
+					$cell->getActiveParagraph()->getAlignment()
+						->setHorizontal(Alignment::HORIZONTAL_RIGHT)
+						->setMarginBottom(4)
+						->setMarginLeft(4)
+						->setMarginRight(4)
+						->setMarginTop(4);
+					$cell->createTextRun($string);
+				}
+			}
+
+		}
+	}
+
+	$names[] = $name;
+}
+
+if($action == "export-pptx") {
+	header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation");
+	$pptx_writer = IOFactory::createWriter($presentation, "PowerPoint2007");
+	$pptx_writer->save("php://output");
+}
+
+if($action == "export-odp") {
+	header("Content-Type: application/vnd.oasis.opendocument.presentation");
+	$odp_writer = IOFactory::createWriter($presentation, "ODPresentation");
+	$odp_writer->save("php://output");
+}
+
+foreach($names as $name) {
+	@unlink($name);
+}
+
+exit;
+
+?>

+ 64 - 4
engine/chart.php

@@ -7,6 +7,7 @@ class Chart {
 	private $type = "bar";
 	private $datasets = [];
 	private $labels = [];
+	private $printLabels = null;
 
 	private $xAxes = [];
 	private $yAxes = [];
@@ -14,9 +15,12 @@ class Chart {
 	private $datalabelSuffixes = [];
 	private $datalabelFormatterCallback = false;
 
+	private $printSuffixes = null;
+
 	public $beforeHtml = null;
 	public $afterHtml = null;
 	public $print = false;
+	public $printPercentages = false;
 
 	public function __construct() {
 		$this->id = bin2hex(random_bytes(8));
@@ -112,10 +116,30 @@ class Chart {
 
 	}
 
+	public function getDatasets() {
+		return $this->datasets;
+	}
+
+	public function getLabels() {
+		return $this->labels;
+	}
+
 	public function setLabels($labels) {
 		$this->labels = $labels;
 	}
 
+	public function getPrintLabels() {
+		return $this->printLabels;
+	}
+
+	public function setPrintLabels($labels) {
+		$this->printLabels = $labels;
+	}
+
+	public function getDatalabelSuffixes() {
+		return $this->datalabelSuffixes;
+	}
+
 	public function setDatalabelSuffixes(... $suffixes) {
 		$this->datalabelSuffixes = $suffixes;
 	}
@@ -124,6 +148,14 @@ class Chart {
 		$this->datalabelFormatterCallback = $callback;
 	}
 
+	public function getPrintSuffixes() {
+		return $this->printSuffixes;
+	}
+
+	public function setPrintSuffixes(... $suffixes) {
+		$this->printSuffixes = $suffixes;
+	}
+
 	public function getJSON() {
 
 		$object = (object) [
@@ -183,7 +215,10 @@ class Chart {
 				<tr>
 					<th></th>
 					<?php
-					foreach($this->labels as $label) {
+
+					$labels = $this->printLabels ?? $this->labels;
+
+					foreach($labels as $label) {
 						?>
 						<th><?=htmlspecialchars($label)?></th>
 						<?php
@@ -196,15 +231,20 @@ class Chart {
 			foreach($this->datasets as $index => $dataset) {
 
 				$suffix = "";
-				if($this->datalabelSuffixes) {
-					$suffix = $this->datalabelSuffixes[$index];
+
+				$suffixes = $this->printSuffixes ?? $this->datalabelSuffixes;
+
+				if($suffixes) {
+					$suffix = $suffixes[$index];
 				}
 
+				$data = $dataset->data;
+
 				?>
 				<tr>
 					<td><?=htmlspecialchars($dataset->label)?></td>
 					<?php
-					foreach($dataset->data as $value) {
+					foreach($data as $value) {
 						?>
 						<td class="text-right"><?=htmlspecialchars($value)?><?=htmlspecialchars($suffix)?></td>
 						<?php
@@ -212,6 +252,26 @@ class Chart {
 					?>
 				</tr>
 				<?php
+
+				if($this->printPercentages) {
+					$total = array_sum($data);
+					$percentages = [];
+					foreach($data as $value) {
+						$percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
+					}
+					?>
+					<tr>
+						<td>Percentage</td>
+						<?php
+						foreach($percentages as $string) {
+							?>
+							<td class="text-right"><?=$string?></td>
+							<?php
+						}
+						?>
+					</tr>
+					<?php
+				}
 			}
 			?>
 			</tbody>

+ 46 - 6
public/js/app-performance.js

@@ -42,6 +42,16 @@ $(function() {
 		}
 	});
 
+	$("#export-pptx-action").click(function() {
+		exportCharts("pptx");
+		return true;
+	});
+
+	$("#export-odp-action").click(function() {
+		exportCharts("odp");
+		return true;
+	});
+
 });
 
 function testsByStageFormatter(value, context) {
@@ -62,9 +72,39 @@ function testsByStageFormatter(value, context) {
 	return label;
 }
 
-window.onbeforeprint = function beforePrintHandler() {
-  for (var id in Chart.instances) {
-    Chart.instances[id].resize();
-	console.log(Chart.instances[id]);
-  }
-};
+function exportCharts(action) {
+
+	let formId = "";
+
+	if(action == "pptx") {
+		formId = "form-pptx";
+	}
+
+	if(action == "odp") {
+		formId = "form-odp";
+	}
+
+	let form = $("#" + formId);
+	let rendered = $(form).data("rendered");
+
+	console.log(formId, form);
+
+	if(rendered == "0") {
+
+		for(var i in Chart.instances) {
+			let chart = Chart.instances[i];
+			let base64 = chart.toBase64Image();
+
+			$(form).append("<input type=\"hidden\" name=\"charts_b64[]\" value=\"" + base64 + "\" />");
+
+		}
+
+		$(form).data("rendered", "1");
+
+	}
+
+	console.log("done");
+
+	$(form).find("input[type=submit]").click();
+
+}

+ 1 - 0
routes.php

@@ -121,6 +121,7 @@ $routes = [
 
 	// Performance
 	"/^account\/performance$/" => ["controller" => "performance", "require_user_level" => User::LEVEL_VENUE],
+	"/^account\/performance\/(export-odp|export-pptx)$/" => ["controller" => "performance", "params" => ["action"], "require_user_level" => User::LEVEL_VENUE],
 
 
 ];

+ 36 - 2
views/performance.php

@@ -18,10 +18,24 @@ $date_range_custom_style = ($date_range == "custom") ? "" : "display: none";
 ?>
 <div class="container">
 	<div class="row d-print-none">
-		<div class="col-auto">
+		<div class="col">
 			<h2><?=htmlspecialchars($customer->name)?> Performance</h2>
 		</div>
-		<div class="col">
+		<div class="col-auto">
+			<?php
+			if($charts) {
+				?>
+				<div class="dropdown">
+					<button type="button" class="btn btn-secondary dropdown-toggle" id="actions-button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
+						Actions
+					</button>
+					<div class="dropdown-menu" aria-labelledby="actions-button">
+						<a class="dropdown-item" href="#" id="export-pptx-action">Export PPTX</a>
+					</div>
+				</div>
+				<?php
+			}
+			?>
 		</div>
 	</div>
 
@@ -289,6 +303,26 @@ $date_range_custom_style = ($date_range == "custom") ? "" : "display: none";
 		</div>
 
 		<?php
+		foreach(["pptx", "odp"] as $format) {
+			?>
+			<form id="form-<?=$format?>" method="post" action="/account/performance/export-<?=$format?>" data-rendered="0">
+				<input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
+				<input type="hidden" name="customer_id" value="<?=intval($customer->id)?>" />
+				<input type="hidden" name="date_range" value="<?=htmlspecialchars($date_range)?>" />
+				<input type="hidden" name="from_quarter" value="<?=htmlspecialchars($from_quarter)?>" />
+				<input type="hidden" name="to_quarter" value="<?=htmlspecialchars($to_quarter)?>" />
+				<input type="hidden" name="date_resolution" value="<?=htmlspecialchars($date_resolution)?>" />
+				<?php
+				foreach($project_ids as $project_id) {
+					?>
+					<input type="hidden" name="project_ids[]" value="<?=intval($project_id)?>" />
+					<?php
+				}
+				?>
+				<input type="submit" value="Submit" class="d-none" />
+			</form>
+			<?php
+		}
 
 		if(PRODUCTION == false) {
 			?>