Html.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\ReportRenderer; |
| 10 | |
| 11 | use Piwik\Piwik; |
| 12 | use Piwik\ReportRenderer; |
| 13 | use Piwik\View; |
| 14 | /** |
| 15 | * HTML report renderer |
| 16 | */ |
| 17 | class Html extends ReportRenderer |
| 18 | { |
| 19 | public const IMAGE_GRAPH_WIDTH = 700; |
| 20 | public const IMAGE_GRAPH_HEIGHT = 200; |
| 21 | public const HTML_CONTENT_TYPE = 'text/html'; |
| 22 | public const HTML_FILE_EXTENSION = 'html'; |
| 23 | public const UNSUBSCRIBE_LINK_PLACEHOLDER = '__unsubscribeLink__'; |
| 24 | protected $renderImageInline = \false; |
| 25 | private $rendering = ""; |
| 26 | public function setLocale($locale) |
| 27 | { |
| 28 | //Nothing to do |
| 29 | } |
| 30 | /** |
| 31 | * Currently only used for HTML reports. |
| 32 | * When sent by mail, images are attached to the mail: renderImageInline = false |
| 33 | * When downloaded, images are included base64 encoded in the report body: renderImageInline = true |
| 34 | * |
| 35 | * @param boolean $renderImageInline |
| 36 | */ |
| 37 | public function setRenderImageInline($renderImageInline) |
| 38 | { |
| 39 | $this->renderImageInline = $renderImageInline; |
| 40 | } |
| 41 | public function sendToDisk($filename) |
| 42 | { |
| 43 | $this->epilogue(); |
| 44 | return ReportRenderer::writeFile($filename, self::HTML_FILE_EXTENSION, $this->rendering); |
| 45 | } |
| 46 | public function sendToBrowserDownload($filename) |
| 47 | { |
| 48 | $this->epilogue(); |
| 49 | ReportRenderer::sendToBrowser($filename, self::HTML_FILE_EXTENSION, self::HTML_CONTENT_TYPE, $this->rendering); |
| 50 | } |
| 51 | public function sendToBrowserInline($filename) |
| 52 | { |
| 53 | $this->epilogue(); |
| 54 | ReportRenderer::inlineToBrowser(self::HTML_CONTENT_TYPE, $this->rendering); |
| 55 | } |
| 56 | public function getRenderedReport() |
| 57 | { |
| 58 | $this->epilogue(); |
| 59 | return $this->rendering; |
| 60 | } |
| 61 | private function epilogue() |
| 62 | { |
| 63 | // the unsubscribe link is specific to the email address the report is sent to, so we can't generate it here. |
| 64 | // instead we use a placeholder value, and replace it with the correct value in ScheduledReports::sendReport(). |
| 65 | $view = new View\HtmlEmailFooterView(self::UNSUBSCRIBE_LINK_PLACEHOLDER); |
| 66 | $this->rendering .= $view->render(); |
| 67 | } |
| 68 | public function renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata, $segment) |
| 69 | { |
| 70 | $frontPageView = new View\HtmlReportEmailHeaderView($reportTitle, $prettyDate, $description, $reportMetadata, $segment, $this->idSite, $this->report['period']); |
| 71 | $this->rendering .= $frontPageView->render(); |
| 72 | } |
| 73 | public function renderReport($processedReport) |
| 74 | { |
| 75 | $reportView = new View('@CoreHome/ReportRenderer/_htmlReportBody'); |
| 76 | View\HtmlReportEmailHeaderView::assignCommonParameters($reportView); |
| 77 | $reportMetadata = $processedReport['metadata']; |
| 78 | $reportData = $processedReport['reportData']; |
| 79 | $columns = $processedReport['columns']; |
| 80 | list($reportData, $columns) = self::processTableFormat($reportMetadata, $reportData, $columns); |
| 81 | $reportView->assign("reportName", $reportMetadata['name']); |
| 82 | $reportView->assign("reportId", $reportMetadata['uniqueId']); |
| 83 | $reportView->assign("reportColumns", $columns); |
| 84 | $reportView->assign("reportRows", $reportData->getRows()); |
| 85 | $reportView->assign("reportRowsMetadata", $processedReport['reportMetadata']->getRows()); |
| 86 | $reportView->assign("displayTable", $processedReport['displayTable']); |
| 87 | $displayGraph = $processedReport['displayGraph']; |
| 88 | $evolutionGraph = $processedReport['evolutionGraph']; |
| 89 | $reportView->assign("displayGraph", $displayGraph); |
| 90 | if ($displayGraph) { |
| 91 | $reportView->assign("graphWidth", self::IMAGE_GRAPH_WIDTH); |
| 92 | $reportView->assign("graphHeight", self::IMAGE_GRAPH_HEIGHT); |
| 93 | $reportView->assign("renderImageInline", $this->renderImageInline); |
| 94 | if ($this->renderImageInline) { |
| 95 | $staticGraph = parent::getStaticGraph($reportMetadata, self::IMAGE_GRAPH_WIDTH, self::IMAGE_GRAPH_HEIGHT, $evolutionGraph, $processedReport['segment']); |
| 96 | $reportView->assign("generatedImageGraph", base64_encode($staticGraph)); |
| 97 | unset($generatedImageGraph); |
| 98 | } |
| 99 | } |
| 100 | $this->rendering .= $reportView->render(); |
| 101 | } |
| 102 | public function getAttachments($report, $processedReports, $prettyDate) |
| 103 | { |
| 104 | $additionalFiles = array(); |
| 105 | foreach ($processedReports as $processedReport) { |
| 106 | if ($processedReport['displayGraph']) { |
| 107 | $additionalFiles[] = $this->getAttachment($report, $processedReport, $prettyDate); |
| 108 | } |
| 109 | } |
| 110 | return $additionalFiles; |
| 111 | } |
| 112 | protected function getAttachment($report, $processedReport, $prettyDate) |
| 113 | { |
| 114 | $additionalFile = array(); |
| 115 | $segment = \Piwik\Plugins\ScheduledReports\API::getSegment($report['idsegment']); |
| 116 | $segmentName = $segment != null ? sprintf(' (%s)', $segment['name']) : ''; |
| 117 | $processedReportMetadata = $processedReport['metadata']; |
| 118 | $additionalFile['filename'] = sprintf('%s - %s - %d - %s %d%s.png', $processedReportMetadata['name'], $prettyDate, $report['idsite'], Piwik::translate('General_Report'), $report['idreport'], $segmentName); |
| 119 | $additionalFile['cid'] = $processedReportMetadata['uniqueId']; |
| 120 | $additionalFile['content'] = ReportRenderer::getStaticGraph($processedReportMetadata, \Piwik\ReportRenderer\Html::IMAGE_GRAPH_WIDTH, \Piwik\ReportRenderer\Html::IMAGE_GRAPH_HEIGHT, $processedReport['evolutionGraph'], $segment); |
| 121 | $additionalFile['mimeType'] = 'image/png'; |
| 122 | return $additionalFile; |
| 123 | } |
| 124 | } |
| 125 |