File size: 3,302 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

namespace Mautic\ReportBundle\Model;

use Mautic\CoreBundle\Twig\Helper\FormatterHelper;
use Mautic\ReportBundle\Crate\ReportDataResult;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Symfony\Contracts\Translation\TranslatorInterface;

class ExcelExporter
{
    public function __construct(
        protected FormatterHelper $formatterHelper,
        private TranslatorInterface $translator
    ) {
    }

    /**
     * @param string $name
     *
     * @throws \Exception
     */
    public function export(ReportDataResult $reportDataResult, $name, string $output = 'php://output'): void
    {
        if (!class_exists(Spreadsheet::class)) {
            throw new \Exception('PHPSpreadsheet is required to export to Excel spreadsheets');
        }

        try {
            $objPHPExcel = new Spreadsheet();
            $objPHPExcel->getProperties()->setTitle($name);
            $objPHPExcel->createSheet();
            $objPHPExcelSheet = $objPHPExcel->getActiveSheet();
            $reportData       = $reportDataResult->getData();
            $rowCount         = 1;

            if (empty($reportData)) {
                throw new \Exception('No report data to be exported');
            }

            $headersRow = $reportDataResult->getHeaders();
            $this->putHeader($headersRow, $objPHPExcelSheet);

            // build the data rows
            foreach ($reportData as $count=>$data) {
                $row = [];
                foreach ($data as $k => $v) {
                    $type      = $reportDataResult->getType($k);
                    $formatted = htmlspecialchars_decode($this->formatterHelper->_($v, $type, true), ENT_QUOTES);
                    $row[]     = $formatted;
                }

                // write the row
                $rowCount = $count + 2;
                $objPHPExcel->getActiveSheet()->fromArray($row, null, "A{$rowCount}");
                // free memory
                unset($row, $reportData['data'][$count]);
            }

            // Add totals to export
            $totalsRow = $reportDataResult->getTotalsToExport($this->formatterHelper);
            if (!empty($totalsRow)) {
                $this->putTotals($totalsRow, $objPHPExcelSheet, 'A'.++$rowCount);
            }

            $objWriter = IOFactory::createWriter($objPHPExcel, 'Xlsx');
            $objWriter->setPreCalculateFormulas(false);

            $objWriter->save($output);
        } catch (Exception $e) {
            throw new \Exception('PHPSpreadsheet Error', 0, $e);
        }
    }

    /**
     * @param array<string> $headers
     */
    public function putHeader(array $headers, Worksheet $activeSheet): void
    {
        $activeSheet->fromArray($headers);
    }

    /**
     * @param array<string> $totals
     */
    public function putTotals(array $totals, Worksheet $activeSheet, string $startCell): void
    {
        // Put label if the first item is empty
        $key = array_key_first($totals);

        if (empty($totals[$key])) {
            $totals[$key] = $this->translator->trans('mautic.report.report.groupby.totals');
        }

        $activeSheet->fromArray($totals, null, $startCell);
    }
}