reports_presentation.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. use PhpOffice\PhpPresentation\PhpPresentation;
  3. use PhpOffice\PhpPresentation\IOFactory;
  4. use PhpOffice\PhpPresentation\Style\Color;
  5. use PhpOffice\PhpPresentation\Style\Alignment;
  6. use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
  7. $presentation = new PhpPresentation(); // 960x720
  8. // Draw title slide
  9. {
  10. // Create slide
  11. $slide = $presentation->getActiveSlide();
  12. // Create a shape (drawing)
  13. $shape = $slide->createDrawingShape();
  14. $shape->setName("Check Policy logo")
  15. ->setDescription("Check Policy logo")
  16. ->setPath("./public/img/logo.png")
  17. ->setWidth(200)
  18. ->setOffsetX(380)
  19. ->setOffsetY(10);
  20. // Title
  21. $shape = $slide->createRichTextShape()
  22. ->setHeight(120)
  23. ->setWidth(940)
  24. ->setOffsetX(10)
  25. ->setOffsetY(315);
  26. $shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER );
  27. $run = $shape->createTextRun($customer->name . " " . date("F Y") . " Tests");
  28. $run->getFont()->setBold(true)
  29. ->setSize(45);
  30. }
  31. $names = [];
  32. $charts_b64 = http::value("charts_b64");
  33. foreach($charts_b64 as $index => $b64) {
  34. $chart = $charts[$index];
  35. $labels = $chart->getLabels();
  36. $datasets = array_merge($chart->getDatasets(), $chart->getPrintDatasets());
  37. $column_count = 1 + count($labels);
  38. $slide = $presentation->createSlide();
  39. list($ignore, $png_b64) = explode("base64,", $b64);
  40. $name = tempnam("/tmp", "ppt");
  41. $name .= ".png";
  42. $handle = fopen($name, "w");
  43. fwrite($handle, base64_decode($png_b64));
  44. fclose($handle);
  45. $chart_offset_y = 100;
  46. if($chart->print) {
  47. $chart_offset_y = 10;
  48. }
  49. $shape = $slide->createDrawingShape();
  50. $shape->setName("Chart")
  51. ->setDescription("Chart")
  52. ->setPath($name)
  53. ->setHeight(520)
  54. ->setOffsetX(133)
  55. ->setOffsetY($chart_offset_y);
  56. if($chart->print) {
  57. // Table
  58. if($chart->printTotalColumn) ++$column_count;
  59. $shape = $slide->createTableShape($column_count)
  60. ->setWidth(900)
  61. ->setOffsetX(30)
  62. ->setOffsetY(540);
  63. // Create header row
  64. $row = $shape->createRow();
  65. $row->setHeight(20);
  66. $cell = $row->nextCell();
  67. $labels = $chart->getPrintLabels() ?? $chart->getLabels();
  68. if($chart->printTotalColumn) $labels[] = "Total";
  69. foreach($labels as $label) {
  70. $cell = $row->nextCell();
  71. $cell->getActiveParagraph()->getAlignment()
  72. ->setMarginBottom(4)
  73. ->setMarginLeft(4)
  74. ->setMarginRight(4)
  75. ->setMarginTop(4);
  76. $cell->createTextRun($label)
  77. ->getFont()->setBold(true);
  78. }
  79. $suffixes = $chart->getPrintSuffixes() ?? $chart->getDatalabelSuffixes();
  80. // Datasets
  81. foreach($datasets as $index => $dataset) {
  82. $label = $dataset->label;
  83. $data = $dataset->data;
  84. $print_data = array_slice($data, 0);
  85. $total = array_sum($data);
  86. $suffix = $suffixes[$index] ?? "";
  87. if($chart->printTotalColumn) {
  88. $print_data[] = ($suffix == "%") ? "" : number_format(array_sum($data)) . $suffix;
  89. }
  90. $row = $shape->createRow();
  91. $row->setHeight(20);
  92. $cell = $row->nextCell();
  93. $cell->getActiveParagraph()->getAlignment()
  94. ->setMarginBottom(4)
  95. ->setMarginLeft(4)
  96. ->setMarginRight(4)
  97. ->setMarginTop(4);
  98. $cell->createTextRun($label)
  99. ->getFont()->setBold(true);
  100. foreach($print_data as $value) {
  101. $string = strlen($value) == 0 ? "" : $value . $suffix;
  102. $cell = $row->nextCell();
  103. $cell->getActiveParagraph()->getAlignment()
  104. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  105. ->setMarginBottom(4)
  106. ->setMarginLeft(4)
  107. ->setMarginRight(4)
  108. ->setMarginTop(4);
  109. $cell->createTextRun($string);
  110. }
  111. if($chart->printPercentages) {
  112. $percentages = [];
  113. foreach($data as $value) {
  114. $percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
  115. }
  116. $row = $shape->createRow();
  117. $row->setHeight(20);
  118. $cell = $row->nextCell();
  119. $cell->getActiveParagraph()->getAlignment()
  120. ->setMarginBottom(4)
  121. ->setMarginLeft(4)
  122. ->setMarginRight(4)
  123. ->setMarginTop(4);
  124. $cell->createTextRun("Percentage")
  125. ->getFont()->setBold(true);
  126. foreach($percentages as $string) {
  127. $cell = $row->nextCell();
  128. $cell->getActiveParagraph()->getAlignment()
  129. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  130. ->setMarginBottom(4)
  131. ->setMarginLeft(4)
  132. ->setMarginRight(4)
  133. ->setMarginTop(4);
  134. $cell->createTextRun($string);
  135. }
  136. }
  137. }
  138. }
  139. $names[] = $name;
  140. }
  141. // Region/Area Manager Stats
  142. foreach([0, 1] as $mode) {
  143. switch($mode) {
  144. case 0:
  145. $current_data = $region_data;
  146. $mode_text = "Region";
  147. break;
  148. case 1:
  149. $current_data = $area_manager_data;
  150. $mode_text = "Area Manager";
  151. break;
  152. }
  153. foreach($current_data as $data_id => $rows) {
  154. $slide = $presentation->createSlide();
  155. // Title
  156. $shape = $slide->createRichTextShape()
  157. ->setHeight(120)->setWidth(940)
  158. ->setOffsetX(30)->setOffsetY(30);
  159. $run = $shape->createTextRun("$mode_text: $data_id");
  160. $run->getFont()->setSize(36);
  161. // Table
  162. $shape = $slide->createTableShape(5)
  163. ->setWidth(900)
  164. ->setOffsetX(30)
  165. ->setOffsetY(110);
  166. // Create header row
  167. $row = $shape->createRow();
  168. $row->setHeight(20);
  169. foreach(["Period", "Tests", "Passes", "Fails", "Rate"] as $label) {
  170. $cell = $row->nextCell();
  171. $cell->getActiveParagraph()->getAlignment()
  172. ->setMarginBottom(4)
  173. ->setMarginLeft(4)
  174. ->setMarginRight(4)
  175. ->setMarginTop(4);
  176. $cell->createTextRun($label)
  177. ->getFont()->setBold(true);
  178. }
  179. $index = 0;
  180. foreach($rows as $key => $data) {
  181. $rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
  182. $label = $key_labels[$index];
  183. $values = [$data->testCount, $data->passCount, $data->failCount, "$rate%"];
  184. $row = $shape->createRow();
  185. $row->setHeight(20);
  186. $cell = $row->nextCell();
  187. $cell->getActiveParagraph()->getAlignment()
  188. ->setMarginBottom(4)
  189. ->setMarginLeft(4)
  190. ->setMarginRight(4)
  191. ->setMarginTop(4);
  192. $cell->createTextRun($label)
  193. ->getFont()->setBold(true);
  194. foreach($values as $value) {
  195. $cell = $row->nextCell();
  196. $cell->getActiveParagraph()->getAlignment()
  197. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  198. ->setMarginBottom(4)
  199. ->setMarginLeft(4)
  200. ->setMarginRight(4)
  201. ->setMarginTop(4);
  202. $cell->createTextRun($value);
  203. }
  204. $index++;
  205. }
  206. }
  207. }
  208. if($action == "export-pptx") {
  209. header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation");
  210. $pptx_writer = IOFactory::createWriter($presentation, "PowerPoint2007");
  211. $pptx_writer->save("php://output");
  212. }
  213. if($action == "export-odp") {
  214. header("Content-Type: application/vnd.oasis.opendocument.presentation");
  215. $odp_writer = IOFactory::createWriter($presentation, "ODPresentation");
  216. $odp_writer->save("php://output");
  217. }
  218. foreach($names as $name) {
  219. @unlink($name);
  220. }
  221. exit;
  222. ?>