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 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * form to process actions on Membership | |
20 | */ | |
21 | class CRM_Member_Form_MembershipBlock extends CRM_Contribute_Form_ContributionPage { | |
22 | ||
23 | /** | |
100fef9d | 24 | * Store membership price set id |
971e129b | 25 | * @var int |
6a488035 TO |
26 | */ |
27 | protected $_memPriceSetId = NULL; | |
28 | ||
29 | /** | |
c490a46a | 30 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
31 | * the default values are retrieved from the database |
32 | * | |
6a488035 TO |
33 | * |
34 | * @return void | |
fd726496 | 35 | */ |
00be9182 | 36 | public function setDefaultValues() { |
6a488035 | 37 | //parent::setDefaultValues(); |
be2fb01f | 38 | $defaults = []; |
6a488035 TO |
39 | if (isset($this->_id)) { |
40 | $defaults = CRM_Member_BAO_Membership::getMembershipBlock($this->_id); | |
41 | } | |
42 | $defaults['member_is_active'] = $defaults['is_active']; | |
43 | ||
44 | // Set Display Minimum Fee default to true if we are adding a new membership block | |
45 | if (!isset($defaults['id'])) { | |
46 | $defaults['display_min_fee'] = 1; | |
47 | } | |
48 | else { | |
49 | $this->assign('membershipBlockId', $defaults['id']); | |
50 | } | |
51 | if ($this->_id && | |
9da8dc8c | 52 | ($priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, 3, 1)) |
6a488035 TO |
53 | ) { |
54 | $defaults['member_price_set_id'] = $priceSetId; | |
55 | $this->_memPriceSetId = $priceSetId; | |
56 | } | |
57 | else { | |
58 | // for membership_types | |
59 | // if ( isset( $defaults['membership_types'] ) ) { | |
9da8dc8c | 60 | $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, 3); |
6a488035 TO |
61 | $this->assign('isQuick', 1); |
62 | $this->_memPriceSetId = $priceSetId; | |
be2fb01f | 63 | $pFIDs = []; |
6a488035 | 64 | if ($priceSetId) { |
be2fb01f | 65 | CRM_Core_DAO::commonRetrieveAll('CRM_Price_DAO_PriceField', 'price_set_id', $priceSetId, $pFIDs, $return = [ |
c5c263ca AH |
66 | 'html_type', |
67 | 'name', | |
68 | 'label', | |
be2fb01f | 69 | ]); |
6a488035 TO |
70 | foreach ($pFIDs as $pid => $pValue) { |
71 | if ($pValue['html_type'] == 'Radio' && $pValue['name'] == 'membership_amount') { | |
72 | $defaults['mem_price_field_id'] = $pValue['id']; | |
21f4797a | 73 | $defaults['membership_type_label'] = $pValue['label']; |
6a488035 TO |
74 | } |
75 | } | |
76 | ||
a7488080 | 77 | if (!empty($defaults['mem_price_field_id'])) { |
be2fb01f | 78 | $options = []; |
9da8dc8c | 79 | $priceFieldOptions = CRM_Price_BAO_PriceFieldValue::getValues($defaults['mem_price_field_id'], $options, 'id', 1); |
6a488035 TO |
80 | foreach ($options as $k => $v) { |
81 | $newMembershipType[$v['membership_type_id']] = 1; | |
3476772a | 82 | if (!empty($defaults['auto_renew'])) { |
92fcb95f | 83 | $defaults["auto_renew_" . $v['membership_type_id']] = $defaults['auto_renew'][$v['membership_type_id']]; |
6a488035 TO |
84 | } |
85 | } | |
86 | $defaults['membership_type'] = $newMembershipType; | |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | return $defaults; | |
92 | } | |
93 | ||
94 | /** | |
fe482240 | 95 | * Build the form object. |
6a488035 TO |
96 | * |
97 | * @return void | |
6a488035 TO |
98 | */ |
99 | public function buildQuickForm() { | |
100 | $membershipTypes = CRM_Member_BAO_MembershipType::getMembershipTypes(); | |
101 | ||
102 | if (!empty($membershipTypes)) { | |
103 | $this->addElement('checkbox', 'member_is_active', ts('Membership Section Enabled?')); | |
104 | ||
105 | $this->addElement('text', 'new_title', ts('Title - New Membership'), CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipBlock', 'new_title')); | |
106 | ||
5d51a2f9 | 107 | $this->add('wysiwyg', 'new_text', ts('Introductory Message - New Memberships'), CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipBlock', 'new_text')); |
6a488035 TO |
108 | |
109 | $this->addElement('text', 'renewal_title', ts('Title - Renewals'), CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipBlock', 'renewal_title')); | |
110 | ||
5d51a2f9 | 111 | $this->add('wysiwyg', 'renewal_text', ts('Introductory Message - Renewals'), CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipBlock', 'renewal_text')); |
6a488035 TO |
112 | |
113 | $this->addElement('checkbox', 'is_required', ts('Require Membership Signup')); | |
114 | $this->addElement('checkbox', 'display_min_fee', ts('Display Membership Fee')); | |
115 | $this->addElement('checkbox', 'is_separate_payment', ts('Separate Membership Payment')); | |
be2fb01f | 116 | $this->addElement('text', 'membership_type_label', ts('Membership Types Label'), ['placeholder' => ts('Membership')]); |
6a488035 TO |
117 | |
118 | $paymentProcessor = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1'); | |
119 | $paymentProcessorIds = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', | |
120 | $this->_id, 'payment_processor' | |
121 | ); | |
122 | $paymentProcessorId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $paymentProcessorIds); | |
123 | $isRecur = TRUE; | |
124 | foreach ($paymentProcessorId as $dontCare => $id) { | |
125 | if (!array_key_exists($id, $paymentProcessor)) { | |
126 | $isRecur = FALSE; | |
127 | continue; | |
128 | } | |
129 | } | |
130 | ||
be2fb01f | 131 | $membership = $membershipDefault = $params = []; |
6a488035 TO |
132 | foreach ($membershipTypes as $k => $v) { |
133 | $membership[] = $this->createElement('advcheckbox', $k, NULL, $v); | |
134 | $membershipDefault[] = $this->createElement('radio', NULL, NULL, NULL, $k); | |
3476772a | 135 | $membershipRequired[$k] = NULL; |
6a488035 | 136 | if ($isRecur) { |
353ffa53 | 137 | $autoRenew = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $k, 'auto_renew'); |
3476772a | 138 | $membershipRequired[$k] = $autoRenew; |
be2fb01f | 139 | $autoRenewOptions = []; |
6a488035 | 140 | if ($autoRenew) { |
be2fb01f | 141 | $autoRenewOptions = [ts('Not offered'), ts('Give option'), ts('Required')]; |
6a488035 | 142 | $this->addElement('select', "auto_renew_$k", ts('Auto-renew'), $autoRenewOptions); |
3476772a | 143 | //CRM-15573 |
22e263ad | 144 | if ($autoRenew == 2) { |
3476772a | 145 | $this->freeze("auto_renew_$k"); |
b09fe5ed | 146 | $params['id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipBlock', $this->_id, 'id', 'entity_id'); |
3476772a | 147 | } |
6a488035 TO |
148 | $this->_renewOption[$k] = $autoRenew; |
149 | } | |
150 | } | |
151 | } | |
152 | ||
3476772a | 153 | //CRM-15573 |
154 | if (!empty($params['id'])) { | |
155 | $params['membership_types'] = serialize($membershipRequired); | |
156 | CRM_Member_BAO_MembershipBlock::create($params); | |
157 | } | |
be2fb01f | 158 | $this->add('hidden', "mem_price_field_id", '', ['id' => "mem_price_field_id"]); |
6a488035 TO |
159 | $this->assign('is_recur', $isRecur); |
160 | if (isset($this->_renewOption)) { | |
161 | $this->assign('auto_renew', $this->_renewOption); | |
162 | } | |
163 | $this->addGroup($membership, 'membership_type', ts('Membership Types')); | |
353ffa53 TO |
164 | $this->addGroup($membershipDefault, 'membership_type_default', ts('Membership Types Default')) |
165 | ->setAttribute('allowClear', TRUE); | |
6a488035 | 166 | |
be2fb01f | 167 | $this->addFormRule(['CRM_Member_Form_MembershipBlock', 'formRule'], $this->_id); |
6a488035 | 168 | } |
9da8dc8c | 169 | $price = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviMember'); |
6a488035 TO |
170 | if (CRM_Utils_System::isNull($price)) { |
171 | $this->assign('price', FALSE); | |
172 | } | |
173 | else { | |
174 | $this->assign('price', TRUE); | |
175 | } | |
a0a2c91d MWMC |
176 | //$this->add('select', 'member_price_set_id', ts('Membership Price Set'), (['' => ts('- none -')] + $price)); |
177 | ||
178 | $this->addField('member_price_set_id', [ | |
179 | 'entity' => 'PriceSet', | |
180 | 'name' => 'price_set_id', | |
181 | 'options' => $price, | |
182 | ]); | |
6a488035 TO |
183 | |
184 | $session = CRM_Core_Session::singleton(); | |
185 | $single = $session->get('singleForm'); | |
186 | if ($single) { | |
be2fb01f CW |
187 | $this->addButtons([ |
188 | [ | |
c5c263ca AH |
189 | 'type' => 'next', |
190 | 'name' => ts('Save'), | |
191 | 'spacing' => ' ', | |
192 | 'isDefault' => TRUE, | |
be2fb01f CW |
193 | ], |
194 | [ | |
c5c263ca AH |
195 | 'type' => 'cancel', |
196 | 'name' => ts('Cancel'), | |
be2fb01f CW |
197 | ], |
198 | ]); | |
6a488035 TO |
199 | } |
200 | else { | |
201 | parent::buildQuickForm(); | |
202 | } | |
203 | } | |
204 | ||
205 | /** | |
fe482240 | 206 | * Validation. |
6a488035 | 207 | * |
b2363ea8 TO |
208 | * @param array $params |
209 | * (ref.) an assoc array of name/value pairs. | |
6a488035 | 210 | * |
da6b46f4 | 211 | * @param $files |
100fef9d | 212 | * @param int $contributionPageId |
da6b46f4 | 213 | * |
72b3a70c CW |
214 | * @return bool|array |
215 | * mixed true or array of errors | |
6a488035 | 216 | */ |
00be9182 | 217 | public static function formRule($params, $files, $contributionPageId = NULL) { |
be2fb01f | 218 | $errors = []; |
6a488035 | 219 | |
a7488080 | 220 | if (!empty($params['member_price_set_id'])) { |
fd726496 | 221 | //check if this price set has membership type both auto-renew and non-auto-renew memberships. |
f72e6e2c | 222 | $bothTypes = CRM_Price_BAO_PriceSet::isMembershipPriceSetContainsMixOfRenewNonRenew($params['member_price_set_id']); |
6a488035 TO |
223 | |
224 | //check for supporting payment processors | |
225 | //if both auto-renew and non-auto-renew memberships | |
226 | if ($bothTypes) { | |
b09fe5ed | 227 | $paymentProcessorIds = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', |
353ffa53 TO |
228 | $contributionPageId, 'payment_processor' |
229 | ); | |
6a488035 | 230 | |
b09fe5ed | 231 | $paymentProcessorId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $paymentProcessorIds); |
6a488035 TO |
232 | |
233 | if (!empty($paymentProcessorId)) { | |
22e263ad | 234 | foreach ($paymentProcessorId as $pid) { |
6a488035 | 235 | if ($pid) { |
b6ffdf22 EM |
236 | $processor = Civi\Payment\System::singleton()->getById($pid); |
237 | if (!$processor->supports('MultipleConcurrentPayments')) { | |
238 | $errors['member_price_set_id'] = ts('The membership price set associated with this online contribution allows a user to select BOTH an auto-renew AND a non-auto-renew membership. This requires submitting multiple processor transactions, and is not supported for one or more of the payment processors enabled under the Amounts tab.'); | |
239 | } | |
6a488035 | 240 | } |
6a488035 TO |
241 | } |
242 | } | |
fd726496 | 243 | } |
6a488035 | 244 | } |
a7488080 | 245 | if (!empty($params['member_is_active'])) { |
6a488035 TO |
246 | |
247 | // don't allow price set w/ membership signup, CRM-5095 | |
9da8dc8c | 248 | if ($contributionPageId && ($setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $contributionPageId, NULL, 1))) { |
6a488035 | 249 | |
9da8dc8c | 250 | $extends = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends'); |
6a488035 TO |
251 | if ($extends != CRM_Core_Component::getComponentID('CiviMember')) { |
252 | $errors['member_is_active'] = ts('You cannot enable both Membership Signup and a Contribution Price Set on the same online contribution page.'); | |
253 | return $errors; | |
254 | } | |
255 | } | |
256 | ||
a7488080 | 257 | if (!empty($params['member_price_set_id'])) { |
6a488035 TO |
258 | return $errors; |
259 | } | |
260 | ||
261 | if (!isset($params['membership_type']) || | |
262 | (!is_array($params['membership_type'])) | |
263 | ) { | |
264 | $errors['membership_type'] = ts('Please select at least one Membership Type to include in the Membership section of this page.'); | |
265 | } | |
266 | else { | |
267 | $membershipType = array_values($params['membership_type']); | |
9cc96227 | 268 | $isRecur = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'is_recur'); |
6a488035 TO |
269 | if (array_sum($membershipType) == 0) { |
270 | $errors['membership_type'] = ts('Please select at least one Membership Type to include in the Membership section of this page.'); | |
271 | } | |
0c165e1c DG |
272 | elseif (array_sum($membershipType) > CRM_Price_Form_Field::NUM_OPTION) { |
273 | // for CRM-13079 | |
be2fb01f | 274 | $errors['membership_type'] = ts('You cannot select more than %1 choices. For more complex functionality, please use a Price Set.', [1 => CRM_Price_Form_Field::NUM_OPTION]); |
0c165e1c | 275 | } |
9cc96227 | 276 | elseif ($isRecur) { |
277 | if (empty($params['is_separate_payment']) && array_sum($membershipType) != 0) { | |
278 | $errors['is_separate_payment'] = ts('You need to enable Separate Membership Payment when online contribution page is configured for both Membership and Recurring Contribution'); | |
279 | } | |
280 | elseif (!empty($params['is_separate_payment'])) { | |
281 | foreach ($params['membership_type'] as $mt => $dontCare) { | |
282 | if (!empty($params["auto_renew_$mt"])) { | |
283 | $errors["auto_renew_$mt"] = ts('You cannot enable both Recurring Contributions and Auto-renew memberships on the same online contribution page'); | |
284 | break; | |
285 | } | |
286 | } | |
287 | } | |
288 | } | |
6a488035 TO |
289 | } |
290 | ||
291 | //for CRM-1302 | |
292 | //if Membership status is not present, then display an error message | |
293 | $dao = new CRM_Member_BAO_MembershipStatus(); | |
294 | if (!$dao->find()) { | |
295 | $errors['_qf_default'] = ts('Add status rules, before configuring membership'); | |
296 | } | |
297 | ||
298 | //give error if default is selected for an unchecked membership type | |
0c165e1c | 299 | if (!empty($params['membership_type_default']) && !$params['membership_type'][$params['membership_type_default']]) { |
6a488035 TO |
300 | $errors['membership_type_default'] = ts('Can\'t set default option for an unchecked membership type.'); |
301 | } | |
302 | ||
303 | if ($contributionPageId) { | |
304 | $amountBlock = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'amount_block_is_active'); | |
305 | ||
8cc574cf | 306 | if (!$amountBlock && !empty($params['is_separate_payment'])) { |
6a488035 TO |
307 | $errors['is_separate_payment'] = ts('Please enable the contribution amount section to use this option.'); |
308 | } | |
309 | } | |
fd726496 | 310 | |
6a488035 TO |
311 | } |
312 | ||
313 | return empty($errors) ? TRUE : $errors; | |
314 | } | |
315 | ||
316 | /** | |
fe482240 | 317 | * Process the form. |
6a488035 TO |
318 | * |
319 | * @return void | |
6a488035 TO |
320 | */ |
321 | public function postProcess() { | |
322 | // get the submitted form values. | |
323 | $params = $this->controller->exportValues($this->_name); | |
324 | $deletePriceSet = 0; | |
325 | if ($params['membership_type']) { | |
326 | // we do this in case the user has hit the forward/back button | |
353ffa53 | 327 | $dao = new CRM_Member_DAO_MembershipBlock(); |
6a488035 | 328 | $dao->entity_table = 'civicrm_contribution_page'; |
353ffa53 | 329 | $dao->entity_id = $this->_id; |
6a488035 TO |
330 | $dao->find(TRUE); |
331 | $membershipID = $dao->id; | |
332 | if ($membershipID) { | |
333 | $params['id'] = $membershipID; | |
334 | } | |
335 | ||
be2fb01f | 336 | $membershipTypes = []; |
6a488035 TO |
337 | if (is_array($params['membership_type'])) { |
338 | foreach ($params['membership_type'] as $k => $v) { | |
339 | if ($v) { | |
9c1bc317 | 340 | $membershipTypes[$k] = $params["auto_renew_$k"] ?? NULL; |
6a488035 TO |
341 | } |
342 | } | |
343 | } | |
344 | ||
2d8ae2ae CW |
345 | if ($this->_id && !empty($params['member_price_set_id'])) { |
346 | CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'amount_block_is_active', 0); | |
347 | } | |
348 | ||
6a488035 | 349 | // check for price set. |
9c1bc317 | 350 | $priceSetID = $params['member_price_set_id'] ?? NULL; |
a7488080 | 351 | if (!empty($params['member_is_active']) && is_array($membershipTypes) && !$priceSetID) { |
9da8dc8c | 352 | $usedPriceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, 2); |
a7488080 | 353 | if (empty($params['mem_price_field_id']) && !$usedPriceSetId) { |
6a488035 TO |
354 | $pageTitle = strtolower(CRM_Utils_String::munge($this->_values['title'], '_', 245)); |
355 | $setParams['title'] = $this->_values['title']; | |
9da8dc8c | 356 | if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $pageTitle, 'id', 'name')) { |
6a488035 TO |
357 | $setParams['name'] = $pageTitle; |
358 | } | |
9da8dc8c | 359 | elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $pageTitle . '_' . $this->_id, 'id', 'name')) { |
6a488035 TO |
360 | $setParams['name'] = $pageTitle . '_' . $this->_id; |
361 | } | |
362 | else { | |
b09fe5ed | 363 | $timeSec = explode(".", microtime(TRUE)); |
6a488035 TO |
364 | $setParams['name'] = $pageTitle . '_' . date('is', $timeSec[0]) . $timeSec[1]; |
365 | } | |
366 | $setParams['is_quick_config'] = 1; | |
367 | $setParams['extends'] = CRM_Core_Component::getComponentID('CiviMember'); | |
9c1bc317 | 368 | $setParams['financial_type_id'] = $this->_values['financial_type_id'] ?? NULL; |
9da8dc8c | 369 | $priceSet = CRM_Price_BAO_PriceSet::create($setParams); |
6a488035 TO |
370 | $priceSetID = $priceSet->id; |
371 | $fieldParams['price_set_id'] = $priceSet->id; | |
372 | } | |
373 | elseif ($usedPriceSetId) { | |
374 | $setParams['extends'] = CRM_Core_Component::getComponentID('CiviMember'); | |
9c1bc317 | 375 | $setParams['financial_type_id'] = $this->_values['financial_type_id'] ?? NULL; |
6a488035 | 376 | $setParams['id'] = $usedPriceSetId; |
9da8dc8c | 377 | $priceSet = CRM_Price_BAO_PriceSet::create($setParams); |
6a488035 TO |
378 | $priceSetID = $priceSet->id; |
379 | $fieldParams['price_set_id'] = $priceSet->id; | |
380 | } | |
381 | else { | |
9c1bc317 | 382 | $fieldParams['id'] = $params['mem_price_field_id'] ?? NULL; |
9da8dc8c | 383 | $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array::value('mem_price_field_id', $params), 'price_set_id'); |
6a488035 | 384 | } |
be2fb01f | 385 | $editedFieldParams = [ |
6a488035 TO |
386 | 'price_set_id' => $priceSetID, |
387 | 'name' => 'membership_amount', | |
be2fb01f CW |
388 | ]; |
389 | $editedResults = []; | |
9da8dc8c | 390 | CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults); |
a7488080 | 391 | if (empty($editedResults['id'])) { |
6a488035 | 392 | $fieldParams['name'] = strtolower(CRM_Utils_String::munge('Membership Amount', '_', 245)); |
a7488080 | 393 | if (empty($params['mem_price_field_id'])) { |
be2fb01f | 394 | CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceField', 0, 1, ['price_set_id' => $priceSetID]); |
6a488035 TO |
395 | } |
396 | $fieldParams['weight'] = 1; | |
397 | } | |
398 | else { | |
9c1bc317 | 399 | $fieldParams['id'] = $editedResults['id'] ?? NULL; |
6a488035 TO |
400 | } |
401 | ||
21f4797a | 402 | $fieldParams['label'] = !empty($params['membership_type_label']) ? $params['membership_type_label'] : ts('Membership'); |
6a488035 TO |
403 | $fieldParams['is_active'] = 1; |
404 | $fieldParams['html_type'] = 'Radio'; | |
0d8afee2 CW |
405 | $fieldParams['is_required'] = !empty($params['is_required']) ? 1 : 0; |
406 | $fieldParams['is_display_amounts'] = !empty($params['display_min_fee']) ? 1 : 0; | |
6a488035 | 407 | $rowCount = 1; |
be2fb01f | 408 | $options = []; |
a7488080 | 409 | if (!empty($fieldParams['id'])) { |
9da8dc8c | 410 | CRM_Core_PseudoConstant::populate($options, 'CRM_Price_DAO_PriceFieldValue', TRUE, 'membership_type_id', NULL, " price_field_id = {$fieldParams['id']} "); |
6a488035 TO |
411 | } |
412 | ||
413 | foreach ($membershipTypes as $memType => $memAutoRenew) { | |
414 | if ($priceFieldID = CRM_Utils_Array::key($memType, $options)) { | |
415 | $fieldParams['option_id'][$rowCount] = $priceFieldID; | |
416 | unset($options[$priceFieldID]); | |
417 | } | |
418 | $membetype = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($memType); | |
9c1bc317 | 419 | $fieldParams['option_label'][$rowCount] = $membetype['name'] ?? NULL; |
353ffa53 | 420 | $fieldParams['option_amount'][$rowCount] = CRM_Utils_Array::value('minimum_fee', $membetype, 0); |
9c1bc317 CW |
421 | $fieldParams['option_weight'][$rowCount] = $membetype['weight'] ?? NULL; |
422 | $fieldParams['option_description'][$rowCount] = $membetype['description'] ?? NULL; | |
423 | $fieldParams['default_option'] = $params['membership_type_default'] ?? NULL; | |
424 | $fieldParams['option_financial_type_id'][$rowCount] = $membetype['financial_type_id'] ?? NULL; | |
6a488035 TO |
425 | |
426 | $fieldParams['membership_type_id'][$rowCount] = $memType; | |
427 | // [$rowCount] = $membetype['']; | |
428 | $rowCount++; | |
429 | } | |
430 | foreach ($options as $priceFieldID => $memType) { | |
9da8dc8c | 431 | CRM_Price_BAO_PriceFieldValue::setIsActive($priceFieldID, '0'); |
6a488035 | 432 | } |
9da8dc8c | 433 | $priceField = CRM_Price_BAO_PriceField::create($fieldParams); |
6a488035 | 434 | } |
9b873358 | 435 | elseif (!$priceSetID) { |
6a488035 TO |
436 | $deletePriceSet = 1; |
437 | } | |
438 | ||
439 | $params['is_required'] = CRM_Utils_Array::value('is_required', $params, FALSE); | |
440 | $params['is_active'] = CRM_Utils_Array::value('member_is_active', $params, FALSE); | |
441 | ||
442 | if ($priceSetID) { | |
443 | $params['membership_types'] = 'null'; | |
444 | $params['membership_type_default'] = CRM_Utils_Array::value('membership_type_default', $params, 'null'); | |
353ffa53 TO |
445 | $params['membership_types'] = serialize($membershipTypes); |
446 | $params['display_min_fee'] = CRM_Utils_Array::value('display_min_fee', $params, FALSE); | |
447 | $params['is_separate_payment'] = CRM_Utils_Array::value('is_separate_payment', $params, FALSE); | |
6a488035 TO |
448 | } |
449 | $params['entity_table'] = 'civicrm_contribution_page'; | |
450 | $params['entity_id'] = $this->_id; | |
451 | ||
452 | $dao = new CRM_Member_DAO_MembershipBlock(); | |
453 | $dao->copyValues($params); | |
454 | $dao->save(); | |
455 | ||
456 | if ($priceSetID && $params['is_active']) { | |
9da8dc8c | 457 | CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $this->_id, $priceSetID); |
6a488035 TO |
458 | } |
459 | ||
460 | if ($deletePriceSet || !CRM_Utils_Array::value('member_is_active', $params, FALSE)) { | |
461 | ||
462 | if ($this->_memPriceSetId) { | |
be2fb01f CW |
463 | $pFIDs = []; |
464 | $conditionParams = [ | |
6a488035 TO |
465 | 'price_set_id' => $this->_memPriceSetId, |
466 | 'html_type' => 'radio', | |
467 | 'name' => 'contribution_amount', | |
be2fb01f | 468 | ]; |
6a488035 | 469 | |
9da8dc8c | 470 | CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceField', $conditionParams, $pFIDs); |
a7488080 | 471 | if (empty($pFIDs['id'])) { |
9da8dc8c | 472 | CRM_Price_BAO_PriceSet::removeFrom('civicrm_contribution_page', $this->_id); |
473 | CRM_Price_BAO_PriceSet::setIsQuickConfig($this->_memPriceSetId, '0'); | |
6a488035 TO |
474 | } |
475 | else { | |
476 | ||
9da8dc8c | 477 | CRM_Price_BAO_PriceField::setIsActive($params['mem_price_field_id'], '0'); |
6a488035 TO |
478 | } |
479 | } | |
480 | } | |
481 | } | |
482 | parent::endPostProcess(); | |
483 | } | |
484 | ||
485 | /** | |
486 | * Return a descriptive name for the page, used in wizard header | |
487 | * | |
488 | * @return string | |
6a488035 TO |
489 | */ |
490 | public function getTitle() { | |
491 | return ts('Memberships'); | |
492 | } | |
96025800 | 493 | |
6a488035 | 494 | } |