From bd6ce89babe4407a370da0179a77d26b262fab79 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 27 Oct 2021 08:13:22 +1300 Subject: [PATCH] Simplify usage of getting domain from email This adds a function that 'just gets the string' - rather than overloading another function and (worse) getting the string back as an array that needs to be 'currented'. It also switches the participant function to use it - as we will soon not be passing domainValues into this function --- CRM/Contact/Form/Task/EmailCommon.php | 4 +-- CRM/Contact/Form/Task/EmailTrait.php | 2 +- CRM/Core/BAO/Domain.php | 35 +++++++++++++++++++++++---- CRM/Event/BAO/Participant.php | 3 ++- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/CRM/Contact/Form/Task/EmailCommon.php b/CRM/Contact/Form/Task/EmailCommon.php index 07b0e8247d..22f1b995c6 100644 --- a/CRM/Contact/Form/Task/EmailCommon.php +++ b/CRM/Contact/Form/Task/EmailCommon.php @@ -28,7 +28,7 @@ class CRM_Contact_Form_Task_EmailCommon { * @param CRM_Core_Form $form * @param bool $bounce determine if we want to throw a status bounce. * - * @throws \API_Exception + * @throws \API_Exception|\CRM_Core_Exception */ public static function preProcessFromAddress(&$form, $bounce = TRUE) { $form->_emails = []; @@ -54,7 +54,7 @@ class CRM_Contact_Form_Task_EmailCommon { $defaults = CRM_Core_BAO_Email::getEmailSignatureDefaults($emailID); } if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) { - $defaults['from_email_address'] = current(CRM_Core_BAO_Domain::getNameAndEmail(FALSE, TRUE)); + $defaults['from_email_address'] = CRM_Core_BAO_Domain::getFromEmail(); } $form->setDefaults($defaults); } diff --git a/CRM/Contact/Form/Task/EmailTrait.php b/CRM/Contact/Form/Task/EmailTrait.php index 743edf6173..c4d6501507 100644 --- a/CRM/Contact/Form/Task/EmailTrait.php +++ b/CRM/Contact/Form/Task/EmailTrait.php @@ -311,7 +311,7 @@ trait CRM_Contact_Form_Task_EmailTrait { $defaults = CRM_Core_BAO_Email::getEmailSignatureDefaults($emailID); } if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) { - $defaults['from_email_address'] = current(CRM_Core_BAO_Domain::getNameAndEmail(FALSE, TRUE)); + $defaults['from_email_address'] = CRM_Core_BAO_Domain::getFromEmail(); } return $defaults; } diff --git a/CRM/Core/BAO/Domain.php b/CRM/Core/BAO/Domain.php index d65de0c187..b874f26a6b 100644 --- a/CRM/Core/BAO/Domain.php +++ b/CRM/Core/BAO/Domain.php @@ -107,6 +107,17 @@ class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { return version_compare(self::version(), $min, '>='); } + /** + * @return string + */ + protected static function getMissingDomainFromEmailMessage(): string { + $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address', + 'reset=1' + ); + $status = ts("There is no valid default from email address configured for the domain. You can configure here Configure From Email Address.", [1 => $url]); + return $status; + } + /** * Get the location values of a domain. * @@ -167,6 +178,8 @@ class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { /** * @param bool $skipFatal * @param bool $returnString + * If you are using this second parameter you probably are better + * calling `getFromEmail()` which will return an actual string. * * @return array * name & email for domain @@ -193,14 +206,26 @@ class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { return [NULL, NULL]; } - $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address', - 'reset=1' - ); - $status = ts("There is no valid default from email address configured for the domain. You can configure here Configure From Email Address.", [1 => $url]); + $status = self::getMissingDomainFromEmailMessage(); throw new CRM_Core_Exception($status); } + /** + * Get the domain email in a format suitable for using as the from address. + * + * @return string + * @throws \CRM_Core_Exception + */ + public static function getFromEmail(): string { + $email = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); + $email = current($email); + if (!$email) { + throw new CRM_Core_Exception(self::getMissingDomainFromEmailMessage()); + } + return $email; + } + /** * @param int $contactID * @@ -326,7 +351,7 @@ class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { $userID = CRM_Core_Session::getLoggedInContactID(); if (!empty($userID)) { - list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); + [$userName, $userEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); } // If still empty fall back to the logged in user details. // return empty values no matter what. diff --git a/CRM/Event/BAO/Participant.php b/CRM/Event/BAO/Participant.php index 5edcb9ea7a..f32cef2b59 100644 --- a/CRM/Event/BAO/Participant.php +++ b/CRM/Event/BAO/Participant.php @@ -1437,7 +1437,8 @@ UPDATE civicrm_participant } //take a receipt from as event else domain. - $receiptFrom = $domainValues['name'] . ' <' . $domainValues['email'] . '>'; + $receiptFrom = CRM_Core_BAO_Domain::getFromEmail(); + if (!empty($eventDetails['confirm_from_name']) && !empty($eventDetails['confirm_from_email'])) { $receiptFrom = $eventDetails['confirm_from_name'] . ' <' . $eventDetails['confirm_from_email'] . '>'; } -- 2.25.1