3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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;
118 function run($fileName,
121 $skipColumnHeader = FALSE,
122 $mode = self
::MODE_PREVIEW
,
123 $contactType = self
::CONTACT_INDIVIDUAL
,
124 $onDuplicate = self
::DUPLICATE_SKIP
126 if (!is_array($fileName)) {
127 CRM_Core_Error
::fatal();
129 $fileName = $fileName['name'];
131 switch ($contactType) {
132 case self
::CONTACT_INDIVIDUAL
:
133 $this->_contactType
= 'Individual';
136 case self
::CONTACT_HOUSEHOLD
:
137 $this->_contactType
= 'Household';
140 case self
::CONTACT_ORGANIZATION
:
141 $this->_contactType
= 'Organization';
146 $this->_haveColumnHeader
= $skipColumnHeader;
148 $this->_seperator
= $seperator;
150 $fd = fopen($fileName, "r");
155 $this->_lineCount
= $this->_warningCount
= $this->_validSoftCreditRowCount
= $this->_validPledgePaymentRowCount
= 0;
156 $this->_invalidRowCount
= $this->_validCount
= $this->_invalidSoftCreditRowCount
= $this->_invalidPledgePaymentRowCount
= 0;
157 $this->_totalCount
= $this->_conflictCount
= 0;
159 $this->_errors
= array();
160 $this->_warnings
= array();
161 $this->_conflicts
= array();
162 $this->_pledgePaymentErrors
= array();
163 $this->_softCreditErrors
= array();
165 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
167 if ($mode == self
::MODE_MAPFIELD
) {
168 $this->_rows
= array();
171 $this->_activeFieldCount
= count($this->_activeFields
);
177 $values = fgetcsv($fd, 8192, $seperator);
182 self
::encloseScrub($values);
184 // skip column header if we're not in mapfield mode
185 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
186 $skipColumnHeader = FALSE;
190 /* trim whitespace around the values */
193 foreach ($values as $k => $v) {
194 $values[$k] = trim($v, " \t\r\n");
197 if (CRM_Utils_System
::isNull($values)) {
201 $this->_totalCount++
;
203 if ($mode == self
::MODE_MAPFIELD
) {
204 $returnCode = $this->mapField($values);
206 elseif ($mode == self
::MODE_PREVIEW
) {
207 $returnCode = $this->preview($values);
209 elseif ($mode == self
::MODE_SUMMARY
) {
210 $returnCode = $this->summary($values);
212 elseif ($mode == self
::MODE_IMPORT
) {
213 $returnCode = $this->import($onDuplicate, $values);
216 $returnCode = self
::ERROR
;
219 // note that a line could be valid but still produce a warning
220 if ($returnCode == self
::VALID
) {
221 $this->_validCount++
;
222 if ($mode == self
::MODE_MAPFIELD
) {
223 $this->_rows
[] = $values;
224 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
228 if ($returnCode == self
::SOFT_CREDIT
) {
229 $this->_validSoftCreditRowCount++
;
230 $this->_validCount++
;
231 if ($mode == self
::MODE_MAPFIELD
) {
232 $this->_rows
[] = $values;
233 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
237 if ($returnCode == self
::PLEDGE_PAYMENT
) {
238 $this->_validPledgePaymentRowCount++
;
239 $this->_validCount++
;
240 if ($mode == self
::MODE_MAPFIELD
) {
241 $this->_rows
[] = $values;
242 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
246 if ($returnCode == self
::WARNING
) {
247 $this->_warningCount++
;
248 if ($this->_warningCount
< $this->_maxWarningCount
) {
249 $this->_warningCount
[] = $line;
253 if ($returnCode == self
::ERROR
) {
254 $this->_invalidRowCount++
;
255 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
256 $recordNumber = $this->_lineCount
;
257 if ($this->_haveColumnHeader
) {
260 array_unshift($values, $recordNumber);
261 $this->_errors
[] = $values;
265 if ($returnCode == self
::PLEDGE_PAYMENT_ERROR
) {
266 $this->_invalidPledgePaymentRowCount++
;
267 if ($this->_invalidPledgePaymentRowCount
< $this->_maxErrorCount
) {
268 $recordNumber = $this->_lineCount
;
269 if ($this->_haveColumnHeader
) {
272 array_unshift($values, $recordNumber);
273 $this->_pledgePaymentErrors
[] = $values;
277 if ($returnCode == self
::SOFT_CREDIT_ERROR
) {
278 $this->_invalidSoftCreditRowCount++
;
279 if ($this->_invalidSoftCreditRowCount
< $this->_maxErrorCount
) {
280 $recordNumber = $this->_lineCount
;
281 if ($this->_haveColumnHeader
) {
284 array_unshift($values, $recordNumber);
285 $this->_softCreditErrors
[] = $values;
289 if ($returnCode == self
::CONFLICT
) {
290 $this->_conflictCount++
;
291 $recordNumber = $this->_lineCount
;
292 if ($this->_haveColumnHeader
) {
295 array_unshift($values, $recordNumber);
296 $this->_conflicts
[] = $values;
299 if ($returnCode == self
::DUPLICATE
) {
300 if ($returnCode == self
::MULTIPLE_DUPE
) {
301 /* TODO: multi-dupes should be counted apart from singles
302 * on non-skip action */
304 $this->_duplicateCount++
;
305 $recordNumber = $this->_lineCount
;
306 if ($this->_haveColumnHeader
) {
309 array_unshift($values, $recordNumber);
310 $this->_duplicates
[] = $values;
311 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
312 $this->_validCount++
;
316 // we give the derived class a way of aborting the process
317 // note that the return code could be multiple code or'ed together
318 if ($returnCode == self
::STOP
) {
322 // if we are done processing the maxNumber of lines, break
323 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
330 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
331 $customHeaders = $mapper;
333 $customfields = CRM_Core_BAO_CustomField
::getFields('Contribution');
334 foreach ($customHeaders as $key => $value) {
335 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
336 $customHeaders[$key] = $customfields[$id][0];
339 if ($this->_invalidRowCount
) {
340 // removed view url for invlaid contacts
341 $headers = array_merge(array(ts('Line Number'),
346 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
347 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
350 if ($this->_invalidPledgePaymentRowCount
) {
351 // removed view url for invlaid contacts
352 $headers = array_merge(array(ts('Line Number'),
357 $this->_pledgePaymentErrorsFileName
= self
::errorFileName(self
::PLEDGE_PAYMENT_ERROR
);
358 self
::exportCSV($this->_pledgePaymentErrorsFileName
, $headers, $this->_pledgePaymentErrors
);
361 if ($this->_invalidSoftCreditRowCount
) {
362 // removed view url for invlaid contacts
363 $headers = array_merge(array(ts('Line Number'),
368 $this->_softCreditErrorsFileName
= self
::errorFileName(self
::SOFT_CREDIT_ERROR
);
369 self
::exportCSV($this->_softCreditErrorsFileName
, $headers, $this->_softCreditErrors
);
372 if ($this->_conflictCount
) {
373 $headers = array_merge(array(ts('Line Number'),
378 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
379 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
381 if ($this->_duplicateCount
) {
382 $headers = array_merge(array(ts('Line Number'),
383 ts('View Contribution URL'),
388 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
389 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
392 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
393 return $this->fini();
397 * Given a list of the importable field keys that the user has selected
398 * set the active fields array to this list
400 * @param array mapped array of values
405 function setActiveFields($fieldKeys) {
406 $this->_activeFieldCount
= count($fieldKeys);
407 foreach ($fieldKeys as $key) {
408 if (empty($this->_fields
[$key])) {
409 $this->_activeFields
[] = new CRM_Contribute_Import_Field('', ts('- do not import -'));
412 $this->_activeFields
[] = clone($this->_fields
[$key]);
417 function setActiveFieldSoftCredit($elements) {
418 for ($i = 0; $i < count($elements); $i++
) {
419 $this->_activeFields
[$i]->_softCreditField
= $elements[$i];
424 * function to format the field values for input to the api
426 * @return array (reference ) associative array of name/value pairs
429 function &getActiveFieldParams() {
431 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
432 if (isset($this->_activeFields
[$i]->_value
)) {
433 if (isset($this->_activeFields
[$i]->_softCreditField
)) {
434 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
435 $params[$this->_activeFields
[$i]->_name
] = array();
437 $params[$this->_activeFields
[$i]->_name
][$this->_activeFields
[$i]->_softCreditField
] = $this->_activeFields
[$i]->_value
;
440 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
441 if (!isset($this->_activeFields
[$i]->_softCreditField
)) {
442 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
450 function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
452 $this->_fields
['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
455 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
456 if (!array_key_exists($name, $tempField)) {
457 $this->_fields
[$name] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
460 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
461 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
468 * Store parser values
470 * @param CRM_Core_Session $store
475 function set($store, $mode = self
::MODE_SUMMARY
) {
476 $store->set('fileSize', $this->_fileSize
);
477 $store->set('lineCount', $this->_lineCount
);
478 $store->set('seperator', $this->_seperator
);
479 $store->set('fields', $this->getSelectValues());
480 $store->set('fieldTypes', $this->getSelectTypes());
482 $store->set('headerPatterns', $this->getHeaderPatterns());
483 $store->set('dataPatterns', $this->getDataPatterns());
484 $store->set('columnCount', $this->_activeFieldCount
);
486 $store->set('totalRowCount', $this->_totalCount
);
487 $store->set('validRowCount', $this->_validCount
);
488 $store->set('invalidRowCount', $this->_invalidRowCount
);
489 $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount
);
490 $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount
);
491 $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount
);
492 $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount
);
493 $store->set('conflictRowCount', $this->_conflictCount
);
495 switch ($this->_contactType
) {
497 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
501 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
505 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
508 if ($this->_invalidRowCount
) {
509 $store->set('errorsFileName', $this->_errorFileName
);
511 if ($this->_conflictCount
) {
512 $store->set('conflictsFileName', $this->_conflictFileName
);
514 if (isset($this->_rows
) && !empty($this->_rows
)) {
515 $store->set('dataValues', $this->_rows
);
518 if ($this->_invalidPledgePaymentRowCount
) {
519 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName
);
522 if ($this->_invalidSoftCreditRowCount
) {
523 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName
);
526 if ($mode == self
::MODE_IMPORT
) {
527 $store->set('duplicateRowCount', $this->_duplicateCount
);
528 if ($this->_duplicateCount
) {
529 $store->set('duplicatesFileName', $this->_duplicateFileName
);
532 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
536 * Export data to a CSV file
538 * @param string $filename
539 * @param array $header
545 static function exportCSV($fileName, $header, $data) {
547 $fd = fopen($fileName, 'w');
549 foreach ($header as $key => $value) {
550 $header[$key] = "\"$value\"";
552 $config = CRM_Core_Config
::singleton();
553 $output[] = implode($config->fieldSeparator
, $header);
555 foreach ($data as $datum) {
556 foreach ($datum as $key => $value) {
557 if (is_array($value[0])) {
558 foreach ($value[0] as $k1 => $v1) {
559 if ($k1 == 'location_type_id') {
566 $datum[$key] = "\"$value\"";
569 $output[] = implode($config->fieldSeparator
, $datum);
571 fwrite($fd, implode("\n", $output));
575 static function errorFileName($type) {
581 $config = CRM_Core_Config
::singleton();
582 $fileName = $config->uploadDir
. "sqlImport";
585 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
586 $fileName .= '.softCreditErrors';
589 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
590 $fileName .= '.pledgePaymentErrors';
594 $fileName = parent
::errorFileName($type);
601 static function saveFileName($type) {
608 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
609 $fileName = 'Import_Soft_Credit_Errors.csv';
612 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
613 $fileName = 'Import_Pledge_Payment_Errors.csv';
617 $fileName = parent
::saveFileName($type);