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