| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * This class generates form components for processing a contribution. |
| 36 | */ |
| 37 | class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { |
| 38 | |
| 39 | /** |
| 40 | * The id of the contribution page that we are processing. |
| 41 | * |
| 42 | * @var int |
| 43 | */ |
| 44 | public $_id; |
| 45 | |
| 46 | /** |
| 47 | * The mode that we are in |
| 48 | * |
| 49 | * @var string |
| 50 | * @protect |
| 51 | */ |
| 52 | public $_mode; |
| 53 | |
| 54 | /** |
| 55 | * The contact id related to a membership |
| 56 | * |
| 57 | * @var int |
| 58 | */ |
| 59 | public $_membershipContactID; |
| 60 | |
| 61 | /** |
| 62 | * The values for the contribution db object |
| 63 | * |
| 64 | * @var array |
| 65 | */ |
| 66 | public $_values; |
| 67 | |
| 68 | /** |
| 69 | * The paymentProcessor attributes for this page |
| 70 | * |
| 71 | * @var array |
| 72 | */ |
| 73 | public $_paymentProcessor; |
| 74 | |
| 75 | public $_paymentObject = NULL; |
| 76 | |
| 77 | /** |
| 78 | * The membership block for this page |
| 79 | * |
| 80 | * @var array |
| 81 | */ |
| 82 | public $_membershipBlock = NULL; |
| 83 | |
| 84 | /** |
| 85 | * Does this form support a separate membership payment |
| 86 | * @var bool |
| 87 | */ |
| 88 | protected $_separateMembershipPayment; |
| 89 | |
| 90 | /** |
| 91 | * The params submitted by the form and computed by the app |
| 92 | * |
| 93 | * @var array |
| 94 | */ |
| 95 | public $_params = array(); |
| 96 | |
| 97 | /** |
| 98 | * The fields involved in this contribution page |
| 99 | * |
| 100 | * @var array |
| 101 | */ |
| 102 | public $_fields = array(); |
| 103 | |
| 104 | /** |
| 105 | * The billing location id for this contribution page. |
| 106 | * |
| 107 | * @var int |
| 108 | */ |
| 109 | public $_bltID; |
| 110 | |
| 111 | /** |
| 112 | * Cache the amount to make things easier |
| 113 | * |
| 114 | * @var float |
| 115 | */ |
| 116 | public $_amount; |
| 117 | |
| 118 | /** |
| 119 | * Pcp id |
| 120 | * |
| 121 | * @var integer |
| 122 | */ |
| 123 | public $_pcpId; |
| 124 | |
| 125 | /** |
| 126 | * Pcp block |
| 127 | * |
| 128 | * @var array |
| 129 | */ |
| 130 | public $_pcpBlock; |
| 131 | |
| 132 | /** |
| 133 | * Pcp info |
| 134 | * |
| 135 | * @var array |
| 136 | */ |
| 137 | public $_pcpInfo; |
| 138 | |
| 139 | /** |
| 140 | * The contact id of the person for whom membership is being added or renewed based on the cid in the url, |
| 141 | * checksum, or session |
| 142 | * @var int |
| 143 | */ |
| 144 | public $_contactID; |
| 145 | |
| 146 | protected $_userID; |
| 147 | |
| 148 | /** |
| 149 | * The Membership ID for membership renewal |
| 150 | * |
| 151 | * @var int |
| 152 | */ |
| 153 | public $_membershipId; |
| 154 | |
| 155 | /** |
| 156 | * Price Set ID, if the new price set method is used |
| 157 | * |
| 158 | * @var int |
| 159 | */ |
| 160 | public $_priceSetId; |
| 161 | |
| 162 | /** |
| 163 | * Array of fields for the price set |
| 164 | * |
| 165 | * @var array |
| 166 | */ |
| 167 | public $_priceSet; |
| 168 | |
| 169 | public $_action; |
| 170 | |
| 171 | /** |
| 172 | * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM |
| 173 | * @var string |
| 174 | */ |
| 175 | public $_contributeMode; |
| 176 | |
| 177 | /** |
| 178 | * Contribution page supports memberships |
| 179 | * @var boolean |
| 180 | */ |
| 181 | public $_useForMember; |
| 182 | |
| 183 | /** |
| 184 | * @deprecated |
| 185 | * |
| 186 | * @var |
| 187 | */ |
| 188 | public $_isBillingAddressRequiredForPayLater; |
| 189 | |
| 190 | /** |
| 191 | * Set variables up before form is built. |
| 192 | * |
| 193 | * @throws \CRM_Contribute_Exception_InactiveContributionPageException |
| 194 | * @throws \Exception |
| 195 | */ |
| 196 | public function preProcess() { |
| 197 | |
| 198 | // current contribution page id |
| 199 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 200 | if (!$this->_id) { |
| 201 | // seems like the session is corrupted and/or we lost the id trail |
| 202 | // lets just bump this to a regular session error and redirect user to main page |
| 203 | $this->controller->invalidKeyRedirect(); |
| 204 | } |
| 205 | |
| 206 | // this was used prior to the cleverer this_>getContactID - unsure now |
| 207 | $this->_userID = CRM_Core_Session::singleton()->get('userID'); |
| 208 | |
| 209 | $this->_contactID = $this->_membershipContactID = $this->getContactID(); |
| 210 | $this->_mid = NULL; |
| 211 | if ($this->_contactID) { |
| 212 | $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this); |
| 213 | if ($this->_mid) { |
| 214 | $membership = new CRM_Member_DAO_Membership(); |
| 215 | $membership->id = $this->_mid; |
| 216 | |
| 217 | if ($membership->find(TRUE)) { |
| 218 | $this->_defaultMemTypeId = $membership->membership_type_id; |
| 219 | if ($membership->contact_id != $this->_contactID) { |
| 220 | $validMembership = FALSE; |
| 221 | $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, NULL, NULL, 'Organization'); |
| 222 | if (!empty($organizations) && array_key_exists($membership->contact_id, $organizations)) { |
| 223 | $this->_membershipContactID = $membership->contact_id; |
| 224 | $this->assign('membershipContactID', $this->_membershipContactID); |
| 225 | $this->assign('membershipContactName', $organizations[$this->_membershipContactID]['name']); |
| 226 | $validMembership = TRUE; |
| 227 | } |
| 228 | else { |
| 229 | $membershipType = new CRM_Member_BAO_MembershipType(); |
| 230 | $membershipType->id = $membership->membership_type_id; |
| 231 | if ($membershipType->find(TRUE)) { |
| 232 | // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values. |
| 233 | // Convert to comma separated list. |
| 234 | $inheritedRelTypes = implode(CRM_Utils_Array::explodePadded($membershipType->relationship_type_id), ','); |
| 235 | $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, $membershipType->relationship_type_id); |
| 236 | if (array_key_exists($membership->contact_id, $permContacts)) { |
| 237 | $this->_membershipContactID = $membership->contact_id; |
| 238 | $validMembership = TRUE; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | if (!$validMembership) { |
| 243 | 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'); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | else { |
| 248 | 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'); |
| 249 | } |
| 250 | unset($membership); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // we do not want to display recently viewed items, so turn off |
| 255 | $this->assign('displayRecent', FALSE); |
| 256 | // Contribution page values are cleared from session, so can't use normal Printer Friendly view. |
| 257 | // Use Browser Print instead. |
| 258 | $this->assign('browserPrint', TRUE); |
| 259 | |
| 260 | // action |
| 261 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); |
| 262 | $this->assign('action', $this->_action); |
| 263 | |
| 264 | // current mode |
| 265 | $this->_mode = ($this->_action == 1024) ? 'test' : 'live'; |
| 266 | |
| 267 | $this->_values = $this->get('values'); |
| 268 | $this->_fields = $this->get('fields'); |
| 269 | $this->_bltID = $this->get('bltID'); |
| 270 | $this->_paymentProcessor = $this->get('paymentProcessor'); |
| 271 | $this->_priceSetId = $this->get('priceSetId'); |
| 272 | $this->_priceSet = $this->get('priceSet'); |
| 273 | |
| 274 | if (!$this->_values) { |
| 275 | // get all the values from the dao object |
| 276 | $this->_values = array(); |
| 277 | $this->_fields = array(); |
| 278 | |
| 279 | CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values); |
| 280 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() |
| 281 | && !CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id'])) |
| 282 | ) { |
| 283 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
| 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($isPayLater); |
| 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 (!empty($this->_values['custom_pre_id'])) { |
| 328 | $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']); |
| 329 | } |
| 330 | |
| 331 | if (!empty($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 | // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel |
| 513 | // function to get correct amount level consistently. Remove setting of the amount level in |
| 514 | // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest |
| 515 | // to cover all variants. |
| 516 | if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) { |
| 517 | $this->_params['amount_level'] = ''; |
| 518 | } |
| 519 | |
| 520 | foreach ($vars as $v) { |
| 521 | if (isset($this->_params[$v])) { |
| 522 | if ($v == "amount" && $this->_params[$v] === 0) { |
| 523 | $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE); |
| 524 | } |
| 525 | $this->assign($v, $this->_params[$v]); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // assign the address formatted up for display |
| 530 | $addressParts = array( |
| 531 | "street_address-{$this->_bltID}", |
| 532 | "city-{$this->_bltID}", |
| 533 | "postal_code-{$this->_bltID}", |
| 534 | "state_province-{$this->_bltID}", |
| 535 | "country-{$this->_bltID}", |
| 536 | ); |
| 537 | |
| 538 | $addressFields = array(); |
| 539 | foreach ($addressParts as $part) { |
| 540 | list($n, $id) = explode('-', $part); |
| 541 | $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $this->_params); |
| 542 | } |
| 543 | |
| 544 | $this->assign('address', CRM_Utils_Address::format($addressFields)); |
| 545 | |
| 546 | if (!empty($this->_params['onbehalf_profile_id']) && !empty($this->_params['onbehalf'])) { |
| 547 | $this->assign('onBehalfName', $this->_params['organization_name']); |
| 548 | $locTypeId = array_keys($this->_params['onbehalf_location']['email']); |
| 549 | $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']); |
| 550 | } |
| 551 | |
| 552 | //fix for CRM-3767 |
| 553 | $assignCCInfo = FALSE; |
| 554 | if ($this->_amount > 0.0) { |
| 555 | $assignCCInfo = TRUE; |
| 556 | } |
| 557 | elseif (!empty($this->_params['selectMembership'])) { |
| 558 | $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee'); |
| 559 | if ($memFee > 0.0) { |
| 560 | $assignCCInfo = TRUE; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // The concept of contributeMode is deprecated. |
| 565 | // The payment processor object can provide info about the fields it shows. |
| 566 | if ($this->_contributeMode == 'direct' && $assignCCInfo) { |
| 567 | if ($this->_paymentProcessor && |
| 568 | $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT |
| 569 | ) { |
| 570 | $this->assign('account_holder', $this->_params['account_holder']); |
| 571 | $this->assign('bank_identification_number', $this->_params['bank_identification_number']); |
| 572 | $this->assign('bank_name', $this->_params['bank_name']); |
| 573 | $this->assign('bank_account_number', $this->_params['bank_account_number']); |
| 574 | } |
| 575 | else { |
| 576 | $date = CRM_Utils_Date::format(CRM_Utils_array::value('credit_card_exp_date', $this->_params)); |
| 577 | $date = CRM_Utils_Date::mysqlToIso($date); |
| 578 | $this->assign('credit_card_exp_date', $date); |
| 579 | $this->assign('credit_card_number', |
| 580 | CRM_Utils_System::mungeCreditCard(CRM_Utils_array::value('credit_card_number', $this->_params)) |
| 581 | ); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | $this->assign('email', |
| 586 | $this->controller->exportValue('Main', "email-{$this->_bltID}") |
| 587 | ); |
| 588 | |
| 589 | // also assign the receipt_text |
| 590 | if (isset($this->_values['receipt_text'])) { |
| 591 | $this->assign('receipt_text', $this->_values['receipt_text']); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Add the custom fields. |
| 597 | * |
| 598 | * @param int $id |
| 599 | * @param string $name |
| 600 | * @param bool $viewOnly |
| 601 | * @param null $profileContactType |
| 602 | * @param array $fieldTypes |
| 603 | */ |
| 604 | public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) { |
| 605 | if ($id) { |
| 606 | $contactID = $this->getContactID(); |
| 607 | |
| 608 | // we don't allow conflicting fields to be |
| 609 | // configured via profile - CRM 2100 |
| 610 | $fieldsToIgnore = array( |
| 611 | 'receive_date' => 1, |
| 612 | 'trxn_id' => 1, |
| 613 | 'invoice_id' => 1, |
| 614 | 'net_amount' => 1, |
| 615 | 'fee_amount' => 1, |
| 616 | 'non_deductible_amount' => 1, |
| 617 | 'total_amount' => 1, |
| 618 | 'amount_level' => 1, |
| 619 | 'contribution_status_id' => 1, |
| 620 | 'payment_instrument' => 1, |
| 621 | 'check_number' => 1, |
| 622 | 'financial_type' => 1, |
| 623 | ); |
| 624 | |
| 625 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE, |
| 626 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL |
| 627 | ); |
| 628 | |
| 629 | if ($fields) { |
| 630 | // unset any email-* fields since we already collect it, CRM-2888 |
| 631 | foreach (array_keys($fields) as $fieldName) { |
| 632 | if (substr($fieldName, 0, 6) == 'email-' && !in_array($profileContactType, array('honor', 'onbehalf'))) { |
| 633 | unset($fields[$fieldName]); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if (array_intersect_key($fields, $fieldsToIgnore)) { |
| 638 | $fields = array_diff_key($fields, $fieldsToIgnore); |
| 639 | CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert'); |
| 640 | } |
| 641 | |
| 642 | $fields = array_diff_assoc($fields, $this->_fields); |
| 643 | |
| 644 | CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID); |
| 645 | $addCaptcha = FALSE; |
| 646 | foreach ($fields as $key => $field) { |
| 647 | if ($viewOnly && |
| 648 | isset($field['data_type']) && |
| 649 | $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL') |
| 650 | ) { |
| 651 | // ignore file upload fields |
| 652 | continue; |
| 653 | } |
| 654 | |
| 655 | if ($profileContactType) { |
| 656 | //Since we are showing honoree name separately so we are removing it from honoree profile just for display |
| 657 | if ($profileContactType == 'honor') { |
| 658 | $honoreeNamefields = array( |
| 659 | 'prefix_id', |
| 660 | 'first_name', |
| 661 | 'last_name', |
| 662 | 'suffix_id', |
| 663 | 'organization_name', |
| 664 | 'household_name', |
| 665 | ); |
| 666 | if (in_array($field['name'], $honoreeNamefields)) { |
| 667 | unset($fields[$field['name']]); |
| 668 | continue; |
| 669 | } |
| 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 | * Add onbehalf/honoree profile fields and native module fields. |
| 715 | * |
| 716 | * @param int $id |
| 717 | * @param CRM_Core_Form $form |
| 718 | */ |
| 719 | public function buildComponentForm($id, $form) { |
| 720 | if (empty($id)) { |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | $contactID = $this->getContactID(); |
| 725 | |
| 726 | foreach (array('soft_credit', 'on_behalf') as $module) { |
| 727 | if ($module == 'soft_credit') { |
| 728 | if (empty($form->_values['honoree_profile_id'])) { |
| 729 | continue; |
| 730 | } |
| 731 | |
| 732 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['honoree_profile_id'], 'is_active')) { |
| 733 | 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.')); |
| 734 | } |
| 735 | |
| 736 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']); |
| 737 | $requiredProfileFields = array( |
| 738 | 'Individual' => array('first_name', 'last_name'), |
| 739 | 'Organization' => array('organization_name', 'email'), |
| 740 | 'Household' => array('household_name', 'email'), |
| 741 | ); |
| 742 | $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_values['honoree_profile_id'], $requiredProfileFields[$profileContactType]); |
| 743 | if (!$validProfile) { |
| 744 | 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.')); |
| 745 | } |
| 746 | |
| 747 | foreach (array('honor_block_title', 'honor_block_text') as $name) { |
| 748 | $form->assign($name, $form->_values[$name]); |
| 749 | } |
| 750 | |
| 751 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); |
| 752 | |
| 753 | // radio button for Honor Type |
| 754 | foreach ($form->_values['soft_credit_types'] as $value) { |
| 755 | $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value); |
| 756 | } |
| 757 | $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE); |
| 758 | |
| 759 | $honoreeProfileFields = CRM_Core_BAO_UFGroup::getFields( |
| 760 | $this->_values['honoree_profile_id'], FALSE, |
| 761 | NULL, NULL, |
| 762 | NULL, FALSE, |
| 763 | NULL, TRUE, |
| 764 | NULL, CRM_Core_Permission::CREATE |
| 765 | ); |
| 766 | $form->assign('honoreeProfileFields', $honoreeProfileFields); |
| 767 | |
| 768 | // add the form elements |
| 769 | foreach ($honoreeProfileFields as $name => $field) { |
| 770 | // If soft credit type is not chosen then make omit requiredness from honoree profile fields |
| 771 | if (count($form->_submitValues) && |
| 772 | empty($form->_submitValues['soft_credit_type_id']) && |
| 773 | !empty($field['is_required']) |
| 774 | ) { |
| 775 | $field['is_required'] = FALSE; |
| 776 | } |
| 777 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, NULL, FALSE, FALSE, NULL, 'honor'); |
| 778 | } |
| 779 | } |
| 780 | else { |
| 781 | if (empty($form->_values['onbehalf_profile_id'])) { |
| 782 | continue; |
| 783 | } |
| 784 | |
| 785 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['onbehalf_profile_id'], 'is_active')) { |
| 786 | 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.')); |
| 787 | } |
| 788 | |
| 789 | $member = CRM_Member_BAO_Membership::getMembershipBlock($form->_id); |
| 790 | if (empty($member['is_active'])) { |
| 791 | $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.'); |
| 792 | $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($form->_values['onbehalf_profile_id']); |
| 793 | foreach (array( |
| 794 | 'Individual', |
| 795 | 'Organization', |
| 796 | 'Household', |
| 797 | ) as $contactType) { |
| 798 | if (in_array($contactType, $onBehalfProfile) && |
| 799 | (in_array('Membership', $onBehalfProfile) || |
| 800 | in_array('Contribution', $onBehalfProfile) |
| 801 | ) |
| 802 | ) { |
| 803 | CRM_Core_Error::fatal($msg); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | if ($contactID) { |
| 809 | // retrieve all permissioned organizations of contact $contactID |
| 810 | $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($contactID, NULL, NULL, 'Organization'); |
| 811 | |
| 812 | if (count($organizations)) { |
| 813 | // Related org url - pass checksum if needed |
| 814 | $args = array( |
| 815 | 'ufId' => $form->_values['onbehalf_profile_id'], |
| 816 | 'cid' => '', |
| 817 | ); |
| 818 | if (!empty($_GET['cs'])) { |
| 819 | $args = array( |
| 820 | 'ufId' => $form->_values['onbehalf_profile_id'], |
| 821 | 'uid' => $this->_contactID, |
| 822 | 'cs' => $_GET['cs'], |
| 823 | 'cid' => '', |
| 824 | ); |
| 825 | } |
| 826 | $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE); |
| 827 | $form->assign('locDataURL', $locDataURL); |
| 828 | } |
| 829 | if (count($organizations) > 0) { |
| 830 | $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $organizations)); |
| 831 | |
| 832 | $orgOptions = array( |
| 833 | 0 => ts('Select an existing organization'), |
| 834 | 1 => ts('Enter a new organization'), |
| 835 | ); |
| 836 | $form->addRadio('org_option', ts('options'), $orgOptions); |
| 837 | $form->setDefaults(array('org_option' => 0)); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | $form->assign('fieldSetTitle', ts('Organization Details')); |
| 842 | |
| 843 | if (CRM_Utils_Array::value('is_for_organization', $form->_values)) { |
| 844 | if ($form->_values['is_for_organization'] == 2) { |
| 845 | $form->assign('onBehalfRequired', TRUE); |
| 846 | } |
| 847 | else { |
| 848 | $form->addElement('checkbox', 'is_for_organization', |
| 849 | $form->_values['for_organization'], |
| 850 | NULL |
| 851 | ); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | $profileFields = CRM_Core_BAO_UFGroup::getFields( |
| 856 | $form->_values['onbehalf_profile_id'], |
| 857 | FALSE, CRM_Core_Action::VIEW, NULL, |
| 858 | NULL, FALSE, NULL, FALSE, NULL, |
| 859 | CRM_Core_Permission::CREATE, NULL |
| 860 | ); |
| 861 | |
| 862 | $form->assign('onBehalfOfFields', $profileFields); |
| 863 | if (!empty($form->_submitValues['onbehalf'])) { |
| 864 | if (!empty($form->_submitValues['onbehalfof_id'])) { |
| 865 | $form->assign('submittedOnBehalf', $form->_submitValues['onbehalfof_id']); |
| 866 | } |
| 867 | $form->assign('submittedOnBehalfInfo', json_encode($form->_submitValues['onbehalf'])); |
| 868 | } |
| 869 | |
| 870 | $fieldTypes = array('Contact', 'Organization'); |
| 871 | $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); |
| 872 | $fieldTypes = array_merge($fieldTypes, $contactSubType); |
| 873 | |
| 874 | foreach ($profileFields as $name => $field) { |
| 875 | if (in_array($field['field_type'], $fieldTypes)) { |
| 876 | list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2); |
| 877 | if (in_array($prefixName, array('organization_name', 'email')) && empty($field['is_required'])) { |
| 878 | $field['is_required'] = 1; |
| 879 | } |
| 880 | if (count($form->_submitValues) && |
| 881 | empty($form->_submitValues['is_for_organization']) && |
| 882 | $form->_values['is_for_organization'] == 1 && |
| 883 | !empty($field['is_required']) |
| 884 | ) { |
| 885 | $field['is_required'] = FALSE; |
| 886 | } |
| 887 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, NULL, NULL, FALSE, 'onbehalf', NULL, 'onbehalf'); |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Check template file exists. |
| 897 | * |
| 898 | * @param string $suffix |
| 899 | * |
| 900 | * @return null|string |
| 901 | */ |
| 902 | public function checkTemplateFileExists($suffix = NULL) { |
| 903 | if ($this->_id) { |
| 904 | $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl"; |
| 905 | $template = CRM_Core_Form::getTemplate(); |
| 906 | if ($template->template_exists($templateFile)) { |
| 907 | return $templateFile; |
| 908 | } |
| 909 | } |
| 910 | return NULL; |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * Use the form name to create the tpl file name. |
| 915 | * |
| 916 | * @return string |
| 917 | */ |
| 918 | public function getTemplateFileName() { |
| 919 | $fileName = $this->checkTemplateFileExists(); |
| 920 | return $fileName ? $fileName : parent::getTemplateFileName(); |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Add the extra.tpl in. |
| 925 | * |
| 926 | * Default extra tpl file basically just replaces .tpl with .extra.tpl |
| 927 | * i.e. we do not override - why isn't this done at the CRM_Core_Form level? |
| 928 | * |
| 929 | * @return string |
| 930 | */ |
| 931 | public function overrideExtraTemplateFileName() { |
| 932 | $fileName = $this->checkTemplateFileExists('extra.'); |
| 933 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); |
| 934 | } |
| 935 | |
| 936 | /** |
| 937 | * Authenticate pledge user during online payment. |
| 938 | */ |
| 939 | public function authenticatePledgeUser() { |
| 940 | //get the userChecksum and contact id |
| 941 | $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this); |
| 942 | $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 943 | |
| 944 | //get pledge status and contact id |
| 945 | $pledgeValues = array(); |
| 946 | $pledgeParams = array('id' => $this->_values['pledge_id']); |
| 947 | $returnProperties = array('contact_id', 'status_id'); |
| 948 | CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties); |
| 949 | |
| 950 | //get all status |
| 951 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 952 | $validStatus = array( |
| 953 | array_search('Pending', $allStatus), |
| 954 | array_search('In Progress', $allStatus), |
| 955 | array_search('Overdue', $allStatus), |
| 956 | ); |
| 957 | |
| 958 | $validUser = FALSE; |
| 959 | if ($this->_userID && |
| 960 | $this->_userID == $pledgeValues['contact_id'] |
| 961 | ) { |
| 962 | //check for authenticated user. |
| 963 | $validUser = TRUE; |
| 964 | } |
| 965 | elseif ($userChecksum && $pledgeValues['contact_id']) { |
| 966 | //check for anonymous user. |
| 967 | $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum); |
| 968 | |
| 969 | //make sure cid is same as pledge contact id |
| 970 | if ($validUser && ($pledgeValues['contact_id'] != $contactID)) { |
| 971 | $validUser = FALSE; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | if (!$validUser) { |
| 976 | 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.")); |
| 977 | } |
| 978 | |
| 979 | //check for valid pledge status. |
| 980 | if (!in_array($pledgeValues['status_id'], $validStatus)) { |
| 981 | 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)))); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * Cancel recurring contributions. |
| 987 | * |
| 988 | * In case user cancel recurring contribution, |
| 989 | * When we get the control back from payment gate way |
| 990 | * lets delete the recurring and related contribution. |
| 991 | */ |
| 992 | public function cancelRecurring() { |
| 993 | $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject); |
| 994 | if ($isCancel) { |
| 995 | $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean', CRM_Core_DAO::$_nullObject); |
| 996 | $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive', CRM_Core_DAO::$_nullObject); |
| 997 | //clean db for recurring contribution. |
| 998 | if ($isRecur && $recurId) { |
| 999 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId); |
| 1000 | } |
| 1001 | $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive', CRM_Core_DAO::$_nullObject); |
| 1002 | if ($contribId) { |
| 1003 | CRM_Contribute_BAO_Contribution::deleteContribution($contribId); |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Build Membership Block in Contribution Pages. |
| 1010 | * |
| 1011 | * @param int $cid |
| 1012 | * Contact checked for having a current membership for a particular membership. |
| 1013 | * @param bool $isContributionMainPage |
| 1014 | * Is this the main page? If so add form input fields. |
| 1015 | * (or better yet don't have this functionality in a function shared with forms that don't share it). |
| 1016 | * @param int $selectedMembershipTypeID |
| 1017 | * Selected membership id. |
| 1018 | * @param bool $thankPage |
| 1019 | * Thank you page. |
| 1020 | * @param null $isTest |
| 1021 | * |
| 1022 | * @return bool |
| 1023 | * Is this a separate membership payment |
| 1024 | */ |
| 1025 | protected function buildMembershipBlock( |
| 1026 | $cid, |
| 1027 | $isContributionMainPage = FALSE, |
| 1028 | $selectedMembershipTypeID = NULL, |
| 1029 | $thankPage = FALSE, |
| 1030 | $isTest = NULL |
| 1031 | ) { |
| 1032 | |
| 1033 | $separateMembershipPayment = FALSE; |
| 1034 | if ($this->_membershipBlock) { |
| 1035 | $this->_currentMemberships = array(); |
| 1036 | |
| 1037 | $membershipTypeIds = $membershipTypes = $radio = array(); |
| 1038 | $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember) ? TRUE : FALSE; |
| 1039 | |
| 1040 | $allowAutoRenewMembership = $autoRenewOption = FALSE; |
| 1041 | $autoRenewMembershipTypeOptions = array(); |
| 1042 | |
| 1043 | $separateMembershipPayment = CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock); |
| 1044 | |
| 1045 | if ($membershipPriceset) { |
| 1046 | foreach ($this->_priceSet['fields'] as $pField) { |
| 1047 | if (empty($pField['options'])) { |
| 1048 | continue; |
| 1049 | } |
| 1050 | foreach ($pField['options'] as $opId => $opValues) { |
| 1051 | if (empty($opValues['membership_type_id'])) { |
| 1052 | continue; |
| 1053 | } |
| 1054 | $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id']; |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | elseif (!empty($this->_membershipBlock['membership_types'])) { |
| 1059 | $membershipTypeIds = explode(',', $this->_membershipBlock['membership_types']); |
| 1060 | } |
| 1061 | |
| 1062 | if (!empty($membershipTypeIds)) { |
| 1063 | //set status message if wrong membershipType is included in membershipBlock |
| 1064 | if (isset($this->_mid) && !$membershipPriceset) { |
| 1065 | $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', |
| 1066 | $this->_mid, |
| 1067 | 'membership_type_id' |
| 1068 | ); |
| 1069 | if (!in_array($membershipTypeID, $membershipTypeIds)) { |
| 1070 | 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'); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIds); |
| 1075 | $this->_membershipTypeValues = $membershipTypeValues; |
| 1076 | $endDate = NULL; |
| 1077 | foreach ($membershipTypeIds as $value) { |
| 1078 | $memType = $membershipTypeValues[$value]; |
| 1079 | if ($selectedMembershipTypeID != NULL) { |
| 1080 | if ($memType['id'] == $selectedMembershipTypeID) { |
| 1081 | $this->assign('minimum_fee', |
| 1082 | CRM_Utils_Array::value('minimum_fee', $memType) |
| 1083 | ); |
| 1084 | $this->assign('membership_name', $memType['name']); |
| 1085 | if (!$thankPage && $cid) { |
| 1086 | $membership = new CRM_Member_DAO_Membership(); |
| 1087 | $membership->contact_id = $cid; |
| 1088 | $membership->membership_type_id = $memType['id']; |
| 1089 | if ($membership->find(TRUE)) { |
| 1090 | $this->assign('renewal_mode', TRUE); |
| 1091 | $memType['current_membership'] = $membership->end_date; |
| 1092 | $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; |
| 1093 | } |
| 1094 | } |
| 1095 | $membershipTypes[] = $memType; |
| 1096 | } |
| 1097 | } |
| 1098 | elseif ($memType['is_active']) { |
| 1099 | $javascriptMethod = NULL; |
| 1100 | $allowAutoRenewOpt = (int) $memType['auto_renew']; |
| 1101 | if (is_array($this->_paymentProcessors)) { |
| 1102 | foreach ($this->_paymentProcessors as $id => $val) { |
| 1103 | if ($id && !$val['is_recur']) { |
| 1104 | $allowAutoRenewOpt = 0; |
| 1105 | continue; |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );"); |
| 1111 | $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array::value($value, CRM_Utils_Array::value('auto_renew', $this->_membershipBlock));; |
| 1112 | |
| 1113 | if ($allowAutoRenewOpt) { |
| 1114 | $allowAutoRenewMembership = TRUE; |
| 1115 | } |
| 1116 | |
| 1117 | //add membership type. |
| 1118 | $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL, |
| 1119 | $memType['id'], $javascriptMethod |
| 1120 | ); |
| 1121 | if ($cid) { |
| 1122 | $membership = new CRM_Member_DAO_Membership(); |
| 1123 | $membership->contact_id = $cid; |
| 1124 | $membership->membership_type_id = $memType['id']; |
| 1125 | |
| 1126 | //show current membership, skip pending and cancelled membership records, |
| 1127 | //because we take first membership record id for renewal |
| 1128 | $membership->whereAdd('status_id != 5 AND status_id !=6'); |
| 1129 | |
| 1130 | if (!is_null($isTest)) { |
| 1131 | $membership->is_test = $isTest; |
| 1132 | } |
| 1133 | |
| 1134 | //CRM-4297 |
| 1135 | $membership->orderBy('end_date DESC'); |
| 1136 | |
| 1137 | if ($membership->find(TRUE)) { |
| 1138 | if (!$membership->end_date) { |
| 1139 | unset($radio[$memType['id']]); |
| 1140 | $this->assign('islifetime', TRUE); |
| 1141 | continue; |
| 1142 | } |
| 1143 | $this->assign('renewal_mode', TRUE); |
| 1144 | $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; |
| 1145 | $memType['current_membership'] = $membership->end_date; |
| 1146 | if (!$endDate) { |
| 1147 | $endDate = $memType['current_membership']; |
| 1148 | $this->_defaultMemTypeId = $memType['id']; |
| 1149 | } |
| 1150 | if ($memType['current_membership'] < $endDate) { |
| 1151 | $endDate = $memType['current_membership']; |
| 1152 | $this->_defaultMemTypeId = $memType['id']; |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | $membershipTypes[] = $memType; |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | $this->assign('membershipBlock', $this->_membershipBlock); |
| 1162 | $this->assign('showRadio', $isContributionMainPage); |
| 1163 | $this->assign('membershipTypes', $membershipTypes); |
| 1164 | $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership); |
| 1165 | $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions)); |
| 1166 | //give preference to user submitted auto_renew value. |
| 1167 | $takeUserSubmittedAutoRenew = (!empty($_POST) || $this->isSubmitted()) ? TRUE : FALSE; |
| 1168 | $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew); |
| 1169 | |
| 1170 | if ($isContributionMainPage) { |
| 1171 | if (!$membershipPriceset) { |
| 1172 | if (!$this->_membershipBlock['is_required']) { |
| 1173 | $this->assign('showRadioNoThanks', TRUE); |
| 1174 | $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL); |
| 1175 | $this->addGroup($radio, 'selectMembership', NULL); |
| 1176 | } |
| 1177 | elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) { |
| 1178 | $temp = array_keys($radio); |
| 1179 | $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership')); |
| 1180 | $this->assign('singleMembership', TRUE); |
| 1181 | $this->assign('showRadio', FALSE); |
| 1182 | } |
| 1183 | else { |
| 1184 | $this->addGroup($radio, 'selectMembership', NULL); |
| 1185 | } |
| 1186 | |
| 1187 | $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required'); |
| 1188 | } |
| 1189 | else { |
| 1190 | $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId); |
| 1191 | $this->assign('autoRenewOption', $autoRenewOption); |
| 1192 | } |
| 1193 | |
| 1194 | if ((!$this->_values['is_pay_later'] || is_array($this->_paymentProcessors)) && ($allowAutoRenewMembership || $autoRenewOption)) { |
| 1195 | $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.')); |
| 1196 | } |
| 1197 | |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | return $separateMembershipPayment; |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * Determine if recurring parameters need to be added to the form parameters. |
| 1206 | * |
| 1207 | * - is_recur |
| 1208 | * - frequency_interval |
| 1209 | * - frequency_unit |
| 1210 | * |
| 1211 | * For membership this is based on the membership type. |
| 1212 | * |
| 1213 | * This needs to be done before processing the pre-approval redirect where relevant on the main page or before any payment processing. |
| 1214 | * |
| 1215 | * Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout. |
| 1216 | */ |
| 1217 | protected function setRecurringMembershipParams() { |
| 1218 | $selectedMembershipTypeID = CRM_Utils_Array::value('selectMembership', $this->_params); |
| 1219 | if ($selectedMembershipTypeID) { |
| 1220 | // @todo the price_x fields will ALWAYS allow us to determine the membership - so we should ignore |
| 1221 | // 'selectMembership' and calculate from the price_x fields so we have one method that always works |
| 1222 | // this is lazy & only catches when selectMembership is set, but the worst of all worlds would be to fix |
| 1223 | // this with an else (calculate for price set). |
| 1224 | $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($this->_priceSetId); |
| 1225 | if (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_required']) |
| 1226 | || (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_optional']) && |
| 1227 | !empty($this->_params['is_recur'])) |
| 1228 | ) { |
| 1229 | $this->_params['auto_renew'] = TRUE; |
| 1230 | } |
| 1231 | } |
| 1232 | if ((!empty($this->_params['selectMembership']) || !empty($this->_params['priceSetId'])) |
| 1233 | && !empty($this->_paymentProcessor['is_recur']) && |
| 1234 | CRM_Utils_Array::value('auto_renew', $this->_params) |
| 1235 | && empty($this->_params['is_recur']) && empty($this->_params['frequency_interval']) |
| 1236 | ) { |
| 1237 | |
| 1238 | $this->_params['is_recur'] = $this->_values['is_recur'] = 1; |
| 1239 | // check if price set is not quick config |
| 1240 | if (!empty($this->_params['priceSetId']) && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config')) { |
| 1241 | list($this->_params['frequency_interval'], $this->_params['frequency_unit']) = CRM_Price_BAO_PriceSet::getRecurDetails($this->_params['priceSetId']); |
| 1242 | } |
| 1243 | else { |
| 1244 | // FIXME: set interval and unit based on selected membership type |
| 1245 | $this->_params['frequency_interval'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', |
| 1246 | $this->_params['selectMembership'], 'duration_interval' |
| 1247 | ); |
| 1248 | $this->_params['frequency_unit'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', |
| 1249 | $this->_params['selectMembership'], 'duration_unit' |
| 1250 | ); |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | } |