Skip to content

Commit

Permalink
Use new lookup trait to eliminate use of undefined properties userDis…
Browse files Browse the repository at this point in the history
…playName and contributorDisplayName
  • Loading branch information
eileenmcnaughton committed Sep 4, 2023
1 parent 6771f4a commit 0aaf9af
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
8 changes: 4 additions & 4 deletions CRM/Contribute/Form/AbstractEditPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,11 +749,11 @@ protected function assignPaymentInfoBlock() {

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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions CRM/Contribute/Form/AdditionalPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,11 @@ public function processCreditCard() {
$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'];
Expand Down
71 changes: 59 additions & 12 deletions CRM/Event/Form/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -290,8 +294,8 @@ public function preProcess() {
$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'));
Expand Down Expand Up @@ -569,6 +573,25 @@ public function setDefaultValues(): array {
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.
*
Expand Down Expand Up @@ -961,7 +984,7 @@ public function submit($params) {
$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
Expand Down Expand Up @@ -1381,19 +1404,19 @@ public function setCustomDataTypes(): void {
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) {
$statusMsg = "{$statusMsg} {$updateStatusMsg}";
}
}
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;
Expand Down Expand Up @@ -1554,9 +1577,9 @@ public function buildEventFeeForm($form) {
$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 {
Expand Down Expand Up @@ -2172,7 +2195,7 @@ protected function sendReceipts($params, $total_amount, array $customFields, arr

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;
Expand Down Expand Up @@ -2247,7 +2270,7 @@ protected function sendReceipts($params, $total_amount, array $customFields, arr
// 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;
Expand Down Expand Up @@ -2353,4 +2376,28 @@ protected function isOverloadFeesMode(): bool {
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;
}

}
4 changes: 2 additions & 2 deletions CRM/Pledge/Form/Pledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public function preProcess(): void {
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'));
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Event/Form/ParticipantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ protected function assertPartialPaymentResult(bool $isQuickConfig, CiviMailUtils

$mut->checkMailLog([
'From: "FIXME" <[email protected]>',
'To: Anthony Anderson <[email protected]>',
'To: "Mr. Anthony Anderson II" <[email protected]>',
'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',
Expand Down

0 comments on commit 0aaf9af

Please sign in to comment.