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