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