Merge pull request #2033 from JoeMurray/master
[civicrm-core.git] / CRM / Contribute / Import / Parser.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36
37 abstract class CRM_Contribute_Import_Parser extends CRM_Import_Parser {
38
39 /**
40 * Contribution-specific result codes
41 * @see CRM_Import_Parser result code constants
42 */
43 CONST SOFT_CREDIT = 512, SOFT_CREDIT_ERROR = 1024, PLEDGE_PAYMENT = 2048, PLEDGE_PAYMENT_ERROR = 4096;
44
45 protected $_fileName;
46
47 /**#@+
48 * @access protected
49 * @var integer
50 */
51
52 /**
53 * imported file size
54 */
55 protected $_fileSize;
56
57 /**
58 * seperator being used
59 */
60 protected $_seperator;
61
62 /**
63 * total number of lines in file
64 */
65 protected $_lineCount;
66
67 /**
68 * running total number of valid soft credit rows
69 */
70 protected $_validSoftCreditRowCount;
71
72 /**
73 * running total number of invalid soft credit rows
74 */
75 protected $_invalidSoftCreditRowCount;
76
77 /**
78 * running total number of valid pledge payment rows
79 */
80 protected $_validPledgePaymentRowCount;
81
82 /**
83 * running total number of invalid pledge payment rows
84 */
85 protected $_invalidPledgePaymentRowCount;
86
87 /**
88 * array of pledge payment error lines, bounded by MAX_ERROR
89 */
90 protected $_pledgePaymentErrors;
91
92 /**
93 * array of pledge payment error lines, bounded by MAX_ERROR
94 */
95 protected $_softCreditErrors;
96
97 /**
98 * filename of pledge payment error data
99 *
100 * @var string
101 */
102 protected $_pledgePaymentErrorsFileName;
103
104 /**
105 * filename of soft credit error data
106 *
107 * @var string
108 */
109 protected $_softCreditErrorsFileName;
110
111 /**
112 * whether the file has a column header or not
113 *
114 * @var boolean
115 */
116 protected $_haveColumnHeader;
117
118 function run($fileName,
119 $seperator = ',',
120 &$mapper,
121 $skipColumnHeader = FALSE,
122 $mode = self::MODE_PREVIEW,
123 $contactType = self::CONTACT_INDIVIDUAL,
124 $onDuplicate = self::DUPLICATE_SKIP
125 ) {
126 if (!is_array($fileName)) {
127 CRM_Core_Error::fatal();
128 }
129 $fileName = $fileName['name'];
130
131 switch ($contactType) {
132 case self::CONTACT_INDIVIDUAL:
133 $this->_contactType = 'Individual';
134 break;
135
136 case self::CONTACT_HOUSEHOLD:
137 $this->_contactType = 'Household';
138 break;
139
140 case self::CONTACT_ORGANIZATION:
141 $this->_contactType = 'Organization';
142 }
143
144 $this->init();
145
146 $this->_haveColumnHeader = $skipColumnHeader;
147
148 $this->_seperator = $seperator;
149
150 $fd = fopen($fileName, "r");
151 if (!$fd) {
152 return FALSE;
153 }
154
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;
158
159 $this->_errors = array();
160 $this->_warnings = array();
161 $this->_conflicts = array();
162 $this->_pledgePaymentErrors = array();
163 $this->_softCreditErrors = array();
164
165 $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2);
166
167 if ($mode == self::MODE_MAPFIELD) {
168 $this->_rows = array();
169 }
170 else {
171 $this->_activeFieldCount = count($this->_activeFields);
172 }
173
174 while (!feof($fd)) {
175 $this->_lineCount++;
176
177 $values = fgetcsv($fd, 8192, $seperator);
178 if (!$values) {
179 continue;
180 }
181
182 self::encloseScrub($values);
183
184 // skip column header if we're not in mapfield mode
185 if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) {
186 $skipColumnHeader = FALSE;
187 continue;
188 }
189
190 /* trim whitespace around the values */
191
192 $empty = TRUE;
193 foreach ($values as $k => $v) {
194 $values[$k] = trim($v, " \t\r\n");
195 }
196
197 if (CRM_Utils_System::isNull($values)) {
198 continue;
199 }
200
201 $this->_totalCount++;
202
203 if ($mode == self::MODE_MAPFIELD) {
204 $returnCode = $this->mapField($values);
205 }
206 elseif ($mode == self::MODE_PREVIEW) {
207 $returnCode = $this->preview($values);
208 }
209 elseif ($mode == self::MODE_SUMMARY) {
210 $returnCode = $this->summary($values);
211 }
212 elseif ($mode == self::MODE_IMPORT) {
213 $returnCode = $this->import($onDuplicate, $values);
214 }
215 else {
216 $returnCode = self::ERROR;
217 }
218
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));
225 }
226 }
227
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));
234 }
235 }
236
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));
243 }
244 }
245
246 if ($returnCode == self::WARNING) {
247 $this->_warningCount++;
248 if ($this->_warningCount < $this->_maxWarningCount) {
249 $this->_warningCount[] = $line;
250 }
251 }
252
253 if ($returnCode == self::ERROR) {
254 $this->_invalidRowCount++;
255 if ($this->_invalidRowCount < $this->_maxErrorCount) {
256 $recordNumber = $this->_lineCount;
257 if ($this->_haveColumnHeader) {
258 $recordNumber--;
259 }
260 array_unshift($values, $recordNumber);
261 $this->_errors[] = $values;
262 }
263 }
264
265 if ($returnCode == self::PLEDGE_PAYMENT_ERROR) {
266 $this->_invalidPledgePaymentRowCount++;
267 if ($this->_invalidPledgePaymentRowCount < $this->_maxErrorCount) {
268 $recordNumber = $this->_lineCount;
269 if ($this->_haveColumnHeader) {
270 $recordNumber--;
271 }
272 array_unshift($values, $recordNumber);
273 $this->_pledgePaymentErrors[] = $values;
274 }
275 }
276
277 if ($returnCode == self::SOFT_CREDIT_ERROR) {
278 $this->_invalidSoftCreditRowCount++;
279 if ($this->_invalidSoftCreditRowCount < $this->_maxErrorCount) {
280 $recordNumber = $this->_lineCount;
281 if ($this->_haveColumnHeader) {
282 $recordNumber--;
283 }
284 array_unshift($values, $recordNumber);
285 $this->_softCreditErrors[] = $values;
286 }
287 }
288
289 if ($returnCode == self::CONFLICT) {
290 $this->_conflictCount++;
291 $recordNumber = $this->_lineCount;
292 if ($this->_haveColumnHeader) {
293 $recordNumber--;
294 }
295 array_unshift($values, $recordNumber);
296 $this->_conflicts[] = $values;
297 }
298
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 */
303 }
304 $this->_duplicateCount++;
305 $recordNumber = $this->_lineCount;
306 if ($this->_haveColumnHeader) {
307 $recordNumber--;
308 }
309 array_unshift($values, $recordNumber);
310 $this->_duplicates[] = $values;
311 if ($onDuplicate != self::DUPLICATE_SKIP) {
312 $this->_validCount++;
313 }
314 }
315
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) {
319 break;
320 }
321
322 // if we are done processing the maxNumber of lines, break
323 if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) {
324 break;
325 }
326 }
327
328 fclose($fd);
329
330 if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) {
331 $customHeaders = $mapper;
332
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];
337 }
338 }
339 if ($this->_invalidRowCount) {
340 // removed view url for invlaid contacts
341 $headers = array_merge(array(ts('Line Number'),
342 ts('Reason'),
343 ),
344 $customHeaders
345 );
346 $this->_errorFileName = self::errorFileName(self::ERROR);
347 self::exportCSV($this->_errorFileName, $headers, $this->_errors);
348 }
349
350 if ($this->_invalidPledgePaymentRowCount) {
351 // removed view url for invlaid contacts
352 $headers = array_merge(array(ts('Line Number'),
353 ts('Reason'),
354 ),
355 $customHeaders
356 );
357 $this->_pledgePaymentErrorsFileName = self::errorFileName(self::PLEDGE_PAYMENT_ERROR);
358 self::exportCSV($this->_pledgePaymentErrorsFileName, $headers, $this->_pledgePaymentErrors);
359 }
360
361 if ($this->_invalidSoftCreditRowCount) {
362 // removed view url for invlaid contacts
363 $headers = array_merge(array(ts('Line Number'),
364 ts('Reason'),
365 ),
366 $customHeaders
367 );
368 $this->_softCreditErrorsFileName = self::errorFileName(self::SOFT_CREDIT_ERROR);
369 self::exportCSV($this->_softCreditErrorsFileName, $headers, $this->_softCreditErrors);
370 }
371
372 if ($this->_conflictCount) {
373 $headers = array_merge(array(ts('Line Number'),
374 ts('Reason'),
375 ),
376 $customHeaders
377 );
378 $this->_conflictFileName = self::errorFileName(self::CONFLICT);
379 self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts);
380 }
381 if ($this->_duplicateCount) {
382 $headers = array_merge(array(ts('Line Number'),
383 ts('View Contribution URL'),
384 ),
385 $customHeaders
386 );
387
388 $this->_duplicateFileName = self::errorFileName(self::DUPLICATE);
389 self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates);
390 }
391 }
392 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
393 return $this->fini();
394 }
395
396 /**
397 * Given a list of the importable field keys that the user has selected
398 * set the active fields array to this list
399 *
400 * @param array mapped array of values
401 *
402 pppp * @return void
403 * @access public
404 */
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 -'));
410 }
411 else {
412 $this->_activeFields[] = clone($this->_fields[$key]);
413 }
414 }
415 }
416
417 function setActiveFieldSoftCredit($elements) {
418 for ($i = 0; $i < count($elements); $i++) {
419 $this->_activeFields[$i]->_softCreditField = $elements[$i];
420 }
421 }
422
423 /**
424 * function to format the field values for input to the api
425 *
426 * @return array (reference ) associative array of name/value pairs
427 * @access public
428 */
429 function &getActiveFieldParams() {
430 $params = array();
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();
436 }
437 $params[$this->_activeFields[$i]->_name][$this->_activeFields[$i]->_softCreditField] = $this->_activeFields[$i]->_value;
438 }
439
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;
443 }
444 }
445 }
446 }
447 return $params;
448 }
449
450 function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') {
451 if (empty($name)) {
452 $this->_fields['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
453 }
454 else {
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);
458 }
459 else {
460 $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
461 CRM_Utils_Array::value('hasLocationType', $tempField[$name])
462 );
463 }
464 }
465 }
466
467 /**
468 * Store parser values
469 *
470 * @param CRM_Core_Session $store
471 *
472 * @return void
473 * @access public
474 */
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());
481
482 $store->set('headerPatterns', $this->getHeaderPatterns());
483 $store->set('dataPatterns', $this->getDataPatterns());
484 $store->set('columnCount', $this->_activeFieldCount);
485
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);
494
495 switch ($this->_contactType) {
496 case 'Individual':
497 $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
498 break;
499
500 case 'Household':
501 $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
502 break;
503
504 case 'Organization':
505 $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
506 }
507
508 if ($this->_invalidRowCount) {
509 $store->set('errorsFileName', $this->_errorFileName);
510 }
511 if ($this->_conflictCount) {
512 $store->set('conflictsFileName', $this->_conflictFileName);
513 }
514 if (isset($this->_rows) && !empty($this->_rows)) {
515 $store->set('dataValues', $this->_rows);
516 }
517
518 if ($this->_invalidPledgePaymentRowCount) {
519 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName);
520 }
521
522 if ($this->_invalidSoftCreditRowCount) {
523 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName);
524 }
525
526 if ($mode == self::MODE_IMPORT) {
527 $store->set('duplicateRowCount', $this->_duplicateCount);
528 if ($this->_duplicateCount) {
529 $store->set('duplicatesFileName', $this->_duplicateFileName);
530 }
531 }
532 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
533 }
534
535 /**
536 * Export data to a CSV file
537 *
538 * @param string $filename
539 * @param array $header
540 * @param data $data
541 *
542 * @return void
543 * @access public
544 */
545 static function exportCSV($fileName, $header, $data) {
546 $output = array();
547 $fd = fopen($fileName, 'w');
548
549 foreach ($header as $key => $value) {
550 $header[$key] = "\"$value\"";
551 }
552 $config = CRM_Core_Config::singleton();
553 $output[] = implode($config->fieldSeparator, $header);
554
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') {
560 continue;
561 }
562 $datum[$k1] = $v1;
563 }
564 }
565 else {
566 $datum[$key] = "\"$value\"";
567 }
568 }
569 $output[] = implode($config->fieldSeparator, $datum);
570 }
571 fwrite($fd, implode("\n", $output));
572 fclose($fd);
573 }
574
575 static function errorFileName($type) {
576 $fileName = NULL;
577 if (empty($type)) {
578 return $fileName;
579 }
580
581 $config = CRM_Core_Config::singleton();
582 $fileName = $config->uploadDir . "sqlImport";
583
584 switch ($type) {
585 case CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR:
586 $fileName .= '.softCreditErrors';
587 break;
588
589 case CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR:
590 $fileName .= '.pledgePaymentErrors';
591 break;
592
593 default:
594 $fileName = parent::errorFileName($type);
595 break;
596 }
597
598 return $fileName;
599 }
600
601 static function saveFileName($type) {
602 $fileName = NULL;
603 if (empty($type)) {
604 return $fileName;
605 }
606
607 switch ($type) {
608 case CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR:
609 $fileName = 'Import_Soft_Credit_Errors.csv';
610 break;
611
612 case CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR:
613 $fileName = 'Import_Pledge_Payment_Errors.csv';
614 break;
615
616 default:
617 $fileName = parent::saveFileName($type);
618 break;
619 }
620
621 return $fileName;
622 }
623 }
624