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