3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 abstract class CRM_Contribute_Import_Parser
extends CRM_Import_Parser
{
36 * Contribution-specific result codes
37 * @see CRM_Import_Parser result code constants
39 const SOFT_CREDIT
= 512, SOFT_CREDIT_ERROR
= 1024, PLEDGE_PAYMENT
= 2048, PLEDGE_PAYMENT_ERROR
= 4096;
49 * Seperator being used
51 protected $_seperator;
54 * Total number of lines in file
56 protected $_lineCount;
59 * Running total number of valid soft credit rows
61 protected $_validSoftCreditRowCount;
64 * Running total number of invalid soft credit rows
66 protected $_invalidSoftCreditRowCount;
69 * Running total number of valid pledge payment rows
71 protected $_validPledgePaymentRowCount;
74 * Running total number of invalid pledge payment rows
76 protected $_invalidPledgePaymentRowCount;
79 * Array of pledge payment error lines, bounded by MAX_ERROR
81 protected $_pledgePaymentErrors;
84 * Array of pledge payment error lines, bounded by MAX_ERROR
86 protected $_softCreditErrors;
89 * Filename of pledge payment error data
93 protected $_pledgePaymentErrorsFileName;
96 * Filename of soft credit error data
100 protected $_softCreditErrorsFileName;
103 * Whether the file has a column header or not
107 protected $_haveColumnHeader;
110 * @param string $fileName
111 * @param string $seperator
113 * @param bool $skipColumnHeader
115 * @param int $contactType
116 * @param int $onDuplicate
125 $skipColumnHeader = FALSE,
126 $mode = self
::MODE_PREVIEW
,
127 $contactType = self
::CONTACT_INDIVIDUAL
,
128 $onDuplicate = self
::DUPLICATE_SKIP
,
130 $totalRowCount = NULL
132 if (!is_array($fileName)) {
133 CRM_Core_Error
::fatal();
135 $fileName = $fileName['name'];
137 switch ($contactType) {
138 case self
::CONTACT_INDIVIDUAL
:
139 $this->_contactType
= 'Individual';
142 case self
::CONTACT_HOUSEHOLD
:
143 $this->_contactType
= 'Household';
146 case self
::CONTACT_ORGANIZATION
:
147 $this->_contactType
= 'Organization';
152 $this->_haveColumnHeader
= $skipColumnHeader;
154 $this->_seperator
= $seperator;
156 $fd = fopen($fileName, "r");
161 $this->_lineCount
= $this->_warningCount
= $this->_validSoftCreditRowCount
= $this->_validPledgePaymentRowCount
= 0;
162 $this->_invalidRowCount
= $this->_validCount
= $this->_invalidSoftCreditRowCount
= $this->_invalidPledgePaymentRowCount
= 0;
163 $this->_totalCount
= $this->_conflictCount
= 0;
166 $this->_warnings
= [];
167 $this->_conflicts
= [];
168 $this->_pledgePaymentErrors
= [];
169 $this->_softCreditErrors
= [];
171 $this->progressImport($statusID);
172 $startTimestamp = $currTimestamp = $prevTimestamp = time();
175 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
177 if ($mode == self
::MODE_MAPFIELD
) {
181 $this->_activeFieldCount
= count($this->_activeFields
);
187 $values = fgetcsv($fd, 8192, $seperator);
192 self
::encloseScrub($values);
194 // skip column header if we're not in mapfield mode
195 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
196 $skipColumnHeader = FALSE;
200 /* trim whitespace around the values */
203 foreach ($values as $k => $v) {
204 $values[$k] = trim($v, " \t\r\n");
207 if (CRM_Utils_System
::isNull($values)) {
211 $this->_totalCount++
;
213 if ($mode == self
::MODE_MAPFIELD
) {
214 $returnCode = $this->mapField($values);
216 elseif ($mode == self
::MODE_PREVIEW
) {
217 $returnCode = $this->preview($values);
219 elseif ($mode == self
::MODE_SUMMARY
) {
220 $returnCode = $this->summary($values);
222 elseif ($mode == self
::MODE_IMPORT
) {
223 $returnCode = $this->import($onDuplicate, $values);
224 if ($statusID && (($this->_lineCount %
50) == 0)) {
225 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
229 $returnCode = self
::ERROR
;
232 // note that a line could be valid but still produce a warning
233 if ($returnCode == self
::VALID
) {
234 $this->_validCount++
;
235 if ($mode == self
::MODE_MAPFIELD
) {
236 $this->_rows
[] = $values;
237 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
241 if ($returnCode == self
::SOFT_CREDIT
) {
242 $this->_validSoftCreditRowCount++
;
243 $this->_validCount++
;
244 if ($mode == self
::MODE_MAPFIELD
) {
245 $this->_rows
[] = $values;
246 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
250 if ($returnCode == self
::PLEDGE_PAYMENT
) {
251 $this->_validPledgePaymentRowCount++
;
252 $this->_validCount++
;
253 if ($mode == self
::MODE_MAPFIELD
) {
254 $this->_rows
[] = $values;
255 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
259 if ($returnCode == self
::WARNING
) {
260 $this->_warningCount++
;
261 if ($this->_warningCount
< $this->_maxWarningCount
) {
262 $this->_warningCount
[] = $line;
266 if ($returnCode == self
::ERROR
) {
267 $this->_invalidRowCount++
;
268 $recordNumber = $this->_lineCount
;
269 if ($this->_haveColumnHeader
) {
272 array_unshift($values, $recordNumber);
273 $this->_errors
[] = $values;
276 if ($returnCode == self
::PLEDGE_PAYMENT_ERROR
) {
277 $this->_invalidPledgePaymentRowCount++
;
278 $recordNumber = $this->_lineCount
;
279 if ($this->_haveColumnHeader
) {
282 array_unshift($values, $recordNumber);
283 $this->_pledgePaymentErrors
[] = $values;
286 if ($returnCode == self
::SOFT_CREDIT_ERROR
) {
287 $this->_invalidSoftCreditRowCount++
;
288 $recordNumber = $this->_lineCount
;
289 if ($this->_haveColumnHeader
) {
292 array_unshift($values, $recordNumber);
293 $this->_softCreditErrors
[] = $values;
296 if ($returnCode == self
::CONFLICT
) {
297 $this->_conflictCount++
;
298 $recordNumber = $this->_lineCount
;
299 if ($this->_haveColumnHeader
) {
302 array_unshift($values, $recordNumber);
303 $this->_conflicts
[] = $values;
306 if ($returnCode == self
::DUPLICATE
) {
307 if ($returnCode == self
::MULTIPLE_DUPE
) {
308 /* TODO: multi-dupes should be counted apart from singles
309 * on non-skip action */
311 $this->_duplicateCount++
;
312 $recordNumber = $this->_lineCount
;
313 if ($this->_haveColumnHeader
) {
316 array_unshift($values, $recordNumber);
317 $this->_duplicates
[] = $values;
318 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
319 $this->_validCount++
;
323 // we give the derived class a way of aborting the process
324 // note that the return code could be multiple code or'ed together
325 if ($returnCode == self
::STOP
) {
329 // if we are done processing the maxNumber of lines, break
330 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
337 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
338 $customHeaders = $mapper;
340 $customfields = CRM_Core_BAO_CustomField
::getFields('Contribution');
341 foreach ($customHeaders as $key => $value) {
342 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
343 $customHeaders[$key] = $customfields[$id][0];
346 if ($this->_invalidRowCount
) {
347 // removed view url for invlaid contacts
348 $headers = array_merge([
354 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
355 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
358 if ($this->_invalidPledgePaymentRowCount
) {
359 // removed view url for invlaid contacts
360 $headers = array_merge([
366 $this->_pledgePaymentErrorsFileName
= self
::errorFileName(self
::PLEDGE_PAYMENT_ERROR
);
367 self
::exportCSV($this->_pledgePaymentErrorsFileName
, $headers, $this->_pledgePaymentErrors
);
370 if ($this->_invalidSoftCreditRowCount
) {
371 // removed view url for invlaid contacts
372 $headers = array_merge([
378 $this->_softCreditErrorsFileName
= self
::errorFileName(self
::SOFT_CREDIT_ERROR
);
379 self
::exportCSV($this->_softCreditErrorsFileName
, $headers, $this->_softCreditErrors
);
382 if ($this->_conflictCount
) {
383 $headers = array_merge([
389 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
390 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
392 if ($this->_duplicateCount
) {
393 $headers = array_merge([
395 ts('View Contribution URL'),
400 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
401 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
404 return $this->fini();
408 * Given a list of the importable field keys that the user has selected
409 * set the active fields array to this list
411 * @param array $fieldKeys mapped array of values
413 public function setActiveFields($fieldKeys) {
414 $this->_activeFieldCount
= count($fieldKeys);
415 foreach ($fieldKeys as $key) {
416 if (empty($this->_fields
[$key])) {
417 $this->_activeFields
[] = new CRM_Contribute_Import_Field('', ts('- do not import -'));
420 $this->_activeFields
[] = clone($this->_fields
[$key]);
426 * @param array $elements
428 public function setActiveFieldSoftCredit($elements) {
429 for ($i = 0; $i < count($elements); $i++
) {
430 $this->_activeFields
[$i]->_softCreditField
= $elements[$i];
435 * @param array $elements
437 public function setActiveFieldSoftCreditType($elements) {
438 for ($i = 0; $i < count($elements); $i++
) {
439 $this->_activeFields
[$i]->_softCreditType
= $elements[$i];
444 * Format the field values for input to the api.
447 * (reference ) associative array of name/value pairs
449 public function &getActiveFieldParams() {
451 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
452 if (isset($this->_activeFields
[$i]->_value
)) {
453 if (isset($this->_activeFields
[$i]->_softCreditField
)) {
454 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
455 $params[$this->_activeFields
[$i]->_name
] = [];
457 $params[$this->_activeFields
[$i]->_name
][$i][$this->_activeFields
[$i]->_softCreditField
] = $this->_activeFields
[$i]->_value
;
458 if (isset($this->_activeFields
[$i]->_softCreditType
)) {
459 $params[$this->_activeFields
[$i]->_name
][$i]['soft_credit_type_id'] = $this->_activeFields
[$i]->_softCreditType
;
463 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
464 if (!isset($this->_activeFields
[$i]->_softCreditField
)) {
465 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
474 * @param string $name
477 * @param string $headerPattern
478 * @param string $dataPattern
480 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
482 $this->_fields
['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
485 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
486 if (!array_key_exists($name, $tempField)) {
487 $this->_fields
[$name] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
490 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
491 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
498 * Store parser values.
500 * @param CRM_Core_Session $store
504 public function set($store, $mode = self
::MODE_SUMMARY
) {
505 $store->set('fileSize', $this->_fileSize
);
506 $store->set('lineCount', $this->_lineCount
);
507 $store->set('seperator', $this->_seperator
);
508 $store->set('fields', $this->getSelectValues());
509 $store->set('fieldTypes', $this->getSelectTypes());
511 $store->set('headerPatterns', $this->getHeaderPatterns());
512 $store->set('dataPatterns', $this->getDataPatterns());
513 $store->set('columnCount', $this->_activeFieldCount
);
515 $store->set('totalRowCount', $this->_totalCount
);
516 $store->set('validRowCount', $this->_validCount
);
517 $store->set('invalidRowCount', $this->_invalidRowCount
);
518 $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount
);
519 $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount
);
520 $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount
);
521 $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount
);
522 $store->set('conflictRowCount', $this->_conflictCount
);
524 switch ($this->_contactType
) {
526 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
530 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
534 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
537 if ($this->_invalidRowCount
) {
538 $store->set('errorsFileName', $this->_errorFileName
);
540 if ($this->_conflictCount
) {
541 $store->set('conflictsFileName', $this->_conflictFileName
);
543 if (isset($this->_rows
) && !empty($this->_rows
)) {
544 $store->set('dataValues', $this->_rows
);
547 if ($this->_invalidPledgePaymentRowCount
) {
548 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName
);
551 if ($this->_invalidSoftCreditRowCount
) {
552 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName
);
555 if ($mode == self
::MODE_IMPORT
) {
556 $store->set('duplicateRowCount', $this->_duplicateCount
);
557 if ($this->_duplicateCount
) {
558 $store->set('duplicatesFileName', $this->_duplicateFileName
);
564 * Export data to a CSV file.
566 * @param string $fileName
567 * @param array $header
570 public static function exportCSV($fileName, $header, $data) {
572 $fd = fopen($fileName, 'w');
574 foreach ($header as $key => $value) {
575 $header[$key] = "\"$value\"";
577 $config = CRM_Core_Config
::singleton();
578 $output[] = implode($config->fieldSeparator
, $header);
580 foreach ($data as $datum) {
581 foreach ($datum as $key => $value) {
582 if (isset($value[0]) && is_array($value)) {
583 foreach ($value[0] as $k1 => $v1) {
584 if ($k1 == 'location_type_id') {
591 $datum[$key] = "\"$value\"";
594 $output[] = implode($config->fieldSeparator
, $datum);
596 fwrite($fd, implode("\n", $output));
601 * Determines the file extension based on error code.
604 * Error code constant.
608 public static function errorFileName($type) {
614 $config = CRM_Core_Config
::singleton();
615 $fileName = $config->uploadDir
. "sqlImport";
618 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
619 $fileName .= '.softCreditErrors';
622 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
623 $fileName .= '.pledgePaymentErrors';
627 $fileName = parent
::errorFileName($type);
635 * Determines the file name based on error code.
638 * Error code constant.
642 public static function saveFileName($type) {
649 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
650 $fileName = 'Import_Soft_Credit_Errors.csv';
653 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
654 $fileName = 'Import_Pledge_Payment_Errors.csv';
658 $fileName = parent
::saveFileName($type);