Commit | Line | Data |
---|---|---|
03e04002 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
03e04002 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
03e04002 | 7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
03e04002 | 27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
03e04002 | 32 | */ |
33 | ||
34 | /** | |
35 | * Base class for Export Formats | |
36 | * Create a subclass for a specific format. | |
37 | * @see http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation | |
38 | */ | |
9b05de29 | 39 | abstract class CRM_Financial_BAO_ExportFormat { |
03e04002 | 40 | |
d424ffde | 41 | /** |
fe482240 | 42 | * data which the individual export formats will output in the desired format. |
d424ffde | 43 | * @var array |
03e04002 | 44 | */ |
45 | protected $_exportParams; | |
46 | ||
d424ffde | 47 | /** |
fe482240 | 48 | * smarty template. |
d424ffde | 49 | * @var CRM_Core_Smarty |
03e04002 | 50 | */ |
51 | static protected $_template; | |
52 | ||
555f69ce PN |
53 | /** |
54 | * Download Exported file. | |
d51c6add | 55 | * @var bool |
555f69ce PN |
56 | */ |
57 | public $_isDownloadFile; | |
58 | ||
03e04002 | 59 | /** |
fe482240 | 60 | * Class constructor. |
03e04002 | 61 | */ |
00be9182 | 62 | public function __construct() { |
481a74f4 | 63 | if (!isset(self::$_template)) { |
03e04002 | 64 | self::$_template = CRM_Core_Smarty::singleton(); |
65 | } | |
66 | } | |
67 | ||
e0ef6999 | 68 | /** |
fe482240 | 69 | * Override to assemble the appropriate subset of financial data for the specific export format. |
100fef9d | 70 | * @param array $exportParams |
e0ef6999 EM |
71 | * |
72 | * @return mixed | |
73 | */ | |
00be9182 | 74 | public function export($exportParams) { |
03e04002 | 75 | $this->_exportParams = $exportParams; |
76 | return $exportParams; | |
77 | } | |
78 | ||
e0ef6999 | 79 | /** |
b6a80401 | 80 | * Exports sbatches in $this->_batchIds, and saves to file. |
2f7f541b | 81 | * |
b6a80401 | 82 | * @param string $fileName - use this file name (if applicable) |
e0ef6999 | 83 | */ |
00be9182 | 84 | public function output($fileName = NULL) { |
b6a80401 JV |
85 | // Default behaviour, override if needed: |
86 | self::createActivityExport($this->_batchIds, $fileName); | |
03e04002 | 87 | } |
88 | ||
9b05de29 JV |
89 | /** |
90 | * Abstract function that generates exports, and downloads them as zip file. | |
91 | * | |
92 | * @param $exportDaos array with DAO's for queries to be exported. | |
93 | */ | |
7b966967 | 94 | abstract public function makeExport($exportDaos); |
9b05de29 | 95 | |
e0ef6999 EM |
96 | /** |
97 | * @return string | |
98 | */ | |
00be9182 | 99 | public function getMimeType() { |
03e04002 | 100 | return 'text/plain'; |
101 | } | |
102 | ||
e0ef6999 | 103 | /** |
57bd5cda JV |
104 | * Returns some kind of identification for your export format. |
105 | * | |
106 | * This does not really has to be a file extension, you can name your | |
107 | * file as you wish as you override output. | |
108 | * | |
e0ef6999 EM |
109 | * @return string |
110 | */ | |
7b966967 | 111 | abstract public function getFileExtension(); |
03e04002 | 112 | |
e0ef6999 EM |
113 | /** |
114 | * @return object | |
115 | */ | |
00be9182 | 116 | public static function &getTemplate() { |
03e04002 | 117 | return self::$_template; |
118 | } | |
119 | ||
e0ef6999 EM |
120 | /** |
121 | * @param $var | |
122 | * @param null $value | |
123 | */ | |
00be9182 | 124 | public function assign($var, $value = NULL) { |
03e04002 | 125 | self::$_template->assign($var, $value); |
126 | } | |
127 | ||
128 | /* | |
129 | * This gets called for every item of data being compiled before being sent to the exporter for output. | |
130 | * | |
131 | * Depending on the output format might want to override this, e.g. for IIF tabs need to be escaped etc, | |
132 | * but for CSV it doesn't make sense because php has built in csv output functions. | |
133 | */ | |
7b966967 | 134 | |
e0ef6999 EM |
135 | /** |
136 | * @param $s | |
137 | * @param string $type | |
138 | * | |
139 | * @return null | |
140 | */ | |
00be9182 | 141 | public static function format($s, $type = 'string') { |
03e04002 | 142 | if (!empty($s)) { |
143 | return $s; | |
144 | } | |
145 | else { | |
146 | return NULL; | |
147 | } | |
148 | } | |
149 | ||
00be9182 | 150 | public function initiateDownload() { |
f3d529fc PN |
151 | if (!$this->_isDownloadFile) { |
152 | return NULL; | |
153 | } | |
03e04002 | 154 | $config = CRM_Core_Config::singleton(); |
cded2ebf | 155 | // zip files if more than one. |
045f52a3 | 156 | if (count($this->_downloadFile) > 1) { |
f4114c3e | 157 | $zip = $config->customFileUploadDir . 'Financial_Transactions_' . date('YmdHis') . '.zip'; |
03e04002 | 158 | $result = $this->createZip($this->_downloadFile, $zip, TRUE); |
159 | if ($result) { | |
d42a224c CW |
160 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/zip'); |
161 | CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename=' . CRM_Utils_File::cleanFileName(basename($zip))); | |
162 | CRM_Utils_System::setHttpHeader('Content-Length', '' . filesize($zip)); | |
03e04002 | 163 | ob_clean(); |
164 | flush(); | |
f4114c3e | 165 | readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($zip))); |
7b966967 SL |
166 | //delete the zip to avoid clutter. |
167 | unlink($zip); | |
03e04002 | 168 | CRM_Utils_System::civiExit(); |
169 | } | |
170 | } | |
171 | else { | |
d42a224c CW |
172 | CRM_Utils_System::setHttpHeader('Content-Type', 'text/plain'); |
173 | CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename=' . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0]))); | |
174 | CRM_Utils_System::setHttpHeader('Content-Length', '' . filesize($this->_downloadFile[0])); | |
03e04002 | 175 | ob_clean(); |
176 | flush(); | |
f4114c3e | 177 | readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0]))); |
03e04002 | 178 | CRM_Utils_System::civiExit(); |
179 | } | |
180 | } | |
181 | ||
e0ef6999 EM |
182 | /** |
183 | * @param $batchIds | |
100fef9d | 184 | * @param string $fileName |
e0ef6999 EM |
185 | * |
186 | * @throws CRM_Core_Exception | |
187 | */ | |
00be9182 | 188 | public static function createActivityExport($batchIds, $fileName) { |
03e04002 | 189 | $session = CRM_Core_Session::singleton(); |
be2fb01f CW |
190 | $values = []; |
191 | $params = ['id' => $batchIds]; | |
03e04002 | 192 | CRM_Batch_BAO_Batch::retrieve($params, $values); |
193 | $createdBy = CRM_Contact_BAO_Contact::displayName($values['created_id']); | |
194 | $modifiedBy = CRM_Contact_BAO_Contact::displayName($values['modified_id']); | |
195 | ||
196 | $values['payment_instrument_id'] = ''; | |
197 | if (isset($values['payment_instrument_id'])) { | |
198 | $paymentInstrument = array_flip(CRM_Contribute_PseudoConstant::paymentInstrument('label')); | |
199 | $values['payment_instrument_id'] = array_search($values['payment_instrument_id'], $paymentInstrument); | |
200 | } | |
536f0e02 | 201 | $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>'; |
03e04002 | 202 | $subject = ''; |
a7488080 | 203 | if (!empty($values['total'])) { |
86bfa4f6 | 204 | $subject .= ts('Total') . '[' . CRM_Utils_Money::format($values['total']) . '],'; |
03e04002 | 205 | } |
a7488080 | 206 | if (!empty($values['item_count'])) { |
86bfa4f6 | 207 | $subject .= ' ' . ts('Count') . '[' . $values['item_count'] . '],'; |
03e04002 | 208 | } |
209 | ||
cded2ebf | 210 | // create activity. |
86bfa4f6 | 211 | $subject .= ' ' . ts('Batch') . '[' . $values['title'] . ']'; |
03e04002 | 212 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); |
be2fb01f | 213 | $activityParams = [ |
03e04002 | 214 | 'activity_type_id' => array_search('Export Accounting Batch', $activityTypes), |
215 | 'subject' => $subject, | |
216 | 'status_id' => 2, | |
217 | 'activity_date_time' => date('YmdHis'), | |
218 | 'source_contact_id' => $session->get('userID'), | |
219 | 'source_record_id' => $values['id'], | |
220 | 'target_contact_id' => $session->get('userID'), | |
221 | 'details' => $details, | |
be2fb01f | 222 | 'attachFile_1' => [ |
03e04002 | 223 | 'uri' => $fileName, |
224 | 'type' => 'text/csv', | |
225 | 'location' => $fileName, | |
226 | 'upload_date' => date('YmdHis'), | |
be2fb01f CW |
227 | ], |
228 | ]; | |
03e04002 | 229 | |
230 | CRM_Activity_BAO_Activity::create($activityParams); | |
231 | } | |
232 | ||
e0ef6999 EM |
233 | /** |
234 | * @param array $files | |
235 | * @param null $destination | |
236 | * @param bool $overwrite | |
237 | * | |
238 | * @return bool | |
239 | */ | |
be2fb01f | 240 | public function createZip($files = [], $destination = NULL, $overwrite = FALSE) { |
cded2ebf | 241 | // if the zip file already exists and overwrite is false, return false |
03e04002 | 242 | if (file_exists($destination) && !$overwrite) { |
243 | return FALSE; | |
244 | } | |
be2fb01f | 245 | $valid_files = []; |
03e04002 | 246 | if (is_array($files)) { |
247 | foreach ($files as $file) { | |
cded2ebf | 248 | // make sure the file exists |
03e04002 | 249 | if (file_exists($file)) { |
250 | $validFiles[] = $file; | |
251 | } | |
252 | } | |
253 | } | |
254 | if (count($validFiles)) { | |
255 | $zip = new ZipArchive(); | |
cc2bd612 | 256 | if ($zip->open($destination, $overwrite ? ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== TRUE) { |
03e04002 | 257 | return FALSE; |
258 | } | |
259 | foreach ($validFiles as $file) { | |
260 | $zip->addFile($file, CRM_Utils_File::cleanFileName(basename($file))); | |
261 | } | |
262 | $zip->close(); | |
263 | return file_exists($destination); | |
264 | } | |
265 | else { | |
045f52a3 TO |
266 | return FALSE; |
267 | } | |
03e04002 | 268 | } |
96025800 | 269 | |
232624b1 | 270 | } |