| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | */ |
| 33 | |
| 34 | use Civi\Payment\System; |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for processing a contribution. |
| 38 | */ |
| 39 | class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { |
| 40 | |
| 41 | /** |
| 42 | * The id of the contribution page that we are processing. |
| 43 | * |
| 44 | * @var int |
| 45 | */ |
| 46 | public $_id; |
| 47 | |
| 48 | /** |
| 49 | * The mode that we are in |
| 50 | * |
| 51 | * @var string |
| 52 | * @protect |
| 53 | */ |
| 54 | public $_mode; |
| 55 | |
| 56 | /** |
| 57 | * The contact id related to a membership |
| 58 | * |
| 59 | * @var int |
| 60 | */ |
| 61 | public $_membershipContactID; |
| 62 | |
| 63 | /** |
| 64 | * The values for the contribution db object |
| 65 | * |
| 66 | * @var array |
| 67 | */ |
| 68 | public $_values; |
| 69 | |
| 70 | /** |
| 71 | * The paymentProcessor attributes for this page |
| 72 | * |
| 73 | * @var array |
| 74 | */ |
| 75 | public $_paymentProcessor; |
| 76 | |
| 77 | public $_paymentObject = NULL; |
| 78 | |
| 79 | /** |
| 80 | * The membership block for this page |
| 81 | * |
| 82 | * @var array |
| 83 | */ |
| 84 | public $_membershipBlock = NULL; |
| 85 | |
| 86 | /** |
| 87 | * Does this form support a separate membership payment |
| 88 | * @var bool |
| 89 | */ |
| 90 | protected $_separateMembershipPayment; |
| 91 | |
| 92 | /** |
| 93 | * The params submitted by the form and computed by the app |
| 94 | * |
| 95 | * @var array |
| 96 | */ |
| 97 | public $_params = array(); |
| 98 | |
| 99 | /** |
| 100 | * The fields involved in this contribution page |
| 101 | * |
| 102 | * @var array |
| 103 | */ |
| 104 | public $_fields = array(); |
| 105 | |
| 106 | /** |
| 107 | * The billing location id for this contribution page. |
| 108 | * |
| 109 | * @var int |
| 110 | */ |
| 111 | public $_bltID; |
| 112 | |
| 113 | /** |
| 114 | * Cache the amount to make things easier |
| 115 | * |
| 116 | * @var float |
| 117 | */ |
| 118 | public $_amount; |
| 119 | |
| 120 | /** |
| 121 | * Pcp id |
| 122 | * |
| 123 | * @var integer |
| 124 | */ |
| 125 | public $_pcpId; |
| 126 | |
| 127 | /** |
| 128 | * Pcp block |
| 129 | * |
| 130 | * @var array |
| 131 | */ |
| 132 | public $_pcpBlock; |
| 133 | |
| 134 | /** |
| 135 | * Pcp info |
| 136 | * |
| 137 | * @var array |
| 138 | */ |
| 139 | public $_pcpInfo; |
| 140 | |
| 141 | /** |
| 142 | * The contact id of the person for whom membership is being added or renewed based on the cid in the url, |
| 143 | * checksum, or session |
| 144 | * @var int |
| 145 | */ |
| 146 | public $_contactID; |
| 147 | |
| 148 | protected $_userID; |
| 149 | |
| 150 | /** |
| 151 | * The Membership ID for membership renewal |
| 152 | * |
| 153 | * @var int |
| 154 | */ |
| 155 | public $_membershipId; |
| 156 | |
| 157 | /** |
| 158 | * Price Set ID, if the new price set method is used |
| 159 | * |
| 160 | * @var int |
| 161 | */ |
| 162 | public $_priceSetId; |
| 163 | |
| 164 | /** |
| 165 | * Array of fields for the price set |
| 166 | * |
| 167 | * @var array |
| 168 | */ |
| 169 | public $_priceSet; |
| 170 | |
| 171 | public $_action; |
| 172 | |
| 173 | /** |
| 174 | * Is honor block is enabled for this contribution? |
| 175 | * |
| 176 | * @var boolean |
| 177 | */ |
| 178 | public $_honor_block_is_active = FALSE; |
| 179 | |
| 180 | /** |
| 181 | * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM |
| 182 | * @var string |
| 183 | */ |
| 184 | public $_contributeMode; |
| 185 | |
| 186 | /** |
| 187 | * Contribution page supports memberships |
| 188 | * @var boolean |
| 189 | */ |
| 190 | public $_useForMember; |
| 191 | |
| 192 | public $_isBillingAddressRequiredForPayLater; |
| 193 | |
| 194 | /** |
| 195 | * Set variables up before form is built. |
| 196 | * |
| 197 | * @throws \CRM_Contribute_Exception_InactiveContributionPageException |
| 198 | * @throws \Exception |
| 199 | */ |
| 200 | public function preProcess() { |
| 201 | |
| 202 | // current contribution page id |
| 203 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 204 | if (!$this->_id) { |
| 205 | // seems like the session is corrupted and/or we lost the id trail |
| 206 | // lets just bump this to a regular session error and redirect user to main page |
| 207 | $this->controller->invalidKeyRedirect(); |
| 208 | } |
| 209 | |
| 210 | // this was used prior to the cleverer this_>getContactID - unsure now |
| 211 | $this->_userID = CRM_Core_Session::singleton()->get('userID'); |
| 212 | |
| 213 | $this->_contactID = $this->_membershipContactID = $this->getContactID(); |
| 214 | $this->_mid = NULL; |
| 215 | if ($this->_contactID) { |
| 216 | $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this); |
| 217 | if ($this->_mid) { |
| 218 | $membership = new CRM_Member_DAO_Membership(); |
| 219 | $membership->id = $this->_mid; |
| 220 | |
| 221 | if ($membership->find(TRUE)) { |
| 222 | $this->_defaultMemTypeId = $membership->membership_type_id; |
| 223 | if ($membership->contact_id != $this->_contactID) { |
| 224 | $validMembership = FALSE; |
| 225 | $employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($this->_userID); |
| 226 | if (!empty($employers) && array_key_exists($membership->contact_id, $employers)) { |
| 227 | $this->_membershipContactID = $membership->contact_id; |
| 228 | $this->assign('membershipContactID', $this->_membershipContactID); |
| 229 | $this->assign('membershipContactName', $employers[$this->_membershipContactID]['name']); |
| 230 | $validMembership = TRUE; |
| 231 | } |
| 232 | else { |
| 233 | $membershipType = new CRM_Member_BAO_MembershipType(); |
| 234 | $membershipType->id = $membership->membership_type_id; |
| 235 | if ($membershipType->find(TRUE)) { |
| 236 | // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values. |
| 237 | // Convert to comma separated list. |
| 238 | $inheritedRelTypes = implode(CRM_Utils_Array::explodePadded($membershipType->relationship_type_id), ','); |
| 239 | $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, $membershipType->relationship_type_id); |
| 240 | if (array_key_exists($membership->contact_id, $permContacts)) { |
| 241 | $this->_membershipContactID = $membership->contact_id; |
| 242 | $validMembership = TRUE; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | if (!$validMembership) { |
| 247 | CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert'); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | else { |
| 252 | CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert'); |
| 253 | } |
| 254 | unset($membership); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // we do not want to display recently viewed items, so turn off |
| 259 | $this->assign('displayRecent', FALSE); |
| 260 | // Contribution page values are cleared from session, so can't use normal Printer Friendly view. |
| 261 | // Use Browser Print instead. |
| 262 | $this->assign('browserPrint', TRUE); |
| 263 | |
| 264 | // action |
| 265 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); |
| 266 | $this->assign('action', $this->_action); |
| 267 | |
| 268 | // current mode |
| 269 | $this->_mode = ($this->_action == 1024) ? 'test' : 'live'; |
| 270 | |
| 271 | $this->_values = $this->get('values'); |
| 272 | $this->_fields = $this->get('fields'); |
| 273 | $this->_bltID = $this->get('bltID'); |
| 274 | $this->_paymentProcessor = $this->get('paymentProcessor'); |
| 275 | $this->_priceSetId = $this->get('priceSetId'); |
| 276 | $this->_priceSet = $this->get('priceSet'); |
| 277 | |
| 278 | if (!$this->_values) { |
| 279 | // get all the values from the dao object |
| 280 | $this->_values = array(); |
| 281 | $this->_fields = array(); |
| 282 | |
| 283 | CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values); |
| 284 | |
| 285 | if (empty($this->_values['is_active'])) { |
| 286 | throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id); |
| 287 | } |
| 288 | |
| 289 | $this->assignBillingType(); |
| 290 | |
| 291 | // check for is_monetary status |
| 292 | $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values); |
| 293 | $isPayLater = CRM_Utils_Array::value('is_pay_later', $this->_values); |
| 294 | |
| 295 | if ($isMonetary && |
| 296 | (!$isPayLater || !empty($this->_values['payment_processor'])) |
| 297 | ) { |
| 298 | $this->_paymentProcessorIDs = explode( |
| 299 | CRM_Core_DAO::VALUE_SEPARATOR, |
| 300 | CRM_Utils_Array::value('payment_processor', $this->_values) |
| 301 | ); |
| 302 | |
| 303 | $this->assignPaymentProcessor(); |
| 304 | } |
| 305 | |
| 306 | // get price info |
| 307 | // CRM-5095 |
| 308 | CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page'); |
| 309 | |
| 310 | // this avoids getting E_NOTICE errors in php |
| 311 | $setNullFields = array( |
| 312 | 'amount_block_is_active', |
| 313 | 'is_allow_other_amount', |
| 314 | 'footer_text', |
| 315 | ); |
| 316 | foreach ($setNullFields as $f) { |
| 317 | if (!isset($this->_values[$f])) { |
| 318 | $this->_values[$f] = NULL; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | //check if Membership Block is enabled, if Membership Fields are included in profile |
| 323 | //get membership section for this contribution page |
| 324 | $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id); |
| 325 | $this->set('membershipBlock', $this->_membershipBlock); |
| 326 | |
| 327 | if ($this->_values['custom_pre_id']) { |
| 328 | $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']); |
| 329 | } |
| 330 | |
| 331 | if ($this->_values['custom_post_id']) { |
| 332 | $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']); |
| 333 | } |
| 334 | |
| 335 | if (((isset($postProfileType) && $postProfileType == 'Membership') || |
| 336 | (isset($preProfileType) && $preProfileType == 'Membership') |
| 337 | ) && |
| 338 | !$this->_membershipBlock['is_active'] |
| 339 | ) { |
| 340 | CRM_Core_Error::fatal(ts('This page includes a Profile with Membership fields - but the Membership Block is NOT enabled. Please notify the site administrator.')); |
| 341 | } |
| 342 | |
| 343 | $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id); |
| 344 | |
| 345 | if ($pledgeBlock) { |
| 346 | $this->_values['pledge_block_id'] = CRM_Utils_Array::value('id', $pledgeBlock); |
| 347 | $this->_values['max_reminders'] = CRM_Utils_Array::value('max_reminders', $pledgeBlock); |
| 348 | $this->_values['initial_reminder_day'] = CRM_Utils_Array::value('initial_reminder_day', $pledgeBlock); |
| 349 | $this->_values['additional_reminder_day'] = CRM_Utils_Array::value('additional_reminder_day', $pledgeBlock); |
| 350 | |
| 351 | //set pledge id in values |
| 352 | $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this); |
| 353 | |
| 354 | //authenticate pledge user for pledge payment. |
| 355 | if ($pledgeId) { |
| 356 | $this->_values['pledge_id'] = $pledgeId; |
| 357 | |
| 358 | //lets override w/ pledge campaign. |
| 359 | $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', |
| 360 | $pledgeId, |
| 361 | 'campaign_id' |
| 362 | ); |
| 363 | self::authenticatePledgeUser(); |
| 364 | } |
| 365 | } |
| 366 | $this->set('values', $this->_values); |
| 367 | $this->set('fields', $this->_fields); |
| 368 | } |
| 369 | |
| 370 | // Handle PCP |
| 371 | $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this); |
| 372 | if ($pcpId) { |
| 373 | $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values); |
| 374 | $this->_pcpId = $pcp['pcpId']; |
| 375 | $this->_pcpBlock = $pcp['pcpBlock']; |
| 376 | $this->_pcpInfo = $pcp['pcpInfo']; |
| 377 | } |
| 378 | |
| 379 | // Link (button) for users to create their own Personal Campaign page |
| 380 | if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) { |
| 381 | $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign', |
| 382 | "action=add&reset=1&pageId={$this->_id}&component=contribute", |
| 383 | FALSE, NULL, TRUE |
| 384 | ); |
| 385 | $this->assign('linkTextUrl', $linkTextUrl); |
| 386 | $this->assign('linkText', $linkText); |
| 387 | } |
| 388 | |
| 389 | //set pledge block if block id is set |
| 390 | if (!empty($this->_values['pledge_block_id'])) { |
| 391 | $this->assign('pledgeBlock', TRUE); |
| 392 | } |
| 393 | |
| 394 | // check if one of the (amount , membership) blocks is active or not. |
| 395 | $this->_membershipBlock = $this->get('membershipBlock'); |
| 396 | |
| 397 | if (!$this->_values['amount_block_is_active'] && |
| 398 | !$this->_membershipBlock['is_active'] && |
| 399 | !$this->_priceSetId |
| 400 | ) { |
| 401 | CRM_Core_Error::fatal(ts('The requested online contribution page is missing a required Contribution Amount section or Membership section or Price Set. Please check with the site administrator for assistance.')); |
| 402 | } |
| 403 | |
| 404 | if ($this->_values['amount_block_is_active']) { |
| 405 | $this->set('amount_block_is_active', $this->_values['amount_block_is_active']); |
| 406 | } |
| 407 | |
| 408 | $this->_contributeMode = $this->get('contributeMode'); |
| 409 | $this->assign('contributeMode', $this->_contributeMode); |
| 410 | |
| 411 | //assigning is_monetary and is_email_receipt to template |
| 412 | $this->assign('is_monetary', $this->_values['is_monetary']); |
| 413 | $this->assign('is_email_receipt', $this->_values['is_email_receipt']); |
| 414 | $this->assign('bltID', $this->_bltID); |
| 415 | |
| 416 | //assign cancelSubscription URL to templates |
| 417 | $this->assign('cancelSubscriptionUrl', |
| 418 | CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values) |
| 419 | ); |
| 420 | |
| 421 | // assigning title to template in case someone wants to use it, also setting CMS page title |
| 422 | if ($this->_pcpId) { |
| 423 | $this->assign('title', $this->_pcpInfo['title']); |
| 424 | CRM_Utils_System::setTitle($this->_pcpInfo['title']); |
| 425 | } |
| 426 | else { |
| 427 | $this->assign('title', $this->_values['title']); |
| 428 | CRM_Utils_System::setTitle($this->_values['title']); |
| 429 | } |
| 430 | $this->_defaults = array(); |
| 431 | |
| 432 | $this->_amount = $this->get('amount'); |
| 433 | |
| 434 | //CRM-6907 |
| 435 | $config = CRM_Core_Config::singleton(); |
| 436 | $config->defaultCurrency = CRM_Utils_Array::value('currency', |
| 437 | $this->_values, |
| 438 | $config->defaultCurrency |
| 439 | ); |
| 440 | |
| 441 | //lets allow user to override campaign. |
| 442 | $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this); |
| 443 | if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) { |
| 444 | $this->_values['campaign_id'] = $campID; |
| 445 | } |
| 446 | |
| 447 | //do check for cancel recurring and clean db, CRM-7696 |
| 448 | if (CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject)) { |
| 449 | self::cancelRecurring(); |
| 450 | } |
| 451 | |
| 452 | // check if billing block is required for pay later |
| 453 | if (CRM_Utils_Array::value('is_pay_later', $this->_values)) { |
| 454 | $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values); |
| 455 | $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Set the default values. |
| 461 | */ |
| 462 | public function setDefaultValues() { |
| 463 | return $this->_defaults; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Assign the minimal set of variables to the template. |
| 468 | */ |
| 469 | public function assignToTemplate() { |
| 470 | $name = CRM_Utils_Array::value('billing_first_name', $this->_params); |
| 471 | if (!empty($this->_params['billing_middle_name'])) { |
| 472 | $name .= " {$this->_params['billing_middle_name']}"; |
| 473 | } |
| 474 | $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params); |
| 475 | $name = trim($name); |
| 476 | $this->assign('billingName', $name); |
| 477 | $this->set('name', $name); |
| 478 | |
| 479 | $this->assign('paymentProcessor', $this->_paymentProcessor); |
| 480 | $vars = array( |
| 481 | 'amount', |
| 482 | 'currencyID', |
| 483 | 'credit_card_type', |
| 484 | 'trxn_id', |
| 485 | 'amount_level', |
| 486 | ); |
| 487 | |
| 488 | $config = CRM_Core_Config::singleton(); |
| 489 | if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) { |
| 490 | $this->assign('is_recur_enabled', 1); |
| 491 | $vars = array_merge($vars, array( |
| 492 | 'is_recur', |
| 493 | 'frequency_interval', |
| 494 | 'frequency_unit', |
| 495 | 'installments', |
| 496 | )); |
| 497 | } |
| 498 | |
| 499 | if (in_array('CiviPledge', $config->enableComponents) && |
| 500 | CRM_Utils_Array::value('is_pledge', $this->_params) == 1 |
| 501 | ) { |
| 502 | $this->assign('pledge_enabled', 1); |
| 503 | |
| 504 | $vars = array_merge($vars, array( |
| 505 | 'is_pledge', |
| 506 | 'pledge_frequency_interval', |
| 507 | 'pledge_frequency_unit', |
| 508 | 'pledge_installments', |
| 509 | )); |
| 510 | } |
| 511 | |
| 512 | if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) { |
| 513 | $this->_params['amount_level'] = ''; |
| 514 | } |
| 515 | |
| 516 | foreach ($vars as $v) { |
| 517 | if (isset($this->_params[$v])) { |
| 518 | if ($v == "amount" && $this->_params[$v] === 0) { |
| 519 | $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE); |
| 520 | } |
| 521 | $this->assign($v, $this->_params[$v]); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // assign the address formatted up for display |
| 526 | $addressParts = array( |
| 527 | "street_address-{$this->_bltID}", |
| 528 | "city-{$this->_bltID}", |
| 529 | "postal_code-{$this->_bltID}", |
| 530 | "state_province-{$this->_bltID}", |
| 531 | "country-{$this->_bltID}", |
| 532 | ); |
| 533 | |
| 534 | $addressFields = array(); |
| 535 | foreach ($addressParts as $part) { |
| 536 | list($n, $id) = explode('-', $part); |
| 537 | $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $this->_params); |
| 538 | } |
| 539 | |
| 540 | $this->assign('address', CRM_Utils_Address::format($addressFields)); |
| 541 | |
| 542 | if (!empty($this->_params['onbehalf_profile_id'])) { |
| 543 | $this->assign('onBehalfName', $this->_params['organization_name']); |
| 544 | $locTypeId = array_keys($this->_params['onbehalf_location']['email']); |
| 545 | $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']); |
| 546 | } |
| 547 | |
| 548 | //fix for CRM-3767 |
| 549 | $assignCCInfo = FALSE; |
| 550 | if ($this->_amount > 0.0) { |
| 551 | $assignCCInfo = TRUE; |
| 552 | } |
| 553 | elseif (!empty($this->_params['selectMembership'])) { |
| 554 | $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee'); |
| 555 | if ($memFee > 0.0) { |
| 556 | $assignCCInfo = TRUE; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | if ($this->_contributeMode == 'direct' && $assignCCInfo) { |
| 561 | if ($this->_paymentProcessor && |
| 562 | $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT |
| 563 | ) { |
| 564 | $this->assign('account_holder', $this->_params['account_holder']); |
| 565 | $this->assign('bank_identification_number', $this->_params['bank_identification_number']); |
| 566 | $this->assign('bank_name', $this->_params['bank_name']); |
| 567 | $this->assign('bank_account_number', $this->_params['bank_account_number']); |
| 568 | } |
| 569 | else { |
| 570 | $date = CRM_Utils_Date::format(CRM_Utils_array::value('credit_card_exp_date', $this->_params)); |
| 571 | $date = CRM_Utils_Date::mysqlToIso($date); |
| 572 | $this->assign('credit_card_exp_date', $date); |
| 573 | $this->assign('credit_card_number', |
| 574 | CRM_Utils_System::mungeCreditCard(CRM_Utils_array::value('credit_card_number', $this->_params)) |
| 575 | ); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | $this->assign('email', |
| 580 | $this->controller->exportValue('Main', "email-{$this->_bltID}") |
| 581 | ); |
| 582 | |
| 583 | // also assign the receipt_text |
| 584 | if (isset($this->_values['receipt_text'])) { |
| 585 | $this->assign('receipt_text', $this->_values['receipt_text']); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Add the custom fields. |
| 591 | * |
| 592 | * @param int $id |
| 593 | * @param string $name |
| 594 | * @param bool $viewOnly |
| 595 | * @param null $profileContactType |
| 596 | * @param array $fieldTypes |
| 597 | */ |
| 598 | public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) { |
| 599 | if ($id) { |
| 600 | $contactID = $this->getContactID(); |
| 601 | |
| 602 | // we don't allow conflicting fields to be |
| 603 | // configured via profile - CRM 2100 |
| 604 | $fieldsToIgnore = array( |
| 605 | 'receive_date' => 1, |
| 606 | 'trxn_id' => 1, |
| 607 | 'invoice_id' => 1, |
| 608 | 'net_amount' => 1, |
| 609 | 'fee_amount' => 1, |
| 610 | 'non_deductible_amount' => 1, |
| 611 | 'total_amount' => 1, |
| 612 | 'amount_level' => 1, |
| 613 | 'contribution_status_id' => 1, |
| 614 | 'payment_instrument' => 1, |
| 615 | 'check_number' => 1, |
| 616 | 'financial_type' => 1, |
| 617 | ); |
| 618 | |
| 619 | $fields = NULL; |
| 620 | if ($contactID && CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) { |
| 621 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE, |
| 622 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL |
| 623 | ); |
| 624 | } |
| 625 | else { |
| 626 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE, |
| 627 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL |
| 628 | ); |
| 629 | } |
| 630 | |
| 631 | if ($fields) { |
| 632 | // unset any email-* fields since we already collect it, CRM-2888 |
| 633 | foreach (array_keys($fields) as $fieldName) { |
| 634 | if (substr($fieldName, 0, 6) == 'email-' && $profileContactType != 'honor') { |
| 635 | unset($fields[$fieldName]); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if (array_intersect_key($fields, $fieldsToIgnore)) { |
| 640 | $fields = array_diff_key($fields, $fieldsToIgnore); |
| 641 | CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert'); |
| 642 | } |
| 643 | |
| 644 | $fields = array_diff_assoc($fields, $this->_fields); |
| 645 | |
| 646 | CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID); |
| 647 | $addCaptcha = FALSE; |
| 648 | foreach ($fields as $key => $field) { |
| 649 | if ($viewOnly && |
| 650 | isset($field['data_type']) && |
| 651 | $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL') |
| 652 | ) { |
| 653 | // ignore file upload fields |
| 654 | continue; |
| 655 | } |
| 656 | |
| 657 | if ($profileContactType) { |
| 658 | //Since we are showing honoree name separately so we are removing it from honoree profile just for display |
| 659 | $honoreeNamefields = array( |
| 660 | 'prefix_id', |
| 661 | 'first_name', |
| 662 | 'last_name', |
| 663 | 'suffix_id', |
| 664 | 'organization_name', |
| 665 | 'household_name', |
| 666 | ); |
| 667 | if ($profileContactType == 'honor' && in_array($field['name'], $honoreeNamefields)) { |
| 668 | unset($fields[$field['name']]); |
| 669 | continue; |
| 670 | } |
| 671 | if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) { |
| 672 | CRM_Core_BAO_UFGroup::buildProfile( |
| 673 | $this, |
| 674 | $field, |
| 675 | CRM_Profile_Form::MODE_CREATE, |
| 676 | $contactID, |
| 677 | TRUE, |
| 678 | $profileContactType |
| 679 | ); |
| 680 | $this->_fields[$profileContactType][$key] = $field; |
| 681 | } |
| 682 | else { |
| 683 | unset($fields[$key]); |
| 684 | } |
| 685 | } |
| 686 | else { |
| 687 | CRM_Core_BAO_UFGroup::buildProfile( |
| 688 | $this, |
| 689 | $field, |
| 690 | CRM_Profile_Form::MODE_CREATE, |
| 691 | $contactID, |
| 692 | TRUE |
| 693 | ); |
| 694 | $this->_fields[$key] = $field; |
| 695 | } |
| 696 | // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor |
| 697 | if ($field['add_captcha'] && !$this->_userID) { |
| 698 | $addCaptcha = TRUE; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | $this->assign($name, $fields); |
| 703 | |
| 704 | if ($addCaptcha && !$viewOnly) { |
| 705 | $captcha = CRM_Utils_ReCAPTCHA::singleton(); |
| 706 | $captcha->add($this); |
| 707 | $this->assign('isCaptcha', TRUE); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | |
| 714 | public function buildComponentForm($id, $form) { |
| 715 | if (empty($id)) { |
| 716 | return; |
| 717 | } |
| 718 | |
| 719 | $contactID = $this->getContactID(); |
| 720 | |
| 721 | foreach (array('soft_credit', 'on_behalf') as $module) { |
| 722 | $ufJoinParams = array( |
| 723 | 'module' => $module, |
| 724 | 'entity_table' => 'civicrm_contribution_page', |
| 725 | 'entity_id' => $id, |
| 726 | ); |
| 727 | |
| 728 | $ufJoin = new CRM_Core_DAO_UFJoin(); |
| 729 | $ufJoin->copyValues($ufJoinParams); |
| 730 | $ufJoin->find(TRUE); |
| 731 | if (!$ufJoin->is_active) { |
| 732 | continue; |
| 733 | } |
| 734 | |
| 735 | if ($module == 'soft_credit') { |
| 736 | $form->_honoreeProfileId = $ufJoin->uf_group_id; |
| 737 | $form->_honor_block_is_active = $ufJoin->is_active; |
| 738 | |
| 739 | if (!$form->_honoreeProfileId || |
| 740 | !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_honoreeProfileId, 'is_active') |
| 741 | ) { |
| 742 | CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of honoree and the selected honoree profile is either disabled or not found.')); |
| 743 | } |
| 744 | |
| 745 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_honoreeProfileId); |
| 746 | $requiredProfileFields = array( |
| 747 | 'Individual' => array('first_name', 'last_name'), |
| 748 | 'Organization' => array('organization_name', 'email'), |
| 749 | 'Household' => array('household_name', 'email'), |
| 750 | ); |
| 751 | $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_honoreeProfileId, $requiredProfileFields[$profileContactType]); |
| 752 | if (!$validProfile) { |
| 753 | CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of honoree and the required fields of the selected honoree profile are disabled or doesn\'t exist.')); |
| 754 | } |
| 755 | |
| 756 | //build soft-credit section |
| 757 | CRM_Contribute_Form_SoftCredit::buildQuickForm($form); |
| 758 | } |
| 759 | else { |
| 760 | $params = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoin->module_data, TRUE, 'on_behalf'); |
| 761 | $form->_values = array_merge($params, $form->_values); |
| 762 | |
| 763 | $onBehalfProfileId = $ufJoin->uf_group_id; |
| 764 | |
| 765 | if (!$onBehalfProfileId || |
| 766 | !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $onBehalfProfileId, 'is_active') |
| 767 | ) { |
| 768 | CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of an organization and the selected onbehalf profile is either disabled or not found.')); |
| 769 | } |
| 770 | |
| 771 | $member = CRM_Member_BAO_Membership::getMembershipBlock($form->_id); |
| 772 | if (empty($member['is_active'])) { |
| 773 | $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.'); |
| 774 | $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($onBehalfProfileId); |
| 775 | foreach (array( |
| 776 | 'Individual', |
| 777 | 'Organization', |
| 778 | 'Household', |
| 779 | ) as $contactType) { |
| 780 | if (in_array($contactType, $onBehalfProfile) && |
| 781 | (in_array('Membership', $onBehalfProfile) || |
| 782 | in_array('Contribution', $onBehalfProfile) |
| 783 | ) |
| 784 | ) { |
| 785 | CRM_Core_Error::fatal($msg); |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | if ($contactID) { |
| 791 | $form->_employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($contactID); |
| 792 | |
| 793 | if (!empty($form->_membershipContactID) && $contactID != $form->_membershipContactID) { |
| 794 | // renewal case - membership being renewed may or may not be for organization |
| 795 | if (!empty($form->_employers) && array_key_exists($form->_membershipContactID, $form->_employers)) { |
| 796 | // if _membershipContactID belongs to employers list, we can say: |
| 797 | $form->_relatedOrganizationFound = TRUE; |
| 798 | } |
| 799 | } |
| 800 | elseif (!empty($form->_employers)) { |
| 801 | // not a renewal case and _employers list is not empty |
| 802 | $form->_relatedOrganizationFound = TRUE; |
| 803 | } |
| 804 | |
| 805 | if ($params['is_for_organization'] != 2) { |
| 806 | $form->assign('relatedOrganizationFound', $form->_relatedOrganizationFound); |
| 807 | } |
| 808 | else { |
| 809 | $form->assign('onBehalfRequired', $form->_onBehalfRequired); |
| 810 | } |
| 811 | |
| 812 | if (count($form->_employers) == 1) { |
| 813 | foreach ($form->_employers as $id => $value) { |
| 814 | $form->_organizationName = $value['name']; |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (CRM_Utils_Array::value('is_for_organization', $params)) { |
| 820 | if ($params['is_for_organization'] == 2) { |
| 821 | $this->assign('onBehalfRequired', TRUE); |
| 822 | } |
| 823 | else { |
| 824 | $form->addElement('checkbox', 'is_for_organization', |
| 825 | $form->_values['for_organization'], |
| 826 | NULL |
| 827 | ); |
| 828 | } |
| 829 | } |
| 830 | $form->assign('onBehalfprofileId', $onBehalfProfileId); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * Check template file exists. |
| 838 | * |
| 839 | * @param string $suffix |
| 840 | * |
| 841 | * @return null|string |
| 842 | */ |
| 843 | public function checkTemplateFileExists($suffix = NULL) { |
| 844 | if ($this->_id) { |
| 845 | $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl"; |
| 846 | $template = CRM_Core_Form::getTemplate(); |
| 847 | if ($template->template_exists($templateFile)) { |
| 848 | return $templateFile; |
| 849 | } |
| 850 | } |
| 851 | return NULL; |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * Use the form name to create the tpl file name. |
| 856 | * |
| 857 | * @return string |
| 858 | */ |
| 859 | public function getTemplateFileName() { |
| 860 | $fileName = $this->checkTemplateFileExists(); |
| 861 | return $fileName ? $fileName : parent::getTemplateFileName(); |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * Add the extra.tpl in. |
| 866 | * |
| 867 | * Default extra tpl file basically just replaces .tpl with .extra.tpl |
| 868 | * i.e. we do not override - why isn't this done at the CRM_Core_Form level? |
| 869 | * |
| 870 | * @return string |
| 871 | */ |
| 872 | public function overrideExtraTemplateFileName() { |
| 873 | $fileName = $this->checkTemplateFileExists('extra.'); |
| 874 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * Authenticate pledge user during online payment. |
| 879 | */ |
| 880 | public function authenticatePledgeUser() { |
| 881 | //get the userChecksum and contact id |
| 882 | $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this); |
| 883 | $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 884 | |
| 885 | //get pledge status and contact id |
| 886 | $pledgeValues = array(); |
| 887 | $pledgeParams = array('id' => $this->_values['pledge_id']); |
| 888 | $returnProperties = array('contact_id', 'status_id'); |
| 889 | CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties); |
| 890 | |
| 891 | //get all status |
| 892 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 893 | $validStatus = array( |
| 894 | array_search('Pending', $allStatus), |
| 895 | array_search('In Progress', $allStatus), |
| 896 | array_search('Overdue', $allStatus), |
| 897 | ); |
| 898 | |
| 899 | $validUser = FALSE; |
| 900 | if ($this->_userID && |
| 901 | $this->_userID == $pledgeValues['contact_id'] |
| 902 | ) { |
| 903 | //check for authenticated user. |
| 904 | $validUser = TRUE; |
| 905 | } |
| 906 | elseif ($userChecksum && $pledgeValues['contact_id']) { |
| 907 | //check for anonymous user. |
| 908 | $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum); |
| 909 | |
| 910 | //make sure cid is same as pledge contact id |
| 911 | if ($validUser && ($pledgeValues['contact_id'] != $contactID)) { |
| 912 | $validUser = FALSE; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if (!$validUser) { |
| 917 | CRM_Core_Error::fatal(ts("Oops. It looks like you have an incorrect or incomplete link (URL). Please make sure you've copied the entire link, and try again. Contact the site administrator if this error persists.")); |
| 918 | } |
| 919 | |
| 920 | //check for valid pledge status. |
| 921 | if (!in_array($pledgeValues['status_id'], $validStatus)) { |
| 922 | CRM_Core_Error::fatal(ts('Oops. You cannot make a payment for this pledge - pledge status is %1.', array(1 => CRM_Utils_Array::value($pledgeValues['status_id'], $allStatus)))); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * Cancel recurring contributions. |
| 928 | * |
| 929 | * In case user cancel recurring contribution, |
| 930 | * When we get the control back from payment gate way |
| 931 | * lets delete the recurring and related contribution. |
| 932 | */ |
| 933 | public function cancelRecurring() { |
| 934 | $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject); |
| 935 | if ($isCancel) { |
| 936 | $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean', CRM_Core_DAO::$_nullObject); |
| 937 | $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive', CRM_Core_DAO::$_nullObject); |
| 938 | //clean db for recurring contribution. |
| 939 | if ($isRecur && $recurId) { |
| 940 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId); |
| 941 | } |
| 942 | $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive', CRM_Core_DAO::$_nullObject); |
| 943 | if ($contribId) { |
| 944 | CRM_Contribute_BAO_Contribution::deleteContribution($contribId); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * Build Membership Block in Contribution Pages. |
| 951 | * |
| 952 | * @param int $cid |
| 953 | * Contact checked for having a current membership for a particular membership. |
| 954 | * @param bool $isContributionMainPage |
| 955 | * Is this the main page? If so add form input fields. |
| 956 | * (or better yet don't have this functionality in a function shared with forms that don't share it). |
| 957 | * @param int $selectedMembershipTypeID |
| 958 | * Selected membership id. |
| 959 | * @param bool $thankPage |
| 960 | * Thank you page. |
| 961 | * @param null $isTest |
| 962 | * |
| 963 | * @return bool |
| 964 | * Is this a separate membership payment |
| 965 | */ |
| 966 | protected function buildMembershipBlock( |
| 967 | $cid, |
| 968 | $isContributionMainPage = FALSE, |
| 969 | $selectedMembershipTypeID = NULL, |
| 970 | $thankPage = FALSE, |
| 971 | $isTest = NULL |
| 972 | ) { |
| 973 | |
| 974 | $separateMembershipPayment = FALSE; |
| 975 | if ($this->_membershipBlock) { |
| 976 | $this->_currentMemberships = array(); |
| 977 | |
| 978 | $membershipTypeIds = $membershipTypes = $radio = array(); |
| 979 | $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember) ? TRUE : FALSE; |
| 980 | |
| 981 | $allowAutoRenewMembership = $autoRenewOption = FALSE; |
| 982 | $autoRenewMembershipTypeOptions = array(); |
| 983 | |
| 984 | $separateMembershipPayment = CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock); |
| 985 | |
| 986 | if ($membershipPriceset) { |
| 987 | foreach ($this->_priceSet['fields'] as $pField) { |
| 988 | if (empty($pField['options'])) { |
| 989 | continue; |
| 990 | } |
| 991 | foreach ($pField['options'] as $opId => $opValues) { |
| 992 | if (empty($opValues['membership_type_id'])) { |
| 993 | continue; |
| 994 | } |
| 995 | $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id']; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | elseif (!empty($this->_membershipBlock['membership_types'])) { |
| 1000 | $membershipTypeIds = explode(',', $this->_membershipBlock['membership_types']); |
| 1001 | } |
| 1002 | |
| 1003 | if (!empty($membershipTypeIds)) { |
| 1004 | //set status message if wrong membershipType is included in membershipBlock |
| 1005 | if (isset($this->_mid) && !$membershipPriceset) { |
| 1006 | $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', |
| 1007 | $this->_mid, |
| 1008 | 'membership_type_id' |
| 1009 | ); |
| 1010 | if (!in_array($membershipTypeID, $membershipTypeIds)) { |
| 1011 | CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Invalid Membership'), 'error'); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIds); |
| 1016 | $this->_membershipTypeValues = $membershipTypeValues; |
| 1017 | $endDate = NULL; |
| 1018 | foreach ($membershipTypeIds as $value) { |
| 1019 | $memType = $membershipTypeValues[$value]; |
| 1020 | if ($selectedMembershipTypeID != NULL) { |
| 1021 | if ($memType['id'] == $selectedMembershipTypeID) { |
| 1022 | $this->assign('minimum_fee', |
| 1023 | CRM_Utils_Array::value('minimum_fee', $memType) |
| 1024 | ); |
| 1025 | $this->assign('membership_name', $memType['name']); |
| 1026 | if (!$thankPage && $cid) { |
| 1027 | $membership = new CRM_Member_DAO_Membership(); |
| 1028 | $membership->contact_id = $cid; |
| 1029 | $membership->membership_type_id = $memType['id']; |
| 1030 | if ($membership->find(TRUE)) { |
| 1031 | $this->assign('renewal_mode', TRUE); |
| 1032 | $memType['current_membership'] = $membership->end_date; |
| 1033 | $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; |
| 1034 | } |
| 1035 | } |
| 1036 | $membershipTypes[] = $memType; |
| 1037 | } |
| 1038 | } |
| 1039 | elseif ($memType['is_active']) { |
| 1040 | $javascriptMethod = NULL; |
| 1041 | $allowAutoRenewOpt = (int) $memType['auto_renew']; |
| 1042 | if (is_array($this->_paymentProcessors)) { |
| 1043 | foreach ($this->_paymentProcessors as $id => $val) { |
| 1044 | if (!$val['is_recur']) { |
| 1045 | $allowAutoRenewOpt = 0; |
| 1046 | continue; |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );"); |
| 1052 | $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array::value($value, CRM_Utils_Array::value('auto_renew', $this->_membershipBlock));; |
| 1053 | |
| 1054 | if ($allowAutoRenewOpt) { |
| 1055 | $allowAutoRenewMembership = TRUE; |
| 1056 | } |
| 1057 | |
| 1058 | //add membership type. |
| 1059 | $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL, |
| 1060 | $memType['id'], $javascriptMethod |
| 1061 | ); |
| 1062 | if ($cid) { |
| 1063 | $membership = new CRM_Member_DAO_Membership(); |
| 1064 | $membership->contact_id = $cid; |
| 1065 | $membership->membership_type_id = $memType['id']; |
| 1066 | |
| 1067 | //show current membership, skip pending and cancelled membership records, |
| 1068 | //because we take first membership record id for renewal |
| 1069 | $membership->whereAdd('status_id != 5 AND status_id !=6'); |
| 1070 | |
| 1071 | if (!is_null($isTest)) { |
| 1072 | $membership->is_test = $isTest; |
| 1073 | } |
| 1074 | |
| 1075 | //CRM-4297 |
| 1076 | $membership->orderBy('end_date DESC'); |
| 1077 | |
| 1078 | if ($membership->find(TRUE)) { |
| 1079 | if (!$membership->end_date) { |
| 1080 | unset($radio[$memType['id']]); |
| 1081 | $this->assign('islifetime', TRUE); |
| 1082 | continue; |
| 1083 | } |
| 1084 | $this->assign('renewal_mode', TRUE); |
| 1085 | $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; |
| 1086 | $memType['current_membership'] = $membership->end_date; |
| 1087 | if (!$endDate) { |
| 1088 | $endDate = $memType['current_membership']; |
| 1089 | $this->_defaultMemTypeId = $memType['id']; |
| 1090 | } |
| 1091 | if ($memType['current_membership'] < $endDate) { |
| 1092 | $endDate = $memType['current_membership']; |
| 1093 | $this->_defaultMemTypeId = $memType['id']; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | $membershipTypes[] = $memType; |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | $this->assign('membershipBlock', $this->_membershipBlock); |
| 1103 | $this->assign('showRadio', $isContributionMainPage); |
| 1104 | $this->assign('membershipTypes', $membershipTypes); |
| 1105 | $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership); |
| 1106 | $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions)); |
| 1107 | //give preference to user submitted auto_renew value. |
| 1108 | $takeUserSubmittedAutoRenew = (!empty($_POST) || $this->isSubmitted()) ? TRUE : FALSE; |
| 1109 | $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew); |
| 1110 | |
| 1111 | if ($isContributionMainPage) { |
| 1112 | if (!$membershipPriceset) { |
| 1113 | if (!$this->_membershipBlock['is_required']) { |
| 1114 | $this->assign('showRadioNoThanks', TRUE); |
| 1115 | $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL); |
| 1116 | $this->addGroup($radio, 'selectMembership', NULL); |
| 1117 | } |
| 1118 | elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) { |
| 1119 | $temp = array_keys($radio); |
| 1120 | $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership')); |
| 1121 | $this->assign('singleMembership', TRUE); |
| 1122 | $this->assign('showRadio', FALSE); |
| 1123 | } |
| 1124 | else { |
| 1125 | $this->addGroup($radio, 'selectMembership', NULL); |
| 1126 | } |
| 1127 | |
| 1128 | $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required'); |
| 1129 | } |
| 1130 | else { |
| 1131 | $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId); |
| 1132 | $this->assign('autoRenewOption', $autoRenewOption); |
| 1133 | } |
| 1134 | |
| 1135 | if (!$this->_values['is_pay_later'] && is_array($this->_paymentProcessors) && ($allowAutoRenewMembership || $autoRenewOption)) { |
| 1136 | $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.')); |
| 1137 | } |
| 1138 | |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | return $separateMembershipPayment; |
| 1143 | } |
| 1144 | |
| 1145 | /** |
| 1146 | * Determine if recurring parameters need to be added to the form parameters. |
| 1147 | * - is_recur |
| 1148 | * - frequency_interval |
| 1149 | * - frequency_unit |
| 1150 | * |
| 1151 | * For membership this is based on the membership type. |
| 1152 | * |
| 1153 | * This needs to be done before processing the pre-approval redirect where relevant on the main page or before any payment processing. |
| 1154 | * |
| 1155 | * Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout. |
| 1156 | */ |
| 1157 | protected function setRecurringMembershipParams() { |
| 1158 | if (!empty($this->_params['priceSetId']) && !empty($this->_params['selectMembership'])) { |
| 1159 | // @todo the price_x fields will ALWAYS allow us to determine the membership - so we should ignore |
| 1160 | // 'selectMembership' and calculate from the price_x fields so we have one method that always works |
| 1161 | // this is lazy & only catches when selectMembership is set, but the worst of all worlds would be to fix |
| 1162 | // this with an else (calculate for price set). |
| 1163 | $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($this->_params['priceSetId']); |
| 1164 | if (in_array($this->_params['selectMembership'], $membershipTypes['autorenew'])) { |
| 1165 | $this->_params['auto_renew'] = TRUE; |
| 1166 | } |
| 1167 | } |
| 1168 | if ((!empty($this->_params['selectMembership']) || !empty($this->_params['priceSetId'])) && !empty($this->_paymentProcessor['is_recur']) && |
| 1169 | CRM_Utils_Array::value('auto_renew', $this->_params) && empty($this->_params['is_recur']) && empty($this->_params['frequency_interval']) |
| 1170 | ) { |
| 1171 | |
| 1172 | $this->_params['is_recur'] = $this->_values['is_recur'] = 1; |
| 1173 | // check if price set is not quick config |
| 1174 | if (!empty($this->_params['priceSetId']) && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config')) { |
| 1175 | list($this->_params['frequency_interval'], $this->_params['frequency_unit']) = CRM_Price_BAO_PriceSet::getRecurDetails($this->_params['priceSetId']); |
| 1176 | } |
| 1177 | else { |
| 1178 | // FIXME: set interval and unit based on selected membership type |
| 1179 | $this->_params['frequency_interval'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', |
| 1180 | $this->_params['selectMembership'], 'duration_interval' |
| 1181 | ); |
| 1182 | $this->_params['frequency_unit'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', |
| 1183 | $this->_params['selectMembership'], 'duration_unit' |
| 1184 | ); |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | } |