Skip to content

Commit

Permalink
Add helper functions for reCAPTCHA extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed May 24, 2021
1 parent 8e1366e commit 29bb652
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 13 deletions.
19 changes: 19 additions & 0 deletions CRM/Campaign/Form/Petition/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions CRM/Contribute/Form/Contribution/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
7 changes: 0 additions & 7 deletions CRM/Contribute/Form/ContributionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}

}
18 changes: 18 additions & 0 deletions CRM/Event/Form/Registration/AdditionalParticipant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
26 changes: 26 additions & 0 deletions CRM/Event/Form/Registration/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
28 changes: 22 additions & 6 deletions CRM/PCP/Form/PCPAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions CRM/Profile/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 29bb652

Please sign in to comment.