PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/ReportRenderer.php

https://github.com/CodeYellowBV/piwik
PHP | 269 lines | 246 code | 5 blank | 18 comment | 0 complexity | db410d033e4c94ff4de434fdfd299681 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. use Exception;
  11. use Piwik\API\Request;
  12. use Piwik\DataTable\Row;
  13. use Piwik\DataTable\Simple;
  14. use Piwik\DataTable;
  15. use Piwik\Plugins\ImageGraph\API;
  16. /**
  17. * A Report Renderer produces user friendly renderings of any given Piwik report.
  18. * All new Renderers must be copied in ReportRenderer and added to the $availableReportRenderers.
  19. */
  20. abstract class ReportRenderer
  21. {
  22. const DEFAULT_REPORT_FONT = 'dejavusans';
  23. const REPORT_TEXT_COLOR = "68,68,68";
  24. const REPORT_TITLE_TEXT_COLOR = "126,115,99";
  25. const TABLE_HEADER_BG_COLOR = "228,226,215";
  26. const TABLE_HEADER_TEXT_COLOR = "37,87,146";
  27. const TABLE_CELL_BORDER_COLOR = "231,231,231";
  28. const TABLE_BG_COLOR = "249,250,250";
  29. const HTML_FORMAT = 'html';
  30. const PDF_FORMAT = 'pdf';
  31. const CSV_FORMAT = 'csv';
  32. static private $availableReportRenderers = array(
  33. self::PDF_FORMAT,
  34. self::HTML_FORMAT,
  35. self::CSV_FORMAT,
  36. );
  37. /**
  38. * Return the ReportRenderer associated to the renderer type $rendererType
  39. *
  40. * @throws exception If the renderer is unknown
  41. * @param string $rendererType
  42. * @return \Piwik\ReportRenderer
  43. */
  44. static public function factory($rendererType)
  45. {
  46. $name = ucfirst(strtolower($rendererType));
  47. $className = 'Piwik\ReportRenderer\\' . $name;
  48. try {
  49. Loader::loadClass($className);
  50. return new $className;
  51. } catch (Exception $e) {
  52. @header('Content-Type: text/html; charset=utf-8');
  53. throw new Exception(
  54. Piwik::translate(
  55. 'General_ExceptionInvalidReportRendererFormat',
  56. array($name, implode(', ', self::$availableReportRenderers))
  57. )
  58. );
  59. }
  60. }
  61. /**
  62. * Initialize locale settings.
  63. * If not called, locale settings defaults to 'en'
  64. *
  65. * @param string $locale
  66. */
  67. abstract public function setLocale($locale);
  68. /**
  69. * Save rendering to disk
  70. *
  71. * @param string $filename without path & without format extension
  72. * @return string path of file
  73. */
  74. abstract public function sendToDisk($filename);
  75. /**
  76. * Send rendering to browser with a 'download file' prompt
  77. *
  78. * @param string $filename without path & without format extension
  79. */
  80. abstract public function sendToBrowserDownload($filename);
  81. /**
  82. * Output rendering to browser
  83. *
  84. * @param string $filename without path & without format extension
  85. */
  86. abstract public function sendToBrowserInline($filename);
  87. /**
  88. * Get rendered report
  89. */
  90. abstract public function getRenderedReport();
  91. /**
  92. * Generate the first page.
  93. *
  94. * @param string $reportTitle
  95. * @param string $prettyDate formatted date
  96. * @param string $description
  97. * @param array $reportMetadata metadata for all reports
  98. * @param array $segment segment applied to all reports
  99. */
  100. abstract public function renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata, $segment);
  101. /**
  102. * Render the provided report.
  103. * Multiple calls to this method before calling outputRendering appends each report content.
  104. *
  105. * @param array $processedReport @see API::getProcessedReport()
  106. */
  107. abstract public function renderReport($processedReport);
  108. /**
  109. * Get report attachments, ex. graph images
  110. *
  111. * @param $report
  112. * @param $processedReports
  113. * @param $prettyDate
  114. * @return array
  115. */
  116. abstract public function getAttachments($report, $processedReports, $prettyDate);
  117. /**
  118. * Append $extension to $filename
  119. *
  120. * @static
  121. * @param string $filename
  122. * @param string $extension
  123. * @return string filename with extension
  124. */
  125. protected static function appendExtension($filename, $extension)
  126. {
  127. return $filename . "." . $extension;
  128. }
  129. /**
  130. * Return $filename with temp directory and delete file
  131. *
  132. * @static
  133. * @param $filename
  134. * @return string path of file in temp directory
  135. */
  136. protected static function getOutputPath($filename)
  137. {
  138. $outputFilename = PIWIK_USER_PATH . '/tmp/assets/' . $filename;
  139. $outputFilename = SettingsPiwik::rewriteTmpPathWithInstanceId($outputFilename);
  140. @chmod($outputFilename, 0600);
  141. @unlink($outputFilename);
  142. return $outputFilename;
  143. }
  144. protected static function writeFile($filename, $extension, $content)
  145. {
  146. $filename = self::appendExtension($filename, $extension);
  147. $outputFilename = self::getOutputPath($filename);
  148. $emailReport = @fopen($outputFilename, "w");
  149. if (!$emailReport) {
  150. throw new Exception ("The file : " . $outputFilename . " can not be opened in write mode.");
  151. }
  152. fwrite($emailReport, $content);
  153. fclose($emailReport);
  154. return $outputFilename;
  155. }
  156. protected static function sendToBrowser($filename, $extension, $contentType, $content)
  157. {
  158. $filename = ReportRenderer::appendExtension($filename, $extension);
  159. ProxyHttp::overrideCacheControlHeaders();
  160. header('Content-Description: File Transfer');
  161. header('Content-Type: ' . $contentType);
  162. header('Content-Disposition: attachment; filename="' . str_replace('"', '\'', basename($filename)) . '";');
  163. header('Content-Length: ' . strlen($content));
  164. echo $content;
  165. }
  166. protected static function inlineToBrowser($contentType, $content)
  167. {
  168. header('Content-Type: ' . $contentType);
  169. echo $content;
  170. }
  171. /**
  172. * Convert a dimension-less report to a multi-row two-column data table
  173. *
  174. * @static
  175. * @param $reportMetadata array
  176. * @param $report DataTable
  177. * @param $reportColumns array
  178. * @return array DataTable $report & array $columns
  179. */
  180. protected static function processTableFormat($reportMetadata, $report, $reportColumns)
  181. {
  182. $finalReport = $report;
  183. if (empty($reportMetadata['dimension'])) {
  184. $simpleReportMetrics = $report->getFirstRow();
  185. if ($simpleReportMetrics) {
  186. $finalReport = new Simple();
  187. foreach ($simpleReportMetrics->getColumns() as $metricId => $metric) {
  188. $newRow = new Row();
  189. $newRow->addColumn("label", $reportColumns[$metricId]);
  190. $newRow->addColumn("value", $metric);
  191. $finalReport->addRow($newRow);
  192. }
  193. }
  194. $reportColumns = array(
  195. 'label' => Piwik::translate('General_Name'),
  196. 'value' => Piwik::translate('General_Value'),
  197. );
  198. }
  199. return array(
  200. $finalReport,
  201. $reportColumns,
  202. );
  203. }
  204. public static function getStaticGraph($reportMetadata, $width, $height, $evolution, $segment)
  205. {
  206. $imageGraphUrl = $reportMetadata['imageGraphUrl'];
  207. if ($evolution && !empty($reportMetadata['imageGraphEvolutionUrl'])) {
  208. $imageGraphUrl = $reportMetadata['imageGraphEvolutionUrl'];
  209. }
  210. $requestGraph = $imageGraphUrl .
  211. '&outputType=' . API::GRAPH_OUTPUT_PHP .
  212. '&format=original&serialize=0' .
  213. '&filter_truncate=' .
  214. '&width=' . $width .
  215. '&height=' . $height .
  216. ($segment != null ? '&segment=' . urlencode($segment['definition']) : '');
  217. $request = new Request($requestGraph);
  218. try {
  219. $imageGraph = $request->process();
  220. // Get image data as string
  221. ob_start();
  222. imagepng($imageGraph);
  223. $imageGraphData = ob_get_contents();
  224. ob_end_clean();
  225. imagedestroy($imageGraph);
  226. return $imageGraphData;
  227. } catch (Exception $e) {
  228. throw new Exception("ImageGraph API returned an error: " . $e->getMessage() . "\n");
  229. }
  230. }
  231. }