Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
f9a5ad43 | 37 | * This class generates form components for processing a contribution |
6a488035 TO |
38 | * |
39 | */ | |
40 | class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { | |
41 | ||
42 | /** | |
100fef9d | 43 | * The id of the contribution page that we are processsing |
6a488035 TO |
44 | * |
45 | * @var int | |
6a488035 TO |
46 | */ |
47 | public $_id; | |
48 | ||
49 | /** | |
100fef9d | 50 | * The mode that we are in |
6a488035 TO |
51 | * |
52 | * @var string | |
53 | * @protect | |
54 | */ | |
55 | public $_mode; | |
56 | ||
57 | /** | |
100fef9d | 58 | * The contact id related to a membership |
6a488035 TO |
59 | * |
60 | * @var int | |
6a488035 TO |
61 | */ |
62 | public $_membershipContactID; | |
63 | ||
64 | /** | |
100fef9d | 65 | * The values for the contribution db object |
6a488035 TO |
66 | * |
67 | * @var array | |
6a488035 TO |
68 | */ |
69 | public $_values; | |
70 | ||
71 | /** | |
100fef9d | 72 | * The paymentProcessor attributes for this page |
6a488035 TO |
73 | * |
74 | * @var array | |
6a488035 TO |
75 | */ |
76 | public $_paymentProcessor; | |
77 | public $_paymentObject = NULL; | |
78 | ||
79 | /** | |
80 | * The membership block for this page | |
81 | * | |
82 | * @var array | |
6a488035 TO |
83 | */ |
84 | public $_membershipBlock = NULL; | |
85 | ||
f64a217a EM |
86 | /** |
87 | * Does this form support a separate membership payment | |
88 | * @var bool | |
89 | */ | |
90 | protected $_separateMembershipPayment; | |
6a488035 | 91 | /** |
100fef9d | 92 | * The default values for the form |
6a488035 TO |
93 | * |
94 | * @var array | |
6a488035 TO |
95 | */ |
96 | protected $_defaults; | |
97 | ||
98 | /** | |
99 | * The params submitted by the form and computed by the app | |
100 | * | |
101 | * @var array | |
6a488035 TO |
102 | */ |
103 | public $_params; | |
104 | ||
105 | /** | |
106 | * The fields involved in this contribution page | |
107 | * | |
108 | * @var array | |
6a488035 | 109 | */ |
532ee86f | 110 | public $_fields = array(); |
6a488035 TO |
111 | |
112 | /** | |
113 | * The billing location id for this contribiution page | |
114 | * | |
115 | * @var int | |
6a488035 TO |
116 | */ |
117 | public $_bltID; | |
118 | ||
119 | /** | |
120 | * Cache the amount to make things easier | |
121 | * | |
122 | * @var float | |
6a488035 TO |
123 | */ |
124 | public $_amount; | |
125 | ||
126 | /** | |
100fef9d | 127 | * Pcp id |
6a488035 TO |
128 | * |
129 | * @var integer | |
6a488035 TO |
130 | */ |
131 | public $_pcpId; | |
132 | ||
133 | /** | |
100fef9d | 134 | * Pcp block |
6a488035 TO |
135 | * |
136 | * @var array | |
6a488035 TO |
137 | */ |
138 | public $_pcpBlock; | |
139 | ||
140 | /** | |
100fef9d | 141 | * Pcp info |
6a488035 TO |
142 | * |
143 | * @var array | |
6a488035 TO |
144 | */ |
145 | public $_pcpInfo; | |
146 | ||
5b757295 | 147 | /** |
148 | * The contact id of the person for whom membership is being added or renewed based on the cid in the url, | |
149 | * checksum, or session | |
0e5e0c2e | 150 | * @var int |
5b757295 | 151 | */ |
0e5e0c2e | 152 | public $_contactID; |
5b757295 | 153 | |
6a488035 TO |
154 | protected $_userID; |
155 | ||
156 | /** | |
100fef9d | 157 | * The Membership ID for membership renewal |
6a488035 TO |
158 | * |
159 | * @var int | |
6a488035 TO |
160 | */ |
161 | public $_membershipId; | |
162 | ||
163 | /** | |
164 | * Price Set ID, if the new price set method is used | |
165 | * | |
166 | * @var int | |
6a488035 TO |
167 | */ |
168 | public $_priceSetId; | |
169 | ||
170 | /** | |
171 | * Array of fields for the price set | |
172 | * | |
173 | * @var array | |
6a488035 TO |
174 | */ |
175 | public $_priceSet; | |
176 | ||
177 | public $_action; | |
178 | ||
874c9be7 | 179 | /** |
353ffa53 TO |
180 | * Is honor block is enabled for this contribution? |
181 | * | |
182 | * @var boolean | |
183 | */ | |
8af73472 | 184 | public $_honor_block_is_active = FALSE; |
185 | ||
dbddfb08 EM |
186 | /** |
187 | * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM | |
188 | * @var string | |
189 | */ | |
190 | public $_contributeMode; | |
191 | ||
192 | /** | |
100fef9d | 193 | * Contribution page supports memberships |
dbddfb08 EM |
194 | * @var boolean |
195 | */ | |
196 | public $_useForMember; | |
8ae4d0d3 | 197 | |
198 | public $_isBillingAddressRequiredForPayLater; | |
353ffa53 | 199 | |
6a488035 | 200 | /** |
fe482240 | 201 | * Set variables up before form is built. |
6a488035 | 202 | * |
7fe37828 EM |
203 | * @throws \CRM_Contribute_Exception_InactiveContributionPageException |
204 | * @throws \Exception | |
6a488035 TO |
205 | */ |
206 | public function preProcess() { | |
4b57bc9f | 207 | |
6a488035 TO |
208 | $config = CRM_Core_Config::singleton(); |
209 | $session = CRM_Core_Session::singleton(); | |
210 | ||
211 | // current contribution page id | |
212 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
213 | if (!$this->_id) { | |
d420cf02 DL |
214 | // seems like the session is corrupted and/or we lost the id trail |
215 | // lets just bump this to a regular session error and redirect user to main page | |
216 | $this->controller->invalidKeyRedirect(); | |
6a488035 | 217 | } |
d420cf02 | 218 | |
5b757295 | 219 | // this was used prior to the cleverer this_>getContactID - unsure now |
6a488035 | 220 | $this->_userID = $session->get('userID'); |
5b757295 | 221 | |
8af73472 | 222 | //Check if honor block is enabled for current contribution |
bc0e993a | 223 | $ufJoinParams = array( |
224 | 'module' => 'soft_credit', | |
225 | 'entity_table' => 'civicrm_contribution_page', | |
226 | 'entity_id' => $this->_id, | |
227 | ); | |
228 | $ufJoin = new CRM_Core_DAO_UFJoin(); | |
229 | $ufJoin->copyValues($ufJoinParams); | |
230 | $ufJoin->find(TRUE); | |
231 | $this->_honor_block_is_active = $ufJoin->is_active; | |
8af73472 | 232 | |
5b757295 | 233 | $this->_contactID = $this->_membershipContactID = $this->getContactID(); |
6a488035 | 234 | $this->_mid = NULL; |
5b757295 | 235 | if ($this->_contactID) { |
6a488035 TO |
236 | $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this); |
237 | if ($this->_mid) { | |
238 | $membership = new CRM_Member_DAO_Membership(); | |
239 | $membership->id = $this->_mid; | |
240 | ||
241 | if ($membership->find(TRUE)) { | |
242 | $this->_defaultMemTypeId = $membership->membership_type_id; | |
5b757295 | 243 | if ($membership->contact_id != $this->_contactID) { |
6fe8deba | 244 | $validMembership = FALSE; |
6a488035 | 245 | $employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($this->_userID); |
6fe8deba DS |
246 | if (!empty($employers) && array_key_exists($membership->contact_id, $employers)) { |
247 | $this->_membershipContactID = $membership->contact_id; | |
248 | $this->assign('membershipContactID', $this->_membershipContactID); | |
249 | $this->assign('membershipContactName', $employers[$this->_membershipContactID]['name']); | |
250 | $validMembership = TRUE; | |
0db6c3e1 TO |
251 | } |
252 | else { | |
51e89def DS |
253 | $membershipType = new CRM_Member_BAO_MembershipType(); |
254 | $membershipType->id = $membership->membership_type_id; | |
255 | if ($membershipType->find(TRUE)) { | |
f9f0eff9 DG |
256 | // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values. |
257 | // Convert to commma separated list. | |
258 | $inheritedRelTypes = implode(CRM_Utils_Array::explodePadded($membershipType->relationship_type_id), ','); | |
51e89def DS |
259 | $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, $membershipType->relationship_type_id); |
260 | if (array_key_exists($membership->contact_id, $permContacts)) { | |
261 | $this->_membershipContactID = $membership->contact_id; | |
6fe8deba | 262 | $validMembership = TRUE; |
51e89def DS |
263 | } |
264 | } | |
6a488035 | 265 | } |
6fe8deba DS |
266 | if (!$validMembership) { |
267 | 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'); | |
268 | } | |
6a488035 TO |
269 | } |
270 | } | |
271 | else { | |
272 | 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'); | |
273 | } | |
274 | unset($membership); | |
275 | } | |
276 | } | |
277 | ||
278 | // we do not want to display recently viewed items, so turn off | |
279 | $this->assign('displayRecent', FALSE); | |
280 | // Contribution page values are cleared from session, so can't use normal Printer Friendly view. | |
281 | // Use Browser Print instead. | |
282 | $this->assign('browserPrint', TRUE); | |
283 | ||
284 | // action | |
285 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
286 | $this->assign('action', $this->_action); | |
287 | ||
288 | // current mode | |
289 | $this->_mode = ($this->_action == 1024) ? 'test' : 'live'; | |
290 | ||
291 | $this->_values = $this->get('values'); | |
292 | $this->_fields = $this->get('fields'); | |
293 | $this->_bltID = $this->get('bltID'); | |
294 | $this->_paymentProcessor = $this->get('paymentProcessor'); | |
295 | $this->_priceSetId = $this->get('priceSetId'); | |
296 | $this->_priceSet = $this->get('priceSet'); | |
297 | ||
298 | if (!$this->_values) { | |
299 | // get all the values from the dao object | |
300 | $this->_values = array(); | |
301 | $this->_fields = array(); | |
302 | ||
303 | CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values); | |
304 | ||
a7488080 | 305 | if (empty($this->_values['is_active'])) { |
4b57bc9f | 306 | throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id); |
6a488035 TO |
307 | } |
308 | ||
309 | // also check for billing informatin | |
310 | // get the billing location type | |
180409a4 | 311 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate'); |
6a488035 TO |
312 | // CRM-8108 remove ts around Billing location type |
313 | //$this->_bltID = array_search( ts('Billing'), $locationTypes ); | |
314 | $this->_bltID = array_search('Billing', $locationTypes); | |
315 | if (!$this->_bltID) { | |
316 | CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing'))); | |
317 | } | |
318 | $this->set('bltID', $this->_bltID); | |
319 | ||
320 | // check for is_monetary status | |
321 | $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values); | |
322 | $isPayLater = CRM_Utils_Array::value('is_pay_later', $this->_values); | |
323 | ||
324 | //FIXME: to support multiple payment processors | |
325 | if ($isMonetary && | |
8cc574cf | 326 | (!$isPayLater || !empty($this->_values['payment_processor'])) |
6a488035 TO |
327 | ) { |
328 | $ppID = CRM_Utils_Array::value('payment_processor', $this->_values); | |
329 | if (!$ppID) { | |
330 | CRM_Core_Error::fatal(ts('A payment processor must be selected for this contribution page (contact the site administrator for assistance).')); | |
331 | } | |
332 | ||
333 | $ppIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ppID); | |
334 | $this->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPayments($ppIds, $this->_mode); | |
335 | ||
336 | $this->set('paymentProcessors', $this->_paymentProcessors); | |
337 | ||
338 | //set default payment processor | |
339 | if (!empty($this->_paymentProcessors) && empty($this->_paymentProcessor)) { | |
340 | foreach ($this->_paymentProcessors as $ppId => $values) { | |
341 | if ($values['is_default'] == 1 || (count($this->_paymentProcessors) == 1)) { | |
342 | $defaultProcessorId = $ppId; | |
343 | break; | |
344 | } | |
345 | } | |
346 | } | |
347 | ||
348 | if (isset($defaultProcessorId)) { | |
349 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($defaultProcessorId, $this->_mode); | |
350 | $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor); | |
351 | } | |
352 | ||
353 | if (!CRM_Utils_System::isNull($this->_paymentProcessors)) { | |
354 | foreach ($this->_paymentProcessors as $eachPaymentProcessor) { | |
355 | // check selected payment processor is active | |
356 | if (empty($eachPaymentProcessor)) { | |
357 | CRM_Core_Error::fatal(ts('A payment processor configured for this page might be disabled (contact the site administrator for assistance).')); | |
358 | } | |
359 | ||
360 | // ensure that processor has a valid config | |
361 | $this->_paymentObject = &CRM_Core_Payment::singleton($this->_mode, $eachPaymentProcessor, $this); | |
362 | $error = $this->_paymentObject->checkConfig(); | |
363 | if (!empty($error)) { | |
364 | CRM_Core_Error::fatal($error); | |
365 | } | |
366 | } | |
367 | } | |
368 | } | |
369 | ||
370 | // get price info | |
371 | // CRM-5095 | |
9da8dc8c | 372 | CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page'); |
6a488035 TO |
373 | |
374 | // this avoids getting E_NOTICE errors in php | |
375 | $setNullFields = array( | |
376 | 'amount_block_is_active', | |
6a488035 TO |
377 | 'is_allow_other_amount', |
378 | 'footer_text', | |
379 | ); | |
380 | foreach ($setNullFields as $f) { | |
381 | if (!isset($this->_values[$f])) { | |
382 | $this->_values[$f] = NULL; | |
383 | } | |
384 | } | |
385 | ||
386 | //check if Membership Block is enabled, if Membership Fields are included in profile | |
387 | //get membership section for this contribution page | |
388 | $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id); | |
389 | $this->set('membershipBlock', $this->_membershipBlock); | |
390 | ||
391 | if ($this->_values['custom_pre_id']) { | |
392 | $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']); | |
393 | } | |
394 | ||
395 | if ($this->_values['custom_post_id']) { | |
396 | $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']); | |
397 | } | |
398 | ||
399 | if (((isset($postProfileType) && $postProfileType == 'Membership') || | |
400 | (isset($preProfileType) && $preProfileType == 'Membership') | |
401 | ) && | |
402 | !$this->_membershipBlock['is_active'] | |
403 | ) { | |
404 | 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.')); | |
405 | } | |
406 | ||
407 | $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id); | |
408 | ||
409 | if ($pledgeBlock) { | |
410 | $this->_values['pledge_block_id'] = CRM_Utils_Array::value('id', $pledgeBlock); | |
411 | $this->_values['max_reminders'] = CRM_Utils_Array::value('max_reminders', $pledgeBlock); | |
412 | $this->_values['initial_reminder_day'] = CRM_Utils_Array::value('initial_reminder_day', $pledgeBlock); | |
413 | $this->_values['additional_reminder_day'] = CRM_Utils_Array::value('additional_reminder_day', $pledgeBlock); | |
414 | ||
415 | //set pledge id in values | |
416 | $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this); | |
417 | ||
418 | //authenticate pledge user for pledge payment. | |
419 | if ($pledgeId) { | |
420 | $this->_values['pledge_id'] = $pledgeId; | |
421 | ||
422 | //lets override w/ pledge campaign. | |
423 | $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', | |
424 | $pledgeId, | |
425 | 'campaign_id' | |
426 | ); | |
427 | self::authenticatePledgeUser(); | |
428 | } | |
429 | } | |
430 | $this->set('values', $this->_values); | |
431 | $this->set('fields', $this->_fields); | |
432 | } | |
433 | ||
434 | // Handle PCP | |
435 | $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this); | |
436 | if ($pcpId) { | |
353ffa53 TO |
437 | $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values); |
438 | $this->_pcpId = $pcp['pcpId']; | |
6a488035 | 439 | $this->_pcpBlock = $pcp['pcpBlock']; |
353ffa53 | 440 | $this->_pcpInfo = $pcp['pcpInfo']; |
6a488035 TO |
441 | } |
442 | ||
443 | // Link (button) for users to create their own Personal Campaign page | |
444 | if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) { | |
445 | $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign', | |
446 | "action=add&reset=1&pageId={$this->_id}&component=contribute", | |
447 | FALSE, NULL, TRUE | |
448 | ); | |
449 | $this->assign('linkTextUrl', $linkTextUrl); | |
450 | $this->assign('linkText', $linkText); | |
451 | } | |
452 | ||
453 | //set pledge block if block id is set | |
a7488080 | 454 | if (!empty($this->_values['pledge_block_id'])) { |
6a488035 TO |
455 | $this->assign('pledgeBlock', TRUE); |
456 | } | |
457 | ||
458 | // check if one of the (amount , membership) bloks is active or not | |
459 | $this->_membershipBlock = $this->get('membershipBlock'); | |
460 | ||
461 | if (!$this->_values['amount_block_is_active'] && | |
462 | !$this->_membershipBlock['is_active'] && | |
463 | !$this->_priceSetId | |
464 | ) { | |
465 | 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.')); | |
466 | } | |
467 | ||
468 | if ($this->_values['amount_block_is_active']) { | |
469 | $this->set('amount_block_is_active', $this->_values['amount_block_is_active']); | |
470 | } | |
471 | ||
472 | $this->_contributeMode = $this->get('contributeMode'); | |
473 | $this->assign('contributeMode', $this->_contributeMode); | |
474 | ||
475 | //assigning is_monetary and is_email_receipt to template | |
476 | $this->assign('is_monetary', $this->_values['is_monetary']); | |
477 | $this->assign('is_email_receipt', $this->_values['is_email_receipt']); | |
478 | $this->assign('bltID', $this->_bltID); | |
479 | ||
480 | //assign cancelSubscription URL to templates | |
481 | $this->assign('cancelSubscriptionUrl', | |
482 | CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values) | |
483 | ); | |
484 | ||
485 | // assigning title to template in case someone wants to use it, also setting CMS page title | |
486 | if ($this->_pcpId) { | |
487 | $this->assign('title', $this->_pcpInfo['title']); | |
488 | CRM_Utils_System::setTitle($this->_pcpInfo['title']); | |
489 | } | |
490 | else { | |
491 | $this->assign('title', $this->_values['title']); | |
492 | CRM_Utils_System::setTitle($this->_values['title']); | |
493 | } | |
494 | $this->_defaults = array(); | |
495 | ||
496 | $this->_amount = $this->get('amount'); | |
497 | ||
498 | //CRM-6907 | |
499 | $config = CRM_Core_Config::singleton(); | |
500 | $config->defaultCurrency = CRM_Utils_Array::value('currency', | |
501 | $this->_values, | |
502 | $config->defaultCurrency | |
503 | ); | |
504 | ||
505 | //lets allow user to override campaign. | |
506 | $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this); | |
507 | if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) { | |
508 | $this->_values['campaign_id'] = $campID; | |
509 | } | |
510 | ||
511 | //do check for cancel recurring and clean db, CRM-7696 | |
512 | if (CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject)) { | |
513 | self::cancelRecurring(); | |
514 | } | |
8ae4d0d3 | 515 | |
516 | // check if billing block is required for pay later | |
517 | if (CRM_Utils_Array::value('is_pay_later', $this->_values)) { | |
518 | $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values); | |
519 | $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater); | |
520 | } | |
6a488035 TO |
521 | } |
522 | ||
523 | /** | |
fe482240 | 524 | * Set the default values. |
6a488035 TO |
525 | * |
526 | * @return void | |
6a488035 | 527 | */ |
00be9182 | 528 | public function setDefaultValues() { |
6a488035 TO |
529 | return $this->_defaults; |
530 | } | |
531 | ||
532 | /** | |
fe482240 | 533 | * Assign the minimal set of variables to the template. |
6a488035 TO |
534 | * |
535 | * @return void | |
6a488035 | 536 | */ |
00be9182 | 537 | public function assignToTemplate() { |
6a488035 | 538 | $name = CRM_Utils_Array::value('billing_first_name', $this->_params); |
a7488080 | 539 | if (!empty($this->_params['billing_middle_name'])) { |
6a488035 TO |
540 | $name .= " {$this->_params['billing_middle_name']}"; |
541 | } | |
542 | $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params); | |
543 | $name = trim($name); | |
544 | $this->assign('billingName', $name); | |
545 | $this->set('name', $name); | |
546 | ||
547 | $this->assign('paymentProcessor', $this->_paymentProcessor); | |
548 | $vars = array( | |
353ffa53 TO |
549 | 'amount', |
550 | 'currencyID', | |
551 | 'credit_card_type', | |
552 | 'trxn_id', | |
553 | 'amount_level', | |
6a488035 TO |
554 | ); |
555 | ||
556 | $config = CRM_Core_Config::singleton(); | |
8cc574cf | 557 | if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) { |
6a488035 TO |
558 | $this->assign('is_recur_enabled', 1); |
559 | $vars = array_merge($vars, array( | |
353ffa53 TO |
560 | 'is_recur', |
561 | 'frequency_interval', | |
562 | 'frequency_unit', | |
563 | 'installments', | |
564 | )); | |
6a488035 TO |
565 | } |
566 | ||
567 | if (in_array('CiviPledge', $config->enableComponents) && | |
568 | CRM_Utils_Array::value('is_pledge', $this->_params) == 1 | |
569 | ) { | |
570 | $this->assign('pledge_enabled', 1); | |
571 | ||
572 | $vars = array_merge($vars, array( | |
573 | 'is_pledge', | |
353ffa53 TO |
574 | 'pledge_frequency_interval', |
575 | 'pledge_frequency_unit', | |
576 | 'pledge_installments', | |
577 | )); | |
6a488035 TO |
578 | } |
579 | ||
580 | if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) { | |
581 | $this->_params['amount_level'] = ''; | |
582 | } | |
583 | ||
584 | foreach ($vars as $v) { | |
3fb990f4 | 585 | if (isset($this->_params[$v])) { |
735fe42d PJ |
586 | if ($v == "amount" && $this->_params[$v] === 0) { |
587 | $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE); | |
3fb990f4 | 588 | } |
6a488035 TO |
589 | $this->assign($v, $this->_params[$v]); |
590 | } | |
591 | } | |
592 | ||
593 | // assign the address formatted up for display | |
594 | $addressParts = array( | |
595 | "street_address-{$this->_bltID}", | |
596 | "city-{$this->_bltID}", | |
597 | "postal_code-{$this->_bltID}", | |
598 | "state_province-{$this->_bltID}", | |
599 | "country-{$this->_bltID}", | |
600 | ); | |
601 | ||
602 | $addressFields = array(); | |
603 | foreach ($addressParts as $part) { | |
604 | list($n, $id) = explode('-', $part); | |
605 | $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $this->_params); | |
606 | } | |
607 | ||
608 | $this->assign('address', CRM_Utils_Address::format($addressFields)); | |
609 | ||
a7488080 | 610 | if (!empty($this->_params['hidden_onbehalf_profile'])) { |
6a488035 TO |
611 | $this->assign('onBehalfName', $this->_params['organization_name']); |
612 | $locTypeId = array_keys($this->_params['onbehalf_location']['email']); | |
613 | $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']); | |
614 | } | |
615 | ||
616 | //fix for CRM-3767 | |
617 | $assignCCInfo = FALSE; | |
618 | if ($this->_amount > 0.0) { | |
619 | $assignCCInfo = TRUE; | |
620 | } | |
a7488080 | 621 | elseif (!empty($this->_params['selectMembership'])) { |
6a488035 TO |
622 | $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee'); |
623 | if ($memFee > 0.0) { | |
624 | $assignCCInfo = TRUE; | |
625 | } | |
626 | } | |
627 | ||
628 | if ($this->_contributeMode == 'direct' && $assignCCInfo) { | |
f92fc7eb CW |
629 | if ($this->_paymentProcessor && |
630 | $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT | |
631 | ) { | |
6a488035 TO |
632 | $this->assign('account_holder', $this->_params['account_holder']); |
633 | $this->assign('bank_identification_number', $this->_params['bank_identification_number']); | |
634 | $this->assign('bank_name', $this->_params['bank_name']); | |
635 | $this->assign('bank_account_number', $this->_params['bank_account_number']); | |
636 | } | |
637 | else { | |
638 | $date = CRM_Utils_Date::format(CRM_Utils_array::value('credit_card_exp_date', $this->_params)); | |
639 | $date = CRM_Utils_Date::mysqlToIso($date); | |
640 | $this->assign('credit_card_exp_date', $date); | |
641 | $this->assign('credit_card_number', | |
353ffa53 | 642 | CRM_Utils_System::mungeCreditCard(CRM_Utils_array::value('credit_card_number', $this->_params)) |
6a488035 TO |
643 | ); |
644 | } | |
645 | } | |
646 | ||
647 | $this->assign('email', | |
648 | $this->controller->exportValue('Main', "email-{$this->_bltID}") | |
649 | ); | |
650 | ||
651 | // also assign the receipt_text | |
652 | if (isset($this->_values['receipt_text'])) { | |
653 | $this->assign('receipt_text', $this->_values['receipt_text']); | |
654 | } | |
655 | } | |
656 | ||
657 | /** | |
fe482240 | 658 | * Add the custom fields. |
6a488035 | 659 | * |
100fef9d CW |
660 | * @param int $id |
661 | * @param string $name | |
f4aaa82a EM |
662 | * @param bool $viewOnly |
663 | * @param null $profileContactType | |
664 | * @param null $fieldTypes | |
665 | * | |
355ba699 | 666 | * @return void |
6a488035 | 667 | */ |
00be9182 | 668 | public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) { |
6a488035 | 669 | if ($id) { |
da8d9879 | 670 | $contactID = $this->getContactID(); |
6a488035 TO |
671 | |
672 | // we don't allow conflicting fields to be | |
673 | // configured via profile - CRM 2100 | |
674 | $fieldsToIgnore = array( | |
675 | 'receive_date' => 1, | |
676 | 'trxn_id' => 1, | |
677 | 'invoice_id' => 1, | |
678 | 'net_amount' => 1, | |
679 | 'fee_amount' => 1, | |
680 | 'non_deductible_amount' => 1, | |
681 | 'total_amount' => 1, | |
682 | 'amount_level' => 1, | |
683 | 'contribution_status_id' => 1, | |
684 | 'payment_instrument' => 1, | |
685 | 'check_number' => 1, | |
686 | 'financial_type' => 1, | |
687 | ); | |
688 | ||
689 | $fields = NULL; | |
690 | if ($contactID && CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) { | |
691 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE, | |
692 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL | |
693 | ); | |
694 | } | |
695 | else { | |
696 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE, | |
697 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL | |
698 | ); | |
699 | } | |
700 | ||
701 | if ($fields) { | |
702 | // unset any email-* fields since we already collect it, CRM-2888 | |
703 | foreach (array_keys($fields) as $fieldName) { | |
8af73472 | 704 | if (substr($fieldName, 0, 6) == 'email-' && $profileContactType != 'honor') { |
6a488035 TO |
705 | unset($fields[$fieldName]); |
706 | } | |
707 | } | |
708 | ||
709 | if (array_intersect_key($fields, $fieldsToIgnore)) { | |
710 | $fields = array_diff_key($fields, $fieldsToIgnore); | |
711 | CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert'); | |
712 | } | |
713 | ||
714 | $fields = array_diff_assoc($fields, $this->_fields); | |
715 | ||
716 | CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID); | |
717 | $addCaptcha = FALSE; | |
718 | foreach ($fields as $key => $field) { | |
719 | if ($viewOnly && | |
720 | isset($field['data_type']) && | |
721 | $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL') | |
722 | ) { | |
723 | // ignore file upload fields | |
724 | continue; | |
725 | } | |
726 | ||
133e2c99 | 727 | if ($profileContactType) { |
728 | //Since we are showing honoree name separately so we are removing it from honoree profile just for display | |
353ffa53 TO |
729 | $honoreeNamefields = array( |
730 | 'prefix_id', | |
731 | 'first_name', | |
732 | 'last_name', | |
733 | 'suffix_id', | |
734 | 'organization_name', | |
389bcebf | 735 | 'household_name', |
353ffa53 | 736 | ); |
133e2c99 | 737 | if ($profileContactType == 'honor' && in_array($field['name'], $honoreeNamefields)) { |
738 | unset($fields[$field['name']]); | |
739 | continue; | |
740 | } | |
6a488035 TO |
741 | if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) { |
742 | CRM_Core_BAO_UFGroup::buildProfile( | |
743 | $this, | |
744 | $field, | |
745 | CRM_Profile_Form::MODE_CREATE, | |
746 | $contactID, | |
133e2c99 | 747 | TRUE, |
748 | $profileContactType | |
6a488035 | 749 | ); |
133e2c99 | 750 | $this->_fields[$profileContactType][$key] = $field; |
6a488035 TO |
751 | } |
752 | else { | |
753 | unset($fields[$key]); | |
754 | } | |
755 | } | |
756 | else { | |
757 | CRM_Core_BAO_UFGroup::buildProfile( | |
758 | $this, | |
759 | $field, | |
760 | CRM_Profile_Form::MODE_CREATE, | |
761 | $contactID, | |
762 | TRUE | |
763 | ); | |
764 | $this->_fields[$key] = $field; | |
765 | } | |
71fc6ea4 DG |
766 | // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor |
767 | if ($field['add_captcha'] && !$this->_userID) { | |
6a488035 TO |
768 | $addCaptcha = TRUE; |
769 | } | |
770 | } | |
771 | ||
772 | $this->assign($name, $fields); | |
773 | ||
6a488035 TO |
774 | if ($addCaptcha && !$viewOnly) { |
775 | $captcha = CRM_Utils_ReCAPTCHA::singleton(); | |
776 | $captcha->add($this); | |
777 | $this->assign('isCaptcha', TRUE); | |
778 | } | |
779 | } | |
780 | } | |
781 | } | |
782 | ||
f4aaa82a | 783 | /** |
fe482240 | 784 | * Check template file exists. |
f4aaa82a EM |
785 | * @param null $suffix |
786 | * | |
787 | * @return null|string | |
788 | */ | |
00be9182 | 789 | public function checkTemplateFileExists($suffix = NULL) { |
6a488035 TO |
790 | if ($this->_id) { |
791 | $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl"; | |
792 | $template = CRM_Core_Form::getTemplate(); | |
793 | if ($template->template_exists($templateFile)) { | |
794 | return $templateFile; | |
795 | } | |
796 | } | |
797 | return NULL; | |
798 | } | |
799 | ||
186c9c17 | 800 | /** |
fe482240 | 801 | * Use the form name to create the tpl file name. |
186c9c17 EM |
802 | * |
803 | * @return string | |
186c9c17 EM |
804 | */ |
805 | /** | |
806 | * @return string | |
807 | */ | |
00be9182 | 808 | public function getTemplateFileName() { |
6a488035 TO |
809 | $fileName = $this->checkTemplateFileExists(); |
810 | return $fileName ? $fileName : parent::getTemplateFileName(); | |
811 | } | |
812 | ||
186c9c17 EM |
813 | /** |
814 | * Default extra tpl file basically just replaces .tpl with .extra.tpl | |
815 | * i.e. we dont override | |
816 | * | |
817 | * @return string | |
186c9c17 EM |
818 | */ |
819 | /** | |
820 | * @return string | |
821 | */ | |
00be9182 | 822 | public function overrideExtraTemplateFileName() { |
6a488035 TO |
823 | $fileName = $this->checkTemplateFileExists('extra.'); |
824 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); | |
825 | } | |
826 | ||
827 | /** | |
100fef9d | 828 | * Authenticate pledge user during online payment. |
6a488035 | 829 | * |
6a488035 | 830 | * |
355ba699 | 831 | * @return void |
6a488035 TO |
832 | */ |
833 | public function authenticatePledgeUser() { | |
834 | //get the userChecksum and contact id | |
835 | $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this); | |
836 | $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
837 | ||
838 | //get pledge status and contact id | |
353ffa53 TO |
839 | $pledgeValues = array(); |
840 | $pledgeParams = array('id' => $this->_values['pledge_id']); | |
6a488035 TO |
841 | $returnProperties = array('contact_id', 'status_id'); |
842 | CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties); | |
843 | ||
844 | //get all status | |
845 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
353ffa53 TO |
846 | $validStatus = array( |
847 | array_search('Pending', $allStatus), | |
6a488035 TO |
848 | array_search('In Progress', $allStatus), |
849 | array_search('Overdue', $allStatus), | |
850 | ); | |
851 | ||
852 | $validUser = FALSE; | |
853 | if ($this->_userID && | |
854 | $this->_userID == $pledgeValues['contact_id'] | |
855 | ) { | |
856 | //check for authenticated user. | |
857 | $validUser = TRUE; | |
858 | } | |
859 | elseif ($userChecksum && $pledgeValues['contact_id']) { | |
860 | //check for anonymous user. | |
861 | $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum); | |
862 | ||
863 | //make sure cid is same as pledge contact id | |
864 | if ($validUser && ($pledgeValues['contact_id'] != $contactID)) { | |
865 | $validUser = FALSE; | |
866 | } | |
867 | } | |
868 | ||
869 | if (!$validUser) { | |
870 | 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.")); | |
871 | } | |
872 | ||
873 | //check for valid pledge status. | |
874 | if (!in_array($pledgeValues['status_id'], $validStatus)) { | |
875 | 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)))); | |
876 | } | |
877 | } | |
878 | ||
879 | /** | |
880 | * In case user cancel recurring contribution, | |
881 | * When we get the control back from payment gate way | |
882 | * lets delete the recurring and related contribution. | |
883 | * | |
389bcebf | 884 | */ |
6a488035 TO |
885 | public function cancelRecurring() { |
886 | $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject); | |
887 | if ($isCancel) { | |
888 | $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean', CRM_Core_DAO::$_nullObject); | |
889 | $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive', CRM_Core_DAO::$_nullObject); | |
890 | //clean db for recurring contribution. | |
891 | if ($isRecur && $recurId) { | |
892 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId); | |
893 | } | |
894 | $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive', CRM_Core_DAO::$_nullObject); | |
895 | if ($contribId) { | |
896 | CRM_Contribute_BAO_Contribution::deleteContribution($contribId); | |
897 | } | |
898 | } | |
899 | } | |
96025800 | 900 | |
6a488035 | 901 | } |