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;
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];
423 function setActiveFieldSoftCreditType($elements) {
424 for ($i = 0; $i < count($elements); $i++
) {
425 $this->_activeFields
[$i]->_softCreditType
= $elements[$i];
429 * function to format the field values for input to the api
431 * @return array (reference ) associative array of name/value pairs
434 function &getActiveFieldParams() {
436 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
437 if (isset($this->_activeFields
[$i]->_value
)) {
438 if (isset($this->_activeFields
[$i]->_softCreditField
)) {
439 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
440 $params[$this->_activeFields
[$i]->_name
] = array();
442 $params[$this->_activeFields
[$i]->_name
][$i][$this->_activeFields
[$i]->_softCreditField
] = $this->_activeFields
[$i]->_value
;
443 if(isset($this->_activeFields
[$i]->_softCreditType
)){
444 $params[$this->_activeFields
[$i]->_name
][$i]['soft_credit_type_id'] = $this->_activeFields
[$i]->_softCreditType
;
448 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
449 if (!isset($this->_activeFields
[$i]->_softCreditField
)) {
450 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
458 function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
460 $this->_fields
['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
463 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
464 if (!array_key_exists($name, $tempField)) {
465 $this->_fields
[$name] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
468 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
469 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
476 * Store parser values
478 * @param CRM_Core_Session $store
483 function set($store, $mode = self
::MODE_SUMMARY
) {
484 $store->set('fileSize', $this->_fileSize
);
485 $store->set('lineCount', $this->_lineCount
);
486 $store->set('seperator', $this->_seperator
);
487 $store->set('fields', $this->getSelectValues());
488 $store->set('fieldTypes', $this->getSelectTypes());
490 $store->set('headerPatterns', $this->getHeaderPatterns());
491 $store->set('dataPatterns', $this->getDataPatterns());
492 $store->set('columnCount', $this->_activeFieldCount
);
494 $store->set('totalRowCount', $this->_totalCount
);
495 $store->set('validRowCount', $this->_validCount
);
496 $store->set('invalidRowCount', $this->_invalidRowCount
);
497 $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount
);
498 $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount
);
499 $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount
);
500 $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount
);
501 $store->set('conflictRowCount', $this->_conflictCount
);
503 switch ($this->_contactType
) {
505 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
509 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
513 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
516 if ($this->_invalidRowCount
) {
517 $store->set('errorsFileName', $this->_errorFileName
);
519 if ($this->_conflictCount
) {
520 $store->set('conflictsFileName', $this->_conflictFileName
);
522 if (isset($this->_rows
) && !empty($this->_rows
)) {
523 $store->set('dataValues', $this->_rows
);
526 if ($this->_invalidPledgePaymentRowCount
) {
527 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName
);
530 if ($this->_invalidSoftCreditRowCount
) {
531 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName
);
534 if ($mode == self
::MODE_IMPORT
) {
535 $store->set('duplicateRowCount', $this->_duplicateCount
);
536 if ($this->_duplicateCount
) {
537 $store->set('duplicatesFileName', $this->_duplicateFileName
);
540 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
544 * Export data to a CSV file
546 * @param string $filename
547 * @param array $header
553 static function exportCSV($fileName, $header, $data) {
555 $fd = fopen($fileName, 'w');
557 foreach ($header as $key => $value) {
558 $header[$key] = "\"$value\"";
560 $config = CRM_Core_Config
::singleton();
561 $output[] = implode($config->fieldSeparator
, $header);
563 foreach ($data as $datum) {
564 foreach ($datum as $key => $value) {
565 if (isset($value[0]) && is_array($value)) {
566 foreach ($value[0] as $k1 => $v1) {
567 if ($k1 == 'location_type_id') {
574 $datum[$key] = "\"$value\"";
577 $output[] = implode($config->fieldSeparator
, $datum);
579 fwrite($fd, implode("\n", $output));
583 static function errorFileName($type) {
589 $config = CRM_Core_Config
::singleton();
590 $fileName = $config->uploadDir
. "sqlImport";
593 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
594 $fileName .= '.softCreditErrors';
597 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
598 $fileName .= '.pledgePaymentErrors';
602 $fileName = parent
::errorFileName($type);
609 static function saveFileName($type) {
616 case CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
:
617 $fileName = 'Import_Soft_Credit_Errors.csv';
620 case CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
:
621 $fileName = 'Import_Pledge_Payment_Errors.csv';
625 $fileName = parent
::saveFileName($type);