Commit | Line | Data |
---|---|---|
03e04002 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
03e04002 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
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. | |
55 | * @var boolean | |
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 | */ | |
94 | public abstract function makeExport($exportDaos); | |
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 | */ | |
57bd5cda | 111 | public abstract 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 | */ | |
e0ef6999 EM |
134 | /** |
135 | * @param $s | |
136 | * @param string $type | |
137 | * | |
138 | * @return null | |
139 | */ | |
00be9182 | 140 | public static function format($s, $type = 'string') { |
03e04002 | 141 | if (!empty($s)) { |
142 | return $s; | |
143 | } | |
144 | else { | |
145 | return NULL; | |
146 | } | |
147 | } | |
148 | ||
00be9182 | 149 | public function initiateDownload() { |
f3d529fc PN |
150 | if (!$this->_isDownloadFile) { |
151 | return NULL; | |
152 | } | |
03e04002 | 153 | $config = CRM_Core_Config::singleton(); |
cded2ebf | 154 | // zip files if more than one. |
045f52a3 | 155 | if (count($this->_downloadFile) > 1) { |
f4114c3e | 156 | $zip = $config->customFileUploadDir . 'Financial_Transactions_' . date('YmdHis') . '.zip'; |
03e04002 | 157 | $result = $this->createZip($this->_downloadFile, $zip, TRUE); |
158 | if ($result) { | |
d42a224c CW |
159 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/zip'); |
160 | CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename=' . CRM_Utils_File::cleanFileName(basename($zip))); | |
161 | CRM_Utils_System::setHttpHeader('Content-Length', '' . filesize($zip)); | |
03e04002 | 162 | ob_clean(); |
163 | flush(); | |
f4114c3e | 164 | readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($zip))); |
03e04002 | 165 | unlink($zip); //delete the zip to avoid clutter. |
166 | CRM_Utils_System::civiExit(); | |
167 | } | |
168 | } | |
169 | else { | |
d42a224c CW |
170 | CRM_Utils_System::setHttpHeader('Content-Type', 'text/plain'); |
171 | CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename=' . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0]))); | |
172 | CRM_Utils_System::setHttpHeader('Content-Length', '' . filesize($this->_downloadFile[0])); | |
03e04002 | 173 | ob_clean(); |
174 | flush(); | |
f4114c3e | 175 | readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0]))); |
03e04002 | 176 | CRM_Utils_System::civiExit(); |
177 | } | |
178 | } | |
179 | ||
e0ef6999 EM |
180 | /** |
181 | * @param $batchIds | |
100fef9d | 182 | * @param string $fileName |
e0ef6999 EM |
183 | * |
184 | * @throws CRM_Core_Exception | |
185 | */ | |
00be9182 | 186 | public static function createActivityExport($batchIds, $fileName) { |
03e04002 | 187 | $session = CRM_Core_Session::singleton(); |
188 | $values = array(); | |
189 | $params = array('id' => $batchIds); | |
190 | CRM_Batch_BAO_Batch::retrieve($params, $values); | |
191 | $createdBy = CRM_Contact_BAO_Contact::displayName($values['created_id']); | |
192 | $modifiedBy = CRM_Contact_BAO_Contact::displayName($values['modified_id']); | |
193 | ||
194 | $values['payment_instrument_id'] = ''; | |
195 | if (isset($values['payment_instrument_id'])) { | |
196 | $paymentInstrument = array_flip(CRM_Contribute_PseudoConstant::paymentInstrument('label')); | |
197 | $values['payment_instrument_id'] = array_search($values['payment_instrument_id'], $paymentInstrument); | |
198 | } | |
536f0e02 | 199 | $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 | 200 | $subject = ''; |
a7488080 | 201 | if (!empty($values['total'])) { |
86bfa4f6 | 202 | $subject .= ts('Total') . '[' . CRM_Utils_Money::format($values['total']) . '],'; |
03e04002 | 203 | } |
a7488080 | 204 | if (!empty($values['item_count'])) { |
86bfa4f6 | 205 | $subject .= ' ' . ts('Count') . '[' . $values['item_count'] . '],'; |
03e04002 | 206 | } |
207 | ||
cded2ebf | 208 | // create activity. |
86bfa4f6 | 209 | $subject .= ' ' . ts('Batch') . '[' . $values['title'] . ']'; |
03e04002 | 210 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); |
211 | $activityParams = array( | |
212 | 'activity_type_id' => array_search('Export Accounting Batch', $activityTypes), | |
213 | 'subject' => $subject, | |
214 | 'status_id' => 2, | |
215 | 'activity_date_time' => date('YmdHis'), | |
216 | 'source_contact_id' => $session->get('userID'), | |
217 | 'source_record_id' => $values['id'], | |
218 | 'target_contact_id' => $session->get('userID'), | |
219 | 'details' => $details, | |
045f52a3 | 220 | 'attachFile_1' => array( |
03e04002 | 221 | 'uri' => $fileName, |
222 | 'type' => 'text/csv', | |
223 | 'location' => $fileName, | |
224 | 'upload_date' => date('YmdHis'), | |
225 | ), | |
226 | ); | |
227 | ||
228 | CRM_Activity_BAO_Activity::create($activityParams); | |
229 | } | |
230 | ||
e0ef6999 EM |
231 | /** |
232 | * @param array $files | |
233 | * @param null $destination | |
234 | * @param bool $overwrite | |
235 | * | |
236 | * @return bool | |
237 | */ | |
00be9182 | 238 | public function createZip($files = array(), $destination = NULL, $overwrite = FALSE) { |
cded2ebf | 239 | // if the zip file already exists and overwrite is false, return false |
03e04002 | 240 | if (file_exists($destination) && !$overwrite) { |
241 | return FALSE; | |
242 | } | |
243 | $valid_files = array(); | |
244 | if (is_array($files)) { | |
245 | foreach ($files as $file) { | |
cded2ebf | 246 | // make sure the file exists |
03e04002 | 247 | if (file_exists($file)) { |
248 | $validFiles[] = $file; | |
249 | } | |
250 | } | |
251 | } | |
252 | if (count($validFiles)) { | |
253 | $zip = new ZipArchive(); | |
045f52a3 | 254 | if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== TRUE) { |
03e04002 | 255 | return FALSE; |
256 | } | |
257 | foreach ($validFiles as $file) { | |
258 | $zip->addFile($file, CRM_Utils_File::cleanFileName(basename($file))); | |
259 | } | |
260 | $zip->close(); | |
261 | return file_exists($destination); | |
262 | } | |
263 | else { | |
045f52a3 TO |
264 | return FALSE; |
265 | } | |
03e04002 | 266 | } |
96025800 | 267 | |
232624b1 | 268 | } |