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