Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 | 32 | */ |
ec3811b1 | 33 | abstract class CRM_Contribute_Import_Parser extends CRM_Import_Parser { |
6a488035 TO |
34 | |
35 | /** | |
ec3811b1 CW |
36 | * Contribution-specific result codes |
37 | * @see CRM_Import_Parser result code constants | |
6a488035 | 38 | */ |
7da04cde | 39 | const SOFT_CREDIT = 512, SOFT_CREDIT_ERROR = 1024, PLEDGE_PAYMENT = 2048, PLEDGE_PAYMENT_ERROR = 4096; |
6a488035 | 40 | |
1330f57a SL |
41 | /** |
42 | * @var string | |
43 | */ | |
6a488035 TO |
44 | protected $_fileName; |
45 | ||
6a488035 | 46 | /** |
100fef9d | 47 | * Imported file size |
1330f57a | 48 | * @var int |
6a488035 TO |
49 | */ |
50 | protected $_fileSize; | |
51 | ||
52 | /** | |
100fef9d | 53 | * Seperator being used |
1330f57a | 54 | * @var string |
6a488035 TO |
55 | */ |
56 | protected $_seperator; | |
57 | ||
58 | /** | |
100fef9d | 59 | * Total number of lines in file |
1330f57a | 60 | * @var int |
6a488035 TO |
61 | */ |
62 | protected $_lineCount; | |
63 | ||
6a488035 | 64 | /** |
100fef9d | 65 | * Running total number of valid soft credit rows |
1330f57a | 66 | * @var int |
6a488035 TO |
67 | */ |
68 | protected $_validSoftCreditRowCount; | |
69 | ||
70 | /** | |
100fef9d | 71 | * Running total number of invalid soft credit rows |
1330f57a | 72 | * @var int |
6a488035 TO |
73 | */ |
74 | protected $_invalidSoftCreditRowCount; | |
75 | ||
76 | /** | |
100fef9d | 77 | * Running total number of valid pledge payment rows |
1330f57a | 78 | * @var int |
6a488035 TO |
79 | */ |
80 | protected $_validPledgePaymentRowCount; | |
81 | ||
82 | /** | |
100fef9d | 83 | * Running total number of invalid pledge payment rows |
1330f57a | 84 | * @var int |
6a488035 TO |
85 | */ |
86 | protected $_invalidPledgePaymentRowCount; | |
87 | ||
6a488035 | 88 | /** |
100fef9d | 89 | * Array of pledge payment error lines, bounded by MAX_ERROR |
1330f57a | 90 | * @var array |
6a488035 TO |
91 | */ |
92 | protected $_pledgePaymentErrors; | |
93 | ||
94 | /** | |
100fef9d | 95 | * Array of pledge payment error lines, bounded by MAX_ERROR |
1330f57a | 96 | * @var array |
6a488035 TO |
97 | */ |
98 | protected $_softCreditErrors; | |
99 | ||
6a488035 | 100 | /** |
100fef9d | 101 | * Filename of pledge payment error data |
6a488035 TO |
102 | * |
103 | * @var string | |
104 | */ | |
105 | protected $_pledgePaymentErrorsFileName; | |
106 | ||
107 | /** | |
100fef9d | 108 | * Filename of soft credit error data |
6a488035 TO |
109 | * |
110 | * @var string | |
111 | */ | |
112 | protected $_softCreditErrorsFileName; | |
113 | ||
6a488035 | 114 | /** |
100fef9d | 115 | * Whether the file has a column header or not |
6a488035 | 116 | * |
b67daa72 | 117 | * @var bool |
6a488035 TO |
118 | */ |
119 | protected $_haveColumnHeader; | |
120 | ||
186c9c17 | 121 | /** |
100fef9d | 122 | * @param string $fileName |
186c9c17 EM |
123 | * @param string $seperator |
124 | * @param $mapper | |
125 | * @param bool $skipColumnHeader | |
126 | * @param int $mode | |
127 | * @param int $contactType | |
128 | * @param int $onDuplicate | |
1330f57a SL |
129 | * @param int $statusID |
130 | * @param int $totalRowCount | |
186c9c17 EM |
131 | * |
132 | * @return mixed | |
133 | * @throws Exception | |
134 | */ | |
317fceb4 | 135 | public function run( |
a13f3d8c | 136 | $fileName, |
6a488035 TO |
137 | $seperator = ',', |
138 | &$mapper, | |
139 | $skipColumnHeader = FALSE, | |
52892e8b CW |
140 | $mode = self::MODE_PREVIEW, |
141 | $contactType = self::CONTACT_INDIVIDUAL, | |
8cebffb2 JP |
142 | $onDuplicate = self::DUPLICATE_SKIP, |
143 | $statusID = NULL, | |
144 | $totalRowCount = NULL | |
6a488035 TO |
145 | ) { |
146 | if (!is_array($fileName)) { | |
147 | CRM_Core_Error::fatal(); | |
148 | } | |
149 | $fileName = $fileName['name']; | |
150 | ||
151 | switch ($contactType) { | |
152 | case self::CONTACT_INDIVIDUAL: | |
153 | $this->_contactType = 'Individual'; | |
154 | break; | |
155 | ||
156 | case self::CONTACT_HOUSEHOLD: | |
157 | $this->_contactType = 'Household'; | |
158 | break; | |
159 | ||
160 | case self::CONTACT_ORGANIZATION: | |
161 | $this->_contactType = 'Organization'; | |
162 | } | |
163 | ||
164 | $this->init(); | |
165 | ||
166 | $this->_haveColumnHeader = $skipColumnHeader; | |
167 | ||
168 | $this->_seperator = $seperator; | |
169 | ||
170 | $fd = fopen($fileName, "r"); | |
171 | if (!$fd) { | |
172 | return FALSE; | |
173 | } | |
174 | ||
175 | $this->_lineCount = $this->_warningCount = $this->_validSoftCreditRowCount = $this->_validPledgePaymentRowCount = 0; | |
176 | $this->_invalidRowCount = $this->_validCount = $this->_invalidSoftCreditRowCount = $this->_invalidPledgePaymentRowCount = 0; | |
177 | $this->_totalCount = $this->_conflictCount = 0; | |
178 | ||
be2fb01f CW |
179 | $this->_errors = []; |
180 | $this->_warnings = []; | |
181 | $this->_conflicts = []; | |
182 | $this->_pledgePaymentErrors = []; | |
183 | $this->_softCreditErrors = []; | |
8cebffb2 JP |
184 | if ($statusID) { |
185 | $this->progressImport($statusID); | |
186 | $startTimestamp = $currTimestamp = $prevTimestamp = time(); | |
187 | } | |
6a488035 TO |
188 | |
189 | $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2); | |
190 | ||
191 | if ($mode == self::MODE_MAPFIELD) { | |
be2fb01f | 192 | $this->_rows = []; |
6a488035 TO |
193 | } |
194 | else { | |
195 | $this->_activeFieldCount = count($this->_activeFields); | |
196 | } | |
197 | ||
198 | while (!feof($fd)) { | |
199 | $this->_lineCount++; | |
200 | ||
201 | $values = fgetcsv($fd, 8192, $seperator); | |
202 | if (!$values) { | |
203 | continue; | |
204 | } | |
205 | ||
206 | self::encloseScrub($values); | |
207 | ||
208 | // skip column header if we're not in mapfield mode | |
209 | if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) { | |
210 | $skipColumnHeader = FALSE; | |
211 | continue; | |
212 | } | |
213 | ||
214 | /* trim whitespace around the values */ | |
215 | ||
216 | $empty = TRUE; | |
217 | foreach ($values as $k => $v) { | |
218 | $values[$k] = trim($v, " \t\r\n"); | |
219 | } | |
220 | ||
221 | if (CRM_Utils_System::isNull($values)) { | |
222 | continue; | |
223 | } | |
224 | ||
225 | $this->_totalCount++; | |
226 | ||
227 | if ($mode == self::MODE_MAPFIELD) { | |
228 | $returnCode = $this->mapField($values); | |
229 | } | |
230 | elseif ($mode == self::MODE_PREVIEW) { | |
231 | $returnCode = $this->preview($values); | |
232 | } | |
233 | elseif ($mode == self::MODE_SUMMARY) { | |
234 | $returnCode = $this->summary($values); | |
235 | } | |
236 | elseif ($mode == self::MODE_IMPORT) { | |
237 | $returnCode = $this->import($onDuplicate, $values); | |
8cebffb2 JP |
238 | if ($statusID && (($this->_lineCount % 50) == 0)) { |
239 | $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount); | |
240 | } | |
6a488035 TO |
241 | } |
242 | else { | |
243 | $returnCode = self::ERROR; | |
244 | } | |
245 | ||
246 | // note that a line could be valid but still produce a warning | |
247 | if ($returnCode == self::VALID) { | |
248 | $this->_validCount++; | |
249 | if ($mode == self::MODE_MAPFIELD) { | |
250 | $this->_rows[] = $values; | |
251 | $this->_activeFieldCount = max($this->_activeFieldCount, count($values)); | |
252 | } | |
253 | } | |
254 | ||
255 | if ($returnCode == self::SOFT_CREDIT) { | |
256 | $this->_validSoftCreditRowCount++; | |
257 | $this->_validCount++; | |
258 | if ($mode == self::MODE_MAPFIELD) { | |
259 | $this->_rows[] = $values; | |
260 | $this->_activeFieldCount = max($this->_activeFieldCount, count($values)); | |
261 | } | |
262 | } | |
263 | ||
264 | if ($returnCode == self::PLEDGE_PAYMENT) { | |
265 | $this->_validPledgePaymentRowCount++; | |
266 | $this->_validCount++; | |
267 | if ($mode == self::MODE_MAPFIELD) { | |
268 | $this->_rows[] = $values; | |
269 | $this->_activeFieldCount = max($this->_activeFieldCount, count($values)); | |
270 | } | |
271 | } | |
272 | ||
273 | if ($returnCode == self::WARNING) { | |
274 | $this->_warningCount++; | |
275 | if ($this->_warningCount < $this->_maxWarningCount) { | |
276 | $this->_warningCount[] = $line; | |
277 | } | |
278 | } | |
279 | ||
280 | if ($returnCode == self::ERROR) { | |
281 | $this->_invalidRowCount++; | |
ca2057ea SM |
282 | $recordNumber = $this->_lineCount; |
283 | if ($this->_haveColumnHeader) { | |
284 | $recordNumber--; | |
6a488035 | 285 | } |
ca2057ea SM |
286 | array_unshift($values, $recordNumber); |
287 | $this->_errors[] = $values; | |
6a488035 TO |
288 | } |
289 | ||
290 | if ($returnCode == self::PLEDGE_PAYMENT_ERROR) { | |
291 | $this->_invalidPledgePaymentRowCount++; | |
ca2057ea SM |
292 | $recordNumber = $this->_lineCount; |
293 | if ($this->_haveColumnHeader) { | |
294 | $recordNumber--; | |
6a488035 | 295 | } |
ca2057ea SM |
296 | array_unshift($values, $recordNumber); |
297 | $this->_pledgePaymentErrors[] = $values; | |
6a488035 TO |
298 | } |
299 | ||
300 | if ($returnCode == self::SOFT_CREDIT_ERROR) { | |
301 | $this->_invalidSoftCreditRowCount++; | |
ca2057ea SM |
302 | $recordNumber = $this->_lineCount; |
303 | if ($this->_haveColumnHeader) { | |
304 | $recordNumber--; | |
6a488035 | 305 | } |
ca2057ea SM |
306 | array_unshift($values, $recordNumber); |
307 | $this->_softCreditErrors[] = $values; | |
6a488035 TO |
308 | } |
309 | ||
310 | if ($returnCode == self::CONFLICT) { | |
311 | $this->_conflictCount++; | |
312 | $recordNumber = $this->_lineCount; | |
313 | if ($this->_haveColumnHeader) { | |
314 | $recordNumber--; | |
315 | } | |
316 | array_unshift($values, $recordNumber); | |
317 | $this->_conflicts[] = $values; | |
318 | } | |
319 | ||
320 | if ($returnCode == self::DUPLICATE) { | |
321 | if ($returnCode == self::MULTIPLE_DUPE) { | |
322 | /* TODO: multi-dupes should be counted apart from singles | |
006389de | 323 | * on non-skip action */ |
6a488035 TO |
324 | } |
325 | $this->_duplicateCount++; | |
326 | $recordNumber = $this->_lineCount; | |
327 | if ($this->_haveColumnHeader) { | |
328 | $recordNumber--; | |
329 | } | |
330 | array_unshift($values, $recordNumber); | |
331 | $this->_duplicates[] = $values; | |
332 | if ($onDuplicate != self::DUPLICATE_SKIP) { | |
333 | $this->_validCount++; | |
334 | } | |
335 | } | |
336 | ||
337 | // we give the derived class a way of aborting the process | |
338 | // note that the return code could be multiple code or'ed together | |
339 | if ($returnCode == self::STOP) { | |
340 | break; | |
341 | } | |
342 | ||
343 | // if we are done processing the maxNumber of lines, break | |
344 | if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) { | |
345 | break; | |
346 | } | |
347 | } | |
348 | ||
349 | fclose($fd); | |
350 | ||
351 | if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) { | |
352 | $customHeaders = $mapper; | |
353 | ||
354 | $customfields = CRM_Core_BAO_CustomField::getFields('Contribution'); | |
355 | foreach ($customHeaders as $key => $value) { | |
356 | if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) { | |
357 | $customHeaders[$key] = $customfields[$id][0]; | |
358 | } | |
359 | } | |
360 | if ($this->_invalidRowCount) { | |
361 | // removed view url for invlaid contacts | |
be2fb01f | 362 | $headers = array_merge([ |
1330f57a SL |
363 | ts('Line Number'), |
364 | ts('Reason'), | |
365 | ], $customHeaders); | |
6a488035 TO |
366 | $this->_errorFileName = self::errorFileName(self::ERROR); |
367 | self::exportCSV($this->_errorFileName, $headers, $this->_errors); | |
368 | } | |
369 | ||
370 | if ($this->_invalidPledgePaymentRowCount) { | |
371 | // removed view url for invlaid contacts | |
be2fb01f | 372 | $headers = array_merge([ |
1330f57a SL |
373 | ts('Line Number'), |
374 | ts('Reason'), | |
375 | ], $customHeaders); | |
6a488035 TO |
376 | $this->_pledgePaymentErrorsFileName = self::errorFileName(self::PLEDGE_PAYMENT_ERROR); |
377 | self::exportCSV($this->_pledgePaymentErrorsFileName, $headers, $this->_pledgePaymentErrors); | |
378 | } | |
379 | ||
380 | if ($this->_invalidSoftCreditRowCount) { | |
381 | // removed view url for invlaid contacts | |
be2fb01f | 382 | $headers = array_merge([ |
1330f57a SL |
383 | ts('Line Number'), |
384 | ts('Reason'), | |
385 | ], $customHeaders); | |
6a488035 TO |
386 | $this->_softCreditErrorsFileName = self::errorFileName(self::SOFT_CREDIT_ERROR); |
387 | self::exportCSV($this->_softCreditErrorsFileName, $headers, $this->_softCreditErrors); | |
388 | } | |
389 | ||
390 | if ($this->_conflictCount) { | |
be2fb01f | 391 | $headers = array_merge([ |
1330f57a SL |
392 | ts('Line Number'), |
393 | ts('Reason'), | |
394 | ], $customHeaders); | |
6a488035 TO |
395 | $this->_conflictFileName = self::errorFileName(self::CONFLICT); |
396 | self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts); | |
397 | } | |
398 | if ($this->_duplicateCount) { | |
be2fb01f | 399 | $headers = array_merge([ |
1330f57a SL |
400 | ts('Line Number'), |
401 | ts('View Contribution URL'), | |
402 | ], $customHeaders); | |
6a488035 TO |
403 | |
404 | $this->_duplicateFileName = self::errorFileName(self::DUPLICATE); | |
405 | self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates); | |
406 | } | |
407 | } | |
6a488035 TO |
408 | return $this->fini(); |
409 | } | |
410 | ||
6a488035 TO |
411 | /** |
412 | * Given a list of the importable field keys that the user has selected | |
413 | * set the active fields array to this list | |
414 | * | |
317fceb4 | 415 | * @param array $fieldKeys mapped array of values |
6a488035 | 416 | */ |
00be9182 | 417 | public function setActiveFields($fieldKeys) { |
6a488035 TO |
418 | $this->_activeFieldCount = count($fieldKeys); |
419 | foreach ($fieldKeys as $key) { | |
420 | if (empty($this->_fields[$key])) { | |
421 | $this->_activeFields[] = new CRM_Contribute_Import_Field('', ts('- do not import -')); | |
422 | } | |
423 | else { | |
424 | $this->_activeFields[] = clone($this->_fields[$key]); | |
425 | } | |
426 | } | |
427 | } | |
428 | ||
186c9c17 | 429 | /** |
d8148eee | 430 | * Store the soft credit field information. |
431 | * | |
432 | * This was perhaps done this way on the believe that a lot of code pain | |
433 | * was worth it to avoid negligible-cost array iterations. Perhaps we could prioritise | |
434 | * readability & maintainability next since we can just work with functions to retrieve | |
435 | * data from the metadata. | |
436 | * | |
c490a46a | 437 | * @param array $elements |
186c9c17 | 438 | */ |
00be9182 | 439 | public function setActiveFieldSoftCredit($elements) { |
d8148eee | 440 | foreach ((array) $elements as $i => $element) { |
441 | $this->_activeFields[$i]->_softCreditField = $element; | |
6a488035 TO |
442 | } |
443 | } | |
444 | ||
186c9c17 | 445 | /** |
d8148eee | 446 | * Store the soft credit field type information. |
447 | * | |
448 | * This was perhaps done this way on the believe that a lot of code pain | |
449 | * was worth it to avoid negligible-cost array iterations. Perhaps we could prioritise | |
450 | * readability & maintainability next since we can just work with functions to retrieve | |
451 | * data from the metadata. | |
452 | * | |
c490a46a | 453 | * @param array $elements |
186c9c17 | 454 | */ |
00be9182 | 455 | public function setActiveFieldSoftCreditType($elements) { |
d8148eee | 456 | foreach ((array) $elements as $i => $element) { |
457 | $this->_activeFields[$i]->_softCreditType = $element; | |
1221efe9 | 458 | } |
459 | } | |
c490a46a | 460 | |
6a488035 | 461 | /** |
fe482240 | 462 | * Format the field values for input to the api. |
6a488035 | 463 | * |
a6c01b45 CW |
464 | * @return array |
465 | * (reference ) associative array of name/value pairs | |
6a488035 | 466 | */ |
00be9182 | 467 | public function &getActiveFieldParams() { |
be2fb01f | 468 | $params = []; |
6a488035 TO |
469 | for ($i = 0; $i < $this->_activeFieldCount; $i++) { |
470 | if (isset($this->_activeFields[$i]->_value)) { | |
471 | if (isset($this->_activeFields[$i]->_softCreditField)) { | |
472 | if (!isset($params[$this->_activeFields[$i]->_name])) { | |
be2fb01f | 473 | $params[$this->_activeFields[$i]->_name] = []; |
6a488035 | 474 | } |
1221efe9 | 475 | $params[$this->_activeFields[$i]->_name][$i][$this->_activeFields[$i]->_softCreditField] = $this->_activeFields[$i]->_value; |
9b873358 | 476 | if (isset($this->_activeFields[$i]->_softCreditType)) { |
1221efe9 | 477 | $params[$this->_activeFields[$i]->_name][$i]['soft_credit_type_id'] = $this->_activeFields[$i]->_softCreditType; |
478 | } | |
6a488035 TO |
479 | } |
480 | ||
481 | if (!isset($params[$this->_activeFields[$i]->_name])) { | |
482 | if (!isset($this->_activeFields[$i]->_softCreditField)) { | |
483 | $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value; | |
484 | } | |
485 | } | |
486 | } | |
487 | } | |
488 | return $params; | |
489 | } | |
490 | ||
186c9c17 | 491 | /** |
100fef9d | 492 | * @param string $name |
186c9c17 EM |
493 | * @param $title |
494 | * @param int $type | |
495 | * @param string $headerPattern | |
496 | * @param string $dataPattern | |
497 | */ | |
00be9182 | 498 | public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { |
6a488035 TO |
499 | if (empty($name)) { |
500 | $this->_fields['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
501 | } | |
502 | else { | |
503 | $tempField = CRM_Contact_BAO_Contact::importableFields('All', NULL); | |
504 | if (!array_key_exists($name, $tempField)) { | |
505 | $this->_fields[$name] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
506 | } | |
507 | else { | |
719a6fec | 508 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, |
6a488035 TO |
509 | CRM_Utils_Array::value('hasLocationType', $tempField[$name]) |
510 | ); | |
511 | } | |
512 | } | |
513 | } | |
514 | ||
6a488035 | 515 | /** |
fe482240 | 516 | * Store parser values. |
6a488035 TO |
517 | * |
518 | * @param CRM_Core_Session $store | |
519 | * | |
2a6da8d7 | 520 | * @param int $mode |
6a488035 | 521 | */ |
00be9182 | 522 | public function set($store, $mode = self::MODE_SUMMARY) { |
6a488035 TO |
523 | $store->set('fileSize', $this->_fileSize); |
524 | $store->set('lineCount', $this->_lineCount); | |
525 | $store->set('seperator', $this->_seperator); | |
526 | $store->set('fields', $this->getSelectValues()); | |
527 | $store->set('fieldTypes', $this->getSelectTypes()); | |
528 | ||
529 | $store->set('headerPatterns', $this->getHeaderPatterns()); | |
530 | $store->set('dataPatterns', $this->getDataPatterns()); | |
531 | $store->set('columnCount', $this->_activeFieldCount); | |
532 | ||
533 | $store->set('totalRowCount', $this->_totalCount); | |
534 | $store->set('validRowCount', $this->_validCount); | |
535 | $store->set('invalidRowCount', $this->_invalidRowCount); | |
536 | $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount); | |
537 | $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount); | |
538 | $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount); | |
539 | $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount); | |
540 | $store->set('conflictRowCount', $this->_conflictCount); | |
541 | ||
542 | switch ($this->_contactType) { | |
543 | case 'Individual': | |
a05662ef | 544 | $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); |
6a488035 TO |
545 | break; |
546 | ||
547 | case 'Household': | |
a05662ef | 548 | $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD); |
6a488035 TO |
549 | break; |
550 | ||
551 | case 'Organization': | |
a05662ef | 552 | $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION); |
6a488035 TO |
553 | } |
554 | ||
555 | if ($this->_invalidRowCount) { | |
556 | $store->set('errorsFileName', $this->_errorFileName); | |
557 | } | |
558 | if ($this->_conflictCount) { | |
559 | $store->set('conflictsFileName', $this->_conflictFileName); | |
560 | } | |
561 | if (isset($this->_rows) && !empty($this->_rows)) { | |
562 | $store->set('dataValues', $this->_rows); | |
563 | } | |
564 | ||
565 | if ($this->_invalidPledgePaymentRowCount) { | |
566 | $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName); | |
567 | } | |
568 | ||
569 | if ($this->_invalidSoftCreditRowCount) { | |
570 | $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName); | |
571 | } | |
572 | ||
573 | if ($mode == self::MODE_IMPORT) { | |
574 | $store->set('duplicateRowCount', $this->_duplicateCount); | |
575 | if ($this->_duplicateCount) { | |
576 | $store->set('duplicatesFileName', $this->_duplicateFileName); | |
577 | } | |
578 | } | |
6a488035 TO |
579 | } |
580 | ||
581 | /** | |
fe482240 | 582 | * Export data to a CSV file. |
6a488035 | 583 | * |
c490a46a | 584 | * @param string $fileName |
6a488035 | 585 | * @param array $header |
c490a46a | 586 | * @param array $data |
6a488035 | 587 | */ |
00be9182 | 588 | public static function exportCSV($fileName, $header, $data) { |
be2fb01f | 589 | $output = []; |
6a488035 TO |
590 | $fd = fopen($fileName, 'w'); |
591 | ||
592 | foreach ($header as $key => $value) { | |
593 | $header[$key] = "\"$value\""; | |
594 | } | |
595 | $config = CRM_Core_Config::singleton(); | |
596 | $output[] = implode($config->fieldSeparator, $header); | |
597 | ||
598 | foreach ($data as $datum) { | |
599 | foreach ($datum as $key => $value) { | |
1221efe9 | 600 | if (isset($value[0]) && is_array($value)) { |
6a488035 TO |
601 | foreach ($value[0] as $k1 => $v1) { |
602 | if ($k1 == 'location_type_id') { | |
603 | continue; | |
604 | } | |
605 | $datum[$k1] = $v1; | |
606 | } | |
607 | } | |
608 | else { | |
609 | $datum[$key] = "\"$value\""; | |
610 | } | |
611 | } | |
612 | $output[] = implode($config->fieldSeparator, $datum); | |
613 | } | |
614 | fwrite($fd, implode("\n", $output)); | |
615 | fclose($fd); | |
616 | } | |
617 | ||
186c9c17 | 618 | /** |
fe482240 | 619 | * Determines the file extension based on error code. |
186c9c17 | 620 | * |
014c4014 TO |
621 | * @param int $type |
622 | * Error code constant. | |
186c9c17 EM |
623 | * |
624 | * @return string | |
625 | */ | |
00be9182 | 626 | public static function errorFileName($type) { |
6a488035 TO |
627 | $fileName = NULL; |
628 | if (empty($type)) { | |
629 | return $fileName; | |
630 | } | |
631 | ||
632 | $config = CRM_Core_Config::singleton(); | |
633 | $fileName = $config->uploadDir . "sqlImport"; | |
634 | ||
635 | switch ($type) { | |
6a488035 TO |
636 | case CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR: |
637 | $fileName .= '.softCreditErrors'; | |
638 | break; | |
639 | ||
640 | case CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR: | |
641 | $fileName .= '.pledgePaymentErrors'; | |
642 | break; | |
69a4c20a CW |
643 | |
644 | default: | |
645 | $fileName = parent::errorFileName($type); | |
646 | break; | |
6a488035 TO |
647 | } |
648 | ||
649 | return $fileName; | |
650 | } | |
651 | ||
186c9c17 | 652 | /** |
fe482240 | 653 | * Determines the file name based on error code. |
186c9c17 | 654 | * |
014c4014 TO |
655 | * @param int $type |
656 | * Error code constant. | |
186c9c17 EM |
657 | * |
658 | * @return string | |
659 | */ | |
00be9182 | 660 | public static function saveFileName($type) { |
6a488035 TO |
661 | $fileName = NULL; |
662 | if (empty($type)) { | |
663 | return $fileName; | |
664 | } | |
665 | ||
666 | switch ($type) { | |
6a488035 TO |
667 | case CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR: |
668 | $fileName = 'Import_Soft_Credit_Errors.csv'; | |
669 | break; | |
670 | ||
671 | case CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR: | |
672 | $fileName = 'Import_Pledge_Payment_Errors.csv'; | |
673 | break; | |
69a4c20a CW |
674 | |
675 | default: | |
676 | $fileName = parent::saveFileName($type); | |
677 | break; | |
6a488035 TO |
678 | } |
679 | ||
680 | return $fileName; | |
681 | } | |
96025800 | 682 | |
6a488035 | 683 | } |