3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 * Class to parse activity csv files.
22 class CRM_Activity_Import_Parser_Activity
extends CRM_Activity_Import_Parser
{
24 protected $_mapperKeys;
26 private $_contactIdIndex;
29 * Array of successfully imported activity id's
33 protected $_newActivity;
38 * @param array $mapperKeys
40 public function __construct($mapperKeys) {
41 parent
::__construct();
42 $this->_mapperKeys
= $mapperKeys;
46 * Function of undocumented functionality required by the interface.
48 protected function fini() {}
51 * The initializer code, called before the processing.
53 public function init() {
54 $activityContact = CRM_Activity_BAO_ActivityContact
::import();
55 $activityTarget['target_contact_id'] = $activityContact['contact_id'];
56 $fields = array_merge(CRM_Activity_BAO_Activity
::importableFields(),
60 $fields = array_merge($fields, [
61 'source_contact_id' => [
62 'title' => ts('Source Contact'),
63 'headerPattern' => '/Source.Contact?/i',
66 'title' => ts('Activity Type Label'),
67 'headerPattern' => '/(activity.)?type label?/i',
71 foreach ($fields as $name => $field) {
72 $field['type'] = CRM_Utils_Array
::value('type', $field, CRM_Utils_Type
::T_INT
);
73 $field['dataPattern'] = CRM_Utils_Array
::value('dataPattern', $field, '//');
74 $field['headerPattern'] = CRM_Utils_Array
::value('headerPattern', $field, '//');
75 $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
78 $this->_newActivity
= [];
80 $this->setActiveFields($this->_mapperKeys
);
82 // FIXME: we should do this in one place together with Form/MapField.php
83 $this->_contactIdIndex
= -1;
86 foreach ($this->_mapperKeys
as $key) {
88 case 'target_contact_id':
89 case 'external_identifier':
90 $this->_contactIdIndex
= $index;
98 * Handle the values in mapField mode.
100 * @param array $values
101 * The array of values belonging to this line.
105 public function mapField(&$values) {
106 return CRM_Import_Parser
::VALID
;
110 * Handle the values in preview mode.
112 * @param array $values
113 * The array of values belonging to this line.
116 * the result of this processing
118 public function preview(&$values) {
119 return $this->summary($values);
123 * Handle the values in summary mode.
125 * @param array $values
126 * The array of values belonging to this line.
129 * the result of this processing
131 public function summary(&$values) {
133 $this->validateValues($values);
135 catch (CRM_Core_Exception
$e) {
136 return $this->addError($values, [$e->getMessage()]);
139 return CRM_Import_Parser
::VALID
;
143 * Handle the values in import mode.
145 * @param int $onDuplicate
146 * The code for what action to take on duplicates.
147 * @param array $values
148 * The array of values belonging to this line.
151 * the result of this processing
152 * @throws \CRM_Core_Exception
154 public function import($onDuplicate, &$values) {
155 // First make sure this is a valid line
157 $this->validateValues($values);
159 catch (CRM_Core_Exception
$e) {
160 return $this->addError($values, [$e->getMessage()]);
162 $params = $this->getApiReadyParams($values);
164 $session = CRM_Core_Session
::singleton();
165 $dateType = $session->get('dateTypes');
166 if (!isset($params['source_contact_id'])) {
167 $params['source_contact_id'] = $session->get('userID');
170 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity');
172 foreach ($params as $key => $val) {
173 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
174 if (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Date') {
175 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $params, $dateType, $key);
177 elseif (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
178 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
181 elseif ($key === 'activity_date_time') {
182 $params[$key] = CRM_Utils_Date
::formatDate($val, $dateType);
184 elseif ($key === 'activity_subject') {
185 $params['subject'] = $val;
189 if ($this->_contactIdIndex
< 0) {
191 // Retrieve contact id using contact dedupe rule.
192 // Since we are supporting only individual's activity import.
193 $params['contact_type'] = 'Individual';
194 $params['version'] = 3;
195 $error = _civicrm_api3_deprecated_duplicate_formatted_contact($params);
197 if (CRM_Core_Error
::isAPIError($error, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
198 $matchedIDs = explode(',', $error['error_message']['params'][0]);
199 if (count($matchedIDs) > 1) {
200 array_unshift($values, 'Multiple matching contact records detected for this row. The activity was not imported');
201 return CRM_Import_Parser
::ERROR
;
203 $cid = $matchedIDs[0];
204 $params['target_contact_id'] = $cid;
205 $params['version'] = 3;
206 $newActivity = civicrm_api('activity', 'create', $params);
207 if (!empty($newActivity['is_error'])) {
208 array_unshift($values, $newActivity['error_message']);
209 return CRM_Import_Parser
::ERROR
;
212 $this->_newActivity
[] = $newActivity['id'];
213 return CRM_Import_Parser
::VALID
;
216 // Using new Dedupe rule.
218 'contact_type' => 'Individual',
219 'used' => 'Unsupervised',
221 $fieldsArray = CRM_Dedupe_BAO_Rule
::dedupeRuleFields($ruleParams);
224 foreach ($fieldsArray as $value) {
225 if (array_key_exists(trim($value), $params)) {
226 $paramValue = $params[trim($value)];
227 if (is_array($paramValue)) {
228 $disp .= $params[trim($value)][0][trim($value)] . " ";
231 $disp .= $params[trim($value)] . " ";
236 if (!empty($params['external_identifier'])) {
238 $disp .= "AND {$params['external_identifier']}";
241 $disp = $params['external_identifier'];
245 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
246 return CRM_Import_Parser
::ERROR
;
248 if (!empty($params['external_identifier'])) {
249 $targetContactId = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
250 $params['external_identifier'], 'id', 'external_identifier'
253 if (!empty($params['target_contact_id']) &&
254 $params['target_contact_id'] != $targetContactId
256 array_unshift($values, 'Mismatch of External ID:' . $params['external_identifier'] . ' and Contact Id:' . $params['target_contact_id']);
257 return CRM_Import_Parser
::ERROR
;
259 if ($targetContactId) {
260 $params['target_contact_id'] = $targetContactId;
263 array_unshift($values, 'No Matching Contact for External ID:' . $params['external_identifier']);
264 return CRM_Import_Parser
::ERROR
;
268 $params['version'] = 3;
269 $newActivity = civicrm_api('activity', 'create', $params);
270 if (!empty($newActivity['is_error'])) {
271 array_unshift($values, $newActivity['error_message']);
272 return CRM_Import_Parser
::ERROR
;
275 $this->_newActivity
[] = $newActivity['id'];
276 return CRM_Import_Parser
::VALID
;
281 * Get the value for the given field from the row of values.
284 * @param string $fieldName
286 * @return null|string
288 protected function getFieldValue(array $row, string $fieldName) {
289 if (!is_numeric($this->getFieldIndex($fieldName))) {
292 return $row[$this->getFieldIndex($fieldName)] ??
NULL;
296 * Get the index for the given field.
298 * @param string $fieldName
302 protected function getFieldIndex(string $fieldName) {
303 return array_search($fieldName, $this->_mapperKeys
, TRUE);
308 * Add an error to the values.
310 * @param array $values
311 * @param array $error
315 protected function addError(array &$values, array $error): int {
316 array_unshift($values, implode(';', $error));
317 return CRM_Import_Parser
::ERROR
;
321 * Validate that the activity type id does not conflict with the label.
323 * @param array $values
326 * @throws \CRM_Core_Exception
328 protected function validateActivityTypeIDAndLabel(array $values): void
{
329 $activityLabel = $this->getFieldValue($values, 'activity_label');
330 $activityTypeID = $this->getFieldValue($values, 'activity_type_id');
331 if ($activityLabel && $activityTypeID
332 && $activityLabel !== CRM_Core_PseudoConstant
::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $activityTypeID)) {
333 throw new CRM_Core_Exception(ts('Activity type label and Activity type ID are in conflict'));
338 * Is the supplied date field valid based on selected date format.
340 * @param string $value
344 protected function isValidDate(string $value): bool {
345 return (bool) CRM_Utils_Date
::formatDate($value, CRM_Core_Session
::singleton()->get('dateTypes'));
349 * Is the supplied field a valid contact id.
351 * @param string|int $value
355 protected function isValidContactID($value): bool {
356 if (!CRM_Utils_Rule
::integer($value)) {
359 if (!CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = " . (int) $value)) {
366 * Validate custom fields.
368 * @param array $values
370 * @throws \CRM_Core_Exception
372 protected function validateCustomFields($values):void
{
373 $this->setActiveFieldValues($values);
374 $params = $this->getActiveFieldParams();
375 $errorMessage = NULL;
376 // Checking error in custom data.
377 $params['contact_type'] = 'Activity';
378 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($params, $errorMessage);
380 throw new CRM_Core_Exception('Invalid value for field(s) : ' . $errorMessage);
385 * @param array $values
387 * @throws \CRM_Core_Exception
389 protected function validateValues(array $values): void
{
390 // Check required fields if this is not an update.
391 if (!$this->getFieldValue($values, 'activity_id')) {
392 if (!$this->getFieldValue($values, 'activity_label')
393 && !$this->getFieldValue($values, 'activity_type_id')) {
394 throw new CRM_Core_Exception(ts('Missing required fields: Activity type label or Activity type ID'));
396 if (!$this->getFieldValue($values, 'activity_date_time')) {
397 throw new CRM_Core_Exception(ts('Missing required fields'));
401 $this->validateActivityTypeIDAndLabel($values);
402 if ($this->getFieldValue($values, 'activity_date_time')
403 && !$this->isValidDate($this->getFieldValue($values, 'activity_date_time'))) {
404 throw new CRM_Core_Exception(ts('Invalid Activity Date'));
407 if ($this->getFieldValue($values, 'activity_engagement_level')
408 && !CRM_Utils_Rule
::positiveInteger($this->getFieldValue($values, 'activity_engagement_level'))) {
409 throw new CRM_Core_Exception(ts('Activity Engagement Index'));
412 $targetContactID = $this->getFieldValue($values, 'target_contact_id');
413 if ($targetContactID && !$this->isValidContactID($targetContactID)) {
414 throw new CRM_Core_Exception("Invalid Contact ID: There is no contact record with contact_id = " . CRM_Utils_Type
::escape($targetContactID, 'String'));
416 $this->validateCustomFields($values);
420 * Get array of parameters formatted for the api from the submitted values.
422 * @param array $values
426 protected function getApiReadyParams(array $values): array {
427 $this->setActiveFieldValues($values);
428 $params = $this->getActiveFieldParams();
429 if ($this->getFieldValue($values, 'activity_label')) {
430 $params['activity_type_id'] = array_search(
431 $this->getFieldValue($values, 'activity_label'),
432 CRM_Activity_BAO_Activity
::buildOptions('activity_type_id', 'create'),