Commit | Line | Data |
---|---|---|
03e04002 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
03e04002 | 5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
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 | */ | |
41 | ||
42 | class CRM_Financial_BAO_ExportFormat { | |
43 | ||
44 | /* | |
45 | * Array of data which the individual export formats will output in the desired format | |
46 | */ | |
47 | protected $_exportParams; | |
48 | ||
49 | /* | |
50 | * smarty template | |
51 | */ | |
52 | static protected $_template; | |
53 | ||
54 | /** | |
55 | * class constructor | |
56 | */ | |
57 | function __construct() { | |
58 | if ( !isset( self::$_template ) ) { | |
59 | self::$_template = CRM_Core_Smarty::singleton(); | |
60 | } | |
61 | } | |
62 | ||
63 | // Override to assemble the appropriate subset of financial data for the specific export format | |
64 | function export($exportParams) { | |
65 | $this->_exportParams = $exportParams; | |
66 | return $exportParams; | |
67 | } | |
68 | ||
69 | function output($fileName = NULL) { | |
70 | switch ($this->getFileExtension()) { | |
71 | case 'csv': | |
72 | self::createActivityExport($this->_batchIds, $fileName); | |
73 | break; | |
74 | ||
75 | case 'iif': | |
232624b1 | 76 | $tplFile = $this->getHookedTemplateFileName(); |
f4114c3e | 77 | $out = self::getTemplate()->fetch($tplFile); |
03e04002 | 78 | $fileName = $this->putFile($out); |
79 | self::createActivityExport($this->_batchIds, $fileName); | |
80 | break; | |
81 | } | |
82 | } | |
83 | ||
84 | function getMimeType() { | |
85 | return 'text/plain'; | |
86 | } | |
87 | ||
88 | function getFileExtension() { | |
89 | return 'txt'; | |
90 | } | |
91 | ||
92 | // Override this if appropriate | |
93 | function getTemplateFileName() { | |
94 | return null; | |
95 | } | |
96 | ||
97 | static function &getTemplate() { | |
98 | return self::$_template; | |
99 | } | |
100 | ||
101 | function assign($var, $value = NULL) { | |
102 | self::$_template->assign($var, $value); | |
103 | } | |
104 | ||
105 | /* | |
106 | * This gets called for every item of data being compiled before being sent to the exporter for output. | |
107 | * | |
108 | * Depending on the output format might want to override this, e.g. for IIF tabs need to be escaped etc, | |
109 | * but for CSV it doesn't make sense because php has built in csv output functions. | |
110 | */ | |
111 | static function format($s, $type = 'string') { | |
112 | if (!empty($s)) { | |
113 | return $s; | |
114 | } | |
115 | else { | |
116 | return NULL; | |
117 | } | |
118 | } | |
119 | ||
120 | function initiateDownload() { | |
121 | $config = CRM_Core_Config::singleton(); | |
122 | //zip files if more than one. | |
123 | if (count($this->_downloadFile)>1) { | |
f4114c3e | 124 | $zip = $config->customFileUploadDir . 'Financial_Transactions_' . date('YmdHis') . '.zip'; |
03e04002 | 125 | $result = $this->createZip($this->_downloadFile, $zip, TRUE); |
126 | if ($result) { | |
127 | header('Content-Type: application/zip'); | |
f4114c3e | 128 | header('Content-Disposition: attachment; filename=' . CRM_Utils_File::cleanFileName(basename($zip))); |
03e04002 | 129 | header('Content-Length: ' . filesize($zip)); |
130 | ob_clean(); | |
131 | flush(); | |
f4114c3e | 132 | readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($zip))); |
03e04002 | 133 | unlink($zip); //delete the zip to avoid clutter. |
134 | CRM_Utils_System::civiExit(); | |
135 | } | |
136 | } | |
137 | else { | |
f4114c3e PN |
138 | header('Content-Type: text/plain'); |
139 | header('Content-Disposition: attachment; filename=' . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0]))); | |
03e04002 | 140 | header('Content-Length: ' . filesize($this->_downloadFile[0])); |
141 | ob_clean(); | |
142 | flush(); | |
f4114c3e | 143 | readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0]))); |
03e04002 | 144 | CRM_Utils_System::civiExit(); |
145 | } | |
146 | } | |
147 | ||
148 | static function createActivityExport($batchIds, $fileName) { | |
149 | $session = CRM_Core_Session::singleton(); | |
150 | $values = array(); | |
151 | $params = array('id' => $batchIds); | |
152 | CRM_Batch_BAO_Batch::retrieve($params, $values); | |
153 | $createdBy = CRM_Contact_BAO_Contact::displayName($values['created_id']); | |
154 | $modifiedBy = CRM_Contact_BAO_Contact::displayName($values['modified_id']); | |
155 | ||
156 | $values['payment_instrument_id'] = ''; | |
157 | if (isset($values['payment_instrument_id'])) { | |
158 | $paymentInstrument = array_flip(CRM_Contribute_PseudoConstant::paymentInstrument('label')); | |
159 | $values['payment_instrument_id'] = array_search($values['payment_instrument_id'], $paymentInstrument); | |
160 | } | |
161 | $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>'; | |
162 | $subject = ''; | |
a7488080 | 163 | if (!empty($values['total'])) { |
03e04002 | 164 | $subject .= ts('Total') . '['. CRM_Utils_Money::format($values['total']) .'],'; |
165 | } | |
a7488080 | 166 | if (!empty($values['item_count'])) { |
03e04002 | 167 | $subject .= ' ' . ts('Count') . '['. $values['item_count'] .'],'; |
168 | } | |
169 | ||
170 | //create activity. | |
171 | $subject .= ' ' . ts('Batch') . '['. $values['title'] .']'; | |
172 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); | |
173 | $activityParams = array( | |
174 | 'activity_type_id' => array_search('Export Accounting Batch', $activityTypes), | |
175 | 'subject' => $subject, | |
176 | 'status_id' => 2, | |
177 | 'activity_date_time' => date('YmdHis'), | |
178 | 'source_contact_id' => $session->get('userID'), | |
179 | 'source_record_id' => $values['id'], | |
180 | 'target_contact_id' => $session->get('userID'), | |
181 | 'details' => $details, | |
182 | 'attachFile_1' => array ( | |
183 | 'uri' => $fileName, | |
184 | 'type' => 'text/csv', | |
185 | 'location' => $fileName, | |
186 | 'upload_date' => date('YmdHis'), | |
187 | ), | |
188 | ); | |
189 | ||
190 | CRM_Activity_BAO_Activity::create($activityParams); | |
191 | } | |
192 | ||
193 | function createZip($files = array(), $destination = NULL, $overwrite = FALSE) { | |
194 | //if the zip file already exists and overwrite is false, return false | |
195 | if (file_exists($destination) && !$overwrite) { | |
196 | return FALSE; | |
197 | } | |
198 | $valid_files = array(); | |
199 | if (is_array($files)) { | |
200 | foreach ($files as $file) { | |
201 | //make sure the file exists | |
202 | if (file_exists($file)) { | |
203 | $validFiles[] = $file; | |
204 | } | |
205 | } | |
206 | } | |
207 | if (count($validFiles)) { | |
208 | $zip = new ZipArchive(); | |
209 | if ($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== TRUE) { | |
210 | return FALSE; | |
211 | } | |
212 | foreach ($validFiles as $file) { | |
213 | $zip->addFile($file, CRM_Utils_File::cleanFileName(basename($file))); | |
214 | } | |
215 | $zip->close(); | |
216 | return file_exists($destination); | |
217 | } | |
218 | else { | |
219 | return FALSE; | |
220 | } | |
221 | } | |
232624b1 | 222 | } |