Merge pull request #9536 from twomice/CRM-19447_activity_details_search
[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-2016 |
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-2016
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 $paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors('live');
91 $recurringPaymentProcessor = $futurePaymentProcessor = $paymentProcessor = array();
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 }
104 }
105 }
106 if (count($recurringPaymentProcessor)) {
107 $this->assign('recurringPaymentProcessor', $recurringPaymentProcessor);
108 }
109 if (count($futurePaymentProcessor)) {
110 $this->assign('futurePaymentProcessor', $futurePaymentProcessor);
111 }
112 if (count($paymentProcessor)) {
113 $this->assign('paymentProcessor', $paymentProcessor);
114 }
115
116 $this->addCheckBox('payment_processor', ts('Payment Processor'),
117 array_flip($paymentProcessor),
118 NULL, NULL, NULL, NULL,
119 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>')
120 );
121
122 //check if selected payment processor supports recurring payment
123 if (!empty($recurringPaymentProcessor)) {
124 $this->addElement('checkbox', 'is_recur', ts('Recurring Contributions'), NULL,
125 array('onclick' => "showHideByValue('is_recur',true,'recurFields','table-row','radio',false);")
126 );
127 $this->addCheckBox('recur_frequency_unit', ts('Supported recurring units'),
128 CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, TRUE),
129 NULL, NULL, NULL, NULL,
130 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>'), TRUE
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 );
142 $this->add('wysiwyg', 'pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage', 'pay_later_receipt'));
143 $this->addElement('checkbox', 'is_billing_required', ts('Billing address required'));
144
145 //add partial payment options
146
147 // add price set fields
148 $price = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviContribute');
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'),
156 array(
157 '' => ts('- none -'),
158 ) + $price,
159 NULL, array('onchange' => "showHideAmountBlock( this.value, 'price_set_id' );")
160 );
161 //CiviPledge fields.
162 $config = CRM_Core_Config::singleton();
163 if (in_array('CiviPledge', $config->enableComponents)) {
164 $this->assign('civiPledge', TRUE);
165 $this->addElement('checkbox', 'is_pledge_active', ts('Pledges'),
166 NULL, array('onclick' => "showHideAmountBlock( this, 'is_pledge_active' ); return showHideByValue('is_pledge_active',true,'pledgeFields','table-row','radio',false);")
167 );
168 $this->addCheckBox('pledge_frequency_unit', ts('Supported pledge frequencies'),
169 CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, TRUE),
170 NULL, NULL, NULL, NULL,
171 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>'), TRUE
172 );
173 $this->addElement('checkbox', 'is_pledge_interval', ts('Allow frequency intervals'));
174 $this->addElement('text', 'initial_reminder_day', ts('Send payment reminder'), array('size' => 3));
175 $this->addElement('text', 'max_reminders', ts('Send up to'), array('size' => 3));
176 $this->addElement('text', 'additional_reminder_day', ts('Send additional reminders'), array('size' => 3));
177 if (!empty($futurePaymentProcessor)) {
178 // CRM-18854
179 $this->addElement('checkbox', 'adjust_recur_start_date', ts('Adjust Recurring Start Date'), NULL,
180 array('onclick' => "showHideByValue('adjust_recur_start_date',true,'recurDefaults','table-row','radio',false);")
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);
185 $pledgeDefaults = array(
186 'contribution_date' => ts('Day of Contribution'),
187 'calendar_date' => ts('Specific Calendar Date'),
188 'calendar_month' => ts('Specific day of Month'),
189 );
190 $this->addRadio('pledge_default_toggle', ts('Recurring Contribution Start Date Default'), $pledgeDefaults, array('allowClear' => FALSE), '<br/><br/>');
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 }
194 }
195
196 //add currency element.
197 $this->addCurrency('currency', ts('Currency'));
198
199 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Amount', 'formRule'), $this);
200
201 parent::buildQuickForm();
202 }
203
204 /**
205 * Set default values for the form. Note that in edit/view mode
206 * the default values are retrieved from the database
207 *
208 *
209 * @return array
210 */
211 public function setDefaultValues() {
212 $defaults = parent::setDefaultValues();
213
214 if (empty($defaults['pay_later_text'])) {
215 $defaults['pay_later_text'] = ts('I will send payment by check');
216 }
217
218 if (!empty($defaults['amount_block_is_active'])) {
219
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')) {
222 $this->assign('isQuick', $isQuick);
223 //$priceField = CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $priceSetId, 'id', 'price_set_id' );
224 $options = $pFIDs = array();
225 $priceFieldParams = array('price_set_id' => $priceSetId);
226 $priceFields = CRM_Core_DAO::commonRetrieveAll('CRM_Price_DAO_PriceField', 'price_set_id', $priceSetId, $pFIDs, $return = array(
227 'html_type',
228 'name',
229 'is_active',
230 'label',
231 ));
232 foreach ($priceFields as $priceField) {
233 if ($priceField['id'] && $priceField['html_type'] == 'Radio' && $priceField['name'] == 'contribution_amount') {
234 $defaults['price_field_id'] = $priceField['id'];
235 $priceFieldOptions = CRM_Price_BAO_PriceFieldValue::getValues($priceField['id'], $options, 'id', 1);
236 if (empty($priceFieldOptions)) {
237 continue;
238 }
239 $countRow = 0;
240 $defaults['amount_label'] = $priceField['label'];
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'];
256 if (!isset($defaults['amount_label'])) {
257 $defaults['amount_label'] = $priceField['label'];
258 }
259 }
260 }
261 }
262 }
263
264 if (empty($defaults['amount_label'])) {
265 $defaults['amount_label'] = ts('Contribution Amount');
266 }
267
268 if (!empty($defaults['value']) && is_array($defaults['value'])) {
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
285 if (!empty($defaults['payment_processor'])) {
286 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
287 $defaults['payment_processor']
288 ), '1');
289 }
290 return $defaults;
291 }
292
293 /**
294 * Global form rule.
295 *
296 * @param array $fields
297 * The input form values.
298 * @param array $files
299 * The uploaded files if any.
300 * @param $self
301 *
302 *
303 * @return bool|array
304 * true if no errors, else array of errors
305 */
306 public static function formRule($fields, $files, $self) {
307 $errors = array();
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)) {
320 if (!empty($fields['amount_block_is_active']) &&
321 ($setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $self->_id, NULL, 1))
322 ) {
323 $extends = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
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;
330 if ($membershipBlock->is_separate_payment && empty($fields['amount_block_is_active'])) {
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 }
333
334 //CRM-16165, Don't allow reccuring contribution if membership block contain any renewable membership option
335 $membershipTypes = unserialize($membershipBlock->membership_types);
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 }
343 }
344 }
345
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
353 //check for the amount label (mandatory)
354 if (!empty($fields['amount_block_is_active']) && empty($fields['amount_label'])) {
355 $errors['amount_label'] = ts('Please enter the contribution amount label.');
356 }
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 }
366
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 }
375
376 // don't allow price set w/ membership signup, CRM-5095
377 if ($priceSetId = CRM_Utils_Array::value('price_set_id', $fields)) {
378 // don't allow price set w/ membership.
379 if ($hasMembershipBlk) {
380 $errors['price_set_id'] = ts('You cannot enable both a Contribution Price Set and Membership Signup on the same online contribution page.');
381 }
382 }
383 else {
384 if (isset($fields['is_recur'])) {
385 if (empty($fields['recur_frequency_unit'])) {
386 $errors['recur_frequency_unit'] = ts('At least one recurring frequency option needs to be checked.');
387 }
388 }
389
390 // validation for pledge fields.
391 if (!empty($fields['is_pledge_active'])) {
392 if (empty($fields['pledge_frequency_unit'])) {
393 $errors['pledge_frequency_unit'] = ts('At least one pledge frequency option needs to be checked.');
394 }
395 if (!empty($fields['is_recur'])) {
396 $errors['is_recur'] = ts('You cannot enable both Recurring Contributions AND Pledges on the same online contribution page.');
397 }
398 }
399
400 // If Contribution amount section is enabled, then
401 // Allow other amounts must be enabled OR the Fixed Contribution
402 // Contribution options must contain at least one set of values.
403 if (!empty($fields['amount_block_is_active'])) {
404 if (empty($fields['is_allow_other_amount']) &&
405 !$priceSetId
406 ) {
407 //get the values of amount block
408 $values = CRM_Utils_Array::value('value', $fields);
409 $isSetRow = FALSE;
410 for ($i = 1; $i < self::NUM_OPTION; $i++) {
411 if ((isset($values[$i]) && (strlen(trim($values[$i])) > 0))) {
412 $isSetRow = TRUE;
413 }
414 }
415 if (!$isSetRow) {
416 $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.');
417 }
418 }
419 }
420 }
421
422 if (!empty($fields['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($self->_defaultValues['financial_type_id'])) {
423 $errors['payment_processor'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
424 }
425
426 if (!empty($fields['is_recur_interval'])) {
427 foreach (array_keys($fields['payment_processor']) as $paymentProcessorID) {
428 $paymentProcessorTypeId = CRM_Core_DAO::getFieldValue(
429 'CRM_Financial_DAO_PaymentProcessor',
430 $paymentProcessorID,
431 'payment_processor_type_id'
432 );
433 $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, $paymentProcessorTypeId, 'name');
434 }
435 }
436
437 return $errors;
438 }
439
440 /**
441 * Process the form.
442 */
443 public function postProcess() {
444 // get the submitted form values.
445 $params = $this->controller->exportValues($this->_name);
446
447 //update 'is_billing_required'
448 if (empty($params['is_pay_later'])) {
449 $params['is_billing_required'] = 0;
450 }
451
452 if (array_key_exists('payment_processor', $params)) {
453 if (array_key_exists(CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', 'AuthNet',
454 'id', 'payment_processor_type_id'
455 ),
456 CRM_Utils_Array::value('payment_processor', $params)
457 )) {
458 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');
459 }
460 }
461
462 // check for price set.
463 $priceSetID = CRM_Utils_Array::value('price_set_id', $params);
464
465 // get required fields.
466 $fields = array(
467 'id' => $this->_id,
468 'is_recur' => FALSE,
469 'min_amount' => "null",
470 'max_amount' => "null",
471 'is_monetary' => FALSE,
472 'is_pay_later' => FALSE,
473 'is_billing_required' => FALSE,
474 'is_recur_interval' => FALSE,
475 'is_recur_installments' => FALSE,
476 'recur_frequency_unit' => "null",
477 'default_amount_id' => "null",
478 'is_allow_other_amount' => FALSE,
479 'amount_block_is_active' => FALSE,
480 );
481 $resetFields = array();
482 if ($priceSetID) {
483 $resetFields = array('min_amount', 'max_amount', 'is_allow_other_amount');
484 }
485
486 if (empty($params['is_recur'])) {
487 $resetFields = array_merge($resetFields, array('is_recur_interval', 'recur_frequency_unit'));
488 }
489
490 foreach ($fields as $field => $defaultVal) {
491 $val = CRM_Utils_Array::value($field, $params, $defaultVal);
492 if (in_array($field, $resetFields)) {
493 $val = $defaultVal;
494 }
495
496 if (in_array($field, array(
497 'min_amount',
498 'max_amount',
499 ))) {
500 $val = CRM_Utils_Rule::cleanMoney($val);
501 }
502
503 $params[$field] = $val;
504 }
505
506 if ($params['is_recur']) {
507 $params['recur_frequency_unit'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
508 array_keys($params['recur_frequency_unit'])
509 );
510 $params['is_recur_interval'] = CRM_Utils_Array::value('is_recur_interval', $params, FALSE);
511 $params['is_recur_installments'] = CRM_Utils_Array::value('is_recur_installments', $params, FALSE);
512 }
513
514 if (CRM_Utils_Array::value('adjust_recur_start_date', $params)) {
515 $fieldValue = '';
516 $pledgeDateFields = array(
517 'calendar_date' => 'pledge_calendar_date',
518 'calendar_month' => 'pledge_calendar_month',
519 );
520 if ($params['pledge_default_toggle'] == 'contribution_date') {
521 $fieldValue = json_encode(array('contribution_date' => date('m/d/Y')));
522 }
523 else {
524 foreach ($pledgeDateFields as $key => $pledgeDateField) {
525 if (CRM_Utils_Array::value($pledgeDateField, $params) && $params['pledge_default_toggle'] == $key) {
526 $fieldValue = json_encode(array($key => $params[$pledgeDateField]));
527 break;
528 }
529 }
530 }
531 $params['pledge_start_date'] = $fieldValue;
532 }
533 else {
534 $params['pledge_start_date'] = '';
535 $params['adjust_recur_start_date'] = 0;
536 $params['is_pledge_start_date_visible'] = 0;
537 $params['is_pledge_start_date_editable'] = 0;
538 }
539 if (!CRM_Utils_Array::value('is_pledge_start_date_visible', $params)) {
540 $params['is_pledge_start_date_visible'] = 0;
541 }
542 if (!CRM_Utils_Array::value('is_pledge_start_date_editable', $params)) {
543 $params['is_pledge_start_date_editable'] = 0;
544 }
545
546 if (array_key_exists('payment_processor', $params) &&
547 !CRM_Utils_System::isNull($params['payment_processor'])
548 ) {
549 $params['payment_processor'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['payment_processor']));
550 }
551 else {
552 $params['payment_processor'] = 'null';
553 }
554
555 $contributionPage = CRM_Contribute_BAO_ContributionPage::create($params);
556 $contributionPageID = $contributionPage->id;
557
558 // prepare for data cleanup.
559 $deleteAmountBlk = $deletePledgeBlk = $deletePriceSet = FALSE;
560 if ($this->_priceSetID) {
561 $deletePriceSet = TRUE;
562 }
563 if ($this->_pledgeBlockID) {
564 $deletePledgeBlk = TRUE;
565 }
566 if (!empty($this->_amountBlock)) {
567 $deleteAmountBlk = TRUE;
568 }
569
570 if ($contributionPageID) {
571
572 if (!empty($params['amount_block_is_active'])) {
573 // handle price set.
574 if ($priceSetID) {
575 // add/update price set.
576 $deletePriceSet = FALSE;
577 if (!empty($params['price_field_id']) || !empty($params['price_field_other'])) {
578 $deleteAmountBlk = TRUE;
579 }
580
581 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageID, $priceSetID);
582 }
583 else {
584
585 $deletePriceSet = FALSE;
586 // process contribution amount block
587 $deleteAmountBlk = FALSE;
588
589 $labels = CRM_Utils_Array::value('label', $params);
590 $values = CRM_Utils_Array::value('value', $params);
591 $default = CRM_Utils_Array::value('default', $params);
592
593 $options = array();
594 for ($i = 1; $i < self::NUM_OPTION; $i++) {
595 if (isset($values[$i]) &&
596 (strlen(trim($values[$i])) > 0)
597 ) {
598 $options[] = array(
599 'label' => trim($labels[$i]),
600 'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i])),
601 'weight' => $i,
602 'is_active' => 1,
603 'is_default' => $default == $i,
604 );
605 }
606 }
607 /* || !empty($params['price_field_value']) || CRM_Utils_Array::value( 'price_field_other', $params )*/
608 if (!empty($options) || !empty($params['is_allow_other_amount'])) {
609 $fieldParams['is_quick_config'] = 1;
610 $noContriAmount = NULL;
611 $usedPriceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, 3);
612 if (!(!empty($params['price_field_id']) || !empty($params['price_field_other'])) && !$usedPriceSetId) {
613 $pageTitle = strtolower(CRM_Utils_String::munge($this->_values['title'], '_', 245));
614 $setParams['title'] = $this->_values['title'];
615 if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $pageTitle, 'id', 'name')) {
616 $setParams['name'] = $pageTitle;
617 }
618 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $pageTitle . '_' . $this->_id, 'id', 'name')) {
619 $setParams['name'] = $pageTitle . '_' . $this->_id;
620 }
621 else {
622 $timeSec = explode(".", microtime(TRUE));
623 $setParams['name'] = $pageTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
624 }
625 $setParams['is_quick_config'] = 1;
626 $setParams['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $this->_values);
627 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviContribute');
628 $priceSet = CRM_Price_BAO_PriceSet::create($setParams);
629 $priceSetId = $priceSet->id;
630 }
631 elseif ($usedPriceSetId && empty($params['price_field_id'])) {
632 $priceSetId = $usedPriceSetId;
633 }
634 else {
635 if ($priceFieldId = CRM_Utils_Array::value('price_field_id', $params)) {
636 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
637 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
638 CRM_Price_BAO_PriceFieldValue::setIsActive($fieldValueID, '0');
639 unset($params['price_field_value'][$arrayID]);
640 }
641 }
642 if (implode('', $params['price_field_value'])) {
643 $fieldParams['id'] = CRM_Utils_Array::value('price_field_id', $params);
644 $fieldParams['option_id'] = $params['price_field_value'];
645 }
646 else {
647 $noContriAmount = 0;
648 CRM_Price_BAO_PriceField::setIsActive($priceFieldId, '0');
649 }
650 }
651 else {
652 $priceFieldId = CRM_Utils_Array::value('price_field_other', $params);
653 }
654 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $priceFieldId, 'price_set_id');
655 }
656 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $this->_id, $priceSetId);
657 if (!empty($options)) {
658 $editedFieldParams = array(
659 'price_set_id' => $priceSetId,
660 'name' => 'contribution_amount',
661 );
662 $editedResults = array();
663 $noContriAmount = 1;
664 CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults);
665 if (empty($editedResults['id'])) {
666 $fieldParams['name'] = strtolower(CRM_Utils_String::munge("Contribution Amount", '_', 245));
667 }
668 else {
669 $fieldParams['id'] = CRM_Utils_Array::value('id', $editedResults);
670 }
671
672 $fieldParams['price_set_id'] = $priceSetId;
673 $fieldParams['is_active'] = 1;
674 $fieldParams['weight'] = 2;
675
676 if (!empty($params['is_allow_other_amount'])) {
677 $fieldParams['is_required'] = 0;
678 }
679 else {
680 $fieldParams['is_required'] = 1;
681 }
682 $fieldParams['label'] = $params['amount_label'];
683 $fieldParams['html_type'] = 'Radio';
684 $fieldParams['option_label'] = $params['label'];
685 $fieldParams['option_amount'] = $params['value'];
686 $fieldParams['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $this->_values);
687 foreach ($options as $value) {
688 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
689 }
690 $fieldParams['default_option'] = $params['default'];
691 $priceField = CRM_Price_BAO_PriceField::create($fieldParams);
692 }
693 if (!empty($params['is_allow_other_amount']) && empty($params['price_field_other'])) {
694 $editedFieldParams = array(
695 'price_set_id' => $priceSetId,
696 'name' => 'other_amount',
697 );
698 $editedResults = array();
699
700 CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults);
701
702 if (!$priceFieldID = CRM_Utils_Array::value('id', $editedResults)) {
703 $fieldParams = array(
704 'name' => 'other_amount',
705 'label' => ts('Other Amount'),
706 'price_set_id' => $priceSetId,
707 'html_type' => 'Text',
708 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $this->_values),
709 'is_display_amounts' => 0,
710 'weight' => 3,
711 );
712 $fieldParams['option_weight'][1] = 1;
713 $fieldParams['option_amount'][1] = 1;
714 if (!$noContriAmount) {
715 $fieldParams['is_required'] = 1;
716 $fieldParams['option_label'][1] = $fieldParams['label'] = $params['amount_label'];
717 }
718 else {
719 $fieldParams['is_required'] = 0;
720 $fieldParams['option_label'][1] = $fieldParams['label'] = ts('Other Amount');
721 }
722
723 $priceField = CRM_Price_BAO_PriceField::create($fieldParams);
724 }
725 else {
726 if (empty($editedResults['is_active'])) {
727 $fieldParams = $editedResults;
728 if (!$noContriAmount) {
729 $priceFieldValueID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldID, 'id', 'price_field_id');
730 CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldValueID, 'label', $params['amount_label']);
731 $fieldParams = array(
732 'is_required' => 1,
733 'label' => $params['amount_label'],
734 'id' => $priceFieldID,
735 );
736 }
737 $fieldParams['is_active'] = 1;
738 $priceField = CRM_Price_BAO_PriceField::add($fieldParams);
739 }
740 }
741 }
742 elseif (empty($params['is_allow_other_amount']) && !empty($params['price_field_other'])) {
743 CRM_Price_BAO_PriceField::setIsActive($params['price_field_other'], '0');
744 }
745 elseif ($priceFieldID = CRM_Utils_Array::value('price_field_other', $params)) {
746 $priceFieldValueID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldID, 'id', 'price_field_id');
747 if (!$noContriAmount) {
748 $fieldParams = array(
749 'is_required' => 1,
750 'label' => $params['amount_label'],
751 'id' => $priceFieldID,
752 );
753 CRM_Price_BAO_PriceField::add($fieldParams);
754 CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldValueID, 'label', $params['amount_label']);
755 }
756 else {
757 CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceField', $priceFieldID, 'is_required', 0);
758 CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldValueID, 'label', ts('Other Amount'));
759 }
760 }
761 }
762
763 if (!empty($params['is_pledge_active'])) {
764 $deletePledgeBlk = FALSE;
765 $pledgeBlockParams = array(
766 'entity_id' => $contributionPageID,
767 'entity_table' => ts('civicrm_contribution_page'),
768 );
769 if ($this->_pledgeBlockID) {
770 $pledgeBlockParams['id'] = $this->_pledgeBlockID;
771 }
772 $pledgeBlock = array(
773 'pledge_frequency_unit',
774 'max_reminders',
775 'initial_reminder_day',
776 'additional_reminder_day',
777 'pledge_start_date',
778 'is_pledge_start_date_visible',
779 'is_pledge_start_date_editable',
780 );
781 foreach ($pledgeBlock as $key) {
782 $pledgeBlockParams[$key] = CRM_Utils_Array::value($key, $params);
783 }
784 $pledgeBlockParams['is_pledge_interval'] = CRM_Utils_Array::value('is_pledge_interval',
785 $params, FALSE
786 );
787 $pledgeBlockParams['pledge_start_date'] = CRM_Utils_Array::value('pledge_start_date',
788 $params, FALSE
789 );
790 // create pledge block.
791 CRM_Pledge_BAO_PledgeBlock::create($pledgeBlockParams);
792 }
793 }
794 }
795 else {
796 if (!empty($params['price_field_id']) || !empty($params['price_field_other'])) {
797 $usedPriceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, 3);
798 if ($usedPriceSetId) {
799 if (!empty($params['price_field_id'])) {
800 CRM_Price_BAO_PriceField::setIsActive($params['price_field_id'], '0');
801 }
802 if (!empty($params['price_field_other'])) {
803 CRM_Price_BAO_PriceField::setIsActive($params['price_field_other'], '0');
804 }
805 }
806 else {
807 $deleteAmountBlk = TRUE;
808 $deletePriceSet = TRUE;
809 }
810 }
811 }
812
813 // delete pledge block.
814 if ($deletePledgeBlk) {
815 CRM_Pledge_BAO_PledgeBlock::deletePledgeBlock($this->_pledgeBlockID);
816 }
817
818 // delete previous price set.
819 if ($deletePriceSet) {
820 CRM_Price_BAO_PriceSet::removeFrom('civicrm_contribution_page', $contributionPageID);
821 }
822
823 if ($deleteAmountBlk) {
824 $priceField = !empty($params['price_field_id']) ? $params['price_field_id'] : CRM_Utils_Array::value('price_field_other', $params);
825 if ($priceField) {
826 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $priceField, 'price_set_id');
827 CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetID, 0);
828 }
829 }
830 }
831 parent::endPostProcess();
832 }
833
834 /**
835 * Return a descriptive name for the page, used in wizard header
836 *
837 * @return string
838 */
839 public function getTitle() {
840 return ts('Amounts');
841 }
842
843 }