Skip to content

Commit

Permalink
Adds a "report output handler", so that we can start untangling the P…
Browse files Browse the repository at this point in the history
…DF, CSV

and email code, and allow extensions to implement their own methods (ex: Excel
Export).
  • Loading branch information
mlutfy authored and Symbiotic Gitlab CI committed Apr 22, 2020
1 parent 2ec49fb commit 102c055
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 20 deletions.
39 changes: 19 additions & 20 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ class CRM_Report_Form extends CRM_Core_Form {
*/
protected $_outputMode;

/**
* Output handler
*
* @var \CRM_Report_Output_Interface
*/
protected $_outputHandler;

/**
* Format of any chart in use.
*
Expand Down Expand Up @@ -1435,8 +1442,7 @@ public function addToDeveloperTab($sql) {
if (!CRM_Core_Permission::check('view report sql')) {
return;
}
$ignored_output_modes = ['pdf', 'csv', 'print'];
if (in_array($this->_outputMode, $ignored_output_modes)) {
if ($this->_outputHandler && !$this->_outputHandler->supportsSqlDeveloperTab()) {
return;
}
$this->tabs['Developer'] = [
Expand Down Expand Up @@ -3403,6 +3409,10 @@ public function endPostProcess(&$rows = NULL) {
$this->_resultSet = $rows;
}

if ($this->_outputHandler) {
$this->_outputHandler->generateOutput($rows);
}

if ($this->_outputMode == 'print' ||
$this->_outputMode == 'pdf' ||
$this->_sendmail
Expand Down Expand Up @@ -3471,26 +3481,8 @@ public function endPostProcess(&$rows = NULL) {
elseif ($this->_outputMode == 'print') {
echo $content;
}
else {
// Nb. Once upon a time we used a package called Open Flash Charts to
// draw charts, and we had a feature whereby a browser could send the
// server a PNG version of the chart, which could then be included in a
// PDF by including <img> tags in the HTML for the conversion below.
//
// This feature stopped working when browsers stopped supporting Flash,
// and although we have a different client-side charting library in
// place, we decided not to reimplement the (rather convoluted)
// browser-sending-rendered-chart-to-server process.
//
// If this feature is required in future we should find a better way to
// render charts on the server side, e.g. server-created SVG.
CRM_Utils_PDF_Utils::html2pdf($content, "CiviReport.pdf", FALSE, ['orientation' => 'landscape']);
}
CRM_Utils_System::civiExit();
}
elseif ($this->_outputMode == 'csv') {
CRM_Report_Utils_Report::export2csv($this, $rows);
}
elseif ($this->_outputMode == 'group') {
$group = $this->_params['groups'];
$this->add2group($group);
Expand Down Expand Up @@ -5084,6 +5076,13 @@ protected function setOutputMode() {
if (isset($this->_params['task'])) {
unset($this->_params['task']);
}

if (!empty($this->_outputMode) && empty($this->_outputHandler)) {
$className = 'CRM_Report_Output_' . ucfirst($this->_outputMode);
if (class_exists($className)) {
$this->_outputHandler = new $className($this);
}
}
}

/**
Expand Down
44 changes: 44 additions & 0 deletions CRM/Report/Output/Csv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

class CRM_Report_Output_Csv implements CRM_Report_Output_Interface {

/**
* Reference of the report instance.
*/
protected $report;

/**
* @inheritDoc
*/
public function __construct(&$report) {
$report->printOnly = TRUE;
$report->absoluteUrl = TRUE;
$report->setAddPaging(FALSE);

$this->report = $report;
}

/**
* @inheritDoc
*/
public function supportsSqlDeveloperTab() {
return FALSE;
}

/**
* @inheritDoc
*/
public function generateOutput(&$rows = NULL) {
CRM_Report_Utils_Report::export2csv($this->report, $rows);
}

}
45 changes: 45 additions & 0 deletions CRM/Report/Output/Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*
* CRM_Report_Output_Interface implements helper functions to determine what to
* do with the report data, such as displaying on the screen, printing, pdf,
* csv, etc. Extensions can also provide their own implementations, such as the
* civiexportexcel extension.
*/
interface CRM_Report_Output_Interface {

/**
* Class constructor.
*
* @param CRM_Report_Form
*/
public function __construct(&$report);

/**
* Checks if the output method supports the SQL developer tab.
*
* @return bool
*/
public function supportsSqlDeveloperTab();

/**
* Generate the output.
*
* @param array
*/
public function generateOutput(&$rows = NULL);

}
62 changes: 62 additions & 0 deletions CRM/Report/Output/Pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

class CRM_Report_Output_Pdf implements CRM_Report_Output_Interface {

/**
* Reference of the report instance.
*/
protected $report;

/**
* @inheritDoc
*/
public function __construct(&$report) {
$report->printOnly = TRUE;
$report->absoluteUrl = TRUE;
$report->setAddPaging(FALSE);

$this->report = $report;
}

/**
* @inheritDoc
*/
public function supportsSqlDeveloperTab() {
return FALSE;
}

/**
* @inheritDoc
*/
public function generateOutput(&$rows = NULL) {
$content = $this->report->compileContent();
$id = $this->report->getID();
$url = CRM_Utils_System::url("civicrm/report/instance/{$id}", "reset=1", TRUE);

// Nb. Once upon a time we used a package called Open Flash Charts to
// draw charts, and we had a feature whereby a browser could send the
// server a PNG version of the chart, which could then be included in a
// PDF by including <img> tags in the HTML for the conversion below.
//
// This feature stopped working when browsers stopped supporting Flash,
// and although we have a different client-side charting library in
// place, we decided not to reimplement the (rather convoluted)
// browser-sending-rendered-chart-to-server process.
//
// If this feature is required in future we should find a better way to
// render charts on the server side, e.g. server-created SVG.
CRM_Utils_PDF_Utils::html2pdf($content, "CiviReport.pdf", FALSE, ['orientation' => 'landscape']);

CRM_Utils_System::civiExit();
}

}

0 comments on commit 102c055

Please sign in to comment.