Merge pull request #22115 from artfulrobot/artfulrobot-api4-count-methods
[civicrm-core.git] / CRM / Activity / Import / Parser / Activity.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18
19 /**
20 * Class to parse activity csv files.
21 */
22 class CRM_Activity_Import_Parser_Activity extends CRM_Import_Parser {
23
24 protected $_mapperKeys;
25
26 private $_contactIdIndex;
27
28 /**
29 * Array of successfully imported activity id's
30 *
31 * @var array
32 */
33 protected $_newActivity;
34
35 protected $_fileName;
36
37 /**
38 * Imported file size.
39 * @var int
40 */
41 protected $_fileSize;
42
43 /**
44 * Separator being used.
45 * @var string
46 */
47 protected $_separator;
48
49 /**
50 * Total number of lines in file.
51 * @var int
52 */
53 protected $_lineCount;
54
55 /**
56 * Whether the file has a column header or not.
57 *
58 * @var bool
59 */
60 protected $_haveColumnHeader;
61
62 /**
63 * Class constructor.
64 *
65 * @param array $mapperKeys
66 */
67 public function __construct($mapperKeys = []) {
68 parent::__construct();
69 $this->_mapperKeys = $mapperKeys;
70 }
71
72 /**
73 * The initializer code, called before the processing.
74 */
75 public function init() {
76 $activityContact = CRM_Activity_BAO_ActivityContact::import();
77 $activityTarget['target_contact_id'] = $activityContact['contact_id'];
78 $fields = array_merge(CRM_Activity_BAO_Activity::importableFields(),
79 $activityTarget
80 );
81
82 $fields = array_merge($fields, [
83 'source_contact_id' => [
84 'title' => ts('Source Contact'),
85 'headerPattern' => '/Source.Contact?/i',
86 ],
87 'activity_label' => [
88 'title' => ts('Activity Type Label'),
89 'headerPattern' => '/(activity.)?type label?/i',
90 ],
91 ]);
92
93 foreach ($fields as $name => $field) {
94 $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT);
95 $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//');
96 $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//');
97 if (!empty($field['custom_group_id'])) {
98 $field['title'] = $field["groupTitle"] . ' :: ' . $field["title"];
99 }
100 $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
101 }
102
103 $this->_newActivity = [];
104
105 $this->setActiveFields($this->_mapperKeys);
106
107 // FIXME: we should do this in one place together with Form/MapField.php
108 $this->_contactIdIndex = -1;
109
110 $index = 0;
111 foreach ($this->_mapperKeys as $key) {
112 switch ($key) {
113 case 'target_contact_id':
114 case 'external_identifier':
115 $this->_contactIdIndex = $index;
116 break;
117 }
118 $index++;
119 }
120 }
121
122 /**
123 * Handle the values in preview mode.
124 *
125 * @param array $values
126 * The array of values belonging to this line.
127 *
128 * @return bool
129 * the result of this processing
130 */
131 public function preview(&$values) {
132 return $this->summary($values);
133 }
134
135 /**
136 * Handle the values in summary mode.
137 *
138 * @param array $values
139 * The array of values belonging to this line.
140 *
141 * @return bool
142 * the result of this processing
143 */
144 public function summary(&$values) {
145 try {
146 $this->validateValues($values);
147 }
148 catch (CRM_Core_Exception $e) {
149 return $this->addError($values, [$e->getMessage()]);
150 }
151
152 return CRM_Import_Parser::VALID;
153 }
154
155 /**
156 * Handle the values in import mode.
157 *
158 * @param int $onDuplicate
159 * The code for what action to take on duplicates.
160 * @param array $values
161 * The array of values belonging to this line.
162 *
163 * @return bool
164 * the result of this processing
165 * @throws \CRM_Core_Exception
166 */
167 public function import($onDuplicate, &$values) {
168 // First make sure this is a valid line
169 try {
170 $this->validateValues($values);
171 }
172 catch (CRM_Core_Exception $e) {
173 return $this->addError($values, [$e->getMessage()]);
174 }
175 $params = $this->getApiReadyParams($values);
176 // For date-Formats.
177 $session = CRM_Core_Session::singleton();
178 $dateType = $session->get('dateTypes');
179
180 $customFields = CRM_Core_BAO_CustomField::getFields('Activity');
181
182 foreach ($params as $key => $val) {
183 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
184 if (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Date') {
185 CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $params, $dateType, $key);
186 }
187 elseif (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
188 $params[$key] = CRM_Utils_String::strtoboolstr($val);
189 }
190 }
191 elseif ($key === 'activity_date_time') {
192 $params[$key] = CRM_Utils_Date::formatDate($val, $dateType);
193 }
194 elseif ($key === 'activity_subject') {
195 $params['subject'] = $val;
196 }
197 }
198
199 if ($this->_contactIdIndex < 0) {
200
201 // Retrieve contact id using contact dedupe rule.
202 // Since we are supporting only individual's activity import.
203 $params['contact_type'] = 'Individual';
204 $params['version'] = 3;
205 $error = _civicrm_api3_deprecated_duplicate_formatted_contact($params);
206
207 if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
208 $matchedIDs = explode(',', $error['error_message']['params'][0]);
209 if (count($matchedIDs) > 1) {
210 array_unshift($values, 'Multiple matching contact records detected for this row. The activity was not imported');
211 return CRM_Import_Parser::ERROR;
212 }
213 $cid = $matchedIDs[0];
214 $params['target_contact_id'] = $cid;
215 $params['version'] = 3;
216 $newActivity = civicrm_api('activity', 'create', $params);
217 if (!empty($newActivity['is_error'])) {
218 array_unshift($values, $newActivity['error_message']);
219 return CRM_Import_Parser::ERROR;
220 }
221
222 $this->_newActivity[] = $newActivity['id'];
223 return CRM_Import_Parser::VALID;
224
225 }
226 // Using new Dedupe rule.
227 $ruleParams = [
228 'contact_type' => 'Individual',
229 'used' => 'Unsupervised',
230 ];
231 $fieldsArray = CRM_Dedupe_BAO_DedupeRule::dedupeRuleFields($ruleParams);
232
233 $disp = NULL;
234 foreach ($fieldsArray as $value) {
235 if (array_key_exists(trim($value), $params)) {
236 $paramValue = $params[trim($value)];
237 if (is_array($paramValue)) {
238 $disp .= $params[trim($value)][0][trim($value)] . " ";
239 }
240 else {
241 $disp .= $params[trim($value)] . " ";
242 }
243 }
244 }
245
246 if (!empty($params['external_identifier'])) {
247 if ($disp) {
248 $disp .= "AND {$params['external_identifier']}";
249 }
250 else {
251 $disp = $params['external_identifier'];
252 }
253 }
254
255 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
256 return CRM_Import_Parser::ERROR;
257 }
258 if (!empty($params['external_identifier'])) {
259 $targetContactId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
260 $params['external_identifier'], 'id', 'external_identifier'
261 );
262
263 if (!empty($params['target_contact_id']) &&
264 $params['target_contact_id'] != $targetContactId
265 ) {
266 array_unshift($values, 'Mismatch of External ID:' . $params['external_identifier'] . ' and Contact Id:' . $params['target_contact_id']);
267 return CRM_Import_Parser::ERROR;
268 }
269 if ($targetContactId) {
270 $params['target_contact_id'] = $targetContactId;
271 }
272 else {
273 array_unshift($values, 'No Matching Contact for External ID:' . $params['external_identifier']);
274 return CRM_Import_Parser::ERROR;
275 }
276 }
277
278 $params['version'] = 3;
279 $newActivity = civicrm_api('activity', 'create', $params);
280 if (!empty($newActivity['is_error'])) {
281 array_unshift($values, $newActivity['error_message']);
282 return CRM_Import_Parser::ERROR;
283 }
284
285 $this->_newActivity[] = $newActivity['id'];
286 return CRM_Import_Parser::VALID;
287 }
288
289 /**
290 *
291 * Get the value for the given field from the row of values.
292 *
293 * @param array $row
294 * @param string $fieldName
295 *
296 * @return null|string
297 */
298 protected function getFieldValue(array $row, string $fieldName) {
299 if (!is_numeric($this->getFieldIndex($fieldName))) {
300 return NULL;
301 }
302 return $row[$this->getFieldIndex($fieldName)] ?? NULL;
303 }
304
305 /**
306 * Get the index for the given field.
307 *
308 * @param string $fieldName
309 *
310 * @return false|int
311 */
312 protected function getFieldIndex(string $fieldName) {
313 return array_search($fieldName, $this->_mapperKeys, TRUE);
314
315 }
316
317 /**
318 * Add an error to the values.
319 *
320 * @param array $values
321 * @param array $error
322 *
323 * @return int
324 */
325 protected function addError(array &$values, array $error): int {
326 array_unshift($values, implode(';', $error));
327 return CRM_Import_Parser::ERROR;
328 }
329
330 /**
331 * Validate that the activity type id does not conflict with the label.
332 *
333 * @param array $values
334 *
335 * @return void
336 * @throws \CRM_Core_Exception
337 */
338 protected function validateActivityTypeIDAndLabel(array $values): void {
339 $activityLabel = $this->getFieldValue($values, 'activity_label');
340 $activityTypeID = $this->getFieldValue($values, 'activity_type_id');
341 if ($activityLabel && $activityTypeID
342 && $activityLabel !== CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $activityTypeID)) {
343 throw new CRM_Core_Exception(ts('Activity type label and Activity type ID are in conflict'));
344 }
345 }
346
347 /**
348 * Is the supplied date field valid based on selected date format.
349 *
350 * @param string $value
351 *
352 * @return bool
353 */
354 protected function isValidDate(string $value): bool {
355 return (bool) CRM_Utils_Date::formatDate($value, CRM_Core_Session::singleton()->get('dateTypes'));
356 }
357
358 /**
359 * Is the supplied field a valid contact id.
360 *
361 * @param string|int $value
362 *
363 * @return bool
364 */
365 protected function isValidContactID($value): bool {
366 if (!CRM_Utils_Rule::integer($value)) {
367 return FALSE;
368 }
369 if (!CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = " . (int) $value)) {
370 return FALSE;
371 }
372 return TRUE;
373 }
374
375 /**
376 * Validate custom fields.
377 *
378 * @param array $values
379 *
380 * @throws \CRM_Core_Exception
381 */
382 protected function validateCustomFields($values):void {
383 $this->setActiveFieldValues($values);
384 $params = $this->getActiveFieldParams();
385 $errorMessage = NULL;
386 // Checking error in custom data.
387 $params['contact_type'] = 'Activity';
388 CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage);
389 if ($errorMessage) {
390 throw new CRM_Core_Exception('Invalid value for field(s) : ' . $errorMessage);
391 }
392 }
393
394 /**
395 * @param array $values
396 *
397 * @throws \CRM_Core_Exception
398 */
399 protected function validateValues(array $values): void {
400 // Check required fields if this is not an update.
401 if (!$this->getFieldValue($values, 'activity_id')) {
402 if (!$this->getFieldValue($values, 'activity_label')
403 && !$this->getFieldValue($values, 'activity_type_id')) {
404 throw new CRM_Core_Exception(ts('Missing required fields: Activity type label or Activity type ID'));
405 }
406 if (!$this->getFieldValue($values, 'activity_date_time')) {
407 throw new CRM_Core_Exception(ts('Missing required fields'));
408 }
409 }
410
411 $this->validateActivityTypeIDAndLabel($values);
412 if ($this->getFieldValue($values, 'activity_date_time')
413 && !$this->isValidDate($this->getFieldValue($values, 'activity_date_time'))) {
414 throw new CRM_Core_Exception(ts('Invalid Activity Date'));
415 }
416
417 if ($this->getFieldValue($values, 'activity_engagement_level')
418 && !CRM_Utils_Rule::positiveInteger($this->getFieldValue($values, 'activity_engagement_level'))) {
419 throw new CRM_Core_Exception(ts('Activity Engagement Index'));
420 }
421
422 $targetContactID = $this->getFieldValue($values, 'target_contact_id');
423 if ($targetContactID && !$this->isValidContactID($targetContactID)) {
424 throw new CRM_Core_Exception("Invalid Contact ID: There is no contact record with contact_id = " . CRM_Utils_Type::escape($targetContactID, 'String'));
425 }
426 $this->validateCustomFields($values);
427 }
428
429 /**
430 * Get array of parameters formatted for the api from the submitted values.
431 *
432 * @param array $values
433 *
434 * @return array
435 */
436 protected function getApiReadyParams(array $values): array {
437 $this->setActiveFieldValues($values);
438 $params = $this->getActiveFieldParams();
439 if ($this->getFieldValue($values, 'activity_label')) {
440 $params['activity_type_id'] = array_search(
441 $this->getFieldValue($values, 'activity_label'),
442 CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'create'),
443 TRUE
444 );
445 }
446 return $params;
447 }
448
449 /**
450 * @param array $fileName
451 * @param string $separator
452 * @param $mapper
453 * @param bool $skipColumnHeader
454 * @param int $mode
455 * @param int $onDuplicate
456 * @param int $statusID
457 * @param int $totalRowCount
458 *
459 * @return mixed
460 * @throws Exception
461 */
462 public function run(
463 array $fileName,
464 $separator,
465 $mapper,
466 $skipColumnHeader = FALSE,
467 $mode = self::MODE_PREVIEW,
468 $onDuplicate = self::DUPLICATE_SKIP,
469 $statusID = NULL,
470 $totalRowCount = NULL
471 ) {
472
473 $fileName = $fileName['name'];
474
475 $this->init();
476
477 $this->_haveColumnHeader = $skipColumnHeader;
478
479 $this->_separator = $separator;
480
481 $fd = fopen($fileName, "r");
482 if (!$fd) {
483 return FALSE;
484 }
485
486 $this->_lineCount = 0;
487 $this->_invalidRowCount = $this->_validCount = 0;
488 $this->_totalCount = $this->_conflictCount = 0;
489
490 $this->_errors = [];
491 $this->_warnings = [];
492 $this->_conflicts = [];
493
494 $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2);
495
496 if ($mode == self::MODE_MAPFIELD) {
497 $this->_rows = [];
498 }
499 else {
500 $this->_activeFieldCount = count($this->_activeFields);
501 }
502 if ($statusID) {
503 $this->progressImport($statusID);
504 $startTimestamp = $currTimestamp = $prevTimestamp = time();
505 }
506
507 while (!feof($fd)) {
508 $this->_lineCount++;
509
510 $values = fgetcsv($fd, 8192, $separator);
511 if (!$values) {
512 continue;
513 }
514
515 self::encloseScrub($values);
516
517 // skip column header if we're not in mapfield mode
518 if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) {
519 $skipColumnHeader = FALSE;
520 continue;
521 }
522
523 // Trim whitespace around the values.
524
525 $empty = TRUE;
526 foreach ($values as $k => $v) {
527 $values[$k] = trim($v, " \t\r\n");
528 }
529
530 if (CRM_Utils_System::isNull($values)) {
531 continue;
532 }
533
534 $this->_totalCount++;
535
536 if ($mode == self::MODE_MAPFIELD) {
537 $returnCode = CRM_Import_Parser::VALID;
538 }
539 elseif ($mode == self::MODE_PREVIEW) {
540 $returnCode = $this->preview($values);
541 }
542 elseif ($mode == self::MODE_SUMMARY) {
543 $returnCode = $this->summary($values);
544 }
545 elseif ($mode == self::MODE_IMPORT) {
546 $returnCode = $this->import($onDuplicate, $values);
547 if ($statusID && (($this->_lineCount % 50) == 0)) {
548 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
549 }
550 }
551 else {
552 $returnCode = self::ERROR;
553 }
554
555 // note that a line could be valid but still produce a warning
556 if ($returnCode & self::VALID) {
557 $this->_validCount++;
558 if ($mode == self::MODE_MAPFIELD) {
559 $this->_rows[] = $values;
560 $this->_activeFieldCount = max($this->_activeFieldCount, count($values));
561 }
562 }
563
564 if ($returnCode & self::ERROR) {
565 $this->_invalidRowCount++;
566 $recordNumber = $this->_lineCount;
567 if ($this->_haveColumnHeader) {
568 $recordNumber--;
569 }
570 array_unshift($values, $recordNumber);
571 $this->_errors[] = $values;
572 }
573
574 if ($returnCode & self::CONFLICT) {
575 $this->_conflictCount++;
576 $recordNumber = $this->_lineCount;
577 if ($this->_haveColumnHeader) {
578 $recordNumber--;
579 }
580 array_unshift($values, $recordNumber);
581 $this->_conflicts[] = $values;
582 }
583
584 if ($returnCode & self::DUPLICATE) {
585 $this->_duplicateCount++;
586 $recordNumber = $this->_lineCount;
587 if ($this->_haveColumnHeader) {
588 $recordNumber--;
589 }
590 array_unshift($values, $recordNumber);
591 $this->_duplicates[] = $values;
592 if ($onDuplicate != self::DUPLICATE_SKIP) {
593 $this->_validCount++;
594 }
595 }
596
597 // if we are done processing the maxNumber of lines, break
598 if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) {
599 break;
600 }
601 }
602
603 fclose($fd);
604
605 if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) {
606 $customHeaders = $mapper;
607
608 $customfields = CRM_Core_BAO_CustomField::getFields('Activity');
609 foreach ($customHeaders as $key => $value) {
610 if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) {
611 $customHeaders[$key] = $customfields[$id][0];
612 }
613 }
614 if ($this->_invalidRowCount) {
615 // removed view url for invlaid contacts
616 $headers = array_merge(
617 [ts('Line Number'), ts('Reason')],
618 $customHeaders
619 );
620 $this->_errorFileName = self::errorFileName(self::ERROR);
621 self::exportCSV($this->_errorFileName, $headers, $this->_errors);
622 }
623 if ($this->_conflictCount) {
624 $headers = array_merge(
625 [ts('Line Number'), ts('Reason')],
626 $customHeaders
627 );
628 $this->_conflictFileName = self::errorFileName(self::CONFLICT);
629 self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts);
630 }
631 if ($this->_duplicateCount) {
632 $headers = array_merge(
633 [ts('Line Number'), ts('View Activity History URL')],
634 $customHeaders
635 );
636
637 $this->_duplicateFileName = self::errorFileName(self::DUPLICATE);
638 self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates);
639 }
640 }
641 }
642
643 /**
644 * Given a list of the importable field keys that the user has selected set the active fields array to this list.
645 *
646 * @param array $fieldKeys
647 */
648 public function setActiveFields($fieldKeys) {
649 $this->_activeFieldCount = count($fieldKeys);
650 foreach ($fieldKeys as $key) {
651 if (empty($this->_fields[$key])) {
652 $this->_activeFields[] = new CRM_Activity_Import_Field('', ts('- do not import -'));
653 }
654 else {
655 $this->_activeFields[] = clone($this->_fields[$key]);
656 }
657 }
658 }
659
660 /**
661 * @param string $name
662 * @param $title
663 * @param int $type
664 * @param string $headerPattern
665 * @param string $dataPattern
666 */
667 public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') {
668 if (empty($name)) {
669 $this->_fields['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
670 }
671 else {
672
673 $tempField = CRM_Contact_BAO_Contact::importableFields('Individual', NULL);
674 if (!array_key_exists($name, $tempField)) {
675 $this->_fields[$name] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
676 }
677 else {
678 $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array::value('hasLocationType', $tempField[$name]));
679 }
680 }
681 }
682
683 /**
684 * Store parser values.
685 *
686 * @param CRM_Core_Session $store
687 *
688 * @param int $mode
689 */
690 public function set($store, $mode = self::MODE_SUMMARY) {
691 $store->set('fileSize', $this->_fileSize);
692 $store->set('lineCount', $this->_lineCount);
693 $store->set('separator', $this->_separator);
694 $store->set('fields', $this->getSelectValues());
695
696 $store->set('headerPatterns', $this->getHeaderPatterns());
697 $store->set('dataPatterns', $this->getDataPatterns());
698 $store->set('columnCount', $this->_activeFieldCount);
699
700 $store->set('totalRowCount', $this->_totalCount);
701 $store->set('validRowCount', $this->_validCount);
702 $store->set('invalidRowCount', $this->_invalidRowCount);
703 $store->set('conflictRowCount', $this->_conflictCount);
704
705 if ($this->_invalidRowCount) {
706 $store->set('errorsFileName', $this->_errorFileName);
707 }
708 if ($this->_conflictCount) {
709 $store->set('conflictsFileName', $this->_conflictFileName);
710 }
711 if (isset($this->_rows) && !empty($this->_rows)) {
712 $store->set('dataValues', $this->_rows);
713 }
714
715 if ($mode == self::MODE_IMPORT) {
716 $store->set('duplicateRowCount', $this->_duplicateCount);
717 if ($this->_duplicateCount) {
718 $store->set('duplicatesFileName', $this->_duplicateFileName);
719 }
720 }
721 }
722
723 /**
724 * Export data to a CSV file.
725 *
726 * @param string $fileName
727 * @param array $header
728 * @param array $data
729 */
730 public static function exportCSV($fileName, $header, $data) {
731 $output = [];
732 $fd = fopen($fileName, 'w');
733
734 foreach ($header as $key => $value) {
735 $header[$key] = "\"$value\"";
736 }
737 $config = CRM_Core_Config::singleton();
738 $output[] = implode($config->fieldSeparator, $header);
739
740 foreach ($data as $datum) {
741 foreach ($datum as $key => $value) {
742 $datum[$key] = "\"$value\"";
743 }
744 $output[] = implode($config->fieldSeparator, $datum);
745 }
746 fwrite($fd, implode("\n", $output));
747 fclose($fd);
748 }
749
750 }