3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * Base class for Export Formats
38 * Create a subclass for a specific format.
39 * @see http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation
42 class CRM_Financial_BAO_ExportFormat
{
45 * Array of data which the individual export formats will output in the desired format
47 protected $_exportParams;
52 static protected $_template;
57 function __construct() {
58 if ( !isset( self
::$_template ) ) {
59 self
::$_template = CRM_Core_Smarty
::singleton();
63 // Override to assemble the appropriate subset of financial data for the specific export format
64 function export($exportParams) {
65 $this->_exportParams
= $exportParams;
69 function output($fileName = NULL) {
70 switch ($this->getFileExtension()) {
72 self
::createActivityExport($this->_batchIds
, $fileName);
76 $tplFile = $this->getHookedTemplateFileName();
77 $out = self
::getTemplate()->fetch($tplFile);
78 $fileName = $this->putFile($out);
79 self
::createActivityExport($this->_batchIds
, $fileName);
84 function getMimeType() {
88 function getFileExtension() {
92 // Override this if appropriate
93 function getTemplateFileName() {
97 static function &getTemplate() {
98 return self
::$_template;
101 function assign($var, $value = NULL) {
102 self
::$_template->assign($var, $value);
106 * This gets called for every item of data being compiled before being sent to the exporter for output.
108 * Depending on the output format might want to override this, e.g. for IIF tabs need to be escaped etc,
109 * but for CSV it doesn't make sense because php has built in csv output functions.
111 static function format($s, $type = 'string') {
120 function initiateDownload() {
121 $config = CRM_Core_Config
::singleton();
122 //zip files if more than one.
123 if (count($this->_downloadFile
)>1) {
124 $zip = $config->customFileUploadDir
. 'Financial_Transactions_' . date('YmdHis') . '.zip';
125 $result = $this->createZip($this->_downloadFile
, $zip, TRUE);
127 header('Content-Type: application/zip');
128 header('Content-Disposition: attachment; filename=' . CRM_Utils_File
::cleanFileName(basename($zip)));
129 header('Content-Length: ' . filesize($zip));
132 readfile($config->customFileUploadDir
. CRM_Utils_File
::cleanFileName(basename($zip)));
133 unlink($zip); //delete the zip to avoid clutter.
134 CRM_Utils_System
::civiExit();
138 header('Content-Type: text/plain');
139 header('Content-Disposition: attachment; filename=' . CRM_Utils_File
::cleanFileName(basename($this->_downloadFile
[0])));
140 header('Content-Length: ' . filesize($this->_downloadFile
[0]));
143 readfile($config->customFileUploadDir
. CRM_Utils_File
::cleanFileName(basename($this->_downloadFile
[0])));
144 CRM_Utils_System
::civiExit();
148 static function createActivityExport($batchIds, $fileName) {
149 $session = CRM_Core_Session
::singleton();
151 $params = array('id' => $batchIds);
152 CRM_Batch_BAO_Batch
::retrieve($params, $values);
153 $createdBy = CRM_Contact_BAO_Contact
::displayName($values['created_id']);
154 $modifiedBy = CRM_Contact_BAO_Contact
::displayName($values['modified_id']);
156 $values['payment_instrument_id'] = '';
157 if (isset($values['payment_instrument_id'])) {
158 $paymentInstrument = array_flip(CRM_Contribute_PseudoConstant
::paymentInstrument('label'));
159 $values['payment_instrument_id'] = array_search($values['payment_instrument_id'], $paymentInstrument);
161 $details = '<p>' . ts('Record: ') . $values['title'] . '</p><p>' . ts('Description: ') . '</p><p>' . ts('Created By: ') . $createdBy . '</p><p>' . ts('Created Date: ') . $values['created_date'] . '</p><p>' . ts('Last Modified By: ') . $modifiedBy . '</p><p>' . ts('Payment Instrument: ') . $values['payment_instrument_id'] . '</p>';
163 if (!empty($values['total'])) {
164 $subject .= ts('Total') . '['. CRM_Utils_Money
::format($values['total']) .'],';
166 if (!empty($values['item_count'])) {
167 $subject .= ' ' . ts('Count') . '['. $values['item_count'] .'],';
171 $subject .= ' ' . ts('Batch') . '['. $values['title'] .']';
172 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, FALSE, FALSE, 'name');
173 $activityParams = array(
174 'activity_type_id' => array_search('Export Accounting Batch', $activityTypes),
175 'subject' => $subject,
177 'activity_date_time' => date('YmdHis'),
178 'source_contact_id' => $session->get('userID'),
179 'source_record_id' => $values['id'],
180 'target_contact_id' => $session->get('userID'),
181 'details' => $details,
182 'attachFile_1' => array (
184 'type' => 'text/csv',
185 'location' => $fileName,
186 'upload_date' => date('YmdHis'),
190 CRM_Activity_BAO_Activity
::create($activityParams);
193 function createZip($files = array(), $destination = NULL, $overwrite = FALSE) {
194 //if the zip file already exists and overwrite is false, return false
195 if (file_exists($destination) && !$overwrite) {
198 $valid_files = array();
199 if (is_array($files)) {
200 foreach ($files as $file) {
201 //make sure the file exists
202 if (file_exists($file)) {
203 $validFiles[] = $file;
207 if (count($validFiles)) {
208 $zip = new ZipArchive();
209 if ($zip->open($destination,$overwrite ? ZIPARCHIVE
::OVERWRITE
: ZIPARCHIVE
::CREATE
) !== TRUE) {
212 foreach ($validFiles as $file) {
213 $zip->addFile($file, CRM_Utils_File
::cleanFileName(basename($file)));
216 return file_exists($destination);