Merge pull request #15902 from eileenmcnaughton/transaction_sillyness
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Custom.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
6a488035 13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
15 */
16
17/**
37b11065 18 * Form to process actions on the group aspect of Custom Data.
6a488035
TO
19 */
20class CRM_Contribute_Form_ContributionPage_Custom extends CRM_Contribute_Form_ContributionPage {
21
22 /**
fe482240 23 * Build the form object.
6a488035
TO
24 */
25 public function buildQuickForm() {
6a488035 26
ede14745 27 // Register 'contact_1' model
be2fb01f
CW
28 $entities = [];
29 $entities[] = ['entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'];
30 $allowCoreTypes = array_merge(['Contact', 'Individual'], CRM_Contact_BAO_ContactType::subTypes('Individual'));
31 $allowSubTypes = [];
c50f55b5
CW
32
33 // Register 'contribution_1'
34 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'financial_type_id');
35 $allowCoreTypes[] = 'Contribution';
99e239bc 36 //CRM-15427
be2fb01f
CW
37 $allowSubTypes['ContributionType'] = [$financialTypeId];
38 $entities[] = [
353ffa53
TO
39 'entity_name' => 'contribution_1',
40 'entity_type' => 'ContributionModel',
389bcebf 41 'entity_sub_type' => '*',
be2fb01f 42 ];
c50f55b5
CW
43
44 // If applicable, register 'membership_1'
45 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
46 if ($member && $member['is_active']) {
99e239bc 47 //CRM-15427
be2fb01f 48 $entities[] = [
353ffa53
TO
49 'entity_name' => 'membership_1',
50 'entity_type' => 'MembershipModel',
389bcebf 51 'entity_sub_type' => '*',
be2fb01f 52 ];
c50f55b5
CW
53 $allowCoreTypes[] = 'Membership';
54 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
6a488035 55 }
99e239bc 56 //CRM-15427
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);
6a488035 59
be2fb01f 60 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Custom', 'formRule'], $this);
6a488035
TO
61
62 parent::buildQuickForm();
63 }
64
65 /**
95cdcc0f 66 * Set default values for the form.
6a488035 67 *
95cdcc0f 68 * Note that in edit/view mode the default values are retrieved from the database.
6a488035 69 */
00be9182 70 public function setDefaultValues() {
6a488035
TO
71 $defaults = parent::setDefaultValues();
72
c0eb40c1 73 $defaults['custom_pre_id'] = $this->_values['custom_pre_id'];
74 $defaults['custom_post_id'] = $this->_values['custom_post_id'];
c50f55b5 75
6a488035
TO
76 return $defaults;
77 }
78
79 /**
fe482240 80 * Process the form.
6a488035
TO
81 */
82 public function postProcess() {
83 // get the submitted form values.
84 $params = $this->controller->exportValues($this->_name);
85
86 if ($this->_action & CRM_Core_Action::UPDATE) {
87 $params['id'] = $this->_id;
88 }
89
90 $transaction = new CRM_Core_Transaction();
91
92 // also update uf join table
be2fb01f 93 $ufJoinParams = [
6a488035
TO
94 'is_active' => 1,
95 'module' => 'CiviContribute',
96 'entity_table' => 'civicrm_contribution_page',
97 'entity_id' => $this->_id,
be2fb01f 98 ];
6a488035
TO
99
100 // first delete all past entries
101 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
102
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);
107 }
108
109 unset($ufJoinParams['id']);
110
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);
115 }
116
117 $transaction->commit();
118 parent::endPostProcess();
119 }
120
121 /**
122 * Return a descriptive name for the page, used in wizard header
123 *
124 * @return string
6a488035
TO
125 */
126 public function getTitle() {
127 return ts('Include Profiles');
128 }
129
130 /**
fe482240 131 * Global form rule.
6a488035 132 *
014c4014
TO
133 * @param array $fields
134 * The input form values.
da6b46f4
EM
135 *
136 * @param $files
c0eb40c1 137 * @param object $form
6a488035 138 *
72b3a70c
CW
139 * @return bool|array
140 * true if no errors, else array of errors
6a488035 141 */
c0eb40c1 142 public static function formRule($fields, $files, $form) {
be2fb01f 143 $errors = [];
6a488035
TO
144 $preProfileType = $postProfileType = NULL;
145 // for membership profile make sure Membership section is enabled
146 // get membership section for this contribution page
353ffa53 147 $dao = new CRM_Member_DAO_MembershipBlock();
6a488035 148 $dao->entity_table = 'civicrm_contribution_page';
c0eb40c1 149 $dao->entity_id = $form->_id;
6a488035
TO
150
151 $membershipEnable = FALSE;
152
153 if ($dao->find(TRUE) && $dao->is_active) {
154 $membershipEnable = TRUE;
155 }
156
157 if ($fields['custom_pre_id']) {
158 $preProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_pre_id']);
159 }
160
161 if ($fields['custom_post_id']) {
162 $postProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_post_id']);
163 }
164
165 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
166
167 if (($preProfileType == 'Membership') && !$membershipEnable) {
168 $errors['custom_pre_id'] = $errorMsg;
169 }
170
171 if (($postProfileType == 'Membership') && !$membershipEnable) {
172 $errors['custom_post_id'] = $errorMsg;
173 }
665e5ec7 174
c0eb40c1 175 $behalf = (!empty($form->_values['onbehalf_profile_id'])) ? $form->_values['onbehalf_profile_id'] : NULL;
6a488035
TO
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;
180 }
181 }
182
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;
187 }
188 }
189 return empty($errors) ? TRUE : $errors;
190 }
96025800 191
6a488035 192}