-
-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure contacts without a name are updated when primary email changes #20403
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,17 +170,9 @@ public function postProcess() { | |
} | ||
CRM_Core_BAO_Block::create('email', $params); | ||
|
||
// If contact has no name, set primary email as display name | ||
// TODO: This should be handled in the BAO for the benefit of the api, etc. | ||
// Changing email might change a contact's display_name so refresh name block content | ||
if (!$this->contactHasName) { | ||
foreach ($params['email'] as $email) { | ||
if ($email['is_primary']) { | ||
CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name', $email['email']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ohh nasty - set from the form! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol |
||
CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'sort_name', $email['email']); | ||
$this->ajaxResponse['reloadBlocks'] = ['#crm-contactname-content']; | ||
break; | ||
} | ||
} | ||
$this->ajaxResponse['reloadBlocks'] = ['#crm-contactname-content']; | ||
} | ||
|
||
$this->log(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -348,7 +348,7 @@ public static function handlePrimary(&$params, $class) { | |
$entity = new $class(); | ||
$entity->id = $params['id']; | ||
$entity->find(TRUE); | ||
$contactId = $entity->contact_id; | ||
$contactId = $params['contact_id'] = $entity->contact_id; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK - this line is only hit if $params['contact_id'] is not set |
||
} | ||
// If entity is not associated with contact, concept of is_primary not relevant | ||
if (!$contactId) { | ||
|
@@ -397,6 +397,9 @@ public static function handlePrimary(&$params, $class) { | |
// primary or return if is already is | ||
$existingEntities->is_primary = 1; | ||
$existingEntities->save(); | ||
if ($class === 'CRM_Core_BAO_Email') { | ||
CRM_Core_BAO_Email::updateContactName($contactId, $existingEntities->email); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,12 @@ public static function create($params) { | |
|
||
$email->save(); | ||
|
||
$contactId = (int) ($email->contact_id ?? CRM_Core_DAO::getFieldValue(__CLASS__, $email->id, 'contact_id')); | ||
if ($contactId && $email->is_primary) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it is changed from primary to not primary no update will happen but I think that is ok because what is set in place of this will trigger a change |
||
$address = $email->email ?? CRM_Core_DAO::getFieldValue(__CLASS__, $email->id, 'email'); | ||
self::updateContactName($contactId, $address); | ||
} | ||
|
||
if ($email->is_primary) { | ||
// update the UF user email if that has changed | ||
CRM_Core_BAO_UFMatch::updateUFName($email->contact_id); | ||
|
@@ -361,4 +367,19 @@ public static function getEntityRefFilters() { | |
return $contactFields; | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @param int $contactId | ||
* @param string $primaryEmail | ||
*/ | ||
public static function updateContactName($contactId, string $primaryEmail) { | ||
if (is_string($primaryEmail) && $primaryEmail !== '' && | ||
!CRM_Contact_BAO_Contact::hasName(['id' => $contactId]) | ||
) { | ||
CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name', $primaryEmail); | ||
CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $contactId, 'sort_name', $primaryEmail); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we can get this all down to one extra query if we retrieve the potential name fields at this point too. It's a bit messier php-wise but I'm very conscious that this is a high traffic function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except that
CRM_Core_DAO::getFieldValue
uses caching so if we don't use it we don't get the potential benefit of the cache.I guess I could write a new
getFieldValues()
(plural) function which re-uses the same cache...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - I don't think we use getFieldValue enough to actually have it cached much? I feel like we have been mostly using the api (increasingly) to retrieve stuff like that. However, I guess it's arguable enough now that I won't hold this up on it