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