Fix opening specific contribution page tab from Manage Contribution Pages
[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 * Set variables up before form is built.
24 */
25 public function preProcess() {
26 parent::preProcess();
27 $this->setSelectedChild('custom');
28 }
29
30 /**
31 * Build the form object.
32 */
33 public function buildQuickForm() {
34
35 // Register 'contact_1' model
36 $entities = [];
37 $entities[] = ['entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'];
38 $allowCoreTypes = array_merge(['Contact', 'Individual'], CRM_Contact_BAO_ContactType::subTypes('Individual'));
39 $allowSubTypes = [];
40
41 // Register 'contribution_1'
42 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'financial_type_id');
43 $allowCoreTypes[] = 'Contribution';
44 //CRM-15427
45 $allowSubTypes['ContributionType'] = [$financialTypeId];
46 $entities[] = [
47 'entity_name' => 'contribution_1',
48 'entity_type' => 'ContributionModel',
49 'entity_sub_type' => '*',
50 ];
51
52 // If applicable, register 'membership_1'
53 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
54 if ($member && $member['is_active']) {
55 //CRM-15427
56 $entities[] = [
57 'entity_name' => 'membership_1',
58 'entity_type' => 'MembershipModel',
59 'entity_sub_type' => '*',
60 ];
61 $allowCoreTypes[] = 'Membership';
62 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
63 }
64 //CRM-15427
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);
67
68 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Custom', 'formRule'], $this);
69
70 parent::buildQuickForm();
71 }
72
73 /**
74 * Set default values for the form.
75 *
76 * Note that in edit/view mode the default values are retrieved from the database.
77 */
78 public function setDefaultValues() {
79 $defaults = parent::setDefaultValues();
80
81 $defaults['custom_pre_id'] = $this->_values['custom_pre_id'];
82 $defaults['custom_post_id'] = $this->_values['custom_post_id'];
83
84 return $defaults;
85 }
86
87 /**
88 * Process the form.
89 */
90 public function postProcess() {
91 // get the submitted form values.
92 $params = $this->controller->exportValues($this->_name);
93
94 if ($this->_action & CRM_Core_Action::UPDATE) {
95 $params['id'] = $this->_id;
96 }
97
98 $transaction = new CRM_Core_Transaction();
99
100 // also update uf join table
101 $ufJoinParams = [
102 'is_active' => 1,
103 'module' => 'CiviContribute',
104 'entity_table' => 'civicrm_contribution_page',
105 'entity_id' => $this->_id,
106 ];
107
108 // first delete all past entries
109 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
110
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);
115 }
116
117 unset($ufJoinParams['id']);
118
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);
123 }
124
125 $transaction->commit();
126 parent::endPostProcess();
127 }
128
129 /**
130 * Return a descriptive name for the page, used in wizard header
131 *
132 * @return string
133 */
134 public function getTitle() {
135 return ts('Include Profiles');
136 }
137
138 /**
139 * Global form rule.
140 *
141 * @param array $fields
142 * The input form values.
143 *
144 * @param $files
145 * @param object $form
146 *
147 * @return bool|array
148 * true if no errors, else array of errors
149 */
150 public static function formRule($fields, $files, $form) {
151 $errors = [];
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;
158
159 $membershipEnable = FALSE;
160
161 if ($dao->find(TRUE) && $dao->is_active) {
162 $membershipEnable = TRUE;
163 }
164
165 if ($fields['custom_pre_id']) {
166 $preProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_pre_id']);
167 }
168
169 if ($fields['custom_post_id']) {
170 $postProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_post_id']);
171 }
172
173 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
174
175 if (($preProfileType == 'Membership') && !$membershipEnable) {
176 $errors['custom_pre_id'] = $errorMsg;
177 }
178
179 if (($postProfileType == 'Membership') && !$membershipEnable) {
180 $errors['custom_post_id'] = $errorMsg;
181 }
182
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;
188 }
189 }
190
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;
195 }
196 }
197 return empty($errors) ? TRUE : $errors;
198 }
199
200 }