Merge pull request #780 from hbergin/CRM-12644
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Amount.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 the group aspect of Custom Data
38 */
39class CRM_Contribute_Form_ContributionPage_Amount extends CRM_Contribute_Form_ContributionPage {
40
41 /**
42 * contribution amount block.
43 *
44 * @var array
45 * @access protected
46 */
47 protected $_amountBlock = array();
48
49 /**
50 * Constants for number of options for data types of multiple option.
51 */
52 CONST NUM_OPTION = 11;
53
54 /**
55 * Function to actually build the form
56 *
57 * @return void
58 * @access public
59 */
60 public function buildQuickForm() {
61
62 // do u want to allow a free form text field for amount
63 $this->addElement('checkbox', 'is_allow_other_amount', ts('Allow other amounts'), NULL, array('onclick' => "minMax(this);showHideAmountBlock( this, 'is_allow_other_amount' );"));
64 $this->add('text', 'min_amount', ts('Minimum Amount'), array('size' => 8, 'maxlength' => 8));
65 $this->addRule('min_amount', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');
66
67 $this->add('text', 'max_amount', ts('Maximum Amount'), array('size' => 8, 'maxlength' => 8));
68 $this->addRule('max_amount', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
69
70 $default = array();
71 $this->add('hidden', "price_field_id", '', array('id' => "price_field_id"));
72 $this->add('hidden', "price_field_other", '', array('id' => "price_field_option"));
73 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
74 // label
75 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
76
77 $this->add('hidden', "price_field_value[$i]", '', array('id' => "price_field_value[$i]"));
78
79 // value
80 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
81 $this->addRule("value[$i]", ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
82
83 // default
84 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
85 }
86
87 $this->addGroup($default, 'default');
88
89 $this->addElement('checkbox', 'amount_block_is_active', ts('Contribution Amounts section enabled'), NULL, array('onclick' => "showHideAmountBlock( this, 'amount_block_is_active' );"));
90
91 $this->addElement('checkbox', 'is_monetary', ts('Execute real-time monetary transactions'));
92
93 $paymentProcessor = CRM_Core_PseudoConstant::paymentProcessor();
94 $recurringPaymentProcessor = array();
95
96 if (!empty($paymentProcessor)) {
97 $paymentProcessorIds = implode(',', array_keys($paymentProcessor));
98 $query = "
99SELECT id
100 FROM civicrm_payment_processor
101 WHERE id IN ({$paymentProcessorIds})
102 AND is_recur = 1";
103 $dao = CRM_Core_DAO::executeQuery($query);
104 while ($dao->fetch()) {
105 $recurringPaymentProcessor[] = $dao->id;
106 }
107 }
108 $this->assign('recurringPaymentProcessor', $recurringPaymentProcessor);
109 if (count($paymentProcessor)) {
110 $this->assign('paymentProcessor', $paymentProcessor);
111 }
112
113 $this->addCheckBox('payment_processor', ts('Payment Processor'),
114 array_flip($paymentProcessor),
115 NULL, NULL, NULL, NULL,
116 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>')
117 );
118
119
120 //check if selected payment processor supports recurring payment
121 if (!empty($recurringPaymentProcessor)) {
122 $this->addElement('checkbox', 'is_recur', ts('Recurring contributions'), NULL,
123 array('onclick' => "showHideByValue('is_recur',true,'recurFields','table-row','radio',false);")
124 );
125 $this->addCheckBox('recur_frequency_unit', ts('Supported recurring units'),
126 CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, FALSE, NULL, 'name'),
127 NULL, NULL, NULL, NULL,
128 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>')
129 );
130 $this->addElement('checkbox', 'is_recur_interval', ts('Support recurring intervals'));
131 $this->addElement('checkbox', 'is_recur_installments', ts('Offer installments'));
132 }
133
134 // add pay later options
135 $this->addElement('checkbox', 'is_pay_later', ts('Pay later option'), NULL);
136 $this->addElement('textarea', 'pay_later_text', ts('Pay later label'),
137 CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage', 'pay_later_text'),
138 FALSE
139 );
140 $this->addElement('textarea', 'pay_later_receipt', ts('Pay later instructions'),
141 CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage', 'pay_later_receipt'),
142 FALSE
143 );
144
145 //add partial payment options
146
147 // add price set fields
148 $price = CRM_Price_BAO_Set::getAssoc(FALSE, 'CiviContribute');
149 if (CRM_Utils_System::isNull($price)) {
150 $this->assign('price', FALSE);
151 }
152 else {
153 $this->assign('price', TRUE);
154 }
155 $this->add('select', 'price_set_id', ts('Price Set'),
156 array(
157 '' => ts('- none -')) + $price,
158 NULL, array('onchange' => "showHideAmountBlock( this.value, 'price_set_id' );")
159 );
160 //CiviPledge fields.
161 $config = CRM_Core_Config::singleton();
162 if (in_array('CiviPledge', $config->enableComponents)) {
163 $this->assign('civiPledge', TRUE);
164 $this->addElement('checkbox', 'is_pledge_active', ts('Pledges'),
165 NULL, array('onclick' => "showHideAmountBlock( this, 'is_pledge_active' ); return showHideByValue('is_pledge_active',true,'pledgeFields','table-row','radio',false);")
166 );
167 $this->addCheckBox('pledge_frequency_unit', ts('Supported pledge frequencies'),
168 CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, FALSE, NULL, 'name'),
169 NULL, NULL, NULL, NULL,
170 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>')
171 );
172 $this->addElement('checkbox', 'is_pledge_interval', ts('Allow frequency intervals'));
173 $this->addElement('text', 'initial_reminder_day', ts('Send payment reminder'), array('size' => 3));
174 $this->addElement('text', 'max_reminders', ts('Send up to'), array('size' => 3));
175 $this->addElement('text', 'additional_reminder_day', ts('Send additional reminders'), array('size' => 3));
176 }
177
178 //add currency element.
179 $this->addCurrency('currency', ts('Currency'));
180
181 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Amount', 'formRule'), $this);
182
183 parent::buildQuickForm();
184 }
185
186 /**
187 * This function sets the default values for the form. Note that in edit/view mode
188 * the default values are retrieved from the database
189 *
190 * @access public
191 *
192 * @return void
193 */
194 function setDefaultValues() {
195 $defaults = parent::setDefaultValues();
196 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'title');
197 CRM_Utils_System::setTitle(ts('Contribution Amounts (%1)', array(1 => $title)));
198
199 if (!CRM_Utils_Array::value('pay_later_text', $defaults)) {
200 $defaults['pay_later_text'] = ts('I will send payment by check');
201 }
202
203 if (CRM_Utils_Array::value('amount_block_is_active', $defaults)) {
204
205 if ($priceSetId = CRM_Price_BAO_Set::getFor('civicrm_contribution_page', $this->_id, NULL)) {
206 if ($isQuick = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $priceSetId, 'is_quick_config')) {
207 $this->assign('isQuick', $isQuick);
208 //$priceField = CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_Field', $priceSetId, 'id', 'price_set_id' );
209 $options = $pFIDs = array();
210 $priceFieldParams = array('price_set_id' => $priceSetId);
211 $priceFields = CRM_Core_DAO::commonRetrieveAll('CRM_Price_DAO_Field', 'price_set_id', $priceSetId, $pFIDs, $return = array('html_type', 'name', 'is_active'));
212 foreach ($priceFields as $priceField) {
213 if ($priceField['id'] && $priceField['html_type'] == 'Radio' && $priceField['name'] == 'contribution_amount') {
214 $defaults['price_field_id'] = $priceField['id'];
215 $priceFieldOptions = CRM_Price_BAO_FieldValue::getValues($priceField['id'], $options, 'id', 1);
216 $countRow = 0;
217 foreach ($options as $optionId => $optionValue) {
218 $countRow++;
219 $defaults['value'][$countRow] = $optionValue['amount'];
220 $defaults['label'][$countRow] = CRM_Utils_Array::value('label', $optionValue);
221 $defaults['name'][$countRow] = CRM_Utils_Array::value('name', $optionValue);
222 $defaults['weight'][$countRow] = $optionValue['weight'];
223
224 $defaults["price_field_value"][$countRow] = $optionValue['id'];
225 if ($optionValue['is_default']) {
226 $defaults['default'] = $countRow;
227 }
228 }
229 }
230 elseif ($priceField['id'] && $priceField['html_type'] == 'Text' && $priceField['name'] = 'other_amount' && $priceField['is_active']) {
231 $defaults['price_field_other'] = $priceField['id'];
232 }
233 }
234 }
235 }
236
237 if (CRM_Utils_Array::value('value', $defaults) && is_array($defaults['value'])) {
238
239 // CRM-4038: fix value display
240 foreach ($defaults['value'] as & $amount) {
241 $amount = trim(CRM_Utils_Money::format($amount, ' '));
242 }
243 }
244 }
245
246 // fix the display of the monetary value, CRM-4038
247 if (isset($defaults['min_amount'])) {
248 $defaults['min_amount'] = CRM_Utils_Money::format($defaults['min_amount'], NULL, '%a');
249 }
250 if (isset($defaults['max_amount'])) {
251 $defaults['max_amount'] = CRM_Utils_Money::format($defaults['max_amount'], NULL, '%a');
252 }
253
254 if (CRM_Utils_Array::value('payment_processor', $defaults)) {
255 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
256 $defaults['payment_processor']
257 ), '1');
258 }
259 return $defaults;
260 }
261
262 /**
263 * global form rule
264 *
265 * @param array $fields the input form values
266 * @param array $files the uploaded files if any
267 * @param array $options additional user data
268 *
269 * @return true if no errors, else array of errors
270 * @access public
271 * @static
272 */
273 static function formRule($fields, $files, $self) {
274 $errors = array();
275 //as for separate membership payment we has to have
276 //contribution amount section enabled, hence to disable it need to
277 //check if separate membership payment enabled,
278 //if so disable first separate membership payment option
279 //then disable contribution amount section. CRM-3801,
280
281 $membershipBlock = new CRM_Member_DAO_MembershipBlock();
282 $membershipBlock->entity_table = 'civicrm_contribution_page';
283 $membershipBlock->entity_id = $self->_id;
284 $membershipBlock->is_active = 1;
285 $hasMembershipBlk = FALSE;
286 if ($membershipBlock->find(TRUE)) {
287 if (CRM_Utils_Array::value('amount_block_is_active', $fields) &&
288 ($setID = CRM_Price_BAO_Set::getFor('civicrm_contribution_page', $self->_id, NULL, 1))
289 ) {
290 $extends = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $setID, 'extends');
291 if ($extends && $extends == CRM_Core_Component::getComponentID('CiviMember')) {
292 $errors['amount_block_is_active'] = ts('You cannot use a Membership Price Set when the Contribution Amounts section is enabled. Click the Memberships tab above, and select your Membership Price Set on that form. Membership Price Sets may include additional fields for non-membership options that require an additional fee (e.g. magazine subscription) or an additional voluntary contribution.');
293 return $errors;
294 }
295 }
296 $hasMembershipBlk = TRUE;
9ce8bcfc 297 if ($membershipBlock->is_separate_payment && !CRM_Utils_Array::value('amount_block_is_active', $fields)) {
6a488035
TO
298 $errors['amount_block_is_active'] = ts('To disable Contribution Amounts section you need to first disable Separate Membership Payment option from Membership Settings.');
299 }
300 }
301
302 $minAmount = CRM_Utils_Array::value('min_amount', $fields);
303 $maxAmount = CRM_Utils_Array::value('max_amount', $fields);
304 if (!empty($minAmount) && !empty($maxAmount)) {
305 $minAmount = CRM_Utils_Rule::cleanMoney($minAmount);
306 $maxAmount = CRM_Utils_Rule::cleanMoney($maxAmount);
307 if ((float ) $minAmount > (float ) $maxAmount) {
308 $errors['min_amount'] = ts('Minimum Amount should be less than Maximum Amount');
309 }
310 }
311
312 if (isset($fields['is_pay_later'])) {
313 if (empty($fields['pay_later_text'])) {
314 $errors['pay_later_text'] = ts('Please enter the text for the \'pay later\' checkbox displayed on the contribution form.');
315 }
316 if (empty($fields['pay_later_receipt'])) {
317 $errors['pay_later_receipt'] = ts('Please enter the instructions to be sent to the contributor when they choose to \'pay later\'.');
318 }
319 }
320
321 // don't allow price set w/ membership signup, CRM-5095
322 if ($priceSetId = CRM_Utils_Array::value('price_set_id', $fields)) {
323 // don't allow price set w/ membership.
324 if ($hasMembershipBlk) {
325 $errors['price_set_id'] = ts('You cannot enable both a Contribution Price Set and Membership Signup on the same online contribution page.');
326 }
327 }
328 else {
329 if (isset($fields['is_recur'])) {
330 if (empty($fields['recur_frequency_unit'])) {
331 $errors['recur_frequency_unit'] = ts('At least one recurring frequency option needs to be checked.');
332 }
333 }
334
335 // validation for pledge fields.
336 if (CRM_Utils_array::value('is_pledge_active', $fields)) {
337 if (empty($fields['pledge_frequency_unit'])) {
338 $errors['pledge_frequency_unit'] = ts('At least one pledge frequency option needs to be checked.');
339 }
340 if (CRM_Utils_array::value('is_recur', $fields)) {
341 $errors['is_recur'] = ts('You cannot enable both Recurring Contributions AND Pledges on the same online contribution page.');
342 }
343 }
344
345 // If Contribution amount section is enabled, then
346 // Allow other amounts must be enabeld OR the Fixed Contribution
347 // Contribution options must contain at least one set of values.
348 if (CRM_Utils_Array::value('amount_block_is_active', $fields)) {
349 if (!CRM_Utils_Array::value('is_allow_other_amount', $fields) &&
350 !$priceSetId
351 ) {
352 //get the values of amount block
353 $values = CRM_Utils_Array::value('value', $fields);
354 $isSetRow = FALSE;
355 for ($i = 1; $i < self::NUM_OPTION; $i++) {
356 if ((isset($values[$i]) && (strlen(trim($values[$i])) > 0))) {
357 $isSetRow = TRUE;
358 }
359 }
360 if (!$isSetRow) {
361 $errors['amount_block_is_active'] = ts('If you want to enable the \'Contribution Amounts section\', you need to either \'Allow Other Amounts\' and/or enter at least one row in the \'Fixed Contribution Amounts\' table.');
362 }
363 }
364 }
365 }
366
367 if (CRM_Utils_Array::value('is_recur_interval', $fields)) {
368 foreach(array_keys($fields['payment_processor']) as $paymentProcessorID) {
369 $paymentProcessorTypeId = CRM_Core_DAO::getFieldValue(
370 'CRM_Financial_DAO_PaymentProcessor',
371 $paymentProcessorID,
372 'payment_processor_type_id'
373 );
374 $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(false, $paymentProcessorTypeId, 'name');
375 if ($paymentProcessorType == 'Google_Checkout') {
376 $errors['is_recur_interval'] = ts('Google Checkout does not support recurring intervals');
377 break;
378 }
379 }
380 }
381
382 return $errors;
383 }
384
385 /**
386 * Process the form
387 *
388 * @return void
389 * @access public
390 */
391 public function postProcess() {
392 // get the submitted form values.
393 $params = $this->controller->exportValues($this->_name);
394 if (array_key_exists('payment_processor', $params)) {
395 if (array_key_exists(CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', 'AuthNet',
396 'id', 'payment_processor_type_id'
397 ),
398 CRM_Utils_Array::value('payment_processor', $params)
399 )) {
400 CRM_Core_Session::setStatus(ts(' Please note that the Authorize.net payment processor only allows recurring contributions and auto-renew memberships with payment intervals from 7-365 days or 1-12 months (i.e. not greater than 1 year).'), '', 'alert');
401 }
402 }
403
404 // check for price set.
405 $priceSetID = CRM_Utils_Array::value('price_set_id', $params);
406
407 // get required fields.
408 $fields = array(
409 'id' => $this->_id,
410 'is_recur' => FALSE,
411 'min_amount' => "null",
412 'max_amount' => "null",
413 'is_monetary' => FALSE,
414 'is_pay_later' => FALSE,
415 'is_recur_interval' => FALSE,
416 'is_recur_installments' => FALSE,
417 'recur_frequency_unit' => "null",
418 'default_amount_id' => "null",
419 'is_allow_other_amount' => FALSE,
420 'amount_block_is_active' => FALSE,
421 );
422 $resetFields = array();
423 if ($priceSetID) {
424 $resetFields = array('min_amount', 'max_amount', 'is_allow_other_amount');
425 }
426
427 if (!CRM_Utils_Array::value('is_recur', $params)) {
428 $resetFields = array_merge($resetFields, array('is_recur_interval', 'recur_frequency_unit'));
429 }
430
431 foreach ($fields as $field => $defaultVal) {
432 $val = CRM_Utils_Array::value($field, $params, $defaultVal);
433 if (in_array($field, $resetFields)) {
434 $val = $defaultVal;
435 }
436
437 if (in_array($field, array(
438 'min_amount', 'max_amount'))) {
439 $val = CRM_Utils_Rule::cleanMoney($val);
440 }
441
442 $params[$field] = $val;
443 }
444
445 if ($params['is_recur']) {
446 $params['recur_frequency_unit'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
447 array_keys($params['recur_frequency_unit'])
448 );
449 $params['is_recur_interval'] = CRM_Utils_Array::value('is_recur_interval', $params, FALSE);
450 $params['is_recur_installments'] = CRM_Utils_Array::value('is_recur_installments', $params, FALSE);
451 }
452
453 if (array_key_exists('payment_processor', $params) &&
454 !CRM_Utils_System::isNull($params['payment_processor'])
455 ) {
456 $params['payment_processor'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['payment_processor']));
457 }
458 else {
459 $params['payment_processor'] = 'null';
460 }
461
462 $contributionPage = CRM_Contribute_BAO_ContributionPage::create($params);
463 $contributionPageID = $contributionPage->id;
464
465 // prepare for data cleanup.
466 $deleteAmountBlk = $deletePledgeBlk = $deletePriceSet = FALSE;
467 if ($this->_priceSetID) {
468 $deletePriceSet = TRUE;
469 }
470 if ($this->_pledgeBlockID) {
471 $deletePledgeBlk = TRUE;
472 }
473 if (!empty($this->_amountBlock)) {
474 $deleteAmountBlk = TRUE;
475 }
476
477 if ($contributionPageID) {
478
479 if (CRM_Utils_Array::value('amount_block_is_active', $params)) {
480 // handle price set.
481 if ($priceSetID) {
482 // add/update price set.
483 $deletePriceSet = FALSE;
484 if (CRM_Utils_Array::value('price_field_id', $params) || CRM_Utils_Array::value('price_field_other', $params) ) {
485 $deleteAmountBlk = TRUE;
486 }
487
488 CRM_Price_BAO_Set::addTo('civicrm_contribution_page', $contributionPageID, $priceSetID);
489 }
490 else {
491
492 $deletePriceSet = FALSE;
493 // process contribution amount block
494 $deleteAmountBlk = FALSE;
495
496 $labels = CRM_Utils_Array::value('label', $params);
497 $values = CRM_Utils_Array::value('value', $params);
498 $default = CRM_Utils_Array::value('default', $params);
499
500 $options = array();
501 for ($i = 1; $i < self::NUM_OPTION; $i++) {
502 if (isset($values[$i]) &&
503 (strlen(trim($values[$i])) > 0)
504 ) {
505 $options[] = array('label' => trim($labels[$i]),
506 'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i])),
507 'weight' => $i,
508 'is_active' => 1,
509 'is_default' => $default == $i,
510 );
511 }
512 }
513 /* || CRM_Utils_Array::value( 'price_field_value', $params )|| CRM_Utils_Array::value( 'price_field_other', $params )*/
514 if (!empty($options) || CRM_Utils_Array::value('is_allow_other_amount', $params)) {
515 $fieldParams['is_quick_config'] = 1;
516 $noContriAmount = NULL;
517 $usedPriceSetId = CRM_Price_BAO_Set::getFor('civicrm_contribution_page', $this->_id, 3);
518 if (!(CRM_Utils_Array::value('price_field_id', $params) || CRM_Utils_Array::value('price_field_other', $params)) && !$usedPriceSetId) {
519 $pageTitle = strtolower(CRM_Utils_String::munge($this->_values['title'], '_', 245));
520 $setParams['title'] = $this->_values['title'];
521 if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Set', $pageTitle, 'id', 'name')) {
522 $setParams['name'] = $pageTitle;
523 }
524 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Set', $pageTitle . '_' . $this->_id, 'id', 'name')) {
525 $setParams['name'] = $pageTitle . '_' . $this->_id;
526 }
527 else {
528 $timeSec = explode(".", microtime(true));
529 $setParams['name'] = $pageTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
530 }
531 $setParams['is_quick_config'] = 1;
532 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviContribute');
533 $priceSet = CRM_Price_BAO_Set::create($setParams);
534 $priceSetId = $priceSet->id;
535 }
536 elseif ($usedPriceSetId && !CRM_Utils_Array::value('price_field_id', $params)) {
537 $priceSetId = $usedPriceSetId;
538 }
539 else {
540 if ($priceFieldId = CRM_Utils_Array::value('price_field_id', $params)) {
541 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
542 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
543 CRM_Price_BAO_FieldValue::setIsActive($fieldValueID, '0');
544 unset($params['price_field_value'][$arrayID]);
545 }
546 }
547 if (implode('', $params['price_field_value'])) {
548 $fieldParams['id'] = CRM_Utils_Array::value('price_field_id', $params);
549 $fieldParams['option_id'] = $params['price_field_value'];
550 }
551 else {
552 $noContriAmount = 0;
553 CRM_Price_BAO_Field::setIsActive($priceFieldId, '0');
554 }
555 }
556 else $priceFieldId = CRM_Utils_Array::value('price_field_other', $params);
557 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $priceFieldId, 'price_set_id');
558 }
559 CRM_Price_BAO_Set::addTo('civicrm_contribution_page', $this->_id, $priceSetId);
560 if (!empty($options)) {
561 $editedFieldParams = array(
562 'price_set_id' => $priceSetId,
563 'name' => 'contribution_amount',
564 );
565 $editedResults = array();
566 $noContriAmount = 1;
567 CRM_Price_BAO_Field::retrieve($editedFieldParams, $editedResults);
568 if (!CRM_Utils_Array::value('id', $editedResults)) {
569 $fieldParams['name'] = strtolower(CRM_Utils_String::munge("Contribution Amount", '_', 245));
570 $fieldParams['label'] = "Contribution Amount";
571 }
572 else {
573 $fieldParams['id'] = CRM_Utils_Array::value('id', $editedResults);
574 }
575
576 $fieldParams['price_set_id'] = $priceSetId;
577 $fieldParams['is_active'] = 1;
578 $fieldParams['weight'] = 2;
579
580 if (CRM_Utils_Array::value('is_allow_other_amount', $params)) {
581 $fieldParams['is_required'] = 0;
582 }
583 else {
584 $fieldParams['is_required'] = 1;
585 }
586 $fieldParams['html_type'] = 'Radio';
587 $fieldParams['option_label'] = $params['label'];
588 $fieldParams['option_amount'] = $params['value'];
589 $fieldParams['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $this->_values);
590 foreach ($options as $value) {
591 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
592 }
593 $fieldParams['default_option'] = $params['default'];
594 $priceField = CRM_Price_BAO_Field::create($fieldParams);
595 }
596 if (CRM_Utils_Array::value('is_allow_other_amount', $params) && !CRM_Utils_Array::value('price_field_other', $params)) {
597 $editedFieldParams = array(
598 'price_set_id' => $priceSetId,
599 'name' => 'other_amount',
600 );
601 $editedResults = array();
602
603 CRM_Price_BAO_Field::retrieve($editedFieldParams, $editedResults);
604
605 if (!$priceFieldID = CRM_Utils_Array::value('id', $editedResults)) {
606 $fieldParams = array(
607 'name' => 'other_amount',
608 'label' => 'Other Amount',
609 'price_set_id' => $priceSetId,
610 'html_type' => 'Text',
611 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $this->_values),
612 'is_display_amounts' => 0,
613 'weight' => 3,
614 );
615 $fieldParams['option_weight'][1] = 1;
616 $fieldParams['option_amount'][1] = 1;
617 if (!$noContriAmount) {
618 $fieldParams['is_required'] = 1;
619 $fieldParams['option_label'][1] = 'Contribution Amount';
620 } else {
621 $fieldParams['is_required'] = 0;
622 $fieldParams['option_label'][1] = 'Other Amount';
623 }
624
625 $priceField = CRM_Price_BAO_Field::create($fieldParams);
626 } else {
627 if (!CRM_Utils_Array::value('is_active', $editedResults)) {
628 CRM_Price_BAO_Field::setIsActive($priceFieldID, '1');
629 }
630 }
631 } elseif (!CRM_Utils_Array::value('is_allow_other_amount', $params) && CRM_Utils_Array::value('price_field_other', $params)) {
632 CRM_Price_BAO_Field::setIsActive($params['price_field_other'], '0');
633 } elseif ($priceFieldID = CRM_Utils_Array::value('price_field_other', $params)) {
634 $priceFieldValueID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_FieldValue', $priceFieldID, 'id', 'price_field_id' );
635 if (!$noContriAmount) {
636 CRM_Core_DAO::setFieldValue('CRM_Price_DAO_Field', $priceFieldID, 'is_required', 1);
637 CRM_Core_DAO::setFieldValue('CRM_Price_DAO_FieldValue', $priceFieldValueID, 'label', 'Contribution Amount' );
638 } else {
639 CRM_Core_DAO::setFieldValue('CRM_Price_DAO_Field', $priceFieldID, 'is_required', 0 );
640 CRM_Core_DAO::setFieldValue('CRM_Price_DAO_FieldValue', $priceFieldValueID, 'label', 'Other Amount' );
641 }
642 }
643 }
644
645 if (CRM_Utils_Array::value('is_pledge_active', $params)) {
646 $deletePledgeBlk = FALSE;
647 $pledgeBlockParams = array(
648 'entity_id' => $contributionPageID,
649 'entity_table' => ts('civicrm_contribution_page'),
650 );
651 if ($this->_pledgeBlockID) {
652 $pledgeBlockParams['id'] = $this->_pledgeBlockID;
653 }
654 $pledgeBlock = array(
655 'pledge_frequency_unit', 'max_reminders',
656 'initial_reminder_day', 'additional_reminder_day',
657 );
658 foreach ($pledgeBlock as $key) {
659 $pledgeBlockParams[$key] = CRM_Utils_Array::value($key, $params);
660 }
661 $pledgeBlockParams['is_pledge_interval'] = CRM_Utils_Array::value('is_pledge_interval',
662 $params, FALSE
663 );
664 // create pledge block.
665 CRM_Pledge_BAO_PledgeBlock::create($pledgeBlockParams);
666 }
667 }
668 }
669 else {
670 if (CRM_Utils_Array::value('price_field_id', $params) || CRM_Utils_Array::value('price_field_other', $params)) {
671 $usedPriceSetId = CRM_Price_BAO_Set::getFor('civicrm_contribution_page', $this->_id, 3);
672 if ($usedPriceSetId) {
673 if (CRM_Utils_Array::value('price_field_id', $params)) {
674 CRM_Price_BAO_Field::setIsActive($params['price_field_id'], '0');
675 }
676 if (CRM_Utils_Array::value('price_field_other', $params)) {
677 CRM_Price_BAO_Field::setIsActive($params['price_field_other'], '0');
678 }
679 }
680 else {
681 $deleteAmountBlk = TRUE;
682 $deletePriceSet = TRUE;
683 }
684 }
685 }
686
687 // delete pledge block.
688 if ($deletePledgeBlk) {
689 CRM_Pledge_BAO_PledgeBlock::deletePledgeBlock($this->_pledgeBlockID);
690 }
691
692 // delete previous price set.
693 if ($deletePriceSet) {
694 CRM_Price_BAO_Set::removeFrom('civicrm_contribution_page', $contributionPageID);
695 }
696
697 if ($deleteAmountBlk ) {
698 $priceField = CRM_Utils_Array::value('price_field_id', $params)?$params['price_field_id']:CRM_Utils_Array::value('price_field_other', $params);
699 if ($priceField) {
700 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $priceField, 'price_set_id');
701 CRM_Price_BAO_Set::setIsQuickConfig($priceSetID,0);
702 }
703 }
704 }
705 parent::endPostProcess();
706 }
707
708 /**
709 * Return a descriptive name for the page, used in wizard header
710 *
711 * @return string
712 * @access public
713 */
714 public function getTitle() {
715 return ts('Amounts');
716 }
717}
718