From ff483a7a7c4e88568168496595131ca4ef52d23d Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 12 Sep 2023 11:17:22 +1200 Subject: [PATCH] php8.x Remove an undefined property from Pledge form This uses the new lookup to get the contact value --- CRM/Contact/Form/ContactFormTrait.php | 58 +++++++++++++++++++++++++++ CRM/Event/Form/Participant.php | 20 +-------- CRM/Pledge/Form/Pledge.php | 14 ++----- 3 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 CRM/Contact/Form/ContactFormTrait.php diff --git a/CRM/Contact/Form/ContactFormTrait.php b/CRM/Contact/Form/ContactFormTrait.php new file mode 100644 index 0000000000..9a7a00ea71 --- /dev/null +++ b/CRM/Contact/Form/ContactFormTrait.php @@ -0,0 +1,58 @@ +isDefined('Contact')) { + return $this->lookup('Contact', $fieldName); + } + $id = $this->getContactID(); + if ($id) { + // GetContactID may or may not have defined this. + if (!$this->isDefined('Contact')) { + $this->define('Contact', 'Contact', ['id' => $id]); + } + return $this->lookup('Contact', $fieldName); + } + return NULL; + } + + /** + * Get the contact ID. + * + * Override this for more complex retrieval as required by the form. + * + * @return int|null + * + * @noinspection PhpUnhandledExceptionInspection + * @noinspection PhpDocMissingThrowsInspection + */ + public function getContactID(): ?int { + $id = (int) CRM_Utils_Request::retrieve('cid', 'Positive', $this); + if ($id) { + $this->define('Contact', 'Contact', ['id' => $id]); + } + return $id ?: NULL; + } + +} diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index b9d3905fb1..21803d04a7 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -24,6 +24,7 @@ use Civi\API\EntityLookupTrait; class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment { use EntityLookupTrait; + use CRM_Contact_Form_ContactFormTrait; /** * Participant ID - use getParticipantID. @@ -573,25 +574,6 @@ 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. * diff --git a/CRM/Pledge/Form/Pledge.php b/CRM/Pledge/Form/Pledge.php index 1da913341f..27f45ca53a 100644 --- a/CRM/Pledge/Form/Pledge.php +++ b/CRM/Pledge/Form/Pledge.php @@ -21,6 +21,8 @@ use Civi\Api4\PledgePayment; * This class generates form components for processing a pledge */ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { + use CRM_Contact_Form_ContactFormTrait; + public $_action; /** @@ -79,11 +81,6 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { return; } - $displayName = $this->userEmail = NULL; - if ($this->_contactID) { - [$displayName, $this->userEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); - } - $this->setPageTitle(ts('Pledge')); // build custom data @@ -169,10 +166,7 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { $defaultPledgeStatus )); - if (isset($this->userEmail)) { - $this->assign('email', $this->userEmail); - } - + $this->assign('email', $this->getContactValue('email_primary.email')); // custom data set defaults $defaults += CRM_Custom_Form_CustomData::setDefaultValues($this); @@ -538,7 +532,7 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { // send Acknowledgment mail. CRM_Pledge_BAO_Pledge::sendAcknowledgment($this, $params); - $statusMsg .= ' ' . ts('An acknowledgment email has been sent to %1.
', [1 => $this->userEmail]); + $statusMsg .= ' ' . ts('An acknowledgment email has been sent to %1.
', [1 => $this->getContactValue('email_primary.email')]); // get the first valid payment id. $nextPaymentID = PledgePayment::get() ->addWhere('pledge_id', '=', $pledgeID) -- 2.25.1