From: Eileen McNaughton Date: Sat, 2 Sep 2023 05:14:27 +0000 (+1200) Subject: Extract getContactID, assignContactID X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6771f4aba0868969ad8f96a1019ff6fcc15cebf7;p=civicrm-core.git Extract getContactID, assignContactID --- diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index 90b2fe1522..c80ee13cf0 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -262,12 +262,7 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { * @throws \CRM_Core_Exception */ public function preProcess() { - $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); - if (empty($this->_contactID) && !empty($this->_id) && $this->entity) { - $this->_contactID = civicrm_api3($this->entity, 'getvalue', ['id' => $this->_id, 'return' => 'contact_id']); - } - $this->assign('contactID', $this->_contactID); - CRM_Core_Resources::singleton()->addVars('coreForm', ['contact_id' => (int) $this->_contactID]); + $this->assignContactID(); $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); $this->_mode = empty($this->_mode) ? CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this) : $this->_mode; $this->assign('isBackOffice', $this->isBackOffice); @@ -275,6 +270,24 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { $this->assignPaymentRelatedVariables(); } + /** + * 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) { + $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); + if (empty($this->_contactID) && !empty($this->_id) && $this->entity) { + $this->_contactID = civicrm_api3($this->entity, 'getvalue', ['id' => $this->_id, 'return' => 'contact_id']); + } + } + return $this->_contactID ? (int) $this->_contactID : NULL; + } + /** * @param int $id */ @@ -620,7 +633,7 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { $fields["email-{$this->_bltID}"] = 1; } - list($hasBillingField, $addressParams) = CRM_Contribute_BAO_Contribution::getPaymentProcessorReadyAddressParams($this->_params, $this->_bltID); + [$hasBillingField, $addressParams] = CRM_Contribute_BAO_Contribution::getPaymentProcessorReadyAddressParams($this->_params, $this->_bltID); $fields = $this->formatParamsForPaymentProcessor($fields); if ($hasBillingField) { @@ -735,13 +748,19 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { } protected function assignContactEmailDetails() { - if ($this->_contactID) { - list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); + 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->_contactID, 'return' => 'display_name']); + $this->userDisplayName = civicrm_api3('contact', 'getvalue', ['id' => $this->getContactID(), 'return' => 'display_name']); } $this->assign('displayName', $this->userDisplayName); } } + protected function assignContactID(): void { + $this->assign('contactID', $this->getContactID()); + CRM_Core_Resources::singleton() + ->addVars('coreForm', ['contact_id' => (int) $this->getContactID()]); + } + } diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index 43ce6e6b5b..a0512fbd1b 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -285,7 +285,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $this->assign('feeBlockPaid', FALSE); // @todo eliminate this duplication. - $this->_contactId = $this->_contactID; + $this->_contactId = $this->getContactID(); $this->_eID = CRM_Utils_Request::retrieve('eid', 'Positive', $this); $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); $this->assign('context', $this->_context); @@ -1515,7 +1515,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment // don't show transaction id in batch update mode $path = CRM_Utils_System::currentPath(); $form->assign('showTransactionId', FALSE); - if ($path != 'civicrm/contact/search/basic') { + if ($path !== 'civicrm/contact/search/basic') { $form->add('text', 'trxn_id', ts('Transaction ID')); $form->addRule('trxn_id', ts('Transaction ID already exists in Database.'), 'objectExists', ['CRM_Contribute_DAO_Contribution', $form->_eventId, 'trxn_id'] @@ -1792,7 +1792,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment } $contribParams['is_test'] = 0; - if ($form->_action & CRM_Core_Action::PREVIEW || ($params['mode'] ?? NULL) == 'test') { + if ($form->_action & CRM_Core_Action::PREVIEW || ($params['mode'] ?? NULL) === 'test') { $contribParams['is_test'] = 1; } @@ -1941,7 +1941,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment */ protected function getParticipantValue($fieldName) { if (!$this->participantRecord) { - $this->participantRecord = civicrm_api3('Participant', 'getsingle', ['id' => $this->_id]); + $this->participantRecord = civicrm_api3('Participant', 'getsingle', ['id' => $this->getParticipantID()]); } return $this->participantRecord[$fieldName] ?? $this->participantRecord['participant_' . $fieldName]; }