Merge pull request #15826 from seamuslee001/dev_core_183_dedupe
[civicrm-core.git] / CRM / Financial / BAO / ExportFormat.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
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
22 */
23 abstract class CRM_Financial_BAO_ExportFormat {
24
25 /**
26 * data which the individual export formats will output in the desired format.
27 * @var array
28 */
29 protected $_exportParams;
30
31 /**
32 * smarty template.
33 * @var CRM_Core_Smarty
34 */
35 static protected $_template;
36
37 /**
38 * Download Exported file.
39 * @var bool
40 */
41 public $_isDownloadFile;
42
43 /**
44 * Class constructor.
45 */
46 public function __construct() {
47 if (!isset(self::$_template)) {
48 self::$_template = CRM_Core_Smarty::singleton();
49 }
50 }
51
52 /**
53 * Override to assemble the appropriate subset of financial data for the specific export format.
54 * @param array $exportParams
55 *
56 * @return mixed
57 */
58 public function export($exportParams) {
59 $this->_exportParams = $exportParams;
60 return $exportParams;
61 }
62
63 /**
64 * Exports sbatches in $this->_batchIds, and saves to file.
65 *
66 * @param string $fileName - use this file name (if applicable)
67 */
68 public function output($fileName = NULL) {
69 // Default behaviour, override if needed:
70 self::createActivityExport($this->_batchIds, $fileName);
71 }
72
73 /**
74 * Abstract function that generates exports, and downloads them as zip file.
75 *
76 * @param $exportDaos array with DAO's for queries to be exported.
77 */
78 abstract public function makeExport($exportDaos);
79
80 /**
81 * @return string
82 */
83 public function getMimeType() {
84 return 'text/plain';
85 }
86
87 /**
88 * Returns some kind of identification for your export format.
89 *
90 * This does not really has to be a file extension, you can name your
91 * file as you wish as you override output.
92 *
93 * @return string
94 */
95 abstract public function getFileExtension();
96
97 /**
98 * @return object
99 */
100 public static function &getTemplate() {
101 return self::$_template;
102 }
103
104 /**
105 * @param $var
106 * @param null $value
107 */
108 public function assign($var, $value = NULL) {
109 self::$_template->assign($var, $value);
110 }
111
112 /*
113 * This gets called for every item of data being compiled before being sent to the exporter for output.
114 *
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.
117 */
118
119 /**
120 * @param $s
121 * @param string $type
122 *
123 * @return null
124 */
125 public static function format($s, $type = 'string') {
126 if (!empty($s)) {
127 return $s;
128 }
129 else {
130 return NULL;
131 }
132 }
133
134 public function initiateDownload() {
135 if (!$this->_isDownloadFile) {
136 return NULL;
137 }
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);
143 if ($result) {
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));
147 ob_clean();
148 flush();
149 readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($zip)));
150 //delete the zip to avoid clutter.
151 unlink($zip);
152 CRM_Utils_System::civiExit();
153 }
154 }
155 else {
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]));
159 ob_clean();
160 flush();
161 readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0])));
162 CRM_Utils_System::civiExit();
163 }
164 }
165
166 /**
167 * @param $batchIds
168 * @param string $fileName
169 *
170 * @throws CRM_Core_Exception
171 */
172 public static function createActivityExport($batchIds, $fileName) {
173 $session = CRM_Core_Session::singleton();
174 $values = [];
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']);
179
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);
184 }
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>';
186 $subject = '';
187 if (!empty($values['total'])) {
188 $subject .= ts('Total') . '[' . CRM_Utils_Money::format($values['total']) . '],';
189 }
190 if (!empty($values['item_count'])) {
191 $subject .= ' ' . ts('Count') . '[' . $values['item_count'] . '],';
192 }
193
194 // create activity.
195 $subject .= ' ' . ts('Batch') . '[' . $values['title'] . ']';
196 $activityParams = [
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,
205 'attachFile_1' => [
206 'uri' => $fileName,
207 'type' => 'text/csv',
208 'location' => $fileName,
209 'upload_date' => date('YmdHis'),
210 ],
211 ];
212 civicrm_api3('Activity', 'create', $activityParams);
213 }
214
215 /**
216 * @param array $files
217 * @param null $destination
218 * @param bool $overwrite
219 *
220 * @return bool
221 */
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) {
225 return FALSE;
226 }
227 $valid_files = [];
228 if (is_array($files)) {
229 foreach ($files as $file) {
230 // make sure the file exists
231 if (file_exists($file)) {
232 $validFiles[] = $file;
233 }
234 }
235 }
236 if (count($validFiles)) {
237 $zip = new ZipArchive();
238 if ($zip->open($destination, $overwrite ? ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== TRUE) {
239 return FALSE;
240 }
241 foreach ($validFiles as $file) {
242 $zip->addFile($file, CRM_Utils_File::cleanFileName(basename($file)));
243 }
244 $zip->close();
245 return file_exists($destination);
246 }
247 else {
248 return FALSE;
249 }
250 }
251
252 }