Commit | Line | Data |
---|---|---|
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 | */ |
20 | class CRM_Contribute_Form_ContributionPage_Custom extends CRM_Contribute_Form_ContributionPage { | |
21 | ||
7885e669 MW |
22 | /** |
23 | * Set variables up before form is built. | |
24 | */ | |
25 | public function preProcess() { | |
26 | parent::preProcess(); | |
27 | $this->setSelectedChild('custom'); | |
28 | } | |
29 | ||
6a488035 | 30 | /** |
fe482240 | 31 | * Build the form object. |
6a488035 TO |
32 | */ |
33 | public function buildQuickForm() { | |
6a488035 | 34 | |
ede14745 | 35 | // Register 'contact_1' model |
be2fb01f CW |
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 = []; | |
c50f55b5 CW |
40 | |
41 | // Register 'contribution_1' | |
42 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'financial_type_id'); | |
43 | $allowCoreTypes[] = 'Contribution'; | |
99e239bc | 44 | //CRM-15427 |
be2fb01f CW |
45 | $allowSubTypes['ContributionType'] = [$financialTypeId]; |
46 | $entities[] = [ | |
353ffa53 TO |
47 | 'entity_name' => 'contribution_1', |
48 | 'entity_type' => 'ContributionModel', | |
389bcebf | 49 | 'entity_sub_type' => '*', |
be2fb01f | 50 | ]; |
c50f55b5 CW |
51 | |
52 | // If applicable, register 'membership_1' | |
53 | $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id); | |
54 | if ($member && $member['is_active']) { | |
99e239bc | 55 | //CRM-15427 |
be2fb01f | 56 | $entities[] = [ |
353ffa53 TO |
57 | 'entity_name' => 'membership_1', |
58 | 'entity_type' => 'MembershipModel', | |
389bcebf | 59 | 'entity_sub_type' => '*', |
be2fb01f | 60 | ]; |
c50f55b5 CW |
61 | $allowCoreTypes[] = 'Membership'; |
62 | $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']); | |
6a488035 | 63 | } |
99e239bc | 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); | |
6a488035 | 67 | |
be2fb01f | 68 | $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Custom', 'formRule'], $this); |
6a488035 TO |
69 | |
70 | parent::buildQuickForm(); | |
71 | } | |
72 | ||
73 | /** | |
95cdcc0f | 74 | * Set default values for the form. |
6a488035 | 75 | * |
95cdcc0f | 76 | * Note that in edit/view mode the default values are retrieved from the database. |
6a488035 | 77 | */ |
00be9182 | 78 | public function setDefaultValues() { |
6a488035 TO |
79 | $defaults = parent::setDefaultValues(); |
80 | ||
c0eb40c1 | 81 | $defaults['custom_pre_id'] = $this->_values['custom_pre_id']; |
82 | $defaults['custom_post_id'] = $this->_values['custom_post_id']; | |
c50f55b5 | 83 | |
6a488035 TO |
84 | return $defaults; |
85 | } | |
86 | ||
87 | /** | |
fe482240 | 88 | * Process the form. |
6a488035 TO |
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 | |
be2fb01f | 101 | $ufJoinParams = [ |
6a488035 TO |
102 | 'is_active' => 1, |
103 | 'module' => 'CiviContribute', | |
104 | 'entity_table' => 'civicrm_contribution_page', | |
105 | 'entity_id' => $this->_id, | |
be2fb01f | 106 | ]; |
6a488035 TO |
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 | |
6a488035 TO |
133 | */ |
134 | public function getTitle() { | |
135 | return ts('Include Profiles'); | |
136 | } | |
137 | ||
138 | /** | |
fe482240 | 139 | * Global form rule. |
6a488035 | 140 | * |
014c4014 TO |
141 | * @param array $fields |
142 | * The input form values. | |
da6b46f4 EM |
143 | * |
144 | * @param $files | |
c0eb40c1 | 145 | * @param object $form |
6a488035 | 146 | * |
72b3a70c CW |
147 | * @return bool|array |
148 | * true if no errors, else array of errors | |
6a488035 | 149 | */ |
c0eb40c1 | 150 | public static function formRule($fields, $files, $form) { |
be2fb01f | 151 | $errors = []; |
6a488035 TO |
152 | $preProfileType = $postProfileType = NULL; |
153 | // for membership profile make sure Membership section is enabled | |
154 | // get membership section for this contribution page | |
353ffa53 | 155 | $dao = new CRM_Member_DAO_MembershipBlock(); |
6a488035 | 156 | $dao->entity_table = 'civicrm_contribution_page'; |
c0eb40c1 | 157 | $dao->entity_id = $form->_id; |
6a488035 TO |
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 | } | |
665e5ec7 | 182 | |
c0eb40c1 | 183 | $behalf = (!empty($form->_values['onbehalf_profile_id'])) ? $form->_values['onbehalf_profile_id'] : NULL; |
6a488035 TO |
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 | } | |
96025800 | 199 | |
6a488035 | 200 | } |