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