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