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