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