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