Merge pull request #15881 from civicrm/5.20
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Custom.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * Form to process actions on the group aspect of Custom Data.
19 */
20 class CRM_Contribute_Form_ContributionPage_Custom extends CRM_Contribute_Form_ContributionPage {
21
22 /**
23 * Build the form object.
24 */
25 public function buildQuickForm() {
26
27 // Register 'contact_1' model
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 = [];
32
33 // Register 'contribution_1'
34 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'financial_type_id');
35 $allowCoreTypes[] = 'Contribution';
36 //CRM-15427
37 $allowSubTypes['ContributionType'] = [$financialTypeId];
38 $entities[] = [
39 'entity_name' => 'contribution_1',
40 'entity_type' => 'ContributionModel',
41 'entity_sub_type' => '*',
42 ];
43
44 // If applicable, register 'membership_1'
45 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
46 if ($member && $member['is_active']) {
47 //CRM-15427
48 $entities[] = [
49 'entity_name' => 'membership_1',
50 'entity_type' => 'MembershipModel',
51 'entity_sub_type' => '*',
52 ];
53 $allowCoreTypes[] = 'Membership';
54 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
55 }
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);
59
60 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Custom', 'formRule'], $this);
61
62 parent::buildQuickForm();
63 }
64
65 /**
66 * Set default values for the form.
67 *
68 * Note that in edit/view mode the default values are retrieved from the database.
69 */
70 public function setDefaultValues() {
71 $defaults = parent::setDefaultValues();
72
73 $defaults['custom_pre_id'] = $this->_values['custom_pre_id'];
74 $defaults['custom_post_id'] = $this->_values['custom_post_id'];
75
76 return $defaults;
77 }
78
79 /**
80 * Process the form.
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
93 $ufJoinParams = [
94 'is_active' => 1,
95 'module' => 'CiviContribute',
96 'entity_table' => 'civicrm_contribution_page',
97 'entity_id' => $this->_id,
98 ];
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
125 */
126 public function getTitle() {
127 return ts('Include Profiles');
128 }
129
130 /**
131 * Global form rule.
132 *
133 * @param array $fields
134 * The input form values.
135 *
136 * @param $files
137 * @param object $form
138 *
139 * @return bool|array
140 * true if no errors, else array of errors
141 */
142 public static function formRule($fields, $files, $form) {
143 $errors = [];
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;
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 }
174
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;
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 }
191
192 }