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