3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 if (!is_array($fileName)) {
131 CRM_Core_Error
::fatal();
133 $fileName = $fileName['name'];
135 switch ($contactType) {
136 case self
::CONTACT_INDIVIDUAL
:
137 $this->_contactType
= 'Individual';
140 case self
::CONTACT_HOUSEHOLD
:
141 $this->_contactType
= 'Household';
144 case self
::CONTACT_ORGANIZATION
:
145 $this->_contactType
= 'Organization';
150 $this->_haveColumnHeader
= $skipColumnHeader;
152 $this->_seperator
= $seperator;
154 $fd = fopen($fileName, "r");
159 $this->_lineCount
= $this->_warningCount
= $this->_validSoftCreditRowCount
= $this->_validPledgePaymentRowCount
= 0;
160 $this->_invalidRowCount
= $this->_validCount
= $this->_invalidSoftCreditRowCount
= $this->_invalidPledgePaymentRowCount
= 0;
161 $this->_totalCount
= $this->_conflictCount
= 0;
163 $this->_errors
= array();
164 $this->_warnings
= array();
165 $this->_conflicts
= array();
166 $this->_pledgePaymentErrors
= array();
167 $this->_softCreditErrors
= array();
169 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
171 if ($mode == self
::MODE_MAPFIELD
) {
172 $this->_rows
= array();
175 $this->_activeFieldCount
= count($this->_activeFields
);
181 $values = fgetcsv($fd, 8192, $seperator);
186 self
::encloseScrub($values);
188 // skip column header if we're not in mapfield mode
189 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
190 $skipColumnHeader = FALSE;
194 /* trim whitespace around the values */
197 foreach ($values as $k => $v) {
198 $values[$k] = trim($v, " \t\r\n");
201 if (CRM_Utils_System
::isNull($values)) {
205 $this->_totalCount++
;
207 if ($mode == self
::MODE_MAPFIELD
) {
208 $returnCode = $this->mapField($values);
210 elseif ($mode == self
::MODE_PREVIEW
) {
211 $returnCode = $this->preview($values);
213 elseif ($mode == self
::MODE_SUMMARY
) {
214 $returnCode = $this->summary($values);
216 elseif ($mode == self
::MODE_IMPORT
) {
217 $returnCode = $this->import($onDuplicate, $values);
220 $returnCode = self
::ERROR
;
223 // note that a line could be valid but still produce a warning
224 if ($returnCode == self
::VALID
) {
225 $this->_validCount++
;
226 if ($mode == self
::MODE_MAPFIELD
) {
227 $this->_rows
[] = $values;
228 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
232 if ($returnCode == self
::SOFT_CREDIT
) {
233 $this->_validSoftCreditRowCount++
;
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
::PLEDGE_PAYMENT
) {
242 $this->_validPledgePaymentRowCount++
;
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
::WARNING
) {
251 $this->_warningCount++
;
252 if ($this->_warningCount
< $this->_maxWarningCount
) {
253 $this->_warningCount
[] = $line;
257 if ($returnCode == self
::ERROR
) {
258 $this->_invalidRowCount++
;
259 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
260 $recordNumber = $this->_lineCount
;
261 if ($this->_haveColumnHeader
) {
264 array_unshift($values, $recordNumber);
265 $this->_errors
[] = $values;
269 if ($returnCode == self
::PLEDGE_PAYMENT_ERROR
) {
270 $this->_invalidPledgePaymentRowCount++
;
271 if ($this->_invalidPledgePaymentRowCount
< $this->_maxErrorCount
) {
272 $recordNumber = $this->_lineCount
;
273 if ($this->_haveColumnHeader
) {
276 array_unshift($values, $recordNumber);
277 $this->_pledgePaymentErrors
[] = $values;
281 if ($returnCode == self
::SOFT_CREDIT_ERROR
) {
282 $this->_invalidSoftCreditRowCount++
;
283 if ($this->_invalidSoftCreditRowCount
< $this->_maxErrorCount
) {
284 $recordNumber = $this->_lineCount
;
285 if ($this->_haveColumnHeader
) {
288 array_unshift($values, $recordNumber);
289 $this->_softCreditErrors
[] = $values;
293 if ($returnCode == self
::CONFLICT
) {
294 $this->_conflictCount++
;
295 $recordNumber = $this->_lineCount
;
296 if ($this->_haveColumnHeader
) {
299 array_unshift($values, $recordNumber);
300 $this->_conflicts
[] = $values;
303 if ($returnCode == self
::DUPLICATE
) {
304 if ($returnCode == self
::MULTIPLE_DUPE
) {
305 /* TODO: multi-dupes should be counted apart from singles
306 * on non-skip action */
308 $this->_duplicateCount++
;
309 $recordNumber = $this->_lineCount
;
310 if ($this->_haveColumnHeader
) {
313 array_unshift($values, $recordNumber);
314 $this->_duplicates
[] = $values;
315 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
316 $this->_validCount++
;
320 // we give the derived class a way of aborting the process
321 // note that the return code could be multiple code or'ed together
322 if ($returnCode == self
::STOP
) {
326 // if we are done processing the maxNumber of lines, break
327 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
334 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
335 $customHeaders = $mapper;
337 $customfields = CRM_Core_BAO_CustomField
::getFields('Contribution');
338 foreach ($customHeaders as $key => $value) {
339 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
340 $customHeaders[$key] = $customfields[$id][0];
343 if ($this->_invalidRowCount
) {
344 // removed view url for invlaid contacts
345 $headers = array_merge(array(
351 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
352 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
355 if ($this->_invalidPledgePaymentRowCount
) {
356 // removed view url for invlaid contacts
357 $headers = array_merge(array(
363 $this->_pledgePaymentErrorsFileName
= self
::errorFileName(self
::PLEDGE_PAYMENT_ERROR
);
364 self
::exportCSV($this->_pledgePaymentErrorsFileName
, $headers, $this->_pledgePaymentErrors
);
367 if ($this->_invalidSoftCreditRowCount
) {
368 // removed view url for invlaid contacts
369 $headers = array_merge(array(
375 $this->_softCreditErrorsFileName
= self
::errorFileName(self
::SOFT_CREDIT_ERROR
);
376 self
::exportCSV($this->_softCreditErrorsFileName
, $headers, $this->_softCreditErrors
);
379 if ($this->_conflictCount
) {
380 $headers = array_merge(array(
386 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
387 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
389 if ($this->_duplicateCount
) {
390 $headers = array_merge(array(
392 ts('View Contribution URL'),
397 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
398 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
401 return $this->fini();
405 * Given a list of the importable field keys that the user has selected
406 * set the active fields array to this list
408 * @param array $fieldKeys mapped array of values
410 public function setActiveFields($fieldKeys) {
411 $this->_activeFieldCount
= count($fieldKeys);
412 foreach ($fieldKeys as $key) {
413 if (empty($this->_fields
[$key])) {
414 $this->_activeFields
[] = new CRM_Contribute_Import_Field('', ts('- do not import -'));
417 $this->_activeFields
[] = clone($this->_fields
[$key]);
423 * @param array $elements
425 public function setActiveFieldSoftCredit($elements) {
426 for ($i = 0; $i < count($elements); $i++
) {
427 $this->_activeFields
[$i]->_softCreditField
= $elements[$i];
432 * @param array $elements
434 public function setActiveFieldSoftCreditType($elements) {
435 for ($i = 0; $i < count($elements); $i++
) {
436 $this->_activeFields
[$i]->_softCreditType
= $elements[$i];
441 * Format the field values for input to the api.
444 * (reference ) associative array of name/value pairs
446 public function &getActiveFieldParams() {
448 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
449 if (isset($this->_activeFields
[$i]->_value
)) {
450 if (isset($this->_activeFields
[$i]->_softCreditField
)) {
451 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
452 $params[$this->_activeFields
[$i]->_name
] = array();
454 $params[$this->_activeFields
[$i]->_name
][$i][$this->_activeFields
[$i]->_softCreditField
] = $this->_activeFields
[$i]->_value
;
455 if (isset($this->_activeFields
[$i]->_softCreditType
)) {
456 $params[$this->_activeFields
[$i]->_name
][$i]['soft_credit_type_id'] = $this->_activeFields
[$i]->_softCreditType
;
460 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
461 if (!isset($this->_activeFields
[$i]->_softCreditField
)) {
462 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
471 * @param string $name
474 * @param string $headerPattern
475 * @param string $dataPattern
477 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
479 $this->_fields
['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
482 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
483 if (!array_key_exists($name, $tempField)) {
484 $this->_fields
[$name] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
487 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
488 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
495 * Store parser values.
497 * @param CRM_Core_Session $store
501 public function set($store, $mode = self
::MODE_SUMMARY
) {
502 $store->set('fileSize', $this->_fileSize
);
503 $store->set('lineCount', $this->_lineCount
);
504 $store->set('seperator', $this->_seperator
);
505 $store->set('fields', $this->getSelectValues());
506 $store->set('fieldTypes', $this->getSelectTypes());
508 $store->set('headerPatterns', $this->getHeaderPatterns());
509 $store->set('dataPatterns', $this->getDataPatterns());
510 $store->set('columnCount', $this->_activeFieldCount
);
512 $store->set('totalRowCount', $this->_totalCount
);
513 $store->set('validRowCount', $this->_validCount
);
514 $store->set('invalidRowCount', $this->_invalidRowCount
);
515 $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount
);
516 $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount
);
517 $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount
);
518 $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount
);
519 $store->set('conflictRowCount', $this->_conflictCount
);
521 switch ($this->_contactType
) {
523 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
527 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
531 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
534 if ($this->_invalidRowCount
) {
535 $store->set('errorsFileName', $this->_errorFileName
);
537 if ($this->_conflictCount
) {
538 $store->set('conflictsFileName', $this->_conflictFileName
);
540 if (isset($this->_rows
) && !empty($this->_rows
)) {
541 $store->set('dataValues', $this->_rows
);
544 if ($this->_invalidPledgePaymentRowCount
) {
545 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName
);
548 if ($this->_invalidSoftCreditRowCount
) {
549 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName
);
552 if ($mode == self
::MODE_IMPORT
) {
553 $store->set('duplicateRowCount', $this->_duplicateCount
);
554 if ($this->_duplicateCount
) {
555 $store->set('duplicatesFileName', $this->_duplicateFileName
);
561 * Export data to a CSV file.
563 * @param string $fileName
564 * @param array $header
567 public static function exportCSV($fileName, $header, $data) {
569 $fd = fopen($fileName, 'w');
571 foreach ($header as $key => $value) {
572 $header[$key] = "\"$value\"";
574 $config = CRM_Core_Config
::singleton();
575 $output[] = implode($config->fieldSeparator
, $header);
577 foreach ($data as $datum) {
578 foreach ($datum as $key => $value) {
579 if (isset($value[0]) && is_array($value)) {
580 foreach ($value[0] as $k1 => $v1) {
581 if ($k1 == 'location_type_id') {
588 $datum[$key] = "\"$value\"";
591 $output[] = implode($config->fieldSeparator
, $datum);
593 fwrite($fd, implode("\n", $output));
598 * Determines the file extension based on error code.
601 * Error code constant.
605 public static function errorFileName($type) {
611 $config = CRM_Core_Config
::singleton();
612 $fileName = $config->uploadDir
. "sqlImport";
615 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
616 $fileName .= '.softCreditErrors';
619 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
620 $fileName .= '.pledgePaymentErrors';
624 $fileName = parent
::errorFileName($type);
632 * Determines the file name based on error code.
635 * Error code constant.
639 public static function saveFileName($type) {
646 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
647 $fileName = 'Import_Soft_Credit_Errors.csv';
650 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
651 $fileName = 'Import_Pledge_Payment_Errors.csv';
655 $fileName = parent
::saveFileName($type);