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