3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
35 abstract class CRM_Contribute_Import_Parser
extends CRM_Import_Parser
{
38 * Contribution-specific result codes
39 * @see CRM_Import_Parser result code constants
41 const SOFT_CREDIT
= 512, SOFT_CREDIT_ERROR
= 1024, PLEDGE_PAYMENT
= 2048, PLEDGE_PAYMENT_ERROR
= 4096;
51 * Seperator being used
53 protected $_seperator;
56 * Total number of lines in file
58 protected $_lineCount;
61 * Running total number of valid soft credit rows
63 protected $_validSoftCreditRowCount;
66 * Running total number of invalid soft credit rows
68 protected $_invalidSoftCreditRowCount;
71 * Running total number of valid pledge payment rows
73 protected $_validPledgePaymentRowCount;
76 * Running total number of invalid pledge payment rows
78 protected $_invalidPledgePaymentRowCount;
81 * Array of pledge payment error lines, bounded by MAX_ERROR
83 protected $_pledgePaymentErrors;
86 * Array of pledge payment error lines, bounded by MAX_ERROR
88 protected $_softCreditErrors;
91 * Filename of pledge payment error data
95 protected $_pledgePaymentErrorsFileName;
98 * Filename of soft credit error data
102 protected $_softCreditErrorsFileName;
105 * Whether the file has a column header or not
109 protected $_haveColumnHeader;
112 * @param string $fileName
113 * @param string $seperator
115 * @param bool $skipColumnHeader
117 * @param int $contactType
118 * @param int $onDuplicate
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(
353 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
354 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
357 if ($this->_invalidPledgePaymentRowCount
) {
358 // removed view url for invlaid contacts
359 $headers = array_merge(array(
365 $this->_pledgePaymentErrorsFileName
= self
::errorFileName(self
::PLEDGE_PAYMENT_ERROR
);
366 self
::exportCSV($this->_pledgePaymentErrorsFileName
, $headers, $this->_pledgePaymentErrors
);
369 if ($this->_invalidSoftCreditRowCount
) {
370 // removed view url for invlaid contacts
371 $headers = array_merge(array(
377 $this->_softCreditErrorsFileName
= self
::errorFileName(self
::SOFT_CREDIT_ERROR
);
378 self
::exportCSV($this->_softCreditErrorsFileName
, $headers, $this->_softCreditErrors
);
381 if ($this->_conflictCount
) {
382 $headers = array_merge(array(
388 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
389 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
391 if ($this->_duplicateCount
) {
392 $headers = array_merge(array(
394 ts('View Contribution URL'),
399 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
400 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
403 return $this->fini();
407 * Given a list of the importable field keys that the user has selected
408 * set the active fields array to this list
410 * @param array $fieldKeys mapped array of values
414 public function setActiveFields($fieldKeys) {
415 $this->_activeFieldCount
= count($fieldKeys);
416 foreach ($fieldKeys as $key) {
417 if (empty($this->_fields
[$key])) {
418 $this->_activeFields
[] = new CRM_Contribute_Import_Field('', ts('- do not import -'));
421 $this->_activeFields
[] = clone($this->_fields
[$key]);
427 * @param array $elements
429 public function setActiveFieldSoftCredit($elements) {
430 for ($i = 0; $i < count($elements); $i++
) {
431 $this->_activeFields
[$i]->_softCreditField
= $elements[$i];
436 * @param array $elements
438 public function setActiveFieldSoftCreditType($elements) {
439 for ($i = 0; $i < count($elements); $i++
) {
440 $this->_activeFields
[$i]->_softCreditType
= $elements[$i];
445 * Format the field values for input to the api.
448 * (reference ) associative array of name/value pairs
450 public function &getActiveFieldParams() {
452 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
453 if (isset($this->_activeFields
[$i]->_value
)) {
454 if (isset($this->_activeFields
[$i]->_softCreditField
)) {
455 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
456 $params[$this->_activeFields
[$i]->_name
] = array();
458 $params[$this->_activeFields
[$i]->_name
][$i][$this->_activeFields
[$i]->_softCreditField
] = $this->_activeFields
[$i]->_value
;
459 if (isset($this->_activeFields
[$i]->_softCreditType
)) {
460 $params[$this->_activeFields
[$i]->_name
][$i]['soft_credit_type_id'] = $this->_activeFields
[$i]->_softCreditType
;
464 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
465 if (!isset($this->_activeFields
[$i]->_softCreditField
)) {
466 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
475 * @param string $name
478 * @param string $headerPattern
479 * @param string $dataPattern
481 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
483 $this->_fields
['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
486 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
487 if (!array_key_exists($name, $tempField)) {
488 $this->_fields
[$name] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
491 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
492 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
499 * Store parser values.
501 * @param CRM_Core_Session $store
505 public function set($store, $mode = self
::MODE_SUMMARY
) {
506 $store->set('fileSize', $this->_fileSize
);
507 $store->set('lineCount', $this->_lineCount
);
508 $store->set('seperator', $this->_seperator
);
509 $store->set('fields', $this->getSelectValues());
510 $store->set('fieldTypes', $this->getSelectTypes());
512 $store->set('headerPatterns', $this->getHeaderPatterns());
513 $store->set('dataPatterns', $this->getDataPatterns());
514 $store->set('columnCount', $this->_activeFieldCount
);
516 $store->set('totalRowCount', $this->_totalCount
);
517 $store->set('validRowCount', $this->_validCount
);
518 $store->set('invalidRowCount', $this->_invalidRowCount
);
519 $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount
);
520 $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount
);
521 $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount
);
522 $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount
);
523 $store->set('conflictRowCount', $this->_conflictCount
);
525 switch ($this->_contactType
) {
527 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
531 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
535 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
538 if ($this->_invalidRowCount
) {
539 $store->set('errorsFileName', $this->_errorFileName
);
541 if ($this->_conflictCount
) {
542 $store->set('conflictsFileName', $this->_conflictFileName
);
544 if (isset($this->_rows
) && !empty($this->_rows
)) {
545 $store->set('dataValues', $this->_rows
);
548 if ($this->_invalidPledgePaymentRowCount
) {
549 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName
);
552 if ($this->_invalidSoftCreditRowCount
) {
553 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName
);
556 if ($mode == self
::MODE_IMPORT
) {
557 $store->set('duplicateRowCount', $this->_duplicateCount
);
558 if ($this->_duplicateCount
) {
559 $store->set('duplicatesFileName', $this->_duplicateFileName
);
565 * Export data to a CSV file.
567 * @param string $fileName
568 * @param array $header
573 public 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.
607 * Error code constant.
611 public static function errorFileName($type) {
617 $config = CRM_Core_Config
::singleton();
618 $fileName = $config->uploadDir
. "sqlImport";
621 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
622 $fileName .= '.softCreditErrors';
625 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
626 $fileName .= '.pledgePaymentErrors';
630 $fileName = parent
::errorFileName($type);
638 * Determines the file name based on error code.
641 * Error code constant.
645 public static function saveFileName($type) {
652 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
653 $fileName = 'Import_Soft_Credit_Errors.csv';
656 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
657 $fileName = 'Import_Pledge_Payment_Errors.csv';
661 $fileName = parent
::saveFileName($type);