From 786a9636b569b0aa0528be280221bda2799dd322 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Sun, 16 May 2021 20:07:21 +0100 Subject: [PATCH] Add helper functions for reCAPTCHA extension --- CRM/Campaign/Form/Petition/Signature.php | 19 +++++++++++++ CRM/Contribute/Form/Contribution/Main.php | 27 ++++++++++++++++++ CRM/Contribute/Form/ContributionBase.php | 7 ----- CRM/Core/Form.php | 12 ++++++++ .../Registration/AdditionalParticipant.php | 18 ++++++++++++ CRM/Event/Form/Registration/Register.php | 26 +++++++++++++++++ CRM/PCP/Form/PCPAccount.php | 28 +++++++++++++++---- CRM/Profile/Form.php | 24 ++++++++++++++++ 8 files changed, 148 insertions(+), 13 deletions(-) diff --git a/CRM/Campaign/Form/Petition/Signature.php b/CRM/Campaign/Form/Petition/Signature.php index f60d55bc0e06..edd084b48fe9 100644 --- a/CRM/Campaign/Form/Petition/Signature.php +++ b/CRM/Campaign/Form/Petition/Signature.php @@ -152,6 +152,25 @@ public function getContactID() { return $session->get('userID'); } + /** + * Get the active UFGroups (profiles) on this form + * Many forms load one or more UFGroups (profiles). + * This provides a standard function to retrieve the IDs of those profiles from the form + * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?" + * + * @return array + */ + public function getUFGroupIDs() { + $ufGroupIDs = []; + if (!empty($this->_contactProfileId)) { + $ufGroupIDs[] = $this->_contactProfileId; + } + if (!empty($this->_activityProfileId)) { + $ufGroupIDs[] = $this->_activityProfileId; + } + return $ufGroupIDs; + } + public function preProcess() { $this->bao = new CRM_Campaign_BAO_Petition(); $this->_mode = self::MODE_CREATE; diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 6f1450dfe6bf..8c23fc214add 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -41,6 +41,33 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu protected $_paymentProcessorID; protected $_snippet; + /** + * Get the active UFGroups (profiles) on this form + * Many forms load one or more UFGroups (profiles). + * This provides a standard function to retrieve the IDs of those profiles from the form + * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?" + * + * @return array + */ + public function getUFGroupIDs() { + $ufGroupIDs = []; + if (!empty($this->_values['custom_pre_id'])) { + $ufGroupIDs[] = $this->_values['custom_pre_id']; + } + if (!empty($this->_values['custom_post_id'])) { + // custom_post_id can be an array (because we can have multiple for events). + // It is handled as array for contribution page as well though they don't support multiple profiles. + if (!is_array($this->_values['custom_post_id'])) { + $ufGroupIDs[] = $this->_values['custom_post_id']; + } + else { + $ufGroupIDs = array_merge($ufGroupIDs, $this->_values['custom_post_id']); + } + } + + return $ufGroupIDs; + } + /** * Set variables up before form is built. */ diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index 58e15df2afd7..5ecbb1b0937e 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -820,13 +820,6 @@ public function assignPaymentFields() { } } - /** - * Check if ReCAPTCHA has to be added on Contribution form forcefully. - */ - protected function hasToAddForcefully() { - return CRM_Utils_ReCAPTCHA::hasToAddForcefully(); - } - /** * Add onbehalf/honoree profile fields and native module fields. * diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 84dce6fa352b..5ecd6735cd91 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -2749,4 +2749,16 @@ protected function getSubmittedValue(string $fieldName) { return $this->exportedValues[$fieldName] ?? NULL; } + /** + * Get the active UFGroups (profiles) on this form + * Many forms load one or more UFGroups (profiles). + * This provides a standard function to retrieve the IDs of those profiles from the form + * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?" + * + * @return array + */ + public function getUFGroupIDs() { + return []; + } + } diff --git a/CRM/Event/Form/Registration/AdditionalParticipant.php b/CRM/Event/Form/Registration/AdditionalParticipant.php index 1d369f93ba57..c6b8ebf5311a 100644 --- a/CRM/Event/Form/Registration/AdditionalParticipant.php +++ b/CRM/Event/Form/Registration/AdditionalParticipant.php @@ -27,6 +27,24 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R */ public $additionalParticipantId = NULL; + /** + * Get the active UFGroups (profiles) on this form + * Many forms load one or more UFGroups (profiles). + * This provides a standard function to retrieve the IDs of those profiles from the form + * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?" + * + * @return array + */ + public function getUFGroupIDs() { + $ufGroupIDs = []; + foreach (['pre', 'post'] as $keys) { + if (isset($this->_values['additional_custom_' . $keys . '_id'])) { + $ufGroupIDs[] = $this->_values['additional_custom_' . $keys . '_id']; + } + } + return $ufGroupIDs; + } + /** * Set variables up before form is built. * diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index d47d6afd4c23..e67127c9cb7a 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -128,6 +128,32 @@ public static function getRegistrationContactID($fields, $form, $isAdditional) { return $contactID; } + /** + * Get the active UFGroups (profiles) on this form + * Many forms load one or more UFGroups (profiles). + * This provides a standard function to retrieve the IDs of those profiles from the form + * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?" + * + * @return array + */ + public function getUFGroupIDs() { + $ufGroupIDs = []; + if (!empty($this->_values['custom_pre_id'])) { + $ufGroupIDs[] = $this->_values['custom_pre_id']; + } + if (!empty($this->_values['custom_post_id'])) { + // custom_post_id can be an array (because we can have multiple for events). + // It is handled as array for contribution page as well though they don't support multiple profiles. + if (!is_array($this->_values['custom_post_id'])) { + $ufGroupIDs[] = $this->_values['custom_post_id']; + } + else { + $ufGroupIDs = array_merge($ufGroupIDs, $this->_values['custom_post_id']); + } + } + return $ufGroupIDs; + } + /** * Set variables up before form is built. * diff --git a/CRM/PCP/Form/PCPAccount.php b/CRM/PCP/Form/PCPAccount.php index 5be2e61e829a..961e75df9b2e 100644 --- a/CRM/PCP/Form/PCPAccount.php +++ b/CRM/PCP/Form/PCPAccount.php @@ -35,6 +35,22 @@ class CRM_PCP_Form_PCPAccount extends CRM_Core_Form { */ public $_single; + /** + * Get the active UFGroups (profiles) on this form + * Many forms load one or more UFGroups (profiles). + * This provides a standard function to retrieve the IDs of those profiles from the form + * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?" + * + * @return array + */ + public function getUFGroupIDs() { + $ufGroupIDs = []; + if (!empty($this->_pageId)) { + $ufGroupIDs[] = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component); + } + return $ufGroupIDs; + } + public function preProcess() { $session = CRM_Core_Session::singleton(); $config = CRM_Core_Config::singleton(); @@ -117,21 +133,21 @@ public function setDefaultValues() { * @return void */ public function buildQuickForm() { - $id = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component); - if (CRM_PCP_BAO_PCP::checkEmailProfile($id)) { + $ufGroupID = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component); + if (CRM_PCP_BAO_PCP::checkEmailProfile($ufGroupID)) { $this->assign('profileDisplay', TRUE); } $fields = NULL; if ($this->_contactID) { - if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $this->_contactID)) { - $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD); + if (CRM_Core_BAO_UFGroup::filterUFGroups($ufGroupID, $this->_contactID)) { + $fields = CRM_Core_BAO_UFGroup::getFields($ufGroupID, FALSE, CRM_Core_Action::ADD); } $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this); } else { - CRM_Core_BAO_CMSUser::buildForm($this, $id, TRUE); + CRM_Core_BAO_CMSUser::buildForm($this, $ufGroupID, TRUE); - $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD); + $fields = CRM_Core_BAO_UFGroup::getFields($ufGroupID, FALSE, CRM_Core_Action::ADD); } if ($fields) { diff --git a/CRM/Profile/Form.php b/CRM/Profile/Form.php index af6642111c68..af3d7d36c797 100644 --- a/CRM/Profile/Form.php +++ b/CRM/Profile/Form.php @@ -270,6 +270,30 @@ public function getDefaultEntity() { return 'Profile'; } + /** + * Get the active UFGroups (profiles) on this form + * Many forms load one or more UFGroups (profiles). + * This provides a standard function to retrieve the IDs of those profiles from the form + * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?" + * + * @return array + */ + public function getUFGroupIDs() { + if (empty($this->_profileIds)) { + $this->_profileIds = $this->get('profileIds'); + } + return $this->_profileIds; + } + + /** + * Are we using the profile in create mode? + * + * @return bool + */ + public function getIsCreateMode() { + return ($this->_mode == self::MODE_CREATE); + } + /** * Pre processing work done here. *