3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 abstract class CRM_Contribute_Import_Parser
extends CRM_Import_Parser
{
40 * Contribution-specific result codes
41 * @see CRM_Import_Parser result code constants
43 CONST SOFT_CREDIT
= 512, SOFT_CREDIT_ERROR
= 1024, PLEDGE_PAYMENT
= 2048, PLEDGE_PAYMENT_ERROR
= 4096;
58 * seperator being used
60 protected $_seperator;
63 * total number of lines in file
65 protected $_lineCount;
68 * running total number of valid soft credit rows
70 protected $_validSoftCreditRowCount;
73 * running total number of invalid soft credit rows
75 protected $_invalidSoftCreditRowCount;
78 * running total number of valid pledge payment rows
80 protected $_validPledgePaymentRowCount;
83 * running total number of invalid pledge payment rows
85 protected $_invalidPledgePaymentRowCount;
88 * array of pledge payment error lines, bounded by MAX_ERROR
90 protected $_pledgePaymentErrors;
93 * array of pledge payment error lines, bounded by MAX_ERROR
95 protected $_softCreditErrors;
98 * filename of pledge payment error data
102 protected $_pledgePaymentErrorsFileName;
105 * filename of soft credit error data
109 protected $_softCreditErrorsFileName;
112 * whether the file has a column header or not
116 protected $_haveColumnHeader;
120 * @param string $seperator
122 * @param bool $skipColumnHeader
124 * @param int $contactType
125 * @param int $onDuplicate
130 function run($fileName,
133 $skipColumnHeader = FALSE,
134 $mode = self
::MODE_PREVIEW
,
135 $contactType = self
::CONTACT_INDIVIDUAL
,
136 $onDuplicate = self
::DUPLICATE_SKIP
138 if (!is_array($fileName)) {
139 CRM_Core_Error
::fatal();
141 $fileName = $fileName['name'];
143 switch ($contactType) {
144 case self
::CONTACT_INDIVIDUAL
:
145 $this->_contactType
= 'Individual';
148 case self
::CONTACT_HOUSEHOLD
:
149 $this->_contactType
= 'Household';
152 case self
::CONTACT_ORGANIZATION
:
153 $this->_contactType
= 'Organization';
158 $this->_haveColumnHeader
= $skipColumnHeader;
160 $this->_seperator
= $seperator;
162 $fd = fopen($fileName, "r");
167 $this->_lineCount
= $this->_warningCount
= $this->_validSoftCreditRowCount
= $this->_validPledgePaymentRowCount
= 0;
168 $this->_invalidRowCount
= $this->_validCount
= $this->_invalidSoftCreditRowCount
= $this->_invalidPledgePaymentRowCount
= 0;
169 $this->_totalCount
= $this->_conflictCount
= 0;
171 $this->_errors
= array();
172 $this->_warnings
= array();
173 $this->_conflicts
= array();
174 $this->_pledgePaymentErrors
= array();
175 $this->_softCreditErrors
= array();
177 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
179 if ($mode == self
::MODE_MAPFIELD
) {
180 $this->_rows
= array();
183 $this->_activeFieldCount
= count($this->_activeFields
);
189 $values = fgetcsv($fd, 8192, $seperator);
194 self
::encloseScrub($values);
196 // skip column header if we're not in mapfield mode
197 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
198 $skipColumnHeader = FALSE;
202 /* trim whitespace around the values */
205 foreach ($values as $k => $v) {
206 $values[$k] = trim($v, " \t\r\n");
209 if (CRM_Utils_System
::isNull($values)) {
213 $this->_totalCount++
;
215 if ($mode == self
::MODE_MAPFIELD
) {
216 $returnCode = $this->mapField($values);
218 elseif ($mode == self
::MODE_PREVIEW
) {
219 $returnCode = $this->preview($values);
221 elseif ($mode == self
::MODE_SUMMARY
) {
222 $returnCode = $this->summary($values);
224 elseif ($mode == self
::MODE_IMPORT
) {
225 $returnCode = $this->import($onDuplicate, $values);
228 $returnCode = self
::ERROR
;
231 // note that a line could be valid but still produce a warning
232 if ($returnCode == self
::VALID
) {
233 $this->_validCount++
;
234 if ($mode == self
::MODE_MAPFIELD
) {
235 $this->_rows
[] = $values;
236 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
240 if ($returnCode == self
::SOFT_CREDIT
) {
241 $this->_validSoftCreditRowCount++
;
242 $this->_validCount++
;
243 if ($mode == self
::MODE_MAPFIELD
) {
244 $this->_rows
[] = $values;
245 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
249 if ($returnCode == self
::PLEDGE_PAYMENT
) {
250 $this->_validPledgePaymentRowCount++
;
251 $this->_validCount++
;
252 if ($mode == self
::MODE_MAPFIELD
) {
253 $this->_rows
[] = $values;
254 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
258 if ($returnCode == self
::WARNING
) {
259 $this->_warningCount++
;
260 if ($this->_warningCount
< $this->_maxWarningCount
) {
261 $this->_warningCount
[] = $line;
265 if ($returnCode == self
::ERROR
) {
266 $this->_invalidRowCount++
;
267 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
268 $recordNumber = $this->_lineCount
;
269 if ($this->_haveColumnHeader
) {
272 array_unshift($values, $recordNumber);
273 $this->_errors
[] = $values;
277 if ($returnCode == self
::PLEDGE_PAYMENT_ERROR
) {
278 $this->_invalidPledgePaymentRowCount++
;
279 if ($this->_invalidPledgePaymentRowCount
< $this->_maxErrorCount
) {
280 $recordNumber = $this->_lineCount
;
281 if ($this->_haveColumnHeader
) {
284 array_unshift($values, $recordNumber);
285 $this->_pledgePaymentErrors
[] = $values;
289 if ($returnCode == self
::SOFT_CREDIT_ERROR
) {
290 $this->_invalidSoftCreditRowCount++
;
291 if ($this->_invalidSoftCreditRowCount
< $this->_maxErrorCount
) {
292 $recordNumber = $this->_lineCount
;
293 if ($this->_haveColumnHeader
) {
296 array_unshift($values, $recordNumber);
297 $this->_softCreditErrors
[] = $values;
301 if ($returnCode == self
::CONFLICT
) {
302 $this->_conflictCount++
;
303 $recordNumber = $this->_lineCount
;
304 if ($this->_haveColumnHeader
) {
307 array_unshift($values, $recordNumber);
308 $this->_conflicts
[] = $values;
311 if ($returnCode == self
::DUPLICATE
) {
312 if ($returnCode == self
::MULTIPLE_DUPE
) {
313 /* TODO: multi-dupes should be counted apart from singles
314 * on non-skip action */
316 $this->_duplicateCount++
;
317 $recordNumber = $this->_lineCount
;
318 if ($this->_haveColumnHeader
) {
321 array_unshift($values, $recordNumber);
322 $this->_duplicates
[] = $values;
323 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
324 $this->_validCount++
;
328 // we give the derived class a way of aborting the process
329 // note that the return code could be multiple code or'ed together
330 if ($returnCode == self
::STOP
) {
334 // if we are done processing the maxNumber of lines, break
335 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
342 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
343 $customHeaders = $mapper;
345 $customfields = CRM_Core_BAO_CustomField
::getFields('Contribution');
346 foreach ($customHeaders as $key => $value) {
347 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
348 $customHeaders[$key] = $customfields[$id][0];
351 if ($this->_invalidRowCount
) {
352 // removed view url for invlaid contacts
353 $headers = array_merge(array(ts('Line Number'),
358 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
359 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
362 if ($this->_invalidPledgePaymentRowCount
) {
363 // removed view url for invlaid contacts
364 $headers = array_merge(array(ts('Line Number'),
369 $this->_pledgePaymentErrorsFileName
= self
::errorFileName(self
::PLEDGE_PAYMENT_ERROR
);
370 self
::exportCSV($this->_pledgePaymentErrorsFileName
, $headers, $this->_pledgePaymentErrors
);
373 if ($this->_invalidSoftCreditRowCount
) {
374 // removed view url for invlaid contacts
375 $headers = array_merge(array(ts('Line Number'),
380 $this->_softCreditErrorsFileName
= self
::errorFileName(self
::SOFT_CREDIT_ERROR
);
381 self
::exportCSV($this->_softCreditErrorsFileName
, $headers, $this->_softCreditErrors
);
384 if ($this->_conflictCount
) {
385 $headers = array_merge(array(ts('Line Number'),
390 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
391 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
393 if ($this->_duplicateCount
) {
394 $headers = array_merge(array(ts('Line Number'),
395 ts('View Contribution URL'),
400 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
401 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
404 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
405 return $this->fini();
409 * Given a list of the importable field keys that the user has selected
410 * set the active fields array to this list
412 * @param array mapped array of values
417 function setActiveFields($fieldKeys) {
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 -'));
424 $this->_activeFields
[] = clone($this->_fields
[$key]);
432 function setActiveFieldSoftCredit($elements) {
433 for ($i = 0; $i < count($elements); $i++
) {
434 $this->_activeFields
[$i]->_softCreditField
= $elements[$i];
441 function setActiveFieldSoftCreditType($elements) {
442 for ($i = 0; $i < count($elements); $i++
) {
443 $this->_activeFields
[$i]->_softCreditType
= $elements[$i];
447 * function to format the field values for input to the api
449 * @return array (reference ) associative array of name/value pairs
452 function &getActiveFieldParams() {
454 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
455 if (isset($this->_activeFields
[$i]->_value
)) {
456 if (isset($this->_activeFields
[$i]->_softCreditField
)) {
457 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
458 $params[$this->_activeFields
[$i]->_name
] = array();
460 $params[$this->_activeFields
[$i]->_name
][$i][$this->_activeFields
[$i]->_softCreditField
] = $this->_activeFields
[$i]->_value
;
461 if(isset($this->_activeFields
[$i]->_softCreditType
)){
462 $params[$this->_activeFields
[$i]->_name
][$i]['soft_credit_type_id'] = $this->_activeFields
[$i]->_softCreditType
;
466 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
467 if (!isset($this->_activeFields
[$i]->_softCreditField
)) {
468 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
480 * @param string $headerPattern
481 * @param string $dataPattern
483 function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
485 $this->_fields
['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
488 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
489 if (!array_key_exists($name, $tempField)) {
490 $this->_fields
[$name] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
493 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
494 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
501 * Store parser values
503 * @param CRM_Core_Session $store
510 function set($store, $mode = self
::MODE_SUMMARY
) {
511 $store->set('fileSize', $this->_fileSize
);
512 $store->set('lineCount', $this->_lineCount
);
513 $store->set('seperator', $this->_seperator
);
514 $store->set('fields', $this->getSelectValues());
515 $store->set('fieldTypes', $this->getSelectTypes());
517 $store->set('headerPatterns', $this->getHeaderPatterns());
518 $store->set('dataPatterns', $this->getDataPatterns());
519 $store->set('columnCount', $this->_activeFieldCount
);
521 $store->set('totalRowCount', $this->_totalCount
);
522 $store->set('validRowCount', $this->_validCount
);
523 $store->set('invalidRowCount', $this->_invalidRowCount
);
524 $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount
);
525 $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount
);
526 $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount
);
527 $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount
);
528 $store->set('conflictRowCount', $this->_conflictCount
);
530 switch ($this->_contactType
) {
532 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
536 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
540 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
543 if ($this->_invalidRowCount
) {
544 $store->set('errorsFileName', $this->_errorFileName
);
546 if ($this->_conflictCount
) {
547 $store->set('conflictsFileName', $this->_conflictFileName
);
549 if (isset($this->_rows
) && !empty($this->_rows
)) {
550 $store->set('dataValues', $this->_rows
);
553 if ($this->_invalidPledgePaymentRowCount
) {
554 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName
);
557 if ($this->_invalidSoftCreditRowCount
) {
558 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName
);
561 if ($mode == self
::MODE_IMPORT
) {
562 $store->set('duplicateRowCount', $this->_duplicateCount
);
563 if ($this->_duplicateCount
) {
564 $store->set('duplicatesFileName', $this->_duplicateFileName
);
567 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
571 * Export data to a CSV file
574 * @param array $header
577 * @internal param string $filename
581 static function exportCSV($fileName, $header, $data) {
583 $fd = fopen($fileName, 'w');
585 foreach ($header as $key => $value) {
586 $header[$key] = "\"$value\"";
588 $config = CRM_Core_Config
::singleton();
589 $output[] = implode($config->fieldSeparator
, $header);
591 foreach ($data as $datum) {
592 foreach ($datum as $key => $value) {
593 if (isset($value[0]) && is_array($value)) {
594 foreach ($value[0] as $k1 => $v1) {
595 if ($k1 == 'location_type_id') {
602 $datum[$key] = "\"$value\"";
605 $output[] = implode($config->fieldSeparator
, $datum);
607 fwrite($fd, implode("\n", $output));
612 * Determines the file extension based on error code
614 * @var $type error code constant
623 static function errorFileName($type) {
629 $config = CRM_Core_Config
::singleton();
630 $fileName = $config->uploadDir
. "sqlImport";
633 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
634 $fileName .= '.softCreditErrors';
637 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
638 $fileName .= '.pledgePaymentErrors';
642 $fileName = parent
::errorFileName($type);
650 * Determines the file name based on error code
652 * @var $type error code constant
661 static function saveFileName($type) {
668 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
669 $fileName = 'Import_Soft_Credit_Errors.csv';
672 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
673 $fileName = 'Import_Pledge_Payment_Errors.csv';
677 $fileName = parent
::saveFileName($type);