Use new lookup trait to eliminate use of undefined properties userDisplayName and...
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 2 Sep 2023 06:14:03 +0000 (18:14 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 4 Sep 2023 01:53:22 +0000 (13:53 +1200)
CRM/Contribute/Form/AbstractEditPayment.php
CRM/Contribute/Form/AdditionalPayment.php
CRM/Event/Form/Participant.php
CRM/Pledge/Form/Pledge.php
tests/phpunit/CRM/Event/Form/ParticipantTest.php

index c80ee13cf0c152f886848ecb3250765b8fa12a52..d14d7e5e338ba7dd8f4135a47fa8bdc1dc78f5d8 100644 (file)
@@ -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);
     }
   }
 
index bbf41548d04678cf51f46d47e6e6cfae868a9b72..de5821dc04f30b4cb92a7b6008f363582ef1cb39 100644 (file)
@@ -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'];
index a0512fbd1bb3804663b43a11077a00ac8bcec5ae..1fb974255a29ddd43bb1012181c3d649fdef3772 100644 (file)
  * @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;
+  }
+
 }
index 453818e92eb88e2294501a6fc8cbac3c150f3c4f..1da913341f8464becafb9207b514a9a0985c4c71 100644 (file)
@@ -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'));
index 9a16722644f40cbe9d9bdddae7a29df73025ce6e..2d484f2820bd6975b8a0864f20b372c7c13aeadc 100644 (file)
@@ -631,7 +631,7 @@ London,',
 
     $mut->checkMailLog([
       'From: "FIXME" <info@EXAMPLE.ORG>',
-      'To: Anthony Anderson <anthony_anderson@civicrm.org>',
+      'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
       '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',