| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?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")
- ->setWidth(200)
- ->setOffsetX(380)
- ->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 = array_merge($chart->getDatasets(), $chart->getPrintDatasets());
- $column_count = 1 + count($labels);
- $slide = $presentation->createSlide();
- list($ignore, $png_b64) = explode("base64,", $b64);
- $name = tempnam("/tmp", "ppt");
- $name .= ".png";
- $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
- if($chart->printTotalColumn) ++$column_count;
- $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();
- if($chart->printTotalColumn) $labels[] = "Total";
- 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;
- $print_data = array_slice($data, 0);
- $total = array_sum($data);
- $suffix = $suffixes[$index] ?? "";
- if($chart->printTotalColumn) {
- $print_data[] = ($suffix == "%") ? "" : number_format(array_sum($data)) . $suffix;
- }
- $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($print_data as $value) {
- $string = strlen($value) == 0 ? "" : $value . $suffix;
- $cell = $row->nextCell();
- $cell->getActiveParagraph()->getAlignment()
- ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
- ->setMarginBottom(4)
- ->setMarginLeft(4)
- ->setMarginRight(4)
- ->setMarginTop(4);
- $cell->createTextRun($string);
- }
- if($chart->printPercentages) {
- $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;
- }
- // Region/Area Manager Stats
- foreach([0, 1] as $mode) {
- switch($mode) {
- case 0:
- $current_data = $region_data;
- $mode_text = "Region";
- break;
- case 1:
- $current_data = $area_manager_data;
- $mode_text = "Area Manager";
- break;
- }
- foreach($current_data as $data_id => $rows) {
- $slide = $presentation->createSlide();
- // Title
- $shape = $slide->createRichTextShape()
- ->setHeight(120)->setWidth(940)
- ->setOffsetX(30)->setOffsetY(30);
- $run = $shape->createTextRun("$mode_text: $data_id");
- $run->getFont()->setSize(36);
- // Table
- $shape = $slide->createTableShape(5)
- ->setWidth(900)
- ->setOffsetX(30)
- ->setOffsetY(110);
- // Create header row
- $row = $shape->createRow();
- $row->setHeight(20);
- foreach(["Period", "Tests", "Passes", "Fails", "Rate"] as $label) {
- $cell = $row->nextCell();
- $cell->getActiveParagraph()->getAlignment()
- ->setMarginBottom(4)
- ->setMarginLeft(4)
- ->setMarginRight(4)
- ->setMarginTop(4);
- $cell->createTextRun($label)
- ->getFont()->setBold(true);
- }
- $index = 0;
- foreach($rows as $key => $data) {
- $rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
- $label = $key_labels[$index];
- $values = [$data->testCount, $data->passCount, $data->failCount, "$rate%"];
- $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($values as $value) {
- $cell = $row->nextCell();
- $cell->getActiveParagraph()->getAlignment()
- ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
- ->setMarginBottom(4)
- ->setMarginLeft(4)
- ->setMarginRight(4)
- ->setMarginTop(4);
- $cell->createTextRun($value);
- }
- $index++;
- }
- }
- }
- 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;
- ?>
|