-
-
Notifications
You must be signed in to change notification settings - Fork 827
/
Copy pathCustom.php
200 lines (167 loc) · 6.38 KB
/
Custom.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
/**
* Form to process actions on the group aspect of Custom Data.
*/
class CRM_Contribute_Form_ContributionPage_Custom extends CRM_Contribute_Form_ContributionPage {
/**
* Set variables up before form is built.
*/
public function preProcess() {
parent::preProcess();
$this->setSelectedChild('custom');
}
/**
* Build the form object.
*/
public function buildQuickForm() {
// Register 'contact_1' model
$entities = [];
$entities[] = ['entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'];
$allowCoreTypes = array_merge(['Contact', 'Individual'], CRM_Contact_BAO_ContactType::subTypes('Individual'));
$allowSubTypes = [];
// Register 'contribution_1'
$financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'financial_type_id');
$allowCoreTypes[] = 'Contribution';
//CRM-15427
$allowSubTypes['ContributionType'] = [$financialTypeId];
$entities[] = [
'entity_name' => 'contribution_1',
'entity_type' => 'ContributionModel',
'entity_sub_type' => '*',
];
// If applicable, register 'membership_1'
$member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
if ($member && $member['is_active']) {
//CRM-15427
$entities[] = [
'entity_name' => 'membership_1',
'entity_type' => 'MembershipModel',
'entity_sub_type' => '*',
];
$allowCoreTypes[] = 'Membership';
$allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
}
//CRM-15427
$this->addProfileSelector('custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
$this->addProfileSelector('custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
$this->addFormRule(['CRM_Contribute_Form_ContributionPage_Custom', 'formRule'], $this);
parent::buildQuickForm();
}
/**
* Set default values for the form.
*
* Note that in edit/view mode the default values are retrieved from the database.
*/
public function setDefaultValues() {
$defaults = parent::setDefaultValues();
$defaults['custom_pre_id'] = $this->_values['custom_pre_id'];
$defaults['custom_post_id'] = $this->_values['custom_post_id'];
return $defaults;
}
/**
* Process the form.
*/
public function postProcess() {
// get the submitted form values.
$params = $this->controller->exportValues($this->_name);
if ($this->_action & CRM_Core_Action::UPDATE) {
$params['id'] = $this->_id;
}
$transaction = new CRM_Core_Transaction();
// also update uf join table
$ufJoinParams = [
'is_active' => 1,
'module' => 'CiviContribute',
'entity_table' => 'civicrm_contribution_page',
'entity_id' => $this->_id,
];
// first delete all past entries
CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
if (!empty($params['custom_pre_id'])) {
$ufJoinParams['weight'] = 1;
$ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
CRM_Core_BAO_UFJoin::create($ufJoinParams);
}
unset($ufJoinParams['id']);
if (!empty($params['custom_post_id'])) {
$ufJoinParams['weight'] = 2;
$ufJoinParams['uf_group_id'] = $params['custom_post_id'];
CRM_Core_BAO_UFJoin::create($ufJoinParams);
}
$transaction->commit();
parent::endPostProcess();
}
/**
* Return a descriptive name for the page, used in wizard header
*
* @return string
*/
public function getTitle() {
return ts('Include Profiles');
}
/**
* Global form rule.
*
* @param array $fields
* The input form values.
*
* @param $files
* @param object $form
*
* @return bool|array
* true if no errors, else array of errors
*/
public static function formRule($fields, $files, $form) {
$errors = [];
$preProfileType = $postProfileType = NULL;
// for membership profile make sure Membership section is enabled
// get membership section for this contribution page
$dao = new CRM_Member_DAO_MembershipBlock();
$dao->entity_table = 'civicrm_contribution_page';
$dao->entity_id = $form->_id;
$membershipEnable = FALSE;
if ($dao->find(TRUE) && $dao->is_active) {
$membershipEnable = TRUE;
}
if ($fields['custom_pre_id']) {
$preProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_pre_id']);
}
if ($fields['custom_post_id']) {
$postProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_post_id']);
}
$errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
if (($preProfileType == 'Membership') && !$membershipEnable) {
$errors['custom_pre_id'] = $errorMsg;
}
if (($postProfileType == 'Membership') && !$membershipEnable) {
$errors['custom_post_id'] = $errorMsg;
}
$behalf = (!empty($form->_values['onbehalf_profile_id'])) ? $form->_values['onbehalf_profile_id'] : NULL;
if ($fields['custom_pre_id']) {
$errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
if ($preProfileType == 'Membership' && $behalf) {
$errors['custom_pre_id'] = isset($errors['custom_pre_id']) ? $errors['custom_pre_id'] . $errorMsg : $errorMsg;
}
}
if ($fields['custom_post_id']) {
$errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
if ($postProfileType == 'Membership' && $behalf) {
$errors['custom_post_id'] = isset($errors['custom_post_id']) ? $errors['custom_post_id'] . $errorMsg : $errorMsg;
}
}
return empty($errors) ? TRUE : $errors;
}
}