Commit | Line | Data |
---|---|---|
03e04002 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
03e04002 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
03e04002 | 32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
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 | |
40 | */ | |
03e04002 | 41 | class CRM_Financial_BAO_ExportFormat { |
42 | ||
43 | /* | |
44 | * Array of data which the individual export formats will output in the desired format | |
45 | */ | |
46 | protected $_exportParams; | |
47 | ||
48 | /* | |
49 | * smarty template | |
50 | */ | |
51 | static protected $_template; | |
52 | ||
53 | /** | |
100fef9d | 54 | * Class constructor |
03e04002 | 55 | */ |
00be9182 | 56 | public function __construct() { |
481a74f4 | 57 | if (!isset(self::$_template)) { |
03e04002 | 58 | self::$_template = CRM_Core_Smarty::singleton(); |
59 | } | |
60 | } | |
61 | ||
e0ef6999 | 62 | /** |
4f1f1f2a | 63 | * Override to assemble the appropriate subset of financial data for the specific export format |
100fef9d | 64 | * @param array $exportParams |
e0ef6999 EM |
65 | * |
66 | * @return mixed | |
67 | */ | |
00be9182 | 68 | public function export($exportParams) { |
03e04002 | 69 | $this->_exportParams = $exportParams; |
70 | return $exportParams; | |
71 | } | |
72 | ||
e0ef6999 EM |
73 | /** |
74 | * @param null $fileName | |
75 | */ | |
00be9182 | 76 | public function output($fileName = NULL) { |
03e04002 | 77 | switch ($this->getFileExtension()) { |
78 | case 'csv': | |
79 | self::createActivityExport($this->_batchIds, $fileName); | |
045f52a3 | 80 | break; |
03e04002 | 81 | |
82 | case 'iif': | |
232624b1 | 83 | $tplFile = $this->getHookedTemplateFileName(); |
f4114c3e | 84 | $out = self::getTemplate()->fetch($tplFile); |
03e04002 | 85 | $fileName = $this->putFile($out); |
86 | self::createActivityExport($this->_batchIds, $fileName); | |
045f52a3 | 87 | break; |
03e04002 | 88 | } |
89 | } | |
90 | ||
e0ef6999 EM |
91 | /** |
92 | * @return string | |
93 | */ | |
00be9182 | 94 | public function getMimeType() { |
03e04002 | 95 | return 'text/plain'; |
96 | } | |
97 | ||
e0ef6999 EM |
98 | /** |
99 | * @return string | |
100 | */ | |
00be9182 | 101 | public function getFileExtension() { |
03e04002 | 102 | return 'txt'; |
103 | } | |
104 | ||
e0ef6999 | 105 | /** |
4f1f1f2a | 106 | * Override this if appropriate |
e0ef6999 EM |
107 | * @return null |
108 | */ | |
00be9182 | 109 | public function getTemplateFileName() { |
045f52a3 | 110 | return NULL; |
03e04002 | 111 | } |
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() { |
03e04002 | 150 | $config = CRM_Core_Config::singleton(); |
151 | //zip files if more than one. | |
045f52a3 | 152 | if (count($this->_downloadFile) > 1) { |
f4114c3e | 153 | $zip = $config->customFileUploadDir . 'Financial_Transactions_' . date('YmdHis') . '.zip'; |
03e04002 | 154 | $result = $this->createZip($this->_downloadFile, $zip, TRUE); |
155 | if ($result) { | |
156 | header('Content-Type: application/zip'); | |
f4114c3e | 157 | header('Content-Disposition: attachment; filename=' . CRM_Utils_File::cleanFileName(basename($zip))); |
03e04002 | 158 | header('Content-Length: ' . filesize($zip)); |
159 | ob_clean(); | |
160 | flush(); | |
f4114c3e | 161 | readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($zip))); |
03e04002 | 162 | unlink($zip); //delete the zip to avoid clutter. |
163 | CRM_Utils_System::civiExit(); | |
164 | } | |
165 | } | |
166 | else { | |
f4114c3e PN |
167 | header('Content-Type: text/plain'); |
168 | header('Content-Disposition: attachment; filename=' . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0]))); | |
03e04002 | 169 | header('Content-Length: ' . filesize($this->_downloadFile[0])); |
170 | ob_clean(); | |
171 | flush(); | |
f4114c3e | 172 | readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0]))); |
03e04002 | 173 | CRM_Utils_System::civiExit(); |
174 | } | |
175 | } | |
176 | ||
e0ef6999 EM |
177 | /** |
178 | * @param $batchIds | |
100fef9d | 179 | * @param string $fileName |
e0ef6999 EM |
180 | * |
181 | * @throws CRM_Core_Exception | |
182 | */ | |
00be9182 | 183 | public static function createActivityExport($batchIds, $fileName) { |
03e04002 | 184 | $session = CRM_Core_Session::singleton(); |
185 | $values = array(); | |
186 | $params = array('id' => $batchIds); | |
187 | CRM_Batch_BAO_Batch::retrieve($params, $values); | |
188 | $createdBy = CRM_Contact_BAO_Contact::displayName($values['created_id']); | |
189 | $modifiedBy = CRM_Contact_BAO_Contact::displayName($values['modified_id']); | |
190 | ||
191 | $values['payment_instrument_id'] = ''; | |
192 | if (isset($values['payment_instrument_id'])) { | |
193 | $paymentInstrument = array_flip(CRM_Contribute_PseudoConstant::paymentInstrument('label')); | |
194 | $values['payment_instrument_id'] = array_search($values['payment_instrument_id'], $paymentInstrument); | |
195 | } | |
ffa40ba9 | 196 | $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>'; |
03e04002 | 197 | $subject = ''; |
a7488080 | 198 | if (!empty($values['total'])) { |
86bfa4f6 | 199 | $subject .= ts('Total') . '[' . CRM_Utils_Money::format($values['total']) . '],'; |
03e04002 | 200 | } |
a7488080 | 201 | if (!empty($values['item_count'])) { |
86bfa4f6 | 202 | $subject .= ' ' . ts('Count') . '[' . $values['item_count'] . '],'; |
03e04002 | 203 | } |
204 | ||
205 | //create activity. | |
86bfa4f6 | 206 | $subject .= ' ' . ts('Batch') . '[' . $values['title'] . ']'; |
03e04002 | 207 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); |
208 | $activityParams = array( | |
209 | 'activity_type_id' => array_search('Export Accounting Batch', $activityTypes), | |
210 | 'subject' => $subject, | |
211 | 'status_id' => 2, | |
212 | 'activity_date_time' => date('YmdHis'), | |
213 | 'source_contact_id' => $session->get('userID'), | |
214 | 'source_record_id' => $values['id'], | |
215 | 'target_contact_id' => $session->get('userID'), | |
216 | 'details' => $details, | |
045f52a3 | 217 | 'attachFile_1' => array( |
03e04002 | 218 | 'uri' => $fileName, |
219 | 'type' => 'text/csv', | |
220 | 'location' => $fileName, | |
221 | 'upload_date' => date('YmdHis'), | |
222 | ), | |
223 | ); | |
224 | ||
225 | CRM_Activity_BAO_Activity::create($activityParams); | |
226 | } | |
227 | ||
e0ef6999 EM |
228 | /** |
229 | * @param array $files | |
230 | * @param null $destination | |
231 | * @param bool $overwrite | |
232 | * | |
233 | * @return bool | |
234 | */ | |
00be9182 | 235 | public function createZip($files = array(), $destination = NULL, $overwrite = FALSE) { |
03e04002 | 236 | //if the zip file already exists and overwrite is false, return false |
237 | if (file_exists($destination) && !$overwrite) { | |
238 | return FALSE; | |
239 | } | |
240 | $valid_files = array(); | |
241 | if (is_array($files)) { | |
242 | foreach ($files as $file) { | |
243 | //make sure the file exists | |
244 | if (file_exists($file)) { | |
245 | $validFiles[] = $file; | |
246 | } | |
247 | } | |
248 | } | |
249 | if (count($validFiles)) { | |
250 | $zip = new ZipArchive(); | |
045f52a3 | 251 | if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== TRUE) { |
03e04002 | 252 | return FALSE; |
253 | } | |
254 | foreach ($validFiles as $file) { | |
255 | $zip->addFile($file, CRM_Utils_File::cleanFileName(basename($file))); | |
256 | } | |
257 | $zip->close(); | |
258 | return file_exists($destination); | |
259 | } | |
260 | else { | |
045f52a3 TO |
261 | return FALSE; |
262 | } | |
03e04002 | 263 | } |
232624b1 | 264 | } |