From 0aaf9af0707304620e28ec5c430f4d194a07ac62 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 2 Sep 2023 18:14:03 +1200 Subject: [PATCH] Use new lookup trait to eliminate use of undefined properties userDisplayName and contributorDisplayName --- CRM/Contribute/Form/AbstractEditPayment.php | 8 +-- CRM/Contribute/Form/AdditionalPayment.php | 6 +- CRM/Event/Form/Participant.php | 71 +++++++++++++++---- CRM/Pledge/Form/Pledge.php | 4 +- .../CRM/Event/Form/ParticipantTest.php | 2 +- 5 files changed, 69 insertions(+), 22 deletions(-) diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index c80ee13cf0..d14d7e5e33 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -749,11 +749,11 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { protected function assignContactEmailDetails() { if ($this->getContactID()) { - [$this->userDisplayName, $this->userEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->getContactID()); - if (empty($this->userDisplayName)) { - $this->userDisplayName = civicrm_api3('contact', 'getvalue', ['id' => $this->getContactID(), 'return' => 'display_name']); + [$displayName, $this->userEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->getContactID()); + if (!$displayName) { + $displayName = civicrm_api3('contact', 'getvalue', ['id' => $this->getContactID(), 'return' => 'display_name']); } - $this->assign('displayName', $this->userDisplayName); + $this->assign('displayName', $displayName); } } diff --git a/CRM/Contribute/Form/AdditionalPayment.php b/CRM/Contribute/Form/AdditionalPayment.php index bbf41548d0..de5821dc04 100644 --- a/CRM/Contribute/Form/AdditionalPayment.php +++ b/CRM/Contribute/Form/AdditionalPayment.php @@ -368,11 +368,11 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract $session = CRM_Core_Session::singleton(); // we need to retrieve email address - if ($this->_context == 'standalone' && !empty($this->_params['is_email_receipt'])) { - list($this->userDisplayName, + if ($this->_context === 'standalone' && !empty($this->_params['is_email_receipt'])) { + list($displayName, $this->userEmail ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId); - $this->assign('displayName', $this->userDisplayName); + $this->assign('displayName', $displayName); } $this->_params['amount'] = $this->_params['total_amount']; diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index a0512fbd1b..1fb974255a 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -16,11 +16,15 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ +use Civi\API\EntityLookupTrait; + /** * Back office participant form. */ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment { + use EntityLookupTrait; + /** * Participant ID - use getParticipantID. * @@ -290,8 +294,8 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); $this->assign('context', $this->_context); - if ($this->_contactID) { - $this->setPageTitle(ts('Event Registration for %1', [1 => $this->userDisplayName])); + if ($this->getContactID()) { + $this->setPageTitle(ts('Event Registration for %1', [1 => $this->getContactValue('display_name')])); } else { $this->setPageTitle(ts('Event Registration')); @@ -569,6 +573,25 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment return $defaults[$this->_id]; } + /** + * Get a value for the contact being acted on in the form. + * + * This can be called from any point in the form flow and if + * the contact can not yet be determined it will return NULL. + * + * @throws \CRM_Core_Exception + */ + public function getContactValue($fieldName) { + if ($this->isDefined('Contact')) { + return $this->lookup('Contact', $fieldName); + } + if ($this->getContactID()) { + $this->define('Contact', 'Contact', ['id' => $this->getContactID()]); + return $this->lookup('Contact', $fieldName); + } + return NULL; + } + /** * Build the form object. * @@ -961,7 +984,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $userName = CRM_Core_Session::singleton()->getLoggedInContactDisplayName(); if ($this->_contactId) { - [$this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail] = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId); + [, $this->_contributorEmail, $this->_toDoNotEmail] = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId); } //modify params according to parameter used in create @@ -1381,9 +1404,9 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment protected function getStatusMsg(array $params, int $numberSent, int $numberNotSent, string $updateStatusMsg): string { $statusMsg = ''; if (($this->_action & CRM_Core_Action::UPDATE)) { - $statusMsg = ts('Event registration information for %1 has been updated.', [1 => $this->_contributorDisplayName]); + $statusMsg = ts('Event registration information for %1 has been updated.', [1 => $this->getContactValue('display_name')]); if (!empty($params['send_receipt']) && $numberSent) { - $statusMsg .= ' ' . ts('A confirmation email has been sent to %1', [1 => $this->_contributorEmail]); + $statusMsg .= ' ' . ts('A confirmation email has been sent to %1', [1 => $this->getContactValue('email_primary.email')]); } if ($updateStatusMsg) { @@ -1391,9 +1414,9 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment } } elseif ($this->_action & CRM_Core_Action::ADD) { - $statusMsg = ts('Event registration for %1 has been added.', [1 => $this->_contributorDisplayName]); + $statusMsg = ts('Event registration for %1 has been added.', [1 => $this->getContactValue('display_name')]); if (!empty($params['send_receipt']) && $numberSent) { - $statusMsg .= ' ' . ts('A confirmation email has been sent to %1.', [1 => $this->_contributorEmail]); + $statusMsg .= ' ' . ts('A confirmation email has been sent to %1.', [1 => $this->getContactValue('email_primary.email')]); } } return $statusMsg; @@ -1554,9 +1577,9 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $form->add('textarea', 'receipt_text', ts('Confirmation Message')); // Retrieve the name and email of the contact - form will be the TO for receipt email ( only if context is not standalone) - if ($form->_context != 'standalone') { - if ($form->_contactId) { - [$form->_contributorDisplayName, $form->_contributorEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($form->_contactId); + if ($form->_context !== 'standalone') { + if ($form->getContactID()) { + [, $form->_contributorEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($form->_contactId); $form->assign('email', $form->_contributorEmail); } else { @@ -2172,7 +2195,7 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_ foreach ($this->_contactIds as $num => $contactID) { // Retrieve the name and email of the contact - this will be the TO for receipt email - [$this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail] = CRM_Contact_BAO_Contact::getContactDetails($contactID); + [, $this->_contributorEmail, $this->_toDoNotEmail] = CRM_Contact_BAO_Contact::getContactDetails($contactID); $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); $waitingStatus = $waitStatus[$params['status_id']] ?? NULL; @@ -2247,7 +2270,7 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_ // and the do-not-email option is not checked for that contact if ($this->_contributorEmail and !$this->_toDoNotEmail) { $sendTemplateParams['from'] = $params['from_email_address']; - $sendTemplateParams['toName'] = $this->_contributorDisplayName; + $sendTemplateParams['toName'] = $this->getContactValue('display_name'); $sendTemplateParams['toEmail'] = $this->_contributorEmail; $sendTemplateParams['cc'] = $this->_fromEmails['cc'] ?? NULL; $sendTemplateParams['bcc'] = $this->_fromEmails['bcc'] ?? NULL; @@ -2353,4 +2376,28 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_ return (bool) ($_GET['eventId'] ?? NULL); } + /** + * Get the contact ID in use. + * + * Ideally override this as appropriate to the form. + * + * @noinspection PhpUnhandledExceptionInspection + * @noinspection PhpDocSignatureIsNotCompleteInspection + */ + public function getContactID():?int { + if ($this->_contactID === NULL) { + if ($this->getSubmittedValue('contact_id')) { + $contactID = $this->getSubmittedValue('contact_id'); + } + else { + $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); + } + if (!$contactID && $this->getParticipantID()) { + $contactID = $this->getParticipantValue('contact_id'); + } + $this->_contactID = $contactID ? (int) $contactID : NULL; + } + return $this->_contactID; + } + } diff --git a/CRM/Pledge/Form/Pledge.php b/CRM/Pledge/Form/Pledge.php index 453818e92e..1da913341f 100644 --- a/CRM/Pledge/Form/Pledge.php +++ b/CRM/Pledge/Form/Pledge.php @@ -79,9 +79,9 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { return; } - $this->userDisplayName = $this->userEmail = NULL; + $displayName = $this->userEmail = NULL; if ($this->_contactID) { - [$this->userDisplayName, $this->userEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); + [$displayName, $this->userEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); } $this->setPageTitle(ts('Pledge')); diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php index 9a16722644..2d484f2820 100644 --- a/tests/phpunit/CRM/Event/Form/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php @@ -631,7 +631,7 @@ London,', $mut->checkMailLog([ 'From: "FIXME" ', - 'To: Anthony Anderson ', + 'To: "Mr. Anthony Anderson II" ', 'Subject: Event Confirmation - Annual CiviCRM meet - Mr. Anthony Anderson II', 'Dear Anthony,Contact the Development Department if you need to make any changes to your registration.', 'Event Information and Location', -- 2.25.1