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