Merge pull request #21878 from mariav0/patch-16
[civicrm-core.git] / CRM / Import / Parser.php
CommitLineData
ec3811b1
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
ec3811b1 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
ec3811b1 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
ec3811b1
CW
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
ec3811b1 16 */
ec3811b1
CW
17abstract class CRM_Import_Parser {
18 /**
19 * Settings
20 */
ca2057ea 21 const MAX_WARNINGS = 25, DEFAULT_TIMEOUT = 30;
ec3811b1
CW
22
23 /**
24 * Return codes
25 */
7da04cde 26 const VALID = 1, WARNING = 2, ERROR = 4, CONFLICT = 8, STOP = 16, DUPLICATE = 32, MULTIPLE_DUPE = 64, NO_MATCH = 128, UNPARSED_ADDRESS_WARNING = 256;
ec3811b1
CW
27
28 /**
29 * Parser modes
30 */
7da04cde 31 const MODE_MAPFIELD = 1, MODE_PREVIEW = 2, MODE_SUMMARY = 4, MODE_IMPORT = 8;
ec3811b1
CW
32
33 /**
34 * Codes for duplicate record handling
35 */
7da04cde 36 const DUPLICATE_SKIP = 1, DUPLICATE_REPLACE = 2, DUPLICATE_UPDATE = 4, DUPLICATE_FILL = 8, DUPLICATE_NOCHECK = 16;
ec3811b1
CW
37
38 /**
39 * Contact types
40 */
7da04cde 41 const CONTACT_INDIVIDUAL = 1, CONTACT_HOUSEHOLD = 2, CONTACT_ORGANIZATION = 4;
69a4c20a
CW
42
43
44 /**
100fef9d 45 * Total number of non empty lines
971e129b 46 * @var int
69a4c20a
CW
47 */
48 protected $_totalCount;
49
50 /**
100fef9d 51 * Running total number of valid lines
971e129b 52 * @var int
69a4c20a
CW
53 */
54 protected $_validCount;
55
56 /**
100fef9d 57 * Running total number of invalid rows
971e129b 58 * @var int
69a4c20a
CW
59 */
60 protected $_invalidRowCount;
61
62 /**
100fef9d 63 * Maximum number of non-empty/comment lines to process
69a4c20a
CW
64 *
65 * @var int
66 */
67 protected $_maxLinesToProcess;
68
69a4c20a 69 /**
100fef9d 70 * Array of error lines, bounded by MAX_ERROR
971e129b 71 * @var array
69a4c20a
CW
72 */
73 protected $_errors;
74
75 /**
100fef9d 76 * Total number of conflict lines
971e129b 77 * @var int
69a4c20a
CW
78 */
79 protected $_conflictCount;
80
81 /**
100fef9d 82 * Array of conflict lines
971e129b 83 * @var array
69a4c20a
CW
84 */
85 protected $_conflicts;
86
87 /**
100fef9d 88 * Total number of duplicate (from database) lines
971e129b 89 * @var int
69a4c20a
CW
90 */
91 protected $_duplicateCount;
92
93 /**
100fef9d 94 * Array of duplicate lines
971e129b 95 * @var array
69a4c20a
CW
96 */
97 protected $_duplicates;
98
99 /**
100fef9d 100 * Running total number of warnings
971e129b 101 * @var int
69a4c20a
CW
102 */
103 protected $_warningCount;
104
105 /**
100fef9d 106 * Maximum number of warnings to store
971e129b 107 * @var int
69a4c20a
CW
108 */
109 protected $_maxWarningCount = self::MAX_WARNINGS;
110
111 /**
100fef9d 112 * Array of warning lines, bounded by MAX_WARNING
971e129b 113 * @var array
69a4c20a
CW
114 */
115 protected $_warnings;
116
117 /**
100fef9d 118 * Array of all the fields that could potentially be part
69a4c20a
CW
119 * of this import process
120 * @var array
121 */
122 protected $_fields;
123
64cafaa3 124 /**
125 * Metadata for all available fields, keyed by unique name.
126 *
127 * This is intended to supercede $_fields which uses a special sauce format which
128 * importableFieldsMetadata uses the standard getfields type format.
129 *
130 * @var array
131 */
132 protected $importableFieldsMetadata = [];
133
134 /**
135 * Get metadata for all importable fields in std getfields style format.
136 *
137 * @return array
138 */
139 public function getImportableFieldsMetadata(): array {
140 return $this->importableFieldsMetadata;
141 }
142
143 /**
144 * Set metadata for all importable fields in std getfields style format.
f25114b4 145 *
64cafaa3 146 * @param array $importableFieldsMetadata
147 */
f25114b4 148 public function setImportableFieldsMetadata(array $importableFieldsMetadata): void {
64cafaa3 149 $this->importableFieldsMetadata = $importableFieldsMetadata;
150 }
151
69a4c20a 152 /**
100fef9d 153 * Array of the fields that are actually part of the import process
69a4c20a
CW
154 * the position in the array also dictates their position in the import
155 * file
156 * @var array
157 */
158 protected $_activeFields;
159
160 /**
100fef9d 161 * Cache the count of active fields
69a4c20a
CW
162 *
163 * @var int
164 */
165 protected $_activeFieldCount;
166
167 /**
100fef9d 168 * Cache of preview rows
69a4c20a
CW
169 *
170 * @var array
171 */
172 protected $_rows;
173
174 /**
100fef9d 175 * Filename of error data
69a4c20a
CW
176 *
177 * @var string
178 */
179 protected $_errorFileName;
180
181 /**
100fef9d 182 * Filename of conflict data
69a4c20a
CW
183 *
184 * @var string
185 */
186 protected $_conflictFileName;
187
188 /**
100fef9d 189 * Filename of duplicate data
69a4c20a
CW
190 *
191 * @var string
192 */
193 protected $_duplicateFileName;
194
195 /**
100fef9d 196 * Contact type
69a4c20a
CW
197 *
198 * @var int
199 */
200 public $_contactType;
e87ff4ce 201 /**
202 * Contact sub-type
203 *
204 * @var int
205 */
206 public $_contactSubType;
69a4c20a
CW
207
208 /**
e87ff4ce 209 * Class constructor.
69a4c20a 210 */
00be9182 211 public function __construct() {
69a4c20a 212 $this->_maxLinesToProcess = 0;
69a4c20a
CW
213 }
214
215 /**
fe482240 216 * Abstract function definitions.
69a4c20a 217 */
bed98343 218 abstract protected function init();
e0ef6999
EM
219
220 /**
221 * @return mixed
222 */
bed98343 223 abstract protected function fini();
e0ef6999
EM
224
225 /**
2b4bc760 226 * Map field.
227 *
228 * @param array $values
e0ef6999
EM
229 *
230 * @return mixed
231 */
bed98343 232 abstract protected function mapField(&$values);
e0ef6999
EM
233
234 /**
2b4bc760 235 * Preview.
236 *
237 * @param array $values
e0ef6999
EM
238 *
239 * @return mixed
240 */
bed98343 241 abstract protected function preview(&$values);
e0ef6999
EM
242
243 /**
244 * @param $values
245 *
246 * @return mixed
247 */
bed98343 248 abstract protected function summary(&$values);
e0ef6999
EM
249
250 /**
251 * @param $onDuplicate
252 * @param $values
253 *
254 * @return mixed
255 */
bed98343 256 abstract protected function import($onDuplicate, &$values);
69a4c20a
CW
257
258 /**
fe482240 259 * Set and validate field values.
69a4c20a 260 *
5a4f6742 261 * @param array $elements
16b10e64 262 * array.
6f69cc11 263 * @param $erroneousField
16b10e64 264 * reference.
77b97be7
EM
265 *
266 * @return int
69a4c20a 267 */
ead76331 268 public function setActiveFieldValues($elements, &$erroneousField = NULL) {
69a4c20a
CW
269 $maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount;
270 for ($i = 0; $i < $maxCount; $i++) {
271 $this->_activeFields[$i]->setValue($elements[$i]);
272 }
273
274 // reset all the values that we did not have an equivalent import element
275 for (; $i < $this->_activeFieldCount; $i++) {
276 $this->_activeFields[$i]->resetValue();
277 }
278
279 // now validate the fields and return false if error
280 $valid = self::VALID;
281 for ($i = 0; $i < $this->_activeFieldCount; $i++) {
282 if (!$this->_activeFields[$i]->validate()) {
283 // no need to do any more validation
284 $erroneousField = $i;
285 $valid = self::ERROR;
286 break;
287 }
288 }
289 return $valid;
290 }
291
292 /**
fe482240 293 * Format the field values for input to the api.
69a4c20a 294 *
a6c01b45
CW
295 * @return array
296 * (reference) associative array of name/value pairs
69a4c20a 297 */
00be9182 298 public function &getActiveFieldParams() {
be2fb01f 299 $params = [];
69a4c20a
CW
300 for ($i = 0; $i < $this->_activeFieldCount; $i++) {
301 if (isset($this->_activeFields[$i]->_value)
302 && !isset($params[$this->_activeFields[$i]->_name])
303 && !isset($this->_activeFields[$i]->_related)
304 ) {
305
306 $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value;
307 }
308 }
309 return $params;
310 }
311
8cebffb2 312 /**
badf5061
JP
313 * Add progress bar to the import process. Calculates time remaining, status etc.
314 *
8cebffb2 315 * @param $statusID
badf5061 316 * status id of the import process saved in $config->uploadDir.
8cebffb2
JP
317 * @param bool $startImport
318 * True when progress bar is to be initiated.
319 * @param $startTimestamp
f25114b4 320 * Initial timestamp when the import was started.
8cebffb2
JP
321 * @param $prevTimestamp
322 * Previous timestamp when this function was last called.
323 * @param $totalRowCount
324 * Total number of rows in the import file.
325 *
326 * @return NULL|$currTimestamp
327 */
328 public function progressImport($statusID, $startImport = TRUE, $startTimestamp = NULL, $prevTimestamp = NULL, $totalRowCount = NULL) {
f25114b4 329 $statusFile = CRM_Core_Config::singleton()->uploadDir . "status_{$statusID}.txt";
8cebffb2
JP
330
331 if ($startImport) {
332 $status = "<div class='description'>&nbsp; " . ts('No processing status reported yet.') . "</div>";
333 //do not force the browser to display the save dialog, CRM-7640
be2fb01f 334 $contents = json_encode([0, $status]);
8cebffb2
JP
335 file_put_contents($statusFile, $contents);
336 }
337 else {
2e1f50d6 338 $rowCount = $this->_rowCount ?? $this->_lineCount;
8cebffb2 339 $currTimestamp = time();
8cebffb2
JP
340 $time = ($currTimestamp - $prevTimestamp);
341 $recordsLeft = $totalRowCount - $rowCount;
342 if ($recordsLeft < 0) {
343 $recordsLeft = 0;
344 }
345 $estimatedTime = ($recordsLeft / 50) * $time;
346 $estMinutes = floor($estimatedTime / 60);
347 $timeFormatted = '';
348 if ($estMinutes > 1) {
349 $timeFormatted = $estMinutes . ' ' . ts('minutes') . ' ';
350 $estimatedTime = $estimatedTime - ($estMinutes * 60);
351 }
352 $timeFormatted .= round($estimatedTime) . ' ' . ts('seconds');
353 $processedPercent = (int ) (($rowCount * 100) / $totalRowCount);
354 $statusMsg = ts('%1 of %2 records - %3 remaining',
be2fb01f 355 [1 => $rowCount, 2 => $totalRowCount, 3 => $timeFormatted]
8cebffb2
JP
356 );
357 $status = "<div class=\"description\">&nbsp; <strong>{$statusMsg}</strong></div>";
be2fb01f 358 $contents = json_encode([$processedPercent, $status]);
8cebffb2
JP
359
360 file_put_contents($statusFile, $contents);
361 return $currTimestamp;
362 }
363 }
364
e0ef6999
EM
365 /**
366 * @return array
367 */
f25114b4 368 public function getSelectValues(): array {
be2fb01f 369 $values = [];
69a4c20a
CW
370 foreach ($this->_fields as $name => $field) {
371 $values[$name] = $field->_title;
372 }
373 return $values;
374 }
375
e0ef6999
EM
376 /**
377 * @return array
378 */
00be9182 379 public function getSelectTypes() {
be2fb01f 380 $values = [];
69a4c20a
CW
381 foreach ($this->_fields as $name => $field) {
382 if (isset($field->_hasLocationType)) {
383 $values[$name] = $field->_hasLocationType;
384 }
385 }
386 return $values;
387 }
388
e0ef6999
EM
389 /**
390 * @return array
391 */
00be9182 392 public function getHeaderPatterns() {
be2fb01f 393 $values = [];
69a4c20a
CW
394 foreach ($this->_fields as $name => $field) {
395 if (isset($field->_headerPattern)) {
396 $values[$name] = $field->_headerPattern;
397 }
398 }
399 return $values;
400 }
401
e0ef6999
EM
402 /**
403 * @return array
404 */
00be9182 405 public function getDataPatterns() {
be2fb01f 406 $values = [];
69a4c20a
CW
407 foreach ($this->_fields as $name => $field) {
408 $values[$name] = $field->_dataPattern;
409 }
410 return $values;
411 }
412
413 /**
2b4bc760 414 * Remove single-quote enclosures from a value array (row).
69a4c20a
CW
415 *
416 * @param array $values
417 * @param string $enclosure
418 *
419 * @return void
69a4c20a 420 */
00be9182 421 public static function encloseScrub(&$values, $enclosure = "'") {
69a4c20a
CW
422 if (empty($values)) {
423 return;
424 }
425
426 foreach ($values as $k => $v) {
427 $values[$k] = preg_replace("/^$enclosure(.*)$enclosure$/", '$1', $v);
428 }
429 }
430
431 /**
fe482240 432 * Setter function.
69a4c20a
CW
433 *
434 * @param int $max
435 *
436 * @return void
69a4c20a 437 */
00be9182 438 public function setMaxLinesToProcess($max) {
69a4c20a
CW
439 $this->_maxLinesToProcess = $max;
440 }
441
442 /**
fe482240 443 * Determines the file extension based on error code.
69a4c20a
CW
444 *
445 * @var $type error code constant
446 * @return string
69a4c20a 447 */
00be9182 448 public static function errorFileName($type) {
69a4c20a
CW
449 $fileName = NULL;
450 if (empty($type)) {
451 return $fileName;
452 }
453
454 $config = CRM_Core_Config::singleton();
455 $fileName = $config->uploadDir . "sqlImport";
456 switch ($type) {
457 case self::ERROR:
458 $fileName .= '.errors';
459 break;
460
461 case self::CONFLICT:
462 $fileName .= '.conflicts';
463 break;
464
465 case self::DUPLICATE:
466 $fileName .= '.duplicates';
467 break;
468
469 case self::NO_MATCH:
470 $fileName .= '.mismatch';
471 break;
472
473 case self::UNPARSED_ADDRESS_WARNING:
474 $fileName .= '.unparsedAddress';
475 break;
476 }
477
478 return $fileName;
479 }
480
481 /**
fe482240 482 * Determines the file name based on error code.
69a4c20a
CW
483 *
484 * @var $type error code constant
485 * @return string
69a4c20a 486 */
00be9182 487 public static function saveFileName($type) {
69a4c20a
CW
488 $fileName = NULL;
489 if (empty($type)) {
490 return $fileName;
491 }
492 switch ($type) {
493 case self::ERROR:
494 $fileName = 'Import_Errors.csv';
495 break;
496
497 case self::CONFLICT:
498 $fileName = 'Import_Conflicts.csv';
499 break;
500
501 case self::DUPLICATE:
502 $fileName = 'Import_Duplicates.csv';
503 break;
504
505 case self::NO_MATCH:
506 $fileName = 'Import_Mismatch.csv';
507 break;
508
509 case self::UNPARSED_ADDRESS_WARNING:
510 $fileName = 'Import_Unparsed_Address.csv';
511 break;
512 }
513
514 return $fileName;
515 }
516
56316747 517 /**
518 * Check if contact is a duplicate .
519 *
520 * @param array $formatValues
521 *
522 * @return array
523 */
524 protected function checkContactDuplicate(&$formatValues) {
525 //retrieve contact id using contact dedupe rule
95519b12 526 $formatValues['contact_type'] = $formatValues['contact_type'] ?? $this->_contactType;
56316747 527 $formatValues['version'] = 3;
528 require_once 'CRM/Utils/DeprecatedUtils.php';
bd7c6219 529 $params = $formatValues;
530 static $cIndieFields = NULL;
531 static $defaultLocationId = NULL;
532
533 $contactType = $params['contact_type'];
534 if ($cIndieFields == NULL) {
535 $cTempIndieFields = CRM_Contact_BAO_Contact::importableFields($contactType);
536 $cIndieFields = $cTempIndieFields;
537
538 $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
539
540 // set the value to default location id else set to 1
541 if (!$defaultLocationId = (int) $defaultLocation->id) {
542 $defaultLocationId = 1;
543 }
544 }
545
546 $locationFields = CRM_Contact_BAO_Query::$_locationSpecificFields;
547
548 $contactFormatted = [];
549 foreach ($params as $key => $field) {
550 if ($field == NULL || $field === '') {
551 continue;
552 }
553 // CRM-17040, Considering only primary contact when importing contributions. So contribution inserts into primary contact
554 // instead of soft credit contact.
555 if (is_array($field) && $key != "soft_credit") {
556 foreach ($field as $value) {
557 $break = FALSE;
558 if (is_array($value)) {
559 foreach ($value as $name => $testForEmpty) {
560 if ($name !== 'phone_type' &&
561 ($testForEmpty === '' || $testForEmpty == NULL)
562 ) {
563 $break = TRUE;
564 break;
565 }
566 }
567 }
568 else {
569 $break = TRUE;
570 }
571 if (!$break) {
572 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
573 }
574 }
575 continue;
576 }
577
578 $value = [$key => $field];
579
580 // check if location related field, then we need to add primary location type
581 if (in_array($key, $locationFields)) {
582 $value['location_type_id'] = $defaultLocationId;
583 }
584 elseif (array_key_exists($key, $cIndieFields)) {
585 $value['contact_type'] = $contactType;
586 }
587
588 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
589 }
590
591 $contactFormatted['contact_type'] = $contactType;
592
593 return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted);
56316747 594 }
595
14b9e069 596 /**
597 * Parse a field which could be represented by a label or name value rather than the DB value.
598 *
9ae10cd7 599 * We will try to match name first or (per https://lab.civicrm.org/dev/core/issues/1285 if we have an id.
600 *
601 * but if not available then see if we have a label that can be converted to a name.
14b9e069 602 *
603 * @param string|int|null $submittedValue
604 * @param array $fieldSpec
605 * Metadata for the field
606 *
607 * @return mixed
608 */
609 protected function parsePseudoConstantField($submittedValue, $fieldSpec) {
0b742997
SL
610 // dev/core#1289 Somehow we have wound up here but the BAO has not been specified in the fieldspec so we need to check this but future us problem, for now lets just return the submittedValue
611 if (!isset($fieldSpec['bao'])) {
612 return $submittedValue;
613 }
14b9e069 614 /* @var \CRM_Core_DAO $bao */
615 $bao = $fieldSpec['bao'];
616 // For historical reasons use validate as context - ie disabled name matches ARE permitted.
617 $nameOptions = $bao::buildOptions($fieldSpec['name'], 'validate');
9ae10cd7 618 if (isset($nameOptions[$submittedValue])) {
619 return $submittedValue;
620 }
621 if (in_array($submittedValue, $nameOptions)) {
622 return array_search($submittedValue, $nameOptions, TRUE);
623 }
624
625 $labelOptions = array_flip($bao::buildOptions($fieldSpec['name'], 'match'));
626 if (isset($labelOptions[$submittedValue])) {
627 return array_search($labelOptions[$submittedValue], $nameOptions, TRUE);
14b9e069 628 }
629 return '';
630 }
631
be40742b
CW
632 /**
633 * This is code extracted from 4 places where this exact snippet was being duplicated.
634 *
635 * FIXME: Extracting this was a first step, but there's also
636 * 1. Inconsistency in the way other select options are handled.
637 * Contribution adds handling for Select/Radio/Autocomplete
638 * Participant/Activity only handles Select/Radio and misses Autocomplete
639 * Membership is missing all of it
640 * 2. Inconsistency with the way this works vs. how it's implemented in Contact import.
641 *
642 * @param $customFieldID
643 * @param $value
644 * @param $fieldType
645 * @return array
646 */
647 public static function unserializeCustomValue($customFieldID, $value, $fieldType) {
648 $mulValues = explode(',', $value);
649 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
650 $values = [];
651 foreach ($mulValues as $v1) {
652 foreach ($customOption as $customValueID => $customLabel) {
653 $customValue = $customLabel['value'];
654 if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) ||
655 (strtolower(trim($customValue)) == strtolower(trim($v1)))
656 ) {
657 if ($fieldType == 'CheckBox') {
658 $values[$customValue] = 1;
659 }
660 else {
661 $values[] = $customValue;
662 }
663 }
664 }
665 }
666 return $values;
667 }
668
a8ea3922 669 /**
670 * Get the ids of any contacts that match according to the rule.
671 *
672 * @param array $formatted
673 *
674 * @return array
675 */
676 protected function getIdsOfMatchingContacts(array $formatted):array {
677 // the call to the deprecated function seems to add no value other that to do an additional
678 // check for the contact_id & type.
679 $error = _civicrm_api3_deprecated_duplicate_formatted_contact($formatted);
680 if (!CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
681 return [];
682 }
683 if (is_array($error['error_message']['params'][0])) {
684 return $error['error_message']['params'][0];
685 }
686 else {
687 return explode(',', $error['error_message']['params'][0]);
688 }
689 }
690
ec3811b1 691}