1
0

reports_presentation.php 7.2 KB

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