Skip to content

Commit

Permalink
[Import][REf] Cleanup required field checking
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed May 10, 2022
1 parent bd0e704 commit 2375d76
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 69 deletions.
87 changes: 18 additions & 69 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
protected $_mapperRelatedContactDetails;
protected $_relationships;

protected $_emailIndex;

protected $_phoneIndex;

/**
Expand Down Expand Up @@ -143,35 +141,38 @@ public function init() {

$this->setActiveFields($this->_mapperKeys);

$this->_phoneIndex = -1;
$this->_emailIndex = -1;
$this->_externalIdentifierIndex = -1;

$index = 0;
foreach ($this->_mapperKeys as $key) {
if (substr($key, 0, 5) == 'email' && substr($key, 0, 14) != 'email_greeting') {
$this->_emailIndex = $index;
}
if (substr($key, 0, 5) == 'phone') {
$this->_phoneIndex = $index;
}
if ($key == 'external_identifier') {
$this->_externalIdentifierIndex = $index;
}
$index++;
}

$this->_updateWithId = FALSE;
if (in_array('id', $this->_mapperKeys) || ($this->_externalIdentifierIndex >= 0 && in_array($this->_onDuplicate, [
CRM_Import_Parser::DUPLICATE_UPDATE,
CRM_Import_Parser::DUPLICATE_FILL,
]))) {
if (in_array('id', $this->_mapperKeys) || ($this->_externalIdentifierIndex >= 0 && $this->isUpdateExistingContacts())) {
$this->_updateWithId = TRUE;
}

$this->_parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options'), FALSE);
}

/**
* Is this a case where the user has opted to update existing contacts.
*
* @return bool
*
* @throws \API_Exception
*/
private function isUpdateExistingContacts(): bool {
return in_array((int) $this->getSubmittedValue('onDuplicate'), [
CRM_Import_Parser::DUPLICATE_UPDATE,
CRM_Import_Parser::DUPLICATE_FILL,
], TRUE);
}

/**
* Gets the fields available for importing in a key-name, title format.
*
Expand Down Expand Up @@ -3154,66 +3155,14 @@ public function isComplete() {
* @param array $values
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public function validateValues(array $values): void {
$errorMessage = NULL;
$errorRequired = FALSE;
$params = $this->getMappedRow($values);
$missingNames = [];
switch ($params['contact_type']) {
case 'Individual':
if (empty($params['first_name'])) {
$missingNames[] = ts('First Name');
}
if (empty($params['last_name'])) {
$missingNames[] = ts('Last Name');
}
break;

case 'Household':
if (empty($params['household_name'])) {
$missingNames[] = ts('Missing required fields:') . ' ' . ts('Household Name');
}
break;

case 'Organization':
if (empty($params['organization_name'])) {
$missingNames[] = ts('Missing required fields:') . ' ' . ts('Organization Name');
}
break;
}
if (!empty($missingNames)) {
$errorMessage = ts('Missing required fields:') . ' ' . implode(' ' . ts('and') . ' ', $missingNames);
$errorRequired = TRUE;
}

if ($this->_emailIndex >= 0) {
/* If we don't have the required fields, bail */

if ($this->_contactType === 'Individual' && !$this->_updateWithId) {
if ($errorRequired && empty($values[$this->_emailIndex])) {
if ($errorMessage) {
$errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address');
}
else {
$errorMessage = ts('Missing required field:') . ' ' . ts('Email Address');
}
throw new CRM_Core_Exception($errorMessage);
}
}
}
elseif ($errorRequired && !$this->_updateWithId) {
if ($errorMessage) {
$errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address');
}
else {
$errorMessage = ts('Missing required field:') . ' ' . ts('Email Address');
}
throw new CRM_Core_Exception($errorMessage);
}
$this->validateRequiredContactFields($params['contact_type'], $params, $this->isUpdateExistingContacts());

//check for duplicate external Identifier
$externalID = $values[$this->_externalIdentifierIndex] ?? NULL;
$externalID = $params['external_identifier'] ?? NULL;
if ($externalID) {
/* If it's a dupe,external Identifier */

Expand Down
55 changes: 55 additions & 0 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,61 @@ public function setMaxLinesToProcess($max) {
$this->_maxLinesToProcess = $max;
}


/**
* Validate that we have the required fields to create the contact or find it to update.
*
* Note that the users duplicate selection affects this as follows
* - if they did not select an update variant then the id field is not
* permitted in the mapping - so we can assume the presence of id means
* we should use it
* - the external_identifier field is valid in place of the other fields
* when they have chosen update or fill - in this case we are only looking
* to update an existing contact.
*
* @param string $contactType
* @param array $params
* @param bool $isPermitExternalIdentifier
*
* @return void
* @throws \CRM_Core_Exception
*/
protected function validateRequiredContactFields(string $contactType, array $params, bool $isPermitExternalIdentifier = TRUE): void {
$requiredFields = [
'Individual' => [
'email' => ts('Email Address'),
'first_name_last_name' => ['first_name' => ts('First Name'), 'last_name' => ts('Last Name')]
],
'Organization' => ['organization_name' => ts('Organization Name')],
'Household' => ['household_name' => ts('Household Name')],
][$contactType];
$requiredFields['id'] = ts('Contact ID');
if ($isPermitExternalIdentifier) {
$requiredFields['external_identifier'] = ts('External Identifier');
}
$missingFields = [];
foreach ($requiredFields as $key => $required) {
if (!is_array($required)) {
if (!empty($params[$key])) {
return;
}
$missingFields[$key] = $required;
}
else {
foreach ($required as $field => $label) {
if (empty($params[$field])) {
$missing[$field] = $label;
}
}
if (empty($missing)) {
return;
}
$missingFields[$key] = implode(' ' . ts('and') . ' ', $missing);
}
throw new CRM_Core_Exception(ts('Missing required fields:') . ' ' . implode(' ' . ts('and') . ' ', $missingFields));
}
}

/**
* Determines the file extension based on error code.
*
Expand Down

0 comments on commit 2375d76

Please sign in to comment.