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
36 abstract class CRM_Contribute_Import_Parser
extends CRM_Import_Parser
{
39 * Contribution-specific result codes
40 * @see CRM_Import_Parser result code constants
42 CONST SOFT_CREDIT
= 512, SOFT_CREDIT_ERROR
= 1024, PLEDGE_PAYMENT
= 2048, PLEDGE_PAYMENT_ERROR
= 4096;
52 * Seperator being used
54 protected $_seperator;
57 * Total number of lines in file
59 protected $_lineCount;
62 * Running total number of valid soft credit rows
64 protected $_validSoftCreditRowCount;
67 * Running total number of invalid soft credit rows
69 protected $_invalidSoftCreditRowCount;
72 * Running total number of valid pledge payment rows
74 protected $_validPledgePaymentRowCount;
77 * Running total number of invalid pledge payment rows
79 protected $_invalidPledgePaymentRowCount;
82 * Array of pledge payment error lines, bounded by MAX_ERROR
84 protected $_pledgePaymentErrors;
87 * Array of pledge payment error lines, bounded by MAX_ERROR
89 protected $_softCreditErrors;
92 * Filename of pledge payment error data
96 protected $_pledgePaymentErrorsFileName;
99 * Filename of soft credit error data
103 protected $_softCreditErrorsFileName;
106 * Whether the file has a column header or not
110 protected $_haveColumnHeader;
113 * @param string $fileName
114 * @param string $seperator
116 * @param bool $skipColumnHeader
118 * @param int $contactType
119 * @param int $onDuplicate
124 function run($fileName,
127 $skipColumnHeader = FALSE,
128 $mode = self
::MODE_PREVIEW
,
129 $contactType = self
::CONTACT_INDIVIDUAL
,
130 $onDuplicate = self
::DUPLICATE_SKIP
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;
165 $this->_errors
= array();
166 $this->_warnings
= array();
167 $this->_conflicts
= array();
168 $this->_pledgePaymentErrors
= array();
169 $this->_softCreditErrors
= array();
171 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
173 if ($mode == self
::MODE_MAPFIELD
) {
174 $this->_rows
= array();
177 $this->_activeFieldCount
= count($this->_activeFields
);
183 $values = fgetcsv($fd, 8192, $seperator);
188 self
::encloseScrub($values);
190 // skip column header if we're not in mapfield mode
191 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
192 $skipColumnHeader = FALSE;
196 /* trim whitespace around the values */
199 foreach ($values as $k => $v) {
200 $values[$k] = trim($v, " \t\r\n");
203 if (CRM_Utils_System
::isNull($values)) {
207 $this->_totalCount++
;
209 if ($mode == self
::MODE_MAPFIELD
) {
210 $returnCode = $this->mapField($values);
212 elseif ($mode == self
::MODE_PREVIEW
) {
213 $returnCode = $this->preview($values);
215 elseif ($mode == self
::MODE_SUMMARY
) {
216 $returnCode = $this->summary($values);
218 elseif ($mode == self
::MODE_IMPORT
) {
219 $returnCode = $this->import($onDuplicate, $values);
222 $returnCode = self
::ERROR
;
225 // note that a line could be valid but still produce a warning
226 if ($returnCode == self
::VALID
) {
227 $this->_validCount++
;
228 if ($mode == self
::MODE_MAPFIELD
) {
229 $this->_rows
[] = $values;
230 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
234 if ($returnCode == self
::SOFT_CREDIT
) {
235 $this->_validSoftCreditRowCount++
;
236 $this->_validCount++
;
237 if ($mode == self
::MODE_MAPFIELD
) {
238 $this->_rows
[] = $values;
239 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
243 if ($returnCode == self
::PLEDGE_PAYMENT
) {
244 $this->_validPledgePaymentRowCount++
;
245 $this->_validCount++
;
246 if ($mode == self
::MODE_MAPFIELD
) {
247 $this->_rows
[] = $values;
248 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
252 if ($returnCode == self
::WARNING
) {
253 $this->_warningCount++
;
254 if ($this->_warningCount
< $this->_maxWarningCount
) {
255 $this->_warningCount
[] = $line;
259 if ($returnCode == self
::ERROR
) {
260 $this->_invalidRowCount++
;
261 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
262 $recordNumber = $this->_lineCount
;
263 if ($this->_haveColumnHeader
) {
266 array_unshift($values, $recordNumber);
267 $this->_errors
[] = $values;
271 if ($returnCode == self
::PLEDGE_PAYMENT_ERROR
) {
272 $this->_invalidPledgePaymentRowCount++
;
273 if ($this->_invalidPledgePaymentRowCount
< $this->_maxErrorCount
) {
274 $recordNumber = $this->_lineCount
;
275 if ($this->_haveColumnHeader
) {
278 array_unshift($values, $recordNumber);
279 $this->_pledgePaymentErrors
[] = $values;
283 if ($returnCode == self
::SOFT_CREDIT_ERROR
) {
284 $this->_invalidSoftCreditRowCount++
;
285 if ($this->_invalidSoftCreditRowCount
< $this->_maxErrorCount
) {
286 $recordNumber = $this->_lineCount
;
287 if ($this->_haveColumnHeader
) {
290 array_unshift($values, $recordNumber);
291 $this->_softCreditErrors
[] = $values;
295 if ($returnCode == self
::CONFLICT
) {
296 $this->_conflictCount++
;
297 $recordNumber = $this->_lineCount
;
298 if ($this->_haveColumnHeader
) {
301 array_unshift($values, $recordNumber);
302 $this->_conflicts
[] = $values;
305 if ($returnCode == self
::DUPLICATE
) {
306 if ($returnCode == self
::MULTIPLE_DUPE
) {
307 /* TODO: multi-dupes should be counted apart from singles
308 * on non-skip action */
310 $this->_duplicateCount++
;
311 $recordNumber = $this->_lineCount
;
312 if ($this->_haveColumnHeader
) {
315 array_unshift($values, $recordNumber);
316 $this->_duplicates
[] = $values;
317 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
318 $this->_validCount++
;
322 // we give the derived class a way of aborting the process
323 // note that the return code could be multiple code or'ed together
324 if ($returnCode == self
::STOP
) {
328 // if we are done processing the maxNumber of lines, break
329 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
336 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
337 $customHeaders = $mapper;
339 $customfields = CRM_Core_BAO_CustomField
::getFields('Contribution');
340 foreach ($customHeaders as $key => $value) {
341 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
342 $customHeaders[$key] = $customfields[$id][0];
345 if ($this->_invalidRowCount
) {
346 // removed view url for invlaid contacts
347 $headers = array_merge(array(ts('Line Number'),
352 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
353 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
356 if ($this->_invalidPledgePaymentRowCount
) {
357 // removed view url for invlaid contacts
358 $headers = array_merge(array(ts('Line Number'),
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(ts('Line Number'),
374 $this->_softCreditErrorsFileName
= self
::errorFileName(self
::SOFT_CREDIT_ERROR
);
375 self
::exportCSV($this->_softCreditErrorsFileName
, $headers, $this->_softCreditErrors
);
378 if ($this->_conflictCount
) {
379 $headers = array_merge(array(ts('Line Number'),
384 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
385 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
387 if ($this->_duplicateCount
) {
388 $headers = array_merge(array(ts('Line Number'),
389 ts('View Contribution URL'),
394 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
395 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
398 return $this->fini();
402 * Given a list of the importable field keys that the user has selected
403 * set the active fields array to this list
405 * @param array mapped array of values
410 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 function setActiveFieldSoftCredit($elements) {
426 for ($i = 0; $i < count($elements); $i++
) {
427 $this->_activeFields
[$i]->_softCreditField
= $elements[$i];
432 * @param array $elements
434 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
443 * @return array (reference ) associative array of name/value pairs
446 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 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
504 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
573 static function exportCSV($fileName, $header, $data) {
575 $fd = fopen($fileName, 'w');
577 foreach ($header as $key => $value) {
578 $header[$key] = "\"$value\"";
580 $config = CRM_Core_Config
::singleton();
581 $output[] = implode($config->fieldSeparator
, $header);
583 foreach ($data as $datum) {
584 foreach ($datum as $key => $value) {
585 if (isset($value[0]) && is_array($value)) {
586 foreach ($value[0] as $k1 => $v1) {
587 if ($k1 == 'location_type_id') {
594 $datum[$key] = "\"$value\"";
597 $output[] = implode($config->fieldSeparator
, $datum);
599 fwrite($fd, implode("\n", $output));
604 * Determines the file extension based on error code
606 * @param int $type error code constant
610 static function errorFileName($type) {
616 $config = CRM_Core_Config
::singleton();
617 $fileName = $config->uploadDir
. "sqlImport";
620 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
621 $fileName .= '.softCreditErrors';
624 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
625 $fileName .= '.pledgePaymentErrors';
629 $fileName = parent
::errorFileName($type);
637 * Determines the file name based on error code
639 * @param int $type error code constant
643 static function saveFileName($type) {
650 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
651 $fileName = 'Import_Soft_Credit_Errors.csv';
654 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
655 $fileName = 'Import_Pledge_Payment_Errors.csv';
659 $fileName = parent
::saveFileName($type);