Merge pull request #21736 from civicrm/5.42
[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[$k] = NULL;
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->addRadio('membership_type_default', ts('Membership Types Default'), $membershipDefault, ['allowClear' => TRUE]);
164
165 $this->addFormRule(['CRM_Member_Form_MembershipBlock', 'formRule'], $this->_id);
166 }
167 $price = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviMember');
168 if (CRM_Utils_System::isNull($price)) {
169 $this->assign('price', FALSE);
170 }
171 else {
172 $this->assign('price', TRUE);
173 }
174 //$this->add('select', 'member_price_set_id', ts('Membership Price Set'), (['' => ts('- none -')] + $price));
175
176 $this->addField('member_price_set_id', [
177 'entity' => 'PriceField',
178 'name' => 'price_set_id',
179 'options' => $price,
180 ]);
181
182 $session = CRM_Core_Session::singleton();
183 $single = $session->get('singleForm');
184 if ($single) {
185 $this->addButtons([
186 [
187 'type' => 'next',
188 'name' => ts('Save'),
189 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
190 'isDefault' => TRUE,
191 ],
192 [
193 'type' => 'cancel',
194 'name' => ts('Cancel'),
195 ],
196 ]);
197 }
198 else {
199 parent::buildQuickForm();
200 }
201 }
202
203 /**
204 * Validation.
205 *
206 * @param array $params
207 * (ref.) an assoc array of name/value pairs.
208 * @param $files
209 * @param int $contributionPageId
210 *
211 * @return bool|array
212 * mixed true or array of errors
213 * @throws \CRM_Core_Exception
214 * @throws \CiviCRM_API3_Exception
215 */
216 public static function formRule($params, $files, $contributionPageId = NULL) {
217 $errors = [];
218
219 if (!empty($params['member_price_set_id'])) {
220 //check if this price set has membership type both auto-renew and non-auto-renew memberships.
221 $bothTypes = CRM_Price_BAO_PriceSet::isMembershipPriceSetContainsMixOfRenewNonRenew($params['member_price_set_id']);
222
223 //check for supporting payment processors
224 //if both auto-renew and non-auto-renew memberships
225 if ($bothTypes) {
226 $paymentProcessorIds = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
227 $contributionPageId, 'payment_processor'
228 );
229
230 $paymentProcessorId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $paymentProcessorIds);
231
232 if (!empty($paymentProcessorId)) {
233 foreach ($paymentProcessorId as $pid) {
234 if ($pid) {
235 $processor = Civi\Payment\System::singleton()->getById($pid);
236 if (!$processor->supports('MultipleConcurrentPayments')) {
237 $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.');
238 }
239 }
240 }
241 }
242 }
243 }
244 if (!empty($params['member_is_active'])) {
245
246 // don't allow price set w/ membership signup, CRM-5095
247 if ($contributionPageId && ($setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $contributionPageId, NULL, 1))) {
248
249 $extends = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
250 if ($extends != CRM_Core_Component::getComponentID('CiviMember')) {
251 $errors['member_is_active'] = ts('You cannot enable both Membership Signup and a Contribution Price Set on the same online contribution page.');
252 return $errors;
253 }
254 }
255
256 if (!empty($params['member_price_set_id'])) {
257 return $errors;
258 }
259
260 if (!isset($params['membership_type']) ||
261 (!is_array($params['membership_type']))
262 ) {
263 $errors['membership_type'] = ts('Please select at least one Membership Type to include in the Membership section of this page.');
264 }
265 else {
266 $membershipType = array_values($params['membership_type']);
267 $isRecur = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'is_recur');
268 if (array_sum($membershipType) == 0) {
269 $errors['membership_type'] = ts('Please select at least one Membership Type to include in the Membership section of this page.');
270 }
271 elseif (array_sum($membershipType) > CRM_Price_Form_Field::NUM_OPTION) {
272 // for CRM-13079
273 $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]);
274 }
275 elseif ($isRecur) {
276 if (empty($params['is_separate_payment']) && array_sum($membershipType) != 0) {
277 $errors['is_separate_payment'] = ts('You need to enable Separate Membership Payment when online contribution page is configured for both Membership and Recurring Contribution');
278 }
279 elseif (!empty($params['is_separate_payment'])) {
280 foreach ($params['membership_type'] as $mt => $dontCare) {
281 if (!empty($params["auto_renew_$mt"])) {
282 $errors["auto_renew_$mt"] = ts('You cannot enable both Recurring Contributions and Auto-renew memberships on the same online contribution page');
283 break;
284 }
285 }
286 }
287 }
288 }
289
290 //for CRM-1302
291 //if Membership status is not present, then display an error message
292 $dao = new CRM_Member_BAO_MembershipStatus();
293 if (!$dao->find()) {
294 $errors['_qf_default'] = ts('Add status rules, before configuring membership');
295 }
296
297 //give error if default is selected for an unchecked membership type
298 if (!empty($params['membership_type_default']) && !$params['membership_type'][$params['membership_type_default']]) {
299 $errors['membership_type_default'] = ts('Can\'t set default option for an unchecked membership type.');
300 }
301
302 if ($contributionPageId) {
303 $amountBlock = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'amount_block_is_active');
304
305 if (!$amountBlock && !empty($params['is_separate_payment'])) {
306 $errors['is_separate_payment'] = ts('Please enable the contribution amount section to use this option.');
307 }
308 }
309
310 }
311
312 return empty($errors) ? TRUE : $errors;
313 }
314
315 /**
316 * Process the form.
317 */
318 public function postProcess() {
319 // get the submitted form values.
320 $params = $this->controller->exportValues($this->_name);
321 $deletePriceSet = 0;
322 if ($params['membership_type']) {
323 // we do this in case the user has hit the forward/back button
324 $dao = new CRM_Member_DAO_MembershipBlock();
325 $dao->entity_table = 'civicrm_contribution_page';
326 $dao->entity_id = $this->_id;
327 $dao->find(TRUE);
328 $membershipID = $dao->id;
329 if ($membershipID) {
330 $params['id'] = $membershipID;
331 }
332
333 $membershipTypes = [];
334 if (is_array($params['membership_type'])) {
335 foreach ($params['membership_type'] as $k => $v) {
336 if ($v) {
337 $membershipTypes[$k] = $params["auto_renew_$k"] ?? NULL;
338 }
339 }
340 }
341
342 if ($this->_id && !empty($params['member_price_set_id'])) {
343 CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'amount_block_is_active', 0);
344 }
345
346 // check for price set.
347 $priceSetID = $params['member_price_set_id'] ?? NULL;
348 if (!empty($params['member_is_active']) && is_array($membershipTypes) && !$priceSetID) {
349 $usedPriceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, 2);
350 if (empty($params['mem_price_field_id']) && !$usedPriceSetId) {
351 $pageTitle = strtolower(CRM_Utils_String::munge($this->_values['title'], '_', 245));
352 $setParams['title'] = $this->_values['title'];
353 if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $pageTitle, 'id', 'name')) {
354 $setParams['name'] = $pageTitle;
355 }
356 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $pageTitle . '_' . $this->_id, 'id', 'name')) {
357 $setParams['name'] = $pageTitle . '_' . $this->_id;
358 }
359 else {
360 $timeSec = explode(".", microtime(TRUE));
361 $setParams['name'] = $pageTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
362 }
363 $setParams['is_quick_config'] = 1;
364 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviMember');
365 $setParams['financial_type_id'] = $this->_values['financial_type_id'] ?? NULL;
366 $priceSet = CRM_Price_BAO_PriceSet::create($setParams);
367 $priceSetID = $priceSet->id;
368 $fieldParams['price_set_id'] = $priceSet->id;
369 }
370 elseif ($usedPriceSetId) {
371 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviMember');
372 $setParams['financial_type_id'] = $this->_values['financial_type_id'] ?? NULL;
373 $setParams['id'] = $usedPriceSetId;
374 $priceSet = CRM_Price_BAO_PriceSet::create($setParams);
375 $priceSetID = $priceSet->id;
376 $fieldParams['price_set_id'] = $priceSet->id;
377 }
378 else {
379 $fieldParams['id'] = $params['mem_price_field_id'] ?? NULL;
380 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array::value('mem_price_field_id', $params), 'price_set_id');
381 }
382 $editedFieldParams = [
383 'price_set_id' => $priceSetID,
384 'name' => 'membership_amount',
385 ];
386 $editedResults = [];
387 CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults);
388 if (empty($editedResults['id'])) {
389 $fieldParams['name'] = 'membership_amount';
390 if (empty($params['mem_price_field_id'])) {
391 CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceField', 0, 1, ['price_set_id' => $priceSetID]);
392 }
393 $fieldParams['weight'] = 1;
394 }
395 else {
396 $fieldParams['id'] = $editedResults['id'] ?? NULL;
397 }
398
399 $fieldParams['label'] = !empty($params['membership_type_label']) ? $params['membership_type_label'] : ts('Membership');
400 $fieldParams['is_active'] = 1;
401 $fieldParams['html_type'] = 'Radio';
402 $fieldParams['is_required'] = !empty($params['is_required']) ? 1 : 0;
403 $fieldParams['is_display_amounts'] = !empty($params['display_min_fee']) ? 1 : 0;
404 $rowCount = 1;
405 $options = [];
406 if (!empty($fieldParams['id'])) {
407 CRM_Core_PseudoConstant::populate($options, 'CRM_Price_DAO_PriceFieldValue', TRUE, 'membership_type_id', NULL, " price_field_id = {$fieldParams['id']} ");
408 }
409
410 foreach ($membershipTypes as $memType => $memAutoRenew) {
411 if ($priceFieldID = CRM_Utils_Array::key($memType, $options)) {
412 $fieldParams['option_id'][$rowCount] = $priceFieldID;
413 unset($options[$priceFieldID]);
414 }
415 $membetype = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($memType);
416 $fieldParams['option_label'][$rowCount] = $membetype['name'] ?? NULL;
417 $fieldParams['option_amount'][$rowCount] = CRM_Utils_Array::value('minimum_fee', $membetype, 0);
418 $fieldParams['option_weight'][$rowCount] = $membetype['weight'] ?? NULL;
419 $fieldParams['option_description'][$rowCount] = $membetype['description'] ?? NULL;
420 $fieldParams['default_option'] = $params['membership_type_default'] ?? NULL;
421 $fieldParams['option_financial_type_id'][$rowCount] = $membetype['financial_type_id'] ?? NULL;
422
423 $fieldParams['membership_type_id'][$rowCount] = $memType;
424 // [$rowCount] = $membetype[''];
425 $rowCount++;
426 }
427 foreach ($options as $priceFieldID => $memType) {
428 CRM_Price_BAO_PriceFieldValue::setIsActive($priceFieldID, '0');
429 }
430 $priceField = CRM_Price_BAO_PriceField::create($fieldParams);
431 }
432 elseif (!$priceSetID) {
433 $deletePriceSet = 1;
434 }
435
436 $params['is_required'] = CRM_Utils_Array::value('is_required', $params, FALSE);
437 $params['is_active'] = CRM_Utils_Array::value('member_is_active', $params, FALSE);
438
439 if ($priceSetID) {
440 $params['membership_types'] = 'null';
441 $params['membership_type_default'] = CRM_Utils_Array::value('membership_type_default', $params, 'null');
442 $params['membership_types'] = serialize($membershipTypes);
443 $params['display_min_fee'] = CRM_Utils_Array::value('display_min_fee', $params, FALSE);
444 $params['is_separate_payment'] = CRM_Utils_Array::value('is_separate_payment', $params, FALSE);
445 }
446 $params['entity_table'] = 'civicrm_contribution_page';
447 $params['entity_id'] = $this->_id;
448
449 $dao = new CRM_Member_DAO_MembershipBlock();
450 $dao->copyValues($params);
451 $dao->save();
452
453 if ($priceSetID && $params['is_active']) {
454 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $this->_id, $priceSetID);
455 }
456
457 if ($deletePriceSet || !CRM_Utils_Array::value('member_is_active', $params, FALSE)) {
458
459 if ($this->_memPriceSetId) {
460 $pFIDs = [];
461 $conditionParams = [
462 'price_set_id' => $this->_memPriceSetId,
463 'html_type' => 'radio',
464 'name' => 'contribution_amount',
465 ];
466
467 CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceField', $conditionParams, $pFIDs);
468 if (empty($pFIDs['id'])) {
469 CRM_Price_BAO_PriceSet::removeFrom('civicrm_contribution_page', $this->_id);
470 CRM_Price_BAO_PriceSet::setIsQuickConfig($this->_memPriceSetId, '0');
471 }
472 else {
473
474 CRM_Price_BAO_PriceField::setIsActive($params['mem_price_field_id'], '0');
475 }
476 }
477 }
478 }
479 parent::endPostProcess();
480 }
481
482 /**
483 * Return a descriptive name for the page, used in wizard header
484 *
485 * @return string
486 */
487 public function getTitle() {
488 return ts('Memberships');
489 }
490
491 }