Skip to content
Snippets Groups Projects
Commit 5a2c705c authored by bgm's avatar bgm
Browse files

Add support for email attachments.

parent 66b2098a
No related branches found
No related tags found
No related merge requests found
......@@ -2,9 +2,9 @@
/**
* @package civiexportexcel
* @copyright Mathieu Lutfy (c) 2014
* @copyright Mathieu Lutfy (c) 2014-2015
*/
class CRM_CiviExportExcel_Utils_Report {
class CRM_CiviExportExcel_Utils_Report extends CRM_Core_Page {
/**
* Generates a XLS 2007 file and forces the browser to download it.
......@@ -27,7 +27,7 @@ class CRM_CiviExportExcel_Utils_Report {
// always modified
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
echo self::makeCsv($form, $rows);
self::generateFile($form, $rows);
CRM_Utils_System::civiExit();
}
......@@ -35,11 +35,13 @@ class CRM_CiviExportExcel_Utils_Report {
* Utility function for export2csv and CRM_Report_Form::endPostProcess
* - make XLS file content and return as string.
*
* FIXME: return as string, not output directly.
* @param Object &$form CRM_Report_Form object.
* @param Array &$rows Resulting rows from the report.
* @param String Full path to the filename to write in (for mailing reports).
*
* See @CRM_Report_Utils_Report::makeCsv().
*/
static function makeCsv(&$form, &$rows) {
static function generateFile(&$form, &$rows, $filename = 'php://output') {
$config = CRM_Core_Config::singleton();
$csv = '';
......@@ -158,7 +160,7 @@ class CRM_CiviExportExcel_Utils_Report {
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
$objWriter->save($filename);
return ''; // FIXME
}
......
......@@ -8,14 +8,16 @@ easier to use).
This extension uses the PHPExcel library. See the "License" section below
for more information (LGPL v2).
For discussion, see: http://forum.civicrm.org/index.php/topic,32954.0.html
For discussion, see: http://forum.civicrm.org/index.php/topic,32954.0.html
See also the "Todo" section for a general roadmap.
To download the latest version of this module:
To download the latest version of this module:
https://github.com/mlutfy/ca.bidon.civiexportexcel
This extension was sponsored by: Projet Montréal <http://projetmontreal.org>
and Development and Peace <http://www.devp.org>.
This extension was sponsored by:
Projet Montréal <http://projetmontreal.org>
Development and Peace <https://www.devp.org>
Coop SymbioTIC <https://www.symbiotic.coop>
Requirements
============
......@@ -33,6 +35,15 @@ Install as any other regular CiviCRM extension:
2- Enable the extension from CiviCRM -> Administer -> System -> Extensions.
3- If you wish to send emails with the report as an Excel attachment,
you must apply the patch in civiexportexcel-core-mail.patch.
Report mails
============
To send report e-mails in Excel2007 format, use: "format=excel2007" in
the "Scheduled Jobs" settings.
Support
=======
......@@ -60,15 +71,14 @@ Or you can send me the equivalent of a beer: <https://www.bidon.ca/en/paypal>
Todo
====
* Support report e-mails (return the output of the xls, do not output directly as download).
* Propose a new hook to CiviCRM for a cleaner postProcess implementation.
* Propose a new hook to CiviCRM for a cleaner postProcess implementation (incl. mail).
* Add OpenDocument (LibreOffice) support.
* Add admin settings form to enable excel/opendocument formats?
License
=======
(C) 2014 Mathieu Lutfy <mathieu@bidon.ca>
(C) 2014-2015 Mathieu Lutfy <mathieu@bidon.ca>
Distributed under the terms of the GNU Affero General public license (AGPL).
See LICENSE.txt for details.
......
diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php
index 055e0d9..4c81bdc 100644
--- a/CRM/Report/Form.php
+++ b/CRM/Report/Form.php
@@ -2094,6 +2094,10 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
$this->_outputMode = 'csv';
$this->_absoluteUrl = TRUE;
}
+ elseif ($output == 'excel2007') {
+ $this->assign('outputMode', 'excel2007');
+ $this->_outputMode = 'excel2007';
+ }
elseif ($this->_groupButtonName == $buttonName || $output == 'group') {
$this->assign('outputMode', 'group');
$this->_outputMode = 'group';
@@ -2570,6 +2574,10 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
);
}
+ if ($this->_outputMode == 'excel2007') {
+ civiexportexcel_attach_to_email($this, $rows, $attachments);
+ }
+
if (CRM_Report_Utils_Report::mailReport($content, $this->_id,
$this->_outputMode, $attachments
)) {
......@@ -100,7 +100,6 @@ function civiexportexcel_civicrm_buildForm($formName, &$form) {
$form->assign('printOnly', TRUE);
$printOnly = TRUE;
$form->assign('outputMode', 'excel2007');
// $form->_absoluteUrl = TRUE;
// FIXME: this duplicates part of CRM_Report_Form::postProcess()
// since we do not have a place to hook into, we hi-jack the form process
......@@ -118,7 +117,8 @@ function civiexportexcel_civicrm_buildForm($formName, &$form) {
$form->buildRows($sql, $rows);
// format result set.
$form->formatDisplay($rows);
// This seems to cause more problems than it fixes.
// $form->formatDisplay($rows);
// assign variables to templates
$form->doTemplateAssignment($rows);
......@@ -155,3 +155,26 @@ function civiexportexcel_civicrm_export($exportTempTable, $headerRows, $sqlColum
CRM_CiviExportExcel_Utils_SearchExport::export2excel2007($headerRows, $sqlColumns, $rows);
}
/**
* Implements hook_civicrm_alterMailParams().
*
* Intercepts outgoing report emails, in order to attach the
* excel2007 version of the report.
*
* TODO: we should really propose a patch to CRM_Report_Form::endPostProcess().
*/
function civiexportexcel_attach_to_email(&$form, &$rows, &$attachments) {
$config = CRM_Core_Config::singleton();
$filename = 'CiviReport.xlsx';
$fullname = $config->templateCompileDir . CRM_Utils_File::makeFileName($filename);
CRM_CiviExportExcel_Utils_Report::generateFile($form, $rows, $fullname);
$attachments[] = array(
'fullPath' => $fullname,
'mime_type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'cleanName' => $filename,
);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment