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 * Set variables up before form is built.
25 public function preProcess() {
27 $this->setSelectedChild('custom');
31 * Build the form object.
33 public function buildQuickForm() {
35 // Register 'contact_1' model
37 $entities[] = ['entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'];
38 $allowCoreTypes = array_merge(['Contact', 'Individual'], CRM_Contact_BAO_ContactType
::subTypes('Individual'));
41 // Register 'contribution_1'
42 $financialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'financial_type_id');
43 $allowCoreTypes[] = 'Contribution';
45 $allowSubTypes['ContributionType'] = [$financialTypeId];
47 'entity_name' => 'contribution_1',
48 'entity_type' => 'ContributionModel',
49 'entity_sub_type' => '*',
52 // If applicable, register 'membership_1'
53 $member = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
54 if ($member && $member['is_active']) {
57 'entity_name' => 'membership_1',
58 'entity_type' => 'MembershipModel',
59 'entity_sub_type' => '*',
61 $allowCoreTypes[] = 'Membership';
62 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
65 $this->addProfileSelector('custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
66 $this->addProfileSelector('custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
68 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Custom', 'formRule'], $this);
70 parent
::buildQuickForm();
74 * Set default values for the form.
76 * Note that in edit/view mode the default values are retrieved from the database.
78 public function setDefaultValues() {
79 $defaults = parent
::setDefaultValues();
81 $defaults['custom_pre_id'] = $this->_values
['custom_pre_id'];
82 $defaults['custom_post_id'] = $this->_values
['custom_post_id'];
90 public function postProcess() {
91 // get the submitted form values.
92 $params = $this->controller
->exportValues($this->_name
);
94 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
95 $params['id'] = $this->_id
;
98 $transaction = new CRM_Core_Transaction();
100 // also update uf join table
103 'module' => 'CiviContribute',
104 'entity_table' => 'civicrm_contribution_page',
105 'entity_id' => $this->_id
,
108 // first delete all past entries
109 CRM_Core_BAO_UFJoin
::deleteAll($ufJoinParams);
111 if (!empty($params['custom_pre_id'])) {
112 $ufJoinParams['weight'] = 1;
113 $ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
114 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
117 unset($ufJoinParams['id']);
119 if (!empty($params['custom_post_id'])) {
120 $ufJoinParams['weight'] = 2;
121 $ufJoinParams['uf_group_id'] = $params['custom_post_id'];
122 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
125 $transaction->commit();
126 parent
::endPostProcess();
130 * Return a descriptive name for the page, used in wizard header
134 public function getTitle() {
135 return ts('Include Profiles');
141 * @param array $fields
142 * The input form values.
145 * @param object $form
148 * true if no errors, else array of errors
150 public static function formRule($fields, $files, $form) {
152 $preProfileType = $postProfileType = NULL;
153 // for membership profile make sure Membership section is enabled
154 // get membership section for this contribution page
155 $dao = new CRM_Member_DAO_MembershipBlock();
156 $dao->entity_table
= 'civicrm_contribution_page';
157 $dao->entity_id
= $form->_id
;
159 $membershipEnable = FALSE;
161 if ($dao->find(TRUE) && $dao->is_active
) {
162 $membershipEnable = TRUE;
165 if ($fields['custom_pre_id']) {
166 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_pre_id']);
169 if ($fields['custom_post_id']) {
170 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_post_id']);
173 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
175 if (($preProfileType == 'Membership') && !$membershipEnable) {
176 $errors['custom_pre_id'] = $errorMsg;
179 if (($postProfileType == 'Membership') && !$membershipEnable) {
180 $errors['custom_post_id'] = $errorMsg;
183 $behalf = (!empty($form->_values
['onbehalf_profile_id'])) ?
$form->_values
['onbehalf_profile_id'] : NULL;
184 if ($fields['custom_pre_id']) {
185 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
186 if ($preProfileType == 'Membership' && $behalf) {
187 $errors['custom_pre_id'] = isset($errors['custom_pre_id']) ?
$errors['custom_pre_id'] . $errorMsg : $errorMsg;
191 if ($fields['custom_post_id']) {
192 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
193 if ($postProfileType == 'Membership' && $behalf) {
194 $errors['custom_post_id'] = isset($errors['custom_post_id']) ?
$errors['custom_post_id'] . $errorMsg : $errorMsg;
197 return empty($errors) ?
TRUE : $errors;