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