3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * form to process actions on Membership
39 class CRM_Member_Form_MembershipBlock
extends CRM_Contribute_Form_ContributionPage
{
42 * store membership price set id
44 protected $_memPriceSetId = NULL;
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
54 function setDefaultValues() {
55 //parent::setDefaultValues();
57 if (isset($this->_id
)) {
58 $defaults = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
60 $defaults['member_is_active'] = $defaults['is_active'];
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;
67 $this->assign('membershipBlockId', $defaults['id']);
70 ($priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $this->_id
, 3, 1))
72 $defaults['member_price_set_id'] = $priceSetId;
73 $this->_memPriceSetId
= $priceSetId;
76 // for membership_types
77 // if ( isset( $defaults['membership_types'] ) ) {
78 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $this->_id
, 3);
79 $this->assign('isQuick', 1);
80 $this->_memPriceSetId
= $priceSetId;
83 CRM_Core_DAO
::commonRetrieveAll('CRM_Price_DAO_PriceField', 'price_set_id', $priceSetId, $pFIDs, $return = array('html_type', 'name'));
84 foreach ($pFIDs as $pid => $pValue) {
85 if ($pValue['html_type'] == 'Radio' && $pValue['name'] == 'membership_amount') {
86 $defaults['mem_price_field_id'] = $pValue['id'];
90 if (!empty($defaults['mem_price_field_id'])) {
92 $priceFieldOptions = CRM_Price_BAO_PriceFieldValue
::getValues($defaults['mem_price_field_id'], $options, 'id', 1);
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']];
99 $defaults['membership_type'] = $newMembershipType;
108 * Function to actually build the form
113 public function buildQuickForm() {
114 $membershipTypes = CRM_Member_BAO_MembershipType
::getMembershipTypes();
116 if (!empty($membershipTypes)) {
117 $this->addElement('checkbox', 'member_is_active', ts('Membership Section Enabled?'));
119 $this->addElement('text', 'new_title', ts('Title - New Membership'), CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipBlock', 'new_title'));
121 $this->addWysiwyg('new_text', ts('Introductory Message - New Memberships'), CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipBlock', 'new_text'));
123 $this->addElement('text', 'renewal_title', ts('Title - Renewals'), CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipBlock', 'renewal_title'));
125 $this->addWysiwyg('renewal_text', ts('Introductory Message - Renewals'), CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipBlock', 'renewal_text'));
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'));
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'
135 $paymentProcessorId = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $paymentProcessorIds);
137 foreach ($paymentProcessorId as $dontCare => $id) {
138 if (!array_key_exists($id, $paymentProcessor)) {
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);
149 $autoRenew = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $k, 'auto_renew');
150 $autoRenewOptions = array();
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;
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
);
164 $this->addGroup($membership, 'membership_type', ts('Membership Types'));
165 $this->addGroup($membershipDefault, 'membership_type_default', ts('Membership Types Default'));
167 $this->addFormRule(array('CRM_Member_Form_MembershipBlock', 'formRule'), $this->_id
);
169 $price = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviMember');
170 if (CRM_Utils_System
::isNull($price)) {
171 $this->assign('price', FALSE);
174 $this->assign('price', TRUE);
176 $this->add('select', 'member_price_set_id', ts('Membership Price Set'), (array('' => ts('- none -')) +
$price));
178 $session = CRM_Core_Session
::singleton();
179 $single = $session->get('singleForm');
181 $this->addButtons(array(
184 'name' => ts('Save'),
185 'spacing' => ' ',
190 'name' => ts('Cancel'),
196 parent
::buildQuickForm();
201 * Function for validation
203 * @param array $params (ref.) an assoc array of name/value pairs
205 * @return mixed true or array of errors
209 static function formRule($params, $files, $contributionPageId = NULL) {
212 if (!empty($params['member_price_set_id'])) {
213 //check if this price set has membership type both auto-renew and non-auto-renew memberships.
214 $bothTypes = CRM_Price_BAO_PriceSet
::checkMembershipPriceSet($params['member_price_set_id']);
216 //check for supporting payment processors
217 //if both auto-renew and non-auto-renew memberships
219 $paymentProcessorIds = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage',
220 $contributionPageId, 'payment_processor'
223 $paymentProcessorId = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $paymentProcessorIds);
225 if (!empty($paymentProcessorId)) {
226 $paymentProcessorType = CRM_Core_PseudoConstant
::paymentProcessorType(false, null, 'name');
227 foreach($paymentProcessorId as $pid) {
229 $paymentProcessorTypeId = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessor',
230 $pid, 'payment_processor_type_id'
233 if (!($paymentProcessorTypeId == CRM_Utils_Array
::key('PayPal', $paymentProcessorType) ||
234 ($paymentProcessorTypeId == CRM_Utils_Array
::key('AuthNet', $paymentProcessorType)))) {
235 $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.');
242 if (!empty($params['member_is_active'])) {
244 // don't allow price set w/ membership signup, CRM-5095
245 if ($contributionPageId && ($setID = CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $contributionPageId, NULL, 1))) {
247 $extends = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
248 if ($extends != CRM_Core_Component
::getComponentID('CiviMember')) {
249 $errors['member_is_active'] = ts('You cannot enable both Membership Signup and a Contribution Price Set on the same online contribution page.');
254 if ($contributionPageId && !empty($params['member_price_set_id']) &&
255 CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'amount_block_is_active')) {
256 $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.');
259 if (!empty($params['member_price_set_id'])) {
263 if (!isset($params['membership_type']) ||
264 (!is_array($params['membership_type']))
266 $errors['membership_type'] = ts('Please select at least one Membership Type to include in the Membership section of this page.');
269 $membershipType = array_values($params['membership_type']);
270 if (array_sum($membershipType) == 0) {
271 $errors['membership_type'] = ts('Please select at least one Membership Type to include in the Membership section of this page.');
273 elseif (array_sum($membershipType) > CRM_Price_Form_Field
::NUM_OPTION
) {
275 $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
));
280 //if Membership status is not present, then display an error message
281 $dao = new CRM_Member_BAO_MembershipStatus();
283 $errors['_qf_default'] = ts('Add status rules, before configuring membership');
286 //give error if default is selected for an unchecked membership type
287 if (!empty($params['membership_type_default']) && !$params['membership_type'][$params['membership_type_default']]) {
288 $errors['membership_type_default'] = ts('Can\'t set default option for an unchecked membership type.');
291 if ($contributionPageId) {
292 $amountBlock = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'amount_block_is_active');
294 if (!$amountBlock && !empty($params['is_separate_payment'])) {
295 $errors['is_separate_payment'] = ts('Please enable the contribution amount section to use this option.');
301 return empty($errors) ?
TRUE : $errors;
310 public function postProcess() {
311 // get the submitted form values.
312 $params = $this->controller
->exportValues($this->_name
);
314 if ($params['membership_type']) {
315 // we do this in case the user has hit the forward/back button
316 $dao = new CRM_Member_DAO_MembershipBlock();
317 $dao->entity_table
= 'civicrm_contribution_page';
318 $dao->entity_id
= $this->_id
;
320 $membershipID = $dao->id
;
322 $params['id'] = $membershipID;
325 $membershipTypes = array();
326 if (is_array($params['membership_type'])) {
327 foreach ($params['membership_type'] as $k => $v) {
329 $membershipTypes[$k] = CRM_Utils_Array
::value("auto_renew_$k", $params);
334 // check for price set.
335 $priceSetID = CRM_Utils_Array
::value('member_price_set_id', $params);
336 if (!empty($params['member_is_active']) && is_array($membershipTypes) && !$priceSetID) {
337 $usedPriceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $this->_id
, 2);
338 if (empty($params['mem_price_field_id']) && !$usedPriceSetId) {
339 $pageTitle = strtolower(CRM_Utils_String
::munge($this->_values
['title'], '_', 245));
340 $setParams['title'] = $this->_values
['title'];
341 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $pageTitle, 'id', 'name')) {
342 $setParams['name'] = $pageTitle;
344 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $pageTitle . '_' . $this->_id
, 'id', 'name')) {
345 $setParams['name'] = $pageTitle . '_' . $this->_id
;
348 $timeSec = explode(".", microtime(true));
349 $setParams['name'] = $pageTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
351 $setParams['is_quick_config'] = 1;
352 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviMember');
353 $setParams['financial_type_id'] = CRM_Utils_Array
::value( 'financial_type_id', $this->_values
);
354 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
355 $priceSetID = $priceSet->id
;
356 $fieldParams['price_set_id'] = $priceSet->id
;
358 elseif ($usedPriceSetId) {
359 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviMember');
360 $setParams['financial_type_id'] = CRM_Utils_Array
::value( 'financial_type_id', $this->_values
);
361 $setParams['id'] = $usedPriceSetId;
362 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
363 $priceSetID = $priceSet->id
;
364 $fieldParams['price_set_id'] = $priceSet->id
;
367 $fieldParams['id'] = CRM_Utils_Array
::value('mem_price_field_id', $params);
368 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('mem_price_field_id', $params), 'price_set_id');
370 $editedFieldParams = array(
371 'price_set_id' => $priceSetID,
372 'name' => 'membership_amount',
374 $editedResults = array();
375 CRM_Price_BAO_PriceField
::retrieve($editedFieldParams, $editedResults);
376 if (empty($editedResults['id'])) {
377 $fieldParams['name'] = strtolower(CRM_Utils_String
::munge('Membership Amount', '_', 245));
378 $fieldParams['label'] = !empty($params['new_title']) ?
$params['new_title'] : ts('Membership');
379 if (empty($params['mem_price_field_id'])) {
380 CRM_Utils_Weight
::updateOtherWeights('CRM_Price_DAO_PriceField', 0, 1, array('price_set_id' => $priceSetID));
382 $fieldParams['weight'] = 1;
385 $fieldParams['id'] = CRM_Utils_Array
::value('id', $editedResults);
388 $fieldParams['is_active'] = 1;
389 $fieldParams['html_type'] = 'Radio';
390 $fieldParams['is_required'] = !empty($params['is_required']) ?
1 : 0;
391 $fieldParams['is_display_amounts'] = !empty($params['display_min_fee']) ?
1 : 0;
394 if (!empty($fieldParams['id'])) {
395 CRM_Core_PseudoConstant
::populate($options, 'CRM_Price_DAO_PriceFieldValue', TRUE, 'membership_type_id', NULL, " price_field_id = {$fieldParams['id']} ");
398 foreach ($membershipTypes as $memType => $memAutoRenew) {
399 if ($priceFieldID = CRM_Utils_Array
::key($memType, $options)) {
400 $fieldParams['option_id'][$rowCount] = $priceFieldID;
401 unset($options[$priceFieldID]);
403 $membetype = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($memType);
404 $fieldParams['option_label'][$rowCount] = CRM_Utils_Array
::value('name', $membetype);
405 $fieldParams['option_amount'][$rowCount] = CRM_Utils_Array
::value('minimum_fee', $membetype, 0);
406 $fieldParams['option_weight'][$rowCount] = CRM_Utils_Array
::value('weight', $membetype);
407 $fieldParams['option_description'][$rowCount] = CRM_Utils_Array
::value('description', $membetype);
408 $fieldParams['default_option'] = CRM_Utils_Array
::value('membership_type_default', $params);
409 $fieldParams['option_financial_type_id'] [$rowCount] = CRM_Utils_Array
::value('financial_type_id', $membetype);
411 $fieldParams['membership_type_id'][$rowCount] = $memType;
412 // [$rowCount] = $membetype[''];
415 foreach ($options as $priceFieldID => $memType) {
416 CRM_Price_BAO_PriceFieldValue
::setIsActive($priceFieldID, '0');
418 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
420 elseif (!$priceSetID){
424 $params['is_required'] = CRM_Utils_Array
::value('is_required', $params, FALSE);
425 $params['is_active'] = CRM_Utils_Array
::value('member_is_active', $params, FALSE);
428 $params['membership_types'] = 'null';
429 $params['membership_type_default'] = CRM_Utils_Array
::value('membership_type_default', $params, 'null');
430 $params['membership_types'] = serialize( $membershipTypes );
431 $params['display_min_fee'] = CRM_Utils_Array
::value('display_min_fee', $params, FALSE);
432 $params['is_separate_payment'] = CRM_Utils_Array
::value('is_separate_payment', $params, FALSE);
434 $params['entity_table'] = 'civicrm_contribution_page';
435 $params['entity_id'] = $this->_id
;
437 $dao = new CRM_Member_DAO_MembershipBlock();
438 $dao->copyValues($params);
441 if ($priceSetID && $params['is_active']) {
442 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $this->_id
, $priceSetID);
445 if ($deletePriceSet ||
!CRM_Utils_Array
::value('member_is_active', $params, FALSE)) {
447 if ($this->_memPriceSetId
) {
449 $conditionParams = array(
450 'price_set_id' => $this->_memPriceSetId
,
451 'html_type' => 'radio',
452 'name' => 'contribution_amount',
455 CRM_Core_DAO
::commonRetrieve('CRM_Price_DAO_PriceField', $conditionParams, $pFIDs);
456 if (empty($pFIDs['id'])) {
457 CRM_Price_BAO_PriceSet
::removeFrom('civicrm_contribution_page', $this->_id
);
458 CRM_Price_BAO_PriceSet
::setIsQuickConfig($this->_memPriceSetId
, '0');
462 CRM_Price_BAO_PriceField
::setIsActive($params['mem_price_field_id'], '0');
467 parent
::endPostProcess();
471 * Return a descriptive name for the page, used in wizard header
476 public function getTitle() {
477 return ts('Memberships');