Merge pull request #1625 from pradpnayak/CRM-13340
[civicrm-core.git] / CRM / Member / Form / MembershipBlock.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * 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
53 */
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 &&
70 ($priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, 3, 1))
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'] ) ) {
78 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, 3);
79 $this->assign('isQuick', 1);
80 $this->_memPriceSetId = $priceSetId;
81 $pFIDs = array();
82 if ($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'];
87 }
88 }
89
90 if (CRM_Utils_Array::value('mem_price_field_id', $defaults)) {
91 $options = array();
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']];
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'));
165 $this->addGroup($membershipDefault, 'membership_type_default', ts('Membership Types Default'));
166
167 $this->addFormRule(array('CRM_Member_Form_MembershipBlock', 'formRule'), $this->_id);
168 }
169 $price = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviMember');
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 *
205 * @return mixed true or array of errors
206 * @access public
207 * @static
208 */
209 static function formRule($params, $files, $contributionPageId = NULL) {
210 $errors = array();
211
212 if (CRM_Utils_Array::value('member_price_set_id', $params)) {
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']);
215
216 //check for supporting payment processors
217 //if both auto-renew and non-auto-renew memberships
218 if ($bothTypes) {
219 $paymentProcessorIds = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
220 $contributionPageId, 'payment_processor'
221 );
222
223 $paymentProcessorId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $paymentProcessorIds);
224
225 if (!empty($paymentProcessorId)) {
226 $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(false, null, 'name');
227 foreach($paymentProcessorId as $pid) {
228 if ($pid) {
229 $paymentProcessorTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor',
230 $pid, 'payment_processor_type_id'
231 );
232 }
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.');
236 }
237
238 }
239 }
240 }
241 }
242 if (CRM_Utils_Array::value('member_is_active', $params)) {
243
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))) {
246
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.');
250 return $errors;
251 }
252 }
253
254 if ($contributionPageId && CRM_Utils_Array::value('member_price_set_id', $params) &&
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.');
257 }
258
259 if (CRM_Utils_Array::value('member_price_set_id', $params)) {
260 return $errors;
261 }
262
263 if (!isset($params['membership_type']) ||
264 (!is_array($params['membership_type']))
265 ) {
266 $errors['membership_type'] = ts('Please select at least one Membership Type to include in the Membership section of this page.');
267 }
268 else {
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.');
272 }
273 elseif (array_sum($membershipType) > CRM_Price_Form_Field::NUM_OPTION) {
274 // for CRM-13079
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));
276 }
277 }
278
279 //for CRM-1302
280 //if Membership status is not present, then display an error message
281 $dao = new CRM_Member_BAO_MembershipStatus();
282 if (!$dao->find()) {
283 $errors['_qf_default'] = ts('Add status rules, before configuring membership');
284 }
285
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.');
289 }
290
291 if ($contributionPageId) {
292 $amountBlock = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'amount_block_is_active');
293
294 if (!$amountBlock && CRM_Utils_Array::value('is_separate_payment', $params)) {
295 $errors['is_separate_payment'] = ts('Please enable the contribution amount section to use this option.');
296 }
297 }
298
299 }
300
301 return empty($errors) ? TRUE : $errors;
302 }
303
304 /**
305 * Process the form
306 *
307 * @return void
308 * @access public
309 */
310 public function postProcess() {
311 // get the submitted form values.
312 $params = $this->controller->exportValues($this->_name);
313 $deletePriceSet = 0;
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;
319 $dao->find(TRUE);
320 $membershipID = $dao->id;
321 if ($membershipID) {
322 $params['id'] = $membershipID;
323 }
324
325 $membershipTypes = array();
326 if (is_array($params['membership_type'])) {
327 foreach ($params['membership_type'] as $k => $v) {
328 if ($v) {
329 $membershipTypes[$k] = CRM_Utils_Array::value("auto_renew_$k", $params);
330 }
331 }
332 }
333
334 // check for price set.
335 $priceSetID = CRM_Utils_Array::value('member_price_set_id', $params);
336 if (CRM_Utils_Array::value('member_is_active', $params) && is_array($membershipTypes) && !$priceSetID) {
337 $usedPriceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, 2);
338 if (!CRM_Utils_Array::value('mem_price_field_id', $params) && !$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;
343 }
344 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $pageTitle . '_' . $this->_id, 'id', 'name')) {
345 $setParams['name'] = $pageTitle . '_' . $this->_id;
346 }
347 else {
348 $timeSec = explode(".", microtime(true));
349 $setParams['name'] = $pageTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
350 }
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;
357 }
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;
365 }
366 else {
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');
369 }
370 $editedFieldParams = array(
371 'price_set_id' => $priceSetID,
372 'name' => 'membership_amount',
373 );
374 $editedResults = array();
375 CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults);
376 if (!CRM_Utils_Array::value('id', $editedResults)) {
377 $fieldParams['name'] = strtolower(CRM_Utils_String::munge('Membership Amount', '_', 245));
378 $fieldParams['label'] = CRM_Utils_Array::value('new_title', $params) ? $params['new_title'] : ts('Membership');
379 if (!CRM_Utils_Array::value('mem_price_field_id', $params)) {
380 CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceField', 0, 1, array('price_set_id' => $priceSetID));
381 }
382 $fieldParams['weight'] = 1;
383 }
384 else {
385 $fieldParams['id'] = CRM_Utils_Array::value('id', $editedResults);
386 }
387
388 $fieldParams['is_active'] = 1;
389 $fieldParams['html_type'] = 'Radio';
390 $fieldParams['is_required'] = CRM_Utils_Array::value('is_required', $params) ? 1 : 0;
391 $fieldParams['is_display_amounts'] = CRM_Utils_Array::value('display_min_fee', $params) ? 1 : 0;
392 $rowCount = 1;
393 $options = array();
394 if (CRM_Utils_Array::value('id', $fieldParams)) {
395 CRM_Core_PseudoConstant::populate($options, 'CRM_Price_DAO_PriceFieldValue', TRUE, 'membership_type_id', NULL, " price_field_id = {$fieldParams['id']} ");
396 }
397
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]);
402 }
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);
410
411 $fieldParams['membership_type_id'][$rowCount] = $memType;
412 // [$rowCount] = $membetype[''];
413 $rowCount++;
414 }
415 foreach ($options as $priceFieldID => $memType) {
416 CRM_Price_BAO_PriceFieldValue::setIsActive($priceFieldID, '0');
417 }
418 $priceField = CRM_Price_BAO_PriceField::create($fieldParams);
419 }
420 elseif (!$priceSetID){
421 $deletePriceSet = 1;
422 }
423
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);
426
427 if ($priceSetID) {
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);
433 }
434 $params['entity_table'] = 'civicrm_contribution_page';
435 $params['entity_id'] = $this->_id;
436
437 $dao = new CRM_Member_DAO_MembershipBlock();
438 $dao->copyValues($params);
439 $dao->save();
440
441 if ($priceSetID && $params['is_active']) {
442 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $this->_id, $priceSetID);
443 }
444
445 if ($deletePriceSet || !CRM_Utils_Array::value('member_is_active', $params, FALSE)) {
446
447 if ($this->_memPriceSetId) {
448 $pFIDs = array();
449 $conditionParams = array(
450 'price_set_id' => $this->_memPriceSetId,
451 'html_type' => 'radio',
452 'name' => 'contribution_amount',
453 );
454
455 CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceField', $conditionParams, $pFIDs);
456 if (!CRM_Utils_Array::value('id', $pFIDs)) {
457 CRM_Price_BAO_PriceSet::removeFrom('civicrm_contribution_page', $this->_id);
458 CRM_Price_BAO_PriceSet::setIsQuickConfig($this->_memPriceSetId, '0');
459 }
460 else {
461
462 CRM_Price_BAO_PriceField::setIsActive($params['mem_price_field_id'], '0');
463 }
464 }
465 }
466 }
467 parent::endPostProcess();
468 }
469
470 /**
471 * Return a descriptive name for the page, used in wizard header
472 *
473 * @return string
474 * @access public
475 */
476 public function getTitle() {
477 return ts('Memberships');
478 }
479 }
480