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