3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * Form to process actions on the group aspect of Custom Data.
20 class CRM_Contribute_Form_ContributionPage_Custom
extends CRM_Contribute_Form_ContributionPage
{
23 * Build the form object.
25 public function buildQuickForm() {
27 // Register 'contact_1' model
29 $entities[] = ['entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'];
30 $allowCoreTypes = array_merge(['Contact', 'Individual'], CRM_Contact_BAO_ContactType
::subTypes('Individual'));
33 // Register 'contribution_1'
34 $financialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'financial_type_id');
35 $allowCoreTypes[] = 'Contribution';
37 $allowSubTypes['ContributionType'] = [$financialTypeId];
39 'entity_name' => 'contribution_1',
40 'entity_type' => 'ContributionModel',
41 'entity_sub_type' => '*',
44 // If applicable, register 'membership_1'
45 $member = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
46 if ($member && $member['is_active']) {
49 'entity_name' => 'membership_1',
50 'entity_type' => 'MembershipModel',
51 'entity_sub_type' => '*',
53 $allowCoreTypes[] = 'Membership';
54 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
57 $this->addProfileSelector('custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
58 $this->addProfileSelector('custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
60 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Custom', 'formRule'], $this);
62 parent
::buildQuickForm();
66 * Set default values for the form.
68 * Note that in edit/view mode the default values are retrieved from the database.
70 public function setDefaultValues() {
71 $defaults = parent
::setDefaultValues();
73 $defaults['custom_pre_id'] = $this->_values
['custom_pre_id'];
74 $defaults['custom_post_id'] = $this->_values
['custom_post_id'];
82 public function postProcess() {
83 // get the submitted form values.
84 $params = $this->controller
->exportValues($this->_name
);
86 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
87 $params['id'] = $this->_id
;
90 $transaction = new CRM_Core_Transaction();
92 // also update uf join table
95 'module' => 'CiviContribute',
96 'entity_table' => 'civicrm_contribution_page',
97 'entity_id' => $this->_id
,
100 // first delete all past entries
101 CRM_Core_BAO_UFJoin
::deleteAll($ufJoinParams);
103 if (!empty($params['custom_pre_id'])) {
104 $ufJoinParams['weight'] = 1;
105 $ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
106 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
109 unset($ufJoinParams['id']);
111 if (!empty($params['custom_post_id'])) {
112 $ufJoinParams['weight'] = 2;
113 $ufJoinParams['uf_group_id'] = $params['custom_post_id'];
114 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
117 $transaction->commit();
118 parent
::endPostProcess();
122 * Return a descriptive name for the page, used in wizard header
126 public function getTitle() {
127 return ts('Include Profiles');
133 * @param array $fields
134 * The input form values.
137 * @param object $form
140 * true if no errors, else array of errors
142 public static function formRule($fields, $files, $form) {
144 $preProfileType = $postProfileType = NULL;
145 // for membership profile make sure Membership section is enabled
146 // get membership section for this contribution page
147 $dao = new CRM_Member_DAO_MembershipBlock();
148 $dao->entity_table
= 'civicrm_contribution_page';
149 $dao->entity_id
= $form->_id
;
151 $membershipEnable = FALSE;
153 if ($dao->find(TRUE) && $dao->is_active
) {
154 $membershipEnable = TRUE;
157 if ($fields['custom_pre_id']) {
158 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_pre_id']);
161 if ($fields['custom_post_id']) {
162 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_post_id']);
165 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
167 if (($preProfileType == 'Membership') && !$membershipEnable) {
168 $errors['custom_pre_id'] = $errorMsg;
171 if (($postProfileType == 'Membership') && !$membershipEnable) {
172 $errors['custom_post_id'] = $errorMsg;
175 $behalf = (!empty($form->_values
['onbehalf_profile_id'])) ?
$form->_values
['onbehalf_profile_id'] : NULL;
176 if ($fields['custom_pre_id']) {
177 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
178 if ($preProfileType == 'Membership' && $behalf) {
179 $errors['custom_pre_id'] = isset($errors['custom_pre_id']) ?
$errors['custom_pre_id'] . $errorMsg : $errorMsg;
183 if ($fields['custom_post_id']) {
184 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
185 if ($postProfileType == 'Membership' && $behalf) {
186 $errors['custom_post_id'] = isset($errors['custom_post_id']) ?
$errors['custom_post_id'] . $errorMsg : $errorMsg;
189 return empty($errors) ?
TRUE : $errors;