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