From 1ebb7282f4c40cb4647e54e67651360966e42a40 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 | 21 +++++++++++++ CRM/Contribute/Form/Contribution/Main.php | 29 ++++++++++++++++++ CRM/Contribute/Form/ContributionBase.php | 7 ----- CRM/Core/Form.php | 14 +++++++++ .../Registration/AdditionalParticipant.php | 20 +++++++++++++ CRM/Event/Form/Registration/Register.php | 28 +++++++++++++++++ CRM/PCP/Form/PCPAccount.php | 30 +++++++++++++++---- CRM/Profile/Form.php | 28 +++++++++++++++++ 8 files changed, 164 insertions(+), 13 deletions(-) diff --git a/CRM/Campaign/Form/Petition/Signature.php b/CRM/Campaign/Form/Petition/Signature.php index f60d55bc0e..ae55710cd4 100644 --- a/CRM/Campaign/Form/Petition/Signature.php +++ b/CRM/Campaign/Form/Petition/Signature.php @@ -152,6 +152,27 @@ class CRM_Campaign_Form_Petition_Signature extends CRM_Core_Form { 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?" + * + * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension. + * + * @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 6f1450dfe6..818fde0b2a 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -41,6 +41,35 @@ 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?" + * + * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension. + * + * @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 58e15df2af..5ecbb1b093 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -820,13 +820,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { } } - /** - * 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 ca3be72142..32f9ec17fc 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -2751,4 +2751,18 @@ class CRM_Core_Form extends HTML_QuickForm_Page { 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?" + * + * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension. + * + * @return array + */ + public function getUFGroupIDs() { + return []; + } + } diff --git a/CRM/Event/Form/Registration/AdditionalParticipant.php b/CRM/Event/Form/Registration/AdditionalParticipant.php index 1d369f93ba..e5614e018b 100644 --- a/CRM/Event/Form/Registration/AdditionalParticipant.php +++ b/CRM/Event/Form/Registration/AdditionalParticipant.php @@ -27,6 +27,26 @@ 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?" + * + * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension. + * + * @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 d47d6afd4c..163badbf5e 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -128,6 +128,34 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { 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?" + * + * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension. + * + * @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 5be2e61e82..9925bdbf70 100644 --- a/CRM/PCP/Form/PCPAccount.php +++ b/CRM/PCP/Form/PCPAccount.php @@ -35,6 +35,24 @@ 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?" + * + * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension. + * + * @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 +135,21 @@ class CRM_PCP_Form_PCPAccount extends CRM_Core_Form { * @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 af6642111c..e8e58cb7f7 100644 --- a/CRM/Profile/Form.php +++ b/CRM/Profile/Form.php @@ -270,6 +270,34 @@ class CRM_Profile_Form extends CRM_Core_Form { 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?" + * + * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension. + * + * @return array + */ + public function getUFGroupIDs() { + if (empty($this->_profileIds)) { + $dao = new CRM_Core_DAO_UFGroup(); + $dao->id = $this->_gid; + $this->_profileIds = (array) $dao; + } + 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. * -- 2.25.1