| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 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-2017 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * This class generates form components for processing a Contribution. |
| 36 | */ |
| 37 | class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_ContributionBase { |
| 38 | |
| 39 | /** |
| 40 | * Define default MembershipType Id. |
| 41 | */ |
| 42 | public $_defaultMemTypeId; |
| 43 | |
| 44 | public $_paymentProcessors; |
| 45 | |
| 46 | public $_membershipTypeValues; |
| 47 | |
| 48 | public $_useForMember; |
| 49 | |
| 50 | /** |
| 51 | * Array of payment related fields to potentially display on this form (generally credit card or debit card fields). This is rendered via billingBlock.tpl |
| 52 | * @var array |
| 53 | */ |
| 54 | public $_paymentFields = array(); |
| 55 | |
| 56 | protected $_paymentProcessorID; |
| 57 | protected $_snippet; |
| 58 | |
| 59 | /** |
| 60 | * Set variables up before form is built. |
| 61 | */ |
| 62 | public function preProcess() { |
| 63 | parent::preProcess(); |
| 64 | |
| 65 | $this->_paymentProcessors = $this->get('paymentProcessors'); |
| 66 | $this->preProcessPaymentOptions(); |
| 67 | |
| 68 | if (!empty($this->_ccid)) { |
| 69 | $payment = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_ccid, 'contribution'); |
| 70 | //bounce if the contribution is not pending. |
| 71 | if (empty($payment['balance'])) { |
| 72 | CRM_Core_Error::statusBounce(ts("Returning since contribution has already been handled.")); |
| 73 | } |
| 74 | if (!empty($payment['total'])) { |
| 75 | $this->_pendingAmount = $payment['total']; |
| 76 | $this->assign('pendingAmount', $this->_pendingAmount); |
| 77 | } |
| 78 | $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($this->_ccid); |
| 79 | foreach (array_keys($lineItems) as $id) { |
| 80 | $lineItems[$id]['id'] = $id; |
| 81 | } |
| 82 | $itemId = key($lineItems); |
| 83 | if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) { |
| 84 | $this->_priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id'); |
| 85 | } |
| 86 | |
| 87 | if (!empty($lineItems[$itemId]['price_field_id'])) { |
| 88 | $this->_lineItem[$this->_priceSetId] = $lineItems; |
| 89 | } |
| 90 | $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); |
| 91 | $this->assign('lineItem', $this->_lineItem); |
| 92 | $this->assign('is_quick_config', $isQuickConfig); |
| 93 | $this->assign('priceSetID', $this->_priceSetId); |
| 94 | } |
| 95 | |
| 96 | // Make the contributionPageID available to the template |
| 97 | $this->assign('contributionPageID', $this->_id); |
| 98 | $this->assign('ccid', $this->_ccid); |
| 99 | $this->assign('isShare', CRM_Utils_Array::value('is_share', $this->_values)); |
| 100 | $this->assign('isConfirmEnabled', CRM_Utils_Array::value('is_confirm_enabled', $this->_values)); |
| 101 | |
| 102 | $this->assign('reset', CRM_Utils_Request::retrieve('reset', 'Boolean')); |
| 103 | $this->assign('mainDisplay', CRM_Utils_Request::retrieve('_qf_Main_display', 'Boolean', |
| 104 | CRM_Core_DAO::$_nullObject)); |
| 105 | |
| 106 | if (!empty($this->_pcpInfo['id']) && !empty($this->_pcpInfo['intro_text'])) { |
| 107 | $this->assign('intro_text', $this->_pcpInfo['intro_text']); |
| 108 | } |
| 109 | elseif (!empty($this->_values['intro_text'])) { |
| 110 | $this->assign('intro_text', $this->_values['intro_text']); |
| 111 | } |
| 112 | |
| 113 | $qParams = "reset=1&id={$this->_id}"; |
| 114 | if ($pcpId = CRM_Utils_Array::value('pcp_id', $this->_pcpInfo)) { |
| 115 | $qParams .= "&pcpId={$pcpId}"; |
| 116 | } |
| 117 | $this->assign('qParams', $qParams); |
| 118 | |
| 119 | if (!empty($this->_values['footer_text'])) { |
| 120 | $this->assign('footer_text', $this->_values['footer_text']); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Set the default values. |
| 126 | */ |
| 127 | public function setDefaultValues() { |
| 128 | // check if the user is registered and we have a contact ID |
| 129 | $contactID = $this->getContactID(); |
| 130 | |
| 131 | if (!empty($contactID)) { |
| 132 | $fields = array(); |
| 133 | $removeCustomFieldTypes = array('Contribution', 'Membership'); |
| 134 | $contribFields = CRM_Contribute_BAO_Contribution::getContributionFields(); |
| 135 | |
| 136 | // remove component related fields |
| 137 | foreach ($this->_fields as $name => $dontCare) { |
| 138 | //don't set custom data Used for Contribution (CRM-1344) |
| 139 | if (substr($name, 0, 7) == 'custom_') { |
| 140 | $id = substr($name, 7); |
| 141 | if (!CRM_Core_BAO_CustomGroup::checkCustomField($id, $removeCustomFieldTypes)) { |
| 142 | continue; |
| 143 | } |
| 144 | // ignore component fields |
| 145 | } |
| 146 | elseif (array_key_exists($name, $contribFields) || (substr($name, 0, 11) == 'membership_') || (substr($name, 0, 13) == 'contribution_')) { |
| 147 | continue; |
| 148 | } |
| 149 | $fields[$name] = 1; |
| 150 | } |
| 151 | |
| 152 | if (!empty($fields)) { |
| 153 | CRM_Core_BAO_UFGroup::setProfileDefaults($contactID, $fields, $this->_defaults); |
| 154 | } |
| 155 | |
| 156 | $billingDefaults = $this->getProfileDefaults('Billing', $contactID); |
| 157 | $this->_defaults = array_merge($this->_defaults, $billingDefaults); |
| 158 | } |
| 159 | if (!empty($this->_ccid) && !empty($this->_pendingAmount)) { |
| 160 | $this->_defaults['total_amount'] = CRM_Utils_Money::format($this->_pendingAmount, NULL, '%a'); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * hack to simplify credit card entry for testing |
| 165 | * |
| 166 | * $this->_defaults['credit_card_type'] = 'Visa'; |
| 167 | * $this->_defaults['amount'] = 168; |
| 168 | * $this->_defaults['credit_card_number'] = '4111111111111111'; |
| 169 | * $this->_defaults['cvv2'] = '000'; |
| 170 | * $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05'); |
| 171 | * // hack to simplify direct debit entry for testing |
| 172 | * $this->_defaults['account_holder'] = 'Max Müller'; |
| 173 | * $this->_defaults['bank_account_number'] = '12345678'; |
| 174 | * $this->_defaults['bank_identification_number'] = '12030000'; |
| 175 | * $this->_defaults['bank_name'] = 'Bankname'; |
| 176 | */ |
| 177 | |
| 178 | //build set default for pledge overdue payment. |
| 179 | if (!empty($this->_values['pledge_id'])) { |
| 180 | //used to record completed pledge payment ids used later for honor default |
| 181 | $completedContributionIds = array(); |
| 182 | $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($this->_values['pledge_id']); |
| 183 | |
| 184 | $paymentAmount = 0; |
| 185 | $duePayment = FALSE; |
| 186 | foreach ($pledgePayments as $payId => $value) { |
| 187 | if ($value['status'] == 'Overdue') { |
| 188 | $this->_defaults['pledge_amount'][$payId] = 1; |
| 189 | $paymentAmount += $value['scheduled_amount']; |
| 190 | } |
| 191 | elseif (!$duePayment && $value['status'] == 'Pending') { |
| 192 | $this->_defaults['pledge_amount'][$payId] = 1; |
| 193 | $paymentAmount += $value['scheduled_amount']; |
| 194 | $duePayment = TRUE; |
| 195 | } |
| 196 | elseif ($value['status'] == 'Completed' && $value['contribution_id']) { |
| 197 | $completedContributionIds[] = $value['contribution_id']; |
| 198 | } |
| 199 | } |
| 200 | $this->_defaults['price_' . $this->_priceSetId] = $paymentAmount; |
| 201 | |
| 202 | if (count($completedContributionIds)) { |
| 203 | $softCredit = array(); |
| 204 | foreach ($completedContributionIds as $id) { |
| 205 | $softCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id); |
| 206 | } |
| 207 | if (isset($softCredit['soft_credit'])) { |
| 208 | $this->_defaults['soft_credit_type_id'] = $softCredit['soft_credit'][1]['soft_credit_type']; |
| 209 | |
| 210 | //since honoree profile fieldname of fields are prefixed with 'honor' |
| 211 | //we need to reformat the fieldname to append prefix during setting default values |
| 212 | CRM_Core_BAO_UFGroup::setProfileDefaults( |
| 213 | $softCredit['soft_credit'][1]['contact_id'], |
| 214 | CRM_Core_BAO_UFGroup::getFields($this->_honoreeProfileId), |
| 215 | $defaults |
| 216 | ); |
| 217 | foreach ($defaults as $fieldName => $value) { |
| 218 | $this->_defaults['honor[' . $fieldName . ']'] = $value; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | elseif (!empty($this->_values['pledge_block_id'])) { |
| 224 | //set default to one time contribution. |
| 225 | $this->_defaults['is_pledge'] = 0; |
| 226 | } |
| 227 | |
| 228 | // to process Custom data that are appended to URL |
| 229 | $getDefaults = CRM_Core_BAO_CustomGroup::extractGetParams($this, "'Contact', 'Individual', 'Contribution'"); |
| 230 | $this->_defaults = array_merge($this->_defaults, $getDefaults); |
| 231 | |
| 232 | $config = CRM_Core_Config::singleton(); |
| 233 | // set default country from config if no country set |
| 234 | if (empty($this->_defaults["billing_country_id-{$this->_bltID}"])) { |
| 235 | $this->_defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; |
| 236 | } |
| 237 | |
| 238 | // set default state/province from config if no state/province set |
| 239 | if (empty($this->_defaults["billing_state_province_id-{$this->_bltID}"])) { |
| 240 | $this->_defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; |
| 241 | } |
| 242 | |
| 243 | $entityId = $memtypeID = NULL; |
| 244 | if ($this->_priceSetId) { |
| 245 | if (($this->_useForMember && !empty($this->_currentMemberships)) || $this->_defaultMemTypeId) { |
| 246 | $selectedCurrentMemTypes = array(); |
| 247 | foreach ($this->_priceSet['fields'] as $key => $val) { |
| 248 | foreach ($val['options'] as $keys => $values) { |
| 249 | $opMemTypeId = CRM_Utils_Array::value('membership_type_id', $values); |
| 250 | $priceFieldName = 'price_' . $values['price_field_id']; |
| 251 | $priceFieldValue = CRM_Price_BAO_PriceSet::getPriceFieldValueFromURL($this, $priceFieldName); |
| 252 | if (!empty($priceFieldValue)) { |
| 253 | CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $priceFieldValue, $val['html_type'], $this->_defaults); |
| 254 | // break here to prevent overwriting of default due to 'is_default' |
| 255 | // option configuration or setting of current membership or |
| 256 | // membership for related organization. |
| 257 | // The value sent via URL get's higher priority. |
| 258 | break; |
| 259 | } |
| 260 | elseif ($opMemTypeId && |
| 261 | in_array($opMemTypeId, $this->_currentMemberships) && |
| 262 | !in_array($opMemTypeId, $selectedCurrentMemTypes) |
| 263 | ) { |
| 264 | CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults); |
| 265 | $memtypeID = $selectedCurrentMemTypes[] = $values['membership_type_id']; |
| 266 | } |
| 267 | elseif (!empty($values['is_default']) && |
| 268 | !$opMemTypeId && |
| 269 | (!isset($this->_defaults[$priceFieldName]) || |
| 270 | ($val['html_type'] == 'CheckBox' && |
| 271 | !isset($this->_defaults[$priceFieldName][$keys])) |
| 272 | )) { |
| 273 | CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults); |
| 274 | $memtypeID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_defaults[$priceFieldName], 'membership_type_id'); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | $entityId = CRM_Utils_Array::value('id', CRM_Member_BAO_Membership::getContactMembership($contactID, $memtypeID, NULL)); |
| 279 | } |
| 280 | else { |
| 281 | CRM_Price_BAO_PriceSet::setDefaultPriceSet($this, $this->_defaults); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | //set custom field defaults set by admin if value is not set |
| 286 | if (!empty($this->_fields)) { |
| 287 | //load default campaign from page. |
| 288 | if (array_key_exists('contribution_campaign_id', $this->_fields)) { |
| 289 | $this->_defaults['contribution_campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values); |
| 290 | } |
| 291 | |
| 292 | //set custom field defaults |
| 293 | foreach ($this->_fields as $name => $field) { |
| 294 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { |
| 295 | if (!isset($this->_defaults[$name])) { |
| 296 | CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults, |
| 297 | $entityId, CRM_Profile_Form::MODE_REGISTER |
| 298 | ); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (!empty($this->_paymentProcessors)) { |
| 305 | foreach ($this->_paymentProcessors as $pid => $value) { |
| 306 | if (!empty($value['is_default'])) { |
| 307 | $this->_defaults['payment_processor_id'] = $pid; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | return $this->_defaults; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Build the form object. |
| 317 | */ |
| 318 | public function buildQuickForm() { |
| 319 | // build profiles first so that we can determine address fields etc |
| 320 | // and then show copy address checkbox |
| 321 | if (empty($this->_ccid)) { |
| 322 | $this->buildCustom($this->_values['custom_pre_id'], 'customPre'); |
| 323 | $this->buildCustom($this->_values['custom_post_id'], 'customPost'); |
| 324 | |
| 325 | // CRM-18399: used by template to pass pre profile id as a url arg |
| 326 | $this->assign('custom_pre_id', $this->_values['custom_pre_id']); |
| 327 | |
| 328 | $this->buildComponentForm($this->_id, $this); |
| 329 | } |
| 330 | |
| 331 | // Build payment processor form |
| 332 | CRM_Core_Payment_ProcessorForm::buildQuickForm($this); |
| 333 | |
| 334 | $config = CRM_Core_Config::singleton(); |
| 335 | |
| 336 | $contactID = $this->getContactID(); |
| 337 | if ($contactID) { |
| 338 | $this->assign('contact_id', $contactID); |
| 339 | $this->assign('display_name', CRM_Contact_BAO_Contact::displayName($contactID)); |
| 340 | } |
| 341 | |
| 342 | $this->applyFilter('__ALL__', 'trim'); |
| 343 | if (empty($this->_ccid)) { |
| 344 | $this->add('text', "email-{$this->_bltID}", |
| 345 | ts('Email Address'), |
| 346 | array('size' => 30, 'maxlength' => 60, 'class' => 'email'), |
| 347 | TRUE |
| 348 | ); |
| 349 | $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email'); |
| 350 | } |
| 351 | else { |
| 352 | $this->addElement('hidden', "email-{$this->_bltID}", 1); |
| 353 | $this->add('text', 'total_amount', ts('Total Amount'), array('readonly' => TRUE), FALSE); |
| 354 | } |
| 355 | $pps = array(); |
| 356 | //@todo - this should be replaced by a check as to whether billing fields are set |
| 357 | $onlinePaymentProcessorEnabled = FALSE; |
| 358 | if (!empty($this->_paymentProcessors)) { |
| 359 | foreach ($this->_paymentProcessors as $key => $name) { |
| 360 | if ($name['billing_mode'] == 1) { |
| 361 | $onlinePaymentProcessorEnabled = TRUE; |
| 362 | } |
| 363 | $pps[$key] = $name['name']; |
| 364 | } |
| 365 | } |
| 366 | if (!empty($this->_values['is_pay_later'])) { |
| 367 | $pps[0] = $this->_values['pay_later_text']; |
| 368 | } |
| 369 | |
| 370 | if (count($pps) > 1) { |
| 371 | $this->addRadio('payment_processor_id', ts('Payment Method'), $pps, |
| 372 | NULL, " " |
| 373 | ); |
| 374 | } |
| 375 | elseif (!empty($pps)) { |
| 376 | $key = array_keys($pps); |
| 377 | $key = array_pop($key); |
| 378 | $this->addElement('hidden', 'payment_processor_id', $key); |
| 379 | if ($key === 0) { |
| 380 | $this->assign('is_pay_later', $this->_values['is_pay_later']); |
| 381 | $this->assign('pay_later_text', $this->_values['pay_later_text']); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | $contactID = $this->getContactID(); |
| 386 | if ($this->getContactID() === 0) { |
| 387 | $this->addCidZeroOptions($onlinePaymentProcessorEnabled); |
| 388 | } |
| 389 | |
| 390 | //build pledge block. |
| 391 | $this->_useForMember = 0; |
| 392 | //don't build membership block when pledge_id is passed |
| 393 | if (empty($this->_values['pledge_id']) && empty($this->_ccid)) { |
| 394 | $this->_separateMembershipPayment = FALSE; |
| 395 | if (in_array('CiviMember', $config->enableComponents)) { |
| 396 | $isTest = 0; |
| 397 | if ($this->_action & CRM_Core_Action::PREVIEW) { |
| 398 | $isTest = 1; |
| 399 | } |
| 400 | |
| 401 | if ($this->_priceSetId && |
| 402 | (CRM_Core_Component::getComponentID('CiviMember') == CRM_Utils_Array::value('extends', $this->_priceSet)) |
| 403 | ) { |
| 404 | $this->_useForMember = 1; |
| 405 | $this->set('useForMember', $this->_useForMember); |
| 406 | } |
| 407 | |
| 408 | $this->_separateMembershipPayment = $this->buildMembershipBlock( |
| 409 | $this->_membershipContactID, |
| 410 | TRUE, NULL, FALSE, |
| 411 | $isTest |
| 412 | ); |
| 413 | } |
| 414 | $this->set('separateMembershipPayment', $this->_separateMembershipPayment); |
| 415 | } |
| 416 | $this->assign('useForMember', $this->_useForMember); |
| 417 | // If we configured price set for contribution page |
| 418 | // we are not allow membership signup as well as any |
| 419 | // other contribution amount field, CRM-5095 |
| 420 | if (isset($this->_priceSetId) && $this->_priceSetId) { |
| 421 | $this->add('hidden', 'priceSetId', $this->_priceSetId); |
| 422 | // build price set form. |
| 423 | $this->set('priceSetId', $this->_priceSetId); |
| 424 | if (empty($this->_ccid)) { |
| 425 | CRM_Price_BAO_PriceSet::buildPriceSet($this); |
| 426 | } |
| 427 | if ($this->_values['is_monetary'] && |
| 428 | $this->_values['is_recur'] && empty($this->_values['pledge_id']) |
| 429 | ) { |
| 430 | self::buildRecur($this); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if ($this->_priceSetId && empty($this->_ccid)) { |
| 435 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); |
| 436 | if ($is_quick_config) { |
| 437 | $this->_useForMember = 0; |
| 438 | $this->set('useForMember', $this->_useForMember); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | //we allow premium for pledge during pledge creation only. |
| 443 | if (empty($this->_values['pledge_id']) && empty($this->_ccid)) { |
| 444 | CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, TRUE); |
| 445 | } |
| 446 | |
| 447 | //don't build pledge block when mid is passed |
| 448 | if (!$this->_mid && empty($this->_ccid)) { |
| 449 | $config = CRM_Core_Config::singleton(); |
| 450 | if (in_array('CiviPledge', $config->enableComponents) && !empty($this->_values['pledge_block_id'])) { |
| 451 | CRM_Pledge_BAO_PledgeBlock::buildPledgeBlock($this); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | //to create an cms user |
| 456 | if (!$this->_contactID && empty($this->_ccid)) { |
| 457 | $createCMSUser = FALSE; |
| 458 | |
| 459 | if ($this->_values['custom_pre_id']) { |
| 460 | $profileID = $this->_values['custom_pre_id']; |
| 461 | $createCMSUser = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_cms_user'); |
| 462 | } |
| 463 | |
| 464 | if (!$createCMSUser && |
| 465 | $this->_values['custom_post_id'] |
| 466 | ) { |
| 467 | if (!is_array($this->_values['custom_post_id'])) { |
| 468 | $profileIDs = array($this->_values['custom_post_id']); |
| 469 | } |
| 470 | else { |
| 471 | $profileIDs = $this->_values['custom_post_id']; |
| 472 | } |
| 473 | foreach ($profileIDs as $pid) { |
| 474 | if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) { |
| 475 | $profileID = $pid; |
| 476 | $createCMSUser = TRUE; |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if ($createCMSUser) { |
| 483 | CRM_Core_BAO_CMSUser::buildForm($this, $profileID, TRUE); |
| 484 | } |
| 485 | } |
| 486 | if ($this->_pcpId && empty($this->_ccid)) { |
| 487 | if ($pcpSupporter = CRM_PCP_BAO_PCP::displayName($this->_pcpId)) { |
| 488 | $pcp_supporter_text = ts('This contribution is being made thanks to the effort of <strong>%1</strong>, who supports our campaign.', array(1 => $pcpSupporter)); |
| 489 | // Only tell people that can also create a PCP if the contribution page has a non-empty value in the "Create Personal Campaign Page link" field. |
| 490 | $text = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute'); |
| 491 | if (!empty($text)) { |
| 492 | $pcp_supporter_text .= ts("You can support it as well - once you complete the donation, you will be able to create your own Personal Campaign Page!"); |
| 493 | } |
| 494 | $this->assign('pcpSupporterText', $pcp_supporter_text); |
| 495 | } |
| 496 | $prms = array('id' => $this->_pcpId); |
| 497 | CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo); |
| 498 | if ($pcpInfo['is_honor_roll']) { |
| 499 | $this->assign('isHonor', TRUE); |
| 500 | $this->add('checkbox', 'pcp_display_in_roll', ts('Show my contribution in the public honor roll'), NULL, NULL, |
| 501 | array('onclick' => "showHideByValue('pcp_display_in_roll','','nameID|nickID|personalNoteID','block','radio',false); pcpAnonymous( );") |
| 502 | ); |
| 503 | $extraOption = array('onclick' => "return pcpAnonymous( );"); |
| 504 | $elements = array(); |
| 505 | $elements[] = &$this->createElement('radio', NULL, '', ts('Include my name and message'), 0, $extraOption); |
| 506 | $elements[] = &$this->createElement('radio', NULL, '', ts('List my contribution anonymously'), 1, $extraOption); |
| 507 | $this->addGroup($elements, 'pcp_is_anonymous', NULL, ' '); |
| 508 | |
| 509 | $this->add('text', 'pcp_roll_nickname', ts('Name'), array('maxlength' => 30)); |
| 510 | $this->add('textarea', 'pcp_personal_note', ts('Personal Note'), array('style' => 'height: 3em; width: 40em;')); |
| 511 | } |
| 512 | } |
| 513 | if (empty($this->_values['fee']) && empty($this->_ccid)) { |
| 514 | CRM_Core_Error::fatal(ts('This page does not have any price fields configured or you may not have permission for them. Please contact the site administrator for more details.')); |
| 515 | } |
| 516 | |
| 517 | //we have to load confirm contribution button in template |
| 518 | //when multiple payment processor as the user |
| 519 | //can toggle with payment processor selection |
| 520 | $billingModePaymentProcessors = 0; |
| 521 | if (!empty($this->_paymentProcessors)) { |
| 522 | foreach ($this->_paymentProcessors as $key => $values) { |
| 523 | if ($values['billing_mode'] == CRM_Core_Payment::BILLING_MODE_BUTTON) { |
| 524 | $billingModePaymentProcessors++; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if ($billingModePaymentProcessors && count($this->_paymentProcessors) == $billingModePaymentProcessors) { |
| 530 | $allAreBillingModeProcessors = TRUE; |
| 531 | } |
| 532 | else { |
| 533 | $allAreBillingModeProcessors = FALSE; |
| 534 | } |
| 535 | |
| 536 | if (!($allAreBillingModeProcessors && !$this->_values['is_pay_later'])) { |
| 537 | $submitButton = array( |
| 538 | 'type' => 'upload', |
| 539 | 'name' => CRM_Utils_Array::value('is_confirm_enabled', $this->_values) ? ts('Confirm Contribution') : ts('Contribute'), |
| 540 | 'spacing' => ' ', |
| 541 | 'isDefault' => TRUE, |
| 542 | ); |
| 543 | // Add submit-once behavior when confirm page disabled |
| 544 | if (empty($this->_values['is_confirm_enabled'])) { |
| 545 | $submitButton['js'] = array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"); |
| 546 | } |
| 547 | //change button name for updating contribution |
| 548 | if (!empty($this->_ccid)) { |
| 549 | $submitButton['name'] = ts('Confirm Payment'); |
| 550 | } |
| 551 | $this->addButtons(array($submitButton)); |
| 552 | } |
| 553 | |
| 554 | $this->addFormRule(array('CRM_Contribute_Form_Contribution_Main', 'formRule'), $this); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Build elements to collect information for recurring contributions. |
| 559 | * |
| 560 | * |
| 561 | * @param CRM_Core_Form $form |
| 562 | */ |
| 563 | public static function buildRecur(&$form) { |
| 564 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionRecur'); |
| 565 | $className = get_class($form); |
| 566 | |
| 567 | $form->assign('is_recur_interval', CRM_Utils_Array::value('is_recur_interval', $form->_values)); |
| 568 | $form->assign('is_recur_installments', CRM_Utils_Array::value('is_recur_installments', $form->_values)); |
| 569 | $paymentObject = $form->getVar('_paymentObject'); |
| 570 | if ($paymentObject) { |
| 571 | $form->assign('recurringHelpText', $paymentObject->getText('contributionPageRecurringHelp', array( |
| 572 | 'is_recur_installments' => !empty($form->_values['is_recur_installments']), |
| 573 | 'is_email_receipt' => !empty($form->_values['is_email_receipt']), |
| 574 | ))); |
| 575 | } |
| 576 | |
| 577 | $form->add('checkbox', 'is_recur', ts('I want to contribute this amount'), NULL); |
| 578 | |
| 579 | if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') { |
| 580 | $form->add('text', 'frequency_interval', ts('Every'), $attributes['frequency_interval']); |
| 581 | $form->addRule('frequency_interval', ts('Frequency must be a whole number (EXAMPLE: Every 3 months).'), 'integer'); |
| 582 | } |
| 583 | else { |
| 584 | // make sure frequency_interval is submitted as 1 if given no choice to user. |
| 585 | $form->add('hidden', 'frequency_interval', 1); |
| 586 | } |
| 587 | |
| 588 | $frUnits = CRM_Utils_Array::value('recur_frequency_unit', $form->_values); |
| 589 | if (empty($frUnits) && |
| 590 | $className == 'CRM_Contribute_Form_Contribution' |
| 591 | ) { |
| 592 | $frUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 593 | CRM_Core_OptionGroup::values('recur_frequency_units') |
| 594 | ); |
| 595 | } |
| 596 | |
| 597 | $unitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $frUnits); |
| 598 | |
| 599 | // CRM 10860, display text instead of a dropdown if there's only 1 frequency unit |
| 600 | if (count($unitVals) == 1) { |
| 601 | $form->assign('one_frequency_unit', TRUE); |
| 602 | $unit = $unitVals[0]; |
| 603 | $form->add('hidden', 'frequency_unit', $unit); |
| 604 | if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') { |
| 605 | $unit .= "(s)"; |
| 606 | } |
| 607 | $form->assign('frequency_unit', $unit); |
| 608 | } |
| 609 | else { |
| 610 | $form->assign('one_frequency_unit', FALSE); |
| 611 | $units = array(); |
| 612 | $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, TRUE); |
| 613 | foreach ($unitVals as $key => $val) { |
| 614 | if (array_key_exists($val, $frequencyUnits)) { |
| 615 | $units[$val] = $frequencyUnits[$val]; |
| 616 | if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') { |
| 617 | $units[$val] = "{$frequencyUnits[$val]}(s)"; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | $frequencyUnit = &$form->add('select', 'frequency_unit', NULL, $units); |
| 622 | } |
| 623 | |
| 624 | // FIXME: Ideally we should freeze select box if there is only |
| 625 | // one option but looks there is some problem /w QF freeze. |
| 626 | //if ( count( $units ) == 1 ) { |
| 627 | //$frequencyUnit->freeze( ); |
| 628 | //} |
| 629 | |
| 630 | $form->add('text', 'installments', ts('installments'), |
| 631 | $attributes['installments'] |
| 632 | ); |
| 633 | $form->addRule('installments', ts('Number of installments must be a whole number.'), 'integer'); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Global form rule. |
| 638 | * |
| 639 | * @param array $fields |
| 640 | * The input form values. |
| 641 | * @param array $files |
| 642 | * The uploaded files if any. |
| 643 | * @param CRM_Core_Form $self |
| 644 | * |
| 645 | * @return bool|array |
| 646 | * true if no errors, else array of errors |
| 647 | */ |
| 648 | public static function formRule($fields, $files, $self) { |
| 649 | $errors = array(); |
| 650 | $amount = self::computeAmount($fields, $self->_values); |
| 651 | if (CRM_Utils_Array::value('auto_renew', $fields) && |
| 652 | CRM_Utils_Array::value('payment_processor_id', $fields) == 0 |
| 653 | ) { |
| 654 | $errors['auto_renew'] = ts('You cannot have auto-renewal on if you are paying later.'); |
| 655 | } |
| 656 | |
| 657 | if ((!empty($fields['selectMembership']) && |
| 658 | $fields['selectMembership'] != 'no_thanks' |
| 659 | ) || |
| 660 | (!empty($fields['priceSetId']) && |
| 661 | $self->_useForMember |
| 662 | ) |
| 663 | ) { |
| 664 | $isTest = ($self->_action & CRM_Core_Action::PREVIEW) ? TRUE : FALSE; |
| 665 | $lifeMember = CRM_Member_BAO_Membership::getAllContactMembership($self->_membershipContactID, $isTest, TRUE); |
| 666 | |
| 667 | $membershipOrgDetails = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization(); |
| 668 | |
| 669 | $unallowedOrgs = array(); |
| 670 | foreach (array_keys($lifeMember) as $memTypeId) { |
| 671 | $unallowedOrgs[] = $membershipOrgDetails[$memTypeId]; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | //check for atleast one pricefields should be selected |
| 676 | if (!empty($fields['priceSetId']) && empty($self->_ccid)) { |
| 677 | $priceField = new CRM_Price_DAO_PriceField(); |
| 678 | $priceField->price_set_id = $fields['priceSetId']; |
| 679 | $priceField->orderBy('weight'); |
| 680 | $priceField->find(); |
| 681 | |
| 682 | $check = array(); |
| 683 | $membershipIsActive = TRUE; |
| 684 | $previousId = $otherAmount = FALSE; |
| 685 | while ($priceField->fetch()) { |
| 686 | |
| 687 | if ($self->_quickConfig && ($priceField->name == 'contribution_amount' || $priceField->name == 'membership_amount')) { |
| 688 | $previousId = $priceField->id; |
| 689 | if ($priceField->name == 'membership_amount' && !$priceField->is_active) { |
| 690 | $membershipIsActive = FALSE; |
| 691 | } |
| 692 | } |
| 693 | if ($priceField->name == 'other_amount') { |
| 694 | if ($self->_quickConfig && empty($fields["price_{$priceField->id}"]) && |
| 695 | array_key_exists("price_{$previousId}", $fields) && isset($fields["price_{$previousId}"]) && $self->_values['fee'][$previousId]['name'] == 'contribution_amount' && empty($fields["price_{$previousId}"]) |
| 696 | ) { |
| 697 | $otherAmount = $priceField->id; |
| 698 | } |
| 699 | elseif (!empty($fields["price_{$priceField->id}"])) { |
| 700 | $otherAmountVal = CRM_Utils_Rule::cleanMoney($fields["price_{$priceField->id}"]); |
| 701 | $min = CRM_Utils_Array::value('min_amount', $self->_values); |
| 702 | $max = CRM_Utils_Array::value('max_amount', $self->_values); |
| 703 | if ($min && $otherAmountVal < $min) { |
| 704 | $errors["price_{$priceField->id}"] = ts('Contribution amount must be at least %1', |
| 705 | array(1 => $min) |
| 706 | ); |
| 707 | } |
| 708 | if ($max && $otherAmountVal > $max) { |
| 709 | $errors["price_{$priceField->id}"] = ts('Contribution amount cannot be more than %1.', |
| 710 | array(1 => $max) |
| 711 | ); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | if (!empty($fields["price_{$priceField->id}"]) || ($previousId == $priceField->id && isset($fields["price_{$previousId}"]) |
| 716 | && empty($fields["price_{$previousId}"])) |
| 717 | ) { |
| 718 | $check[] = $priceField->id; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | $currentMemberships = NULL; |
| 723 | if ($membershipIsActive) { |
| 724 | $is_test = $self->_mode != 'live' ? 1 : 0; |
| 725 | $memContactID = $self->_membershipContactID; |
| 726 | |
| 727 | // For anonymous user check using dedupe rule |
| 728 | // if user has Cancelled Membership |
| 729 | if (!$memContactID) { |
| 730 | $memContactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($fields, 'Individual', 'Unsupervised', array(), FALSE); |
| 731 | } |
| 732 | $currentMemberships = CRM_Member_BAO_Membership::getContactsCancelledMembership($memContactID, |
| 733 | $is_test |
| 734 | ); |
| 735 | |
| 736 | $errorText = 'Your %1 membership was previously cancelled and can not be renewed online. Please contact the site administrator for assistance.'; |
| 737 | foreach ($self->_values['fee'] as $fieldKey => $fieldValue) { |
| 738 | if ($fieldValue['html_type'] != 'Text' && CRM_Utils_Array::value('price_' . $fieldKey, $fields)) { |
| 739 | if (!is_array($fields['price_' . $fieldKey]) && isset($fieldValue['options'][$fields['price_' . $fieldKey]])) { |
| 740 | if (array_key_exists('membership_type_id', $fieldValue['options'][$fields['price_' . $fieldKey]]) |
| 741 | && in_array($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'], $currentMemberships) |
| 742 | ) { |
| 743 | $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant::membershipType($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id']))); |
| 744 | } |
| 745 | } |
| 746 | else { |
| 747 | if (is_array($fields['price_' . $fieldKey])) { |
| 748 | foreach (array_keys($fields['price_' . $fieldKey]) as $key) { |
| 749 | if (array_key_exists('membership_type_id', $fieldValue['options'][$key]) |
| 750 | && in_array($fieldValue['options'][$key]['membership_type_id'], $currentMemberships) |
| 751 | ) { |
| 752 | $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant::membershipType($fieldValue['options'][$key]['membership_type_id']))); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | // CRM-12233 |
| 762 | if ($membershipIsActive && !$self->_membershipBlock['is_required'] |
| 763 | && $self->_values['amount_block_is_active'] |
| 764 | ) { |
| 765 | $membershipFieldId = $contributionFieldId = $errorKey = $otherFieldId = NULL; |
| 766 | foreach ($self->_values['fee'] as $fieldKey => $fieldValue) { |
| 767 | // if 'No thank you' membership is selected then set $membershipFieldId |
| 768 | if ($fieldValue['name'] == 'membership_amount' && CRM_Utils_Array::value('price_' . $fieldKey, $fields) == 0) { |
| 769 | $membershipFieldId = $fieldKey; |
| 770 | } |
| 771 | elseif ($membershipFieldId) { |
| 772 | if ($fieldValue['name'] == 'other_amount') { |
| 773 | $otherFieldId = $fieldKey; |
| 774 | } |
| 775 | elseif ($fieldValue['name'] == 'contribution_amount') { |
| 776 | $contributionFieldId = $fieldKey; |
| 777 | } |
| 778 | |
| 779 | if (!$errorKey || CRM_Utils_Array::value('price_' . $contributionFieldId, $fields) == '0') { |
| 780 | $errorKey = $fieldKey; |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | // $membershipFieldId is set and additional amount is 'No thank you' or NULL then throw error |
| 785 | if ($membershipFieldId && !(CRM_Utils_Array::value('price_' . $contributionFieldId, $fields, -1) > 0) && empty($fields['price_' . $otherFieldId])) { |
| 786 | $errors["price_{$errorKey}"] = ts('Additional Contribution is required.'); |
| 787 | } |
| 788 | } |
| 789 | if (empty($check) && empty($self->_ccid)) { |
| 790 | if ($self->_useForMember == 1 && $membershipIsActive) { |
| 791 | $errors['_qf_default'] = ts('Select at least one option from Membership Type(s).'); |
| 792 | } |
| 793 | else { |
| 794 | $errors['_qf_default'] = ts('Select at least one option from Contribution(s).'); |
| 795 | } |
| 796 | } |
| 797 | if ($otherAmount && !empty($check)) { |
| 798 | $errors["price_{$otherAmount}"] = ts('Amount is required field.'); |
| 799 | } |
| 800 | |
| 801 | if ($self->_useForMember == 1 && !empty($check) && $membershipIsActive) { |
| 802 | $priceFieldIDS = array(); |
| 803 | $priceFieldMemTypes = array(); |
| 804 | |
| 805 | foreach ($self->_priceSet['fields'] as $priceId => $value) { |
| 806 | if (!empty($fields['price_' . $priceId]) || ($self->_quickConfig && $value['name'] == 'membership_amount' && empty($self->_membershipBlock['is_required']))) { |
| 807 | if (!empty($fields['price_' . $priceId]) && is_array($fields['price_' . $priceId])) { |
| 808 | foreach ($fields['price_' . $priceId] as $priceFldVal => $isSet) { |
| 809 | if ($isSet) { |
| 810 | $priceFieldIDS[] = $priceFldVal; |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | elseif (!$value['is_enter_qty'] && !empty($fields['price_' . $priceId])) { |
| 815 | // The check for {!$value['is_enter_qty']} is done since, quantity fields allow entering |
| 816 | // quantity. And the quantity can't be conisdered as civicrm_price_field_value.id, CRM-9577 |
| 817 | $priceFieldIDS[] = $fields['price_' . $priceId]; |
| 818 | } |
| 819 | |
| 820 | if (!empty($value['options'])) { |
| 821 | foreach ($value['options'] as $val) { |
| 822 | if (!empty($val['membership_type_id']) && ( |
| 823 | ($fields['price_' . $priceId] == $val['id']) || |
| 824 | (isset($fields['price_' . $priceId]) && !empty($fields['price_' . $priceId][$val['id']])) |
| 825 | ) |
| 826 | ) { |
| 827 | $priceFieldMemTypes[] = $val['membership_type_id']; |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | if (!empty($lifeMember)) { |
| 835 | foreach ($priceFieldIDS as $priceFieldId) { |
| 836 | if (($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) && |
| 837 | in_array($membershipOrgDetails[$id], $unallowedOrgs) |
| 838 | ) { |
| 839 | $errors['_qf_default'] = ts('You already have a lifetime membership and cannot select a membership with a shorter term.'); |
| 840 | break; |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | if (!empty($priceFieldIDS)) { |
| 846 | $ids = implode(',', $priceFieldIDS); |
| 847 | |
| 848 | $priceFieldIDS['id'] = $fields['priceSetId']; |
| 849 | $self->set('memberPriceFieldIDS', $priceFieldIDS); |
| 850 | $count = CRM_Price_BAO_PriceSet::getMembershipCount($ids); |
| 851 | foreach ($count as $id => $occurrence) { |
| 852 | if ($occurrence > 1) { |
| 853 | $errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity. Contact the site administrator if you need assistance.'); |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | if (empty($priceFieldMemTypes) && $self->_membershipBlock['is_required'] == 1) { |
| 859 | $errors['_qf_default'] = ts('Please select at least one membership option.'); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'], |
| 864 | $fields, $lineItem |
| 865 | ); |
| 866 | |
| 867 | $minAmt = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $fields['priceSetId'], 'min_amount'); |
| 868 | if ($fields['amount'] < 0) { |
| 869 | $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly'); |
| 870 | } |
| 871 | elseif (!empty($minAmt) && $fields['amount'] < $minAmt) { |
| 872 | $errors['_qf_default'] = ts('A minimum amount of %1 should be selected from Contribution(s).', array( |
| 873 | 1 => CRM_Utils_Money::format($minAmt), |
| 874 | )); |
| 875 | } |
| 876 | |
| 877 | $amount = $fields['amount']; |
| 878 | } |
| 879 | |
| 880 | if (isset($fields['selectProduct']) && |
| 881 | $fields['selectProduct'] != 'no_thanks' |
| 882 | ) { |
| 883 | $productDAO = new CRM_Contribute_DAO_Product(); |
| 884 | $productDAO->id = $fields['selectProduct']; |
| 885 | $productDAO->find(TRUE); |
| 886 | $min_amount = $productDAO->min_contribution; |
| 887 | |
| 888 | if ($amount < $min_amount) { |
| 889 | $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($min_amount))); |
| 890 | CRM_Core_Session::setStatus($errors['selectProduct']); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | //CRM-16285 - Function to handle validation errors on form, for recurring contribution field. |
| 895 | CRM_Contribute_BAO_ContributionRecur::validateRecurContribution($fields, $files, $self, $errors); |
| 896 | |
| 897 | if (!empty($fields['is_recur']) && |
| 898 | CRM_Utils_Array::value('payment_processor_id', $fields) == 0 |
| 899 | ) { |
| 900 | $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.'); |
| 901 | } |
| 902 | |
| 903 | // validate PCP fields - if not anonymous, we need a nick name value |
| 904 | if ($self->_pcpId && !empty($fields['pcp_display_in_roll']) && |
| 905 | (CRM_Utils_Array::value('pcp_is_anonymous', $fields) == 0) && |
| 906 | CRM_Utils_Array::value('pcp_roll_nickname', $fields) == '' |
| 907 | ) { |
| 908 | $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.'); |
| 909 | } |
| 910 | |
| 911 | // return if this is express mode |
| 912 | $config = CRM_Core_Config::singleton(); |
| 913 | if ($self->_paymentProcessor && |
| 914 | $self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON |
| 915 | ) { |
| 916 | if (!empty($fields[$self->_expressButtonName . '_x']) || !empty($fields[$self->_expressButtonName . '_y']) || |
| 917 | CRM_Utils_Array::value($self->_expressButtonName, $fields) |
| 918 | ) { |
| 919 | return $errors; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | //validate the pledge fields. |
| 924 | if (!empty($self->_values['pledge_block_id'])) { |
| 925 | //validation for pledge payment. |
| 926 | if (!empty($self->_values['pledge_id'])) { |
| 927 | if (empty($fields['pledge_amount'])) { |
| 928 | $errors['pledge_amount'] = ts('At least one payment option needs to be checked.'); |
| 929 | } |
| 930 | } |
| 931 | elseif (!empty($fields['is_pledge'])) { |
| 932 | if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_installments', $fields)) == FALSE) { |
| 933 | $errors['pledge_installments'] = ts('Please enter a valid number of pledge installments.'); |
| 934 | } |
| 935 | else { |
| 936 | if (CRM_Utils_Array::value('pledge_installments', $fields) == NULL) { |
| 937 | $errors['pledge_installments'] = ts('Pledge Installments is required field.'); |
| 938 | } |
| 939 | elseif (CRM_Utils_Array::value('pledge_installments', $fields) == 1) { |
| 940 | $errors['pledge_installments'] = ts('Pledges consist of multiple scheduled payments. Select one-time contribution if you want to make your gift in a single payment.'); |
| 941 | } |
| 942 | elseif (CRM_Utils_Array::value('pledge_installments', $fields) == 0) { |
| 943 | $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.'); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | //validation for Pledge Frequency Interval. |
| 948 | if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_frequency_interval', $fields)) == FALSE) { |
| 949 | $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.'); |
| 950 | } |
| 951 | else { |
| 952 | if (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == NULL) { |
| 953 | $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.'); |
| 954 | } |
| 955 | elseif (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == 0) { |
| 956 | $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0'); |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | // if the user has chosen a free membership or the amount is less than zero |
| 963 | // i.e. we don't need to validate payment related fields or profiles. |
| 964 | if ((float) $amount <= 0.0) { |
| 965 | return $errors; |
| 966 | } |
| 967 | |
| 968 | if (CRM_Utils_Array::value('payment_processor_id', $fields) == NULL) { |
| 969 | $errors['payment_processor_id'] = ts('Payment Method is a required field.'); |
| 970 | } |
| 971 | else { |
| 972 | CRM_Core_Payment_Form::validatePaymentInstrument( |
| 973 | $fields['payment_processor_id'], |
| 974 | $fields, |
| 975 | $errors, |
| 976 | (!$self->_isBillingAddressRequiredForPayLater ? NULL : 'billing') |
| 977 | ); |
| 978 | } |
| 979 | |
| 980 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { |
| 981 | if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) { |
| 982 | $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); |
| 983 | if ($customizedValue == $greetingType && empty($fielse[$greeting . '_custom'])) { |
| 984 | $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.', |
| 985 | array(1 => ucwords(str_replace('_', " ", $greeting))) |
| 986 | ); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | return empty($errors) ? TRUE : $errors; |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * Compute amount to be paid. |
| 996 | * |
| 997 | * @param array $params |
| 998 | * @param array $formValues |
| 999 | * |
| 1000 | * @return int|mixed|null|string |
| 1001 | */ |
| 1002 | public static function computeAmount($params, $formValues) { |
| 1003 | $amount = 0; |
| 1004 | // First clean up the other amount field if present. |
| 1005 | if (isset($params['amount_other'])) { |
| 1006 | $params['amount_other'] = CRM_Utils_Rule::cleanMoney($params['amount_other']); |
| 1007 | } |
| 1008 | |
| 1009 | if (CRM_Utils_Array::value('amount', $params) == 'amount_other_radio' || !empty($params['amount_other'])) { |
| 1010 | $amount = $params['amount_other']; |
| 1011 | } |
| 1012 | elseif (!empty($params['pledge_amount'])) { |
| 1013 | foreach ($params['pledge_amount'] as $paymentId => $dontCare) { |
| 1014 | $amount += CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $paymentId, 'scheduled_amount'); |
| 1015 | } |
| 1016 | } |
| 1017 | else { |
| 1018 | if (!empty($formValues['amount'])) { |
| 1019 | $amountID = CRM_Utils_Array::value('amount', $params); |
| 1020 | |
| 1021 | if ($amountID) { |
| 1022 | // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel |
| 1023 | // function to get correct amount level consistently. Remove setting of the amount level in |
| 1024 | // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest |
| 1025 | // to cover all variants. |
| 1026 | $params['amount_level'] = CRM_Utils_Array::value('label', $formValues[$amountID]); |
| 1027 | $amount = CRM_Utils_Array::value('value', $formValues[$amountID]); |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | return $amount; |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * Process the form submission. |
| 1036 | */ |
| 1037 | public function postProcess() { |
| 1038 | // we first reset the confirm page so it accepts new values |
| 1039 | $this->controller->resetPage('Confirm'); |
| 1040 | |
| 1041 | // get the submitted form values. |
| 1042 | $params = $this->controller->exportValues($this->_name); |
| 1043 | $this->submit($params); |
| 1044 | |
| 1045 | if (empty($this->_values['is_confirm_enabled'])) { |
| 1046 | $this->skipToThankYouPage(); |
| 1047 | } |
| 1048 | |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Submit function. |
| 1053 | * |
| 1054 | * This is the guts of the postProcess made also accessible to the test suite. |
| 1055 | * |
| 1056 | * @param array $params |
| 1057 | * Submitted values. |
| 1058 | */ |
| 1059 | public function submit($params) { |
| 1060 | //carry campaign from profile. |
| 1061 | if (array_key_exists('contribution_campaign_id', $params)) { |
| 1062 | $params['campaign_id'] = $params['contribution_campaign_id']; |
| 1063 | } |
| 1064 | |
| 1065 | $params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency; |
| 1066 | |
| 1067 | // @todo refactor this & leverage it from the unit tests. |
| 1068 | if (!empty($params['priceSetId'])) { |
| 1069 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); |
| 1070 | if ($is_quick_config) { |
| 1071 | $priceField = new CRM_Price_DAO_PriceField(); |
| 1072 | $priceField->price_set_id = $params['priceSetId']; |
| 1073 | $priceField->orderBy('weight'); |
| 1074 | $priceField->find(); |
| 1075 | |
| 1076 | $priceOptions = array(); |
| 1077 | while ($priceField->fetch()) { |
| 1078 | CRM_Price_BAO_PriceFieldValue::getValues($priceField->id, $priceOptions); |
| 1079 | if (($selectedPriceOptionID = CRM_Utils_Array::value("price_{$priceField->id}", $params)) != FALSE && $selectedPriceOptionID > 0) { |
| 1080 | switch ($priceField->name) { |
| 1081 | case 'membership_amount': |
| 1082 | $this->_params['selectMembership'] = $params['selectMembership'] = CRM_Utils_Array::value('membership_type_id', $priceOptions[$selectedPriceOptionID]); |
| 1083 | $this->set('selectMembership', $params['selectMembership']); |
| 1084 | |
| 1085 | case 'contribution_amount': |
| 1086 | $params['amount'] = $selectedPriceOptionID; |
| 1087 | if ($priceField->name == 'contribution_amount' || |
| 1088 | ($priceField->name == 'membership_amount' && |
| 1089 | CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock) == 0) |
| 1090 | ) { |
| 1091 | $this->_values['amount'] = CRM_Utils_Array::value('amount', $priceOptions[$selectedPriceOptionID]); |
| 1092 | } |
| 1093 | $this->_values[$selectedPriceOptionID]['value'] = CRM_Utils_Array::value('amount', $priceOptions[$selectedPriceOptionID]); |
| 1094 | $this->_values[$selectedPriceOptionID]['label'] = CRM_Utils_Array::value('label', $priceOptions[$selectedPriceOptionID]); |
| 1095 | $this->_values[$selectedPriceOptionID]['amount_id'] = CRM_Utils_Array::value('id', $priceOptions[$selectedPriceOptionID]); |
| 1096 | $this->_values[$selectedPriceOptionID]['weight'] = CRM_Utils_Array::value('weight', $priceOptions[$selectedPriceOptionID]); |
| 1097 | break; |
| 1098 | |
| 1099 | case 'other_amount': |
| 1100 | $params['amount_other'] = $selectedPriceOptionID; |
| 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | if (!empty($this->_ccid) && !empty($this->_pendingAmount)) { |
| 1109 | $params['amount'] = $this->_pendingAmount; |
| 1110 | } |
| 1111 | else { |
| 1112 | // from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID |
| 1113 | $params['amount'] = self::computeAmount($params, $this->_values); |
| 1114 | } |
| 1115 | |
| 1116 | $params['separate_amount'] = $params['amount']; |
| 1117 | $memFee = NULL; |
| 1118 | if (!empty($params['selectMembership'])) { |
| 1119 | if (empty($this->_membershipTypeValues)) { |
| 1120 | $this->_membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, |
| 1121 | (array) $params['selectMembership'] |
| 1122 | ); |
| 1123 | } |
| 1124 | $membershipTypeValues = $this->_membershipTypeValues[$params['selectMembership']]; |
| 1125 | $memFee = $membershipTypeValues['minimum_fee']; |
| 1126 | if (!$params['amount'] && !$this->_separateMembershipPayment) { |
| 1127 | $params['amount'] = $memFee ? $memFee : 0; |
| 1128 | } |
| 1129 | } |
| 1130 | //If the membership & contribution is used in contribution page & not separate payment |
| 1131 | $memPresent = $membershipLabel = $fieldOption = $is_quick_config = NULL; |
| 1132 | $proceFieldAmount = 0; |
| 1133 | if (property_exists($this, '_separateMembershipPayment') && $this->_separateMembershipPayment == 0) { |
| 1134 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); |
| 1135 | if ($is_quick_config) { |
| 1136 | foreach ($this->_priceSet['fields'] as $fieldKey => $fieldVal) { |
| 1137 | if ($fieldVal['name'] == 'membership_amount' && !empty($params['price_' . $fieldKey])) { |
| 1138 | $fieldId = $fieldVal['id']; |
| 1139 | $fieldOption = $params['price_' . $fieldId]; |
| 1140 | $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount']; |
| 1141 | $memPresent = TRUE; |
| 1142 | } |
| 1143 | else { |
| 1144 | if (!empty($params['price_' . $fieldKey]) && $memPresent && ($fieldVal['name'] == 'other_amount' || $fieldVal['name'] == 'contribution_amount')) { |
| 1145 | $fieldId = $fieldVal['id']; |
| 1146 | if ($fieldVal['name'] == 'other_amount') { |
| 1147 | $proceFieldAmount += $this->_submitValues['price_' . $fieldId]; |
| 1148 | } |
| 1149 | elseif ($fieldVal['name'] == 'contribution_amount' && $this->_submitValues['price_' . $fieldId] > 0) { |
| 1150 | $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount']; |
| 1151 | } |
| 1152 | unset($params['price_' . $fieldId]); |
| 1153 | break; |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | if (!isset($params['amount_other'])) { |
| 1161 | $this->set('amount_level', CRM_Utils_Array::value('amount_level', $params)); |
| 1162 | } |
| 1163 | |
| 1164 | if (!empty($this->_ccid)) { |
| 1165 | $this->set('lineItem', $this->_lineItem); |
| 1166 | } |
| 1167 | elseif ($priceSetId = CRM_Utils_Array::value('priceSetId', $params)) { |
| 1168 | $lineItem = array(); |
| 1169 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config'); |
| 1170 | if ($is_quick_config) { |
| 1171 | foreach ($this->_values['fee'] as $key => & $val) { |
| 1172 | if ($val['name'] == 'other_amount' && $val['html_type'] == 'Text' && !empty($params['price_' . $key])) { |
| 1173 | // Clean out any currency symbols. |
| 1174 | $params['price_' . $key] = CRM_Utils_Rule::cleanMoney($params['price_' . $key]); |
| 1175 | if ($params['price_' . $key] != 0) { |
| 1176 | foreach ($val['options'] as $optionKey => & $options) { |
| 1177 | $options['amount'] = CRM_Utils_Array::value('price_' . $key, $params); |
| 1178 | break; |
| 1179 | } |
| 1180 | } |
| 1181 | $params['price_' . $key] = 1; |
| 1182 | break; |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | $component = ''; |
| 1187 | if ($this->_membershipBlock) { |
| 1188 | $component = 'membership'; |
| 1189 | } |
| 1190 | |
| 1191 | CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId], $component); |
| 1192 | if ($params['tax_amount']) { |
| 1193 | $this->set('tax_amount', $params['tax_amount']); |
| 1194 | } |
| 1195 | |
| 1196 | if ($proceFieldAmount) { |
| 1197 | $lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount; |
| 1198 | $lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount; |
| 1199 | if (isset($lineItem[$params['priceSetId']][$fieldOption]['tax_amount'])) { |
| 1200 | $proceFieldAmount += $lineItem[$params['priceSetId']][$fieldOption]['tax_amount']; |
| 1201 | } |
| 1202 | if (!$this->_membershipBlock['is_separate_payment']) { |
| 1203 | //require when separate membership not used |
| 1204 | $params['amount'] = $proceFieldAmount; |
| 1205 | } |
| 1206 | } |
| 1207 | $this->set('lineItem', $lineItem); |
| 1208 | } |
| 1209 | |
| 1210 | if ($params['amount'] != 0 && (($this->_values['is_pay_later'] && |
| 1211 | empty($this->_paymentProcessor) && |
| 1212 | !array_key_exists('hidden_processor', $params)) || |
| 1213 | (CRM_Utils_Array::value('payment_processor_id', $params) == 0)) |
| 1214 | ) { |
| 1215 | $params['is_pay_later'] = 1; |
| 1216 | } |
| 1217 | else { |
| 1218 | $params['is_pay_later'] = 0; |
| 1219 | } |
| 1220 | |
| 1221 | // Would be nice to someday understand the point of this set. |
| 1222 | $this->set('is_pay_later', $params['is_pay_later']); |
| 1223 | // assign pay later stuff |
| 1224 | $this->_params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE); |
| 1225 | $this->assign('is_pay_later', $params['is_pay_later']); |
| 1226 | if ($params['is_pay_later']) { |
| 1227 | $this->assign('pay_later_text', $this->_values['pay_later_text']); |
| 1228 | $this->assign('pay_later_receipt', CRM_Utils_Array::value('pay_later_receipt', $this->_values)); |
| 1229 | } |
| 1230 | |
| 1231 | if ($this->_membershipBlock['is_separate_payment'] && !empty($params['separate_amount'])) { |
| 1232 | $this->set('amount', $params['separate_amount']); |
| 1233 | } |
| 1234 | else { |
| 1235 | $this->set('amount', $params['amount']); |
| 1236 | } |
| 1237 | |
| 1238 | // generate and set an invoiceID for this transaction |
| 1239 | $invoiceID = md5(uniqid(rand(), TRUE)); |
| 1240 | $this->set('invoiceID', $invoiceID); |
| 1241 | $params['invoiceID'] = $invoiceID; |
| 1242 | $params['description'] = ts('Online Contribution') . ': ' . ((!empty($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title'])); |
| 1243 | $params['button'] = $this->controller->getButtonName(); |
| 1244 | // required only if is_monetary and valid positive amount |
| 1245 | // @todo it seems impossible for $memFee to be greater than 0 & $params['amount'] not to |
| 1246 | // be & by requiring $memFee down here we make it harder to do a sensible refactoring of the function |
| 1247 | // above (ie. extract the amount in a small function). |
| 1248 | if ($this->_values['is_monetary'] && |
| 1249 | !empty($this->_paymentProcessor) && |
| 1250 | ((float ) $params['amount'] > 0.0 || $memFee > 0.0) |
| 1251 | ) { |
| 1252 | // The concept of contributeMode is deprecated - as should be the 'is_monetary' setting. |
| 1253 | $this->setContributeMode(); |
| 1254 | // Really this setting of $this->_params & params within it should be done earlier on in the function |
| 1255 | // probably the values determined here should be reused in confirm postProcess as there is no opportunity to alter anything |
| 1256 | // on the confirm page. However as we are dealing with a stable release we go as close to where it is used |
| 1257 | // as possible. |
| 1258 | // In general the form has a lack of clarity of the logic of why things are set on the form in some cases & |
| 1259 | // the logic around when $this->_params is used compared to other params arrays. |
| 1260 | $this->_params = array_merge($params, $this->_params); |
| 1261 | $this->setRecurringMembershipParams(); |
| 1262 | if ($this->_paymentProcessor && |
| 1263 | $this->_paymentProcessor['object']->supports('preApproval') |
| 1264 | ) { |
| 1265 | $this->handlePreApproval($this->_params); |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | /** |
| 1271 | * Assign the billing mode to the template. |
| 1272 | * |
| 1273 | * This is required for legacy support for contributeMode in templates. |
| 1274 | * |
| 1275 | * The goal is to remove this parameter & use more relevant parameters. |
| 1276 | */ |
| 1277 | protected function setContributeMode() { |
| 1278 | switch ($this->_paymentProcessor['billing_mode']) { |
| 1279 | case CRM_Core_Payment::BILLING_MODE_FORM: |
| 1280 | $this->set('contributeMode', 'direct'); |
| 1281 | break; |
| 1282 | |
| 1283 | case CRM_Core_Payment::BILLING_MODE_BUTTON: |
| 1284 | $this->set('contributeMode', 'express'); |
| 1285 | break; |
| 1286 | |
| 1287 | case CRM_Core_Payment::BILLING_MODE_NOTIFY: |
| 1288 | $this->set('contributeMode', 'notify'); |
| 1289 | break; |
| 1290 | } |
| 1291 | |
| 1292 | } |
| 1293 | |
| 1294 | /** |
| 1295 | * Process confirm function and pass browser to the thank you page. |
| 1296 | */ |
| 1297 | protected function skipToThankYouPage() { |
| 1298 | // call the post process hook for the main page before we switch to confirm |
| 1299 | $this->postProcessHook(); |
| 1300 | |
| 1301 | // build the confirm page |
| 1302 | $confirmForm = &$this->controller->_pages['Confirm']; |
| 1303 | $confirmForm->preProcess(); |
| 1304 | $confirmForm->buildQuickForm(); |
| 1305 | |
| 1306 | // the confirmation page is valid |
| 1307 | $data = &$this->controller->container(); |
| 1308 | $data['valid']['Confirm'] = 1; |
| 1309 | |
| 1310 | // confirm the contribution |
| 1311 | // mainProcess calls the hook also |
| 1312 | $confirmForm->mainProcess(); |
| 1313 | $qfKey = $this->controller->_key; |
| 1314 | |
| 1315 | // redirect to thank you page |
| 1316 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey=$qfKey", TRUE, NULL, FALSE)); |
| 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * Function for unit tests on the postProcess function. |
| 1321 | * |
| 1322 | * @param array $params |
| 1323 | */ |
| 1324 | public function testSubmit($params) { |
| 1325 | $_SERVER['REQUEST_METHOD'] = 'GET'; |
| 1326 | $this->controller = new CRM_Contribute_Controller_Contribution(); |
| 1327 | $this->submit($params); |
| 1328 | } |
| 1329 | |
| 1330 | } |