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