3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Base class for Export Formats
20 * Create a subclass for a specific format.
21 * @see http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation
23 abstract class CRM_Financial_BAO_ExportFormat
{
26 * data which the individual export formats will output in the desired format.
29 protected $_exportParams;
33 * @var CRM_Core_Smarty
35 static protected $_template;
38 * Download Exported file.
41 public $_isDownloadFile;
46 public function __construct() {
47 if (!isset(self
::$_template)) {
48 self
::$_template = CRM_Core_Smarty
::singleton();
53 * Override to assemble the appropriate subset of financial data for the specific export format.
54 * @param array $exportParams
58 public function export($exportParams) {
59 $this->_exportParams
= $exportParams;
64 * Exports sbatches in $this->_batchIds, and saves to file.
66 * @param string $fileName - use this file name (if applicable)
68 public function output($fileName = NULL) {
69 // Default behaviour, override if needed:
70 self
::createActivityExport($this->_batchIds
, $fileName);
74 * Abstract function that generates exports, and downloads them as zip file.
76 * @param $exportDaos array with DAO's for queries to be exported.
78 abstract public function makeExport($exportDaos);
83 public function getMimeType() {
88 * Returns some kind of identification for your export format.
90 * This does not really has to be a file extension, you can name your
91 * file as you wish as you override output.
95 abstract public function getFileExtension();
100 public static function &getTemplate() {
101 return self
::$_template;
108 public function assign($var, $value = NULL) {
109 self
::$_template->assign($var, $value);
113 * This gets called for every item of data being compiled before being sent to the exporter for output.
115 * Depending on the output format might want to override this, e.g. for IIF tabs need to be escaped etc,
116 * but for CSV it doesn't make sense because php has built in csv output functions.
121 * @param string $type
125 public static function format($s, $type = 'string') {
134 public function initiateDownload() {
135 if (!$this->_isDownloadFile
) {
138 $config = CRM_Core_Config
::singleton();
139 // zip files if more than one.
140 if (count($this->_downloadFile
) > 1) {
141 $zip = $config->customFileUploadDir
. 'Financial_Transactions_' . date('YmdHis') . '.zip';
142 $result = $this->createZip($this->_downloadFile
, $zip, TRUE);
144 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/zip');
145 CRM_Utils_System
::setHttpHeader('Content-Disposition', 'attachment; filename=' . CRM_Utils_File
::cleanFileName(basename($zip)));
146 CRM_Utils_System
::setHttpHeader('Content-Length', '' . filesize($zip));
149 readfile($config->customFileUploadDir
. CRM_Utils_File
::cleanFileName(basename($zip)));
150 //delete the zip to avoid clutter.
152 CRM_Utils_System
::civiExit();
156 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/plain');
157 CRM_Utils_System
::setHttpHeader('Content-Disposition', 'attachment; filename=' . CRM_Utils_File
::cleanFileName(basename($this->_downloadFile
[0])));
158 CRM_Utils_System
::setHttpHeader('Content-Length', '' . filesize($this->_downloadFile
[0]));
161 readfile($config->customFileUploadDir
. CRM_Utils_File
::cleanFileName(basename($this->_downloadFile
[0])));
162 CRM_Utils_System
::civiExit();
168 * @param string $fileName
170 * @throws CRM_Core_Exception
172 public static function createActivityExport($batchIds, $fileName) {
173 $session = CRM_Core_Session
::singleton();
175 $params = ['id' => $batchIds];
176 CRM_Batch_BAO_Batch
::retrieve($params, $values);
177 $createdBy = CRM_Contact_BAO_Contact
::displayName($values['created_id']);
178 $modifiedBy = CRM_Contact_BAO_Contact
::displayName($values['modified_id']);
180 $values['payment_instrument_id'] = '';
181 if (isset($values['payment_instrument_id'])) {
182 $paymentInstrument = array_flip(CRM_Contribute_PseudoConstant
::paymentInstrument('label'));
183 $values['payment_instrument_id'] = array_search($values['payment_instrument_id'], $paymentInstrument);
185 $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 Method:') . ' ' . $values['payment_instrument_id'] . '</p>';
187 if (!empty($values['total'])) {
188 $subject .= ts('Total') . '[' . CRM_Utils_Money
::format($values['total']) . '],';
190 if (!empty($values['item_count'])) {
191 $subject .= ' ' . ts('Count') . '[' . $values['item_count'] . '],';
195 $subject .= ' ' . ts('Batch') . '[' . $values['title'] . ']';
197 'activity_type_id' => 'Export Accounting Batch',
198 'subject' => $subject,
199 'status_id' => 'Completed',
200 'activity_date_time' => date('YmdHis'),
201 'source_contact_id' => $session->get('userID'),
202 'source_record_id' => $values['id'],
203 'target_contact_id' => $session->get('userID'),
204 'details' => $details,
207 'type' => 'text/csv',
208 'location' => $fileName,
209 'upload_date' => date('YmdHis'),
212 civicrm_api3('Activity', 'create', $activityParams);
216 * @param array $files
217 * @param string $destination
218 * @param bool $overwrite
222 public function createZip($files = [], $destination = NULL, $overwrite = FALSE) {
223 // if the zip file already exists and overwrite is false, return false
224 if (file_exists($destination) && !$overwrite) {
228 if (is_array($files)) {
229 foreach ($files as $file) {
230 // make sure the file exists
231 if (file_exists($file)) {
232 $validFiles[] = $file;
236 if (count($validFiles)) {
237 $zip = new ZipArchive();
238 if ($zip->open($destination, $overwrite ? ZIPARCHIVE
::CREATE | ZIPARCHIVE
::OVERWRITE
: ZIPARCHIVE
::CREATE
) !== TRUE) {
241 foreach ($validFiles as $file) {
242 $zip->addFile($file, CRM_Utils_File
::cleanFileName(basename($file)));
245 return file_exists($destination);