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