Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
f92d1e2a | 35 | * This class generates form components for processing a contribution. |
6a488035 TO |
36 | */ |
37 | class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { | |
38 | ||
39 | /** | |
f92d1e2a | 40 | * The id of the contribution page that we are processing. |
6a488035 TO |
41 | * |
42 | * @var int | |
6a488035 TO |
43 | */ |
44 | public $_id; | |
45 | ||
46 | /** | |
100fef9d | 47 | * The mode that we are in |
6a488035 TO |
48 | * |
49 | * @var string | |
50 | * @protect | |
51 | */ | |
52 | public $_mode; | |
53 | ||
54 | /** | |
100fef9d | 55 | * The contact id related to a membership |
6a488035 TO |
56 | * |
57 | * @var int | |
6a488035 TO |
58 | */ |
59 | public $_membershipContactID; | |
60 | ||
61 | /** | |
100fef9d | 62 | * The values for the contribution db object |
6a488035 TO |
63 | * |
64 | * @var array | |
6a488035 TO |
65 | */ |
66 | public $_values; | |
67 | ||
68 | /** | |
100fef9d | 69 | * The paymentProcessor attributes for this page |
6a488035 TO |
70 | * |
71 | * @var array | |
6a488035 TO |
72 | */ |
73 | public $_paymentProcessor; | |
1b9f9ca3 | 74 | |
6a488035 TO |
75 | public $_paymentObject = NULL; |
76 | ||
77 | /** | |
78 | * The membership block for this page | |
79 | * | |
80 | * @var array | |
6a488035 TO |
81 | */ |
82 | public $_membershipBlock = NULL; | |
83 | ||
f64a217a EM |
84 | /** |
85 | * Does this form support a separate membership payment | |
86 | * @var bool | |
87 | */ | |
88 | protected $_separateMembershipPayment; | |
6a488035 TO |
89 | |
90 | /** | |
91 | * The params submitted by the form and computed by the app | |
92 | * | |
93 | * @var array | |
6a488035 | 94 | */ |
90102a32 | 95 | public $_params = array(); |
6a488035 TO |
96 | |
97 | /** | |
98 | * The fields involved in this contribution page | |
99 | * | |
100 | * @var array | |
6a488035 | 101 | */ |
532ee86f | 102 | public $_fields = array(); |
6a488035 TO |
103 | |
104 | /** | |
f92d1e2a | 105 | * The billing location id for this contribution page. |
6a488035 TO |
106 | * |
107 | * @var int | |
6a488035 TO |
108 | */ |
109 | public $_bltID; | |
110 | ||
111 | /** | |
112 | * Cache the amount to make things easier | |
113 | * | |
114 | * @var float | |
6a488035 TO |
115 | */ |
116 | public $_amount; | |
117 | ||
118 | /** | |
100fef9d | 119 | * Pcp id |
6a488035 TO |
120 | * |
121 | * @var integer | |
6a488035 TO |
122 | */ |
123 | public $_pcpId; | |
124 | ||
125 | /** | |
100fef9d | 126 | * Pcp block |
6a488035 TO |
127 | * |
128 | * @var array | |
6a488035 TO |
129 | */ |
130 | public $_pcpBlock; | |
131 | ||
132 | /** | |
100fef9d | 133 | * Pcp info |
6a488035 TO |
134 | * |
135 | * @var array | |
6a488035 TO |
136 | */ |
137 | public $_pcpInfo; | |
138 | ||
5b757295 | 139 | /** |
140 | * The contact id of the person for whom membership is being added or renewed based on the cid in the url, | |
141 | * checksum, or session | |
0e5e0c2e | 142 | * @var int |
5b757295 | 143 | */ |
0e5e0c2e | 144 | public $_contactID; |
5b757295 | 145 | |
6a488035 TO |
146 | protected $_userID; |
147 | ||
148 | /** | |
100fef9d | 149 | * The Membership ID for membership renewal |
6a488035 TO |
150 | * |
151 | * @var int | |
6a488035 TO |
152 | */ |
153 | public $_membershipId; | |
154 | ||
155 | /** | |
156 | * Price Set ID, if the new price set method is used | |
157 | * | |
158 | * @var int | |
6a488035 TO |
159 | */ |
160 | public $_priceSetId; | |
161 | ||
162 | /** | |
163 | * Array of fields for the price set | |
164 | * | |
165 | * @var array | |
6a488035 TO |
166 | */ |
167 | public $_priceSet; | |
168 | ||
169 | public $_action; | |
170 | ||
dbddfb08 EM |
171 | /** |
172 | * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM | |
173 | * @var string | |
174 | */ | |
175 | public $_contributeMode; | |
176 | ||
177 | /** | |
100fef9d | 178 | * Contribution page supports memberships |
dbddfb08 EM |
179 | * @var boolean |
180 | */ | |
181 | public $_useForMember; | |
8ae4d0d3 | 182 | |
0f2b049e | 183 | /** |
184 | * @deprecated | |
185 | * | |
186 | * @var | |
187 | */ | |
8ae4d0d3 | 188 | public $_isBillingAddressRequiredForPayLater; |
353ffa53 | 189 | |
6a488035 | 190 | /** |
fe482240 | 191 | * Set variables up before form is built. |
6a488035 | 192 | * |
7fe37828 EM |
193 | * @throws \CRM_Contribute_Exception_InactiveContributionPageException |
194 | * @throws \Exception | |
6a488035 TO |
195 | */ |
196 | public function preProcess() { | |
6a488035 TO |
197 | |
198 | // current contribution page id | |
199 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
37e877b2 | 200 | $this->_ccid = CRM_Utils_Request::retrieve('ccid', 'Positive', $this); |
6a488035 | 201 | if (!$this->_id) { |
d420cf02 DL |
202 | // seems like the session is corrupted and/or we lost the id trail |
203 | // lets just bump this to a regular session error and redirect user to main page | |
204 | $this->controller->invalidKeyRedirect(); | |
6a488035 | 205 | } |
d420cf02 | 206 | |
5b757295 | 207 | // this was used prior to the cleverer this_>getContactID - unsure now |
a6c15c46 | 208 | $this->_userID = CRM_Core_Session::singleton()->get('userID'); |
5b757295 | 209 | |
210 | $this->_contactID = $this->_membershipContactID = $this->getContactID(); | |
6a488035 | 211 | $this->_mid = NULL; |
5b757295 | 212 | if ($this->_contactID) { |
6a488035 TO |
213 | $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this); |
214 | if ($this->_mid) { | |
215 | $membership = new CRM_Member_DAO_Membership(); | |
216 | $membership->id = $this->_mid; | |
217 | ||
218 | if ($membership->find(TRUE)) { | |
219 | $this->_defaultMemTypeId = $membership->membership_type_id; | |
5b757295 | 220 | if ($membership->contact_id != $this->_contactID) { |
6fe8deba | 221 | $validMembership = FALSE; |
33260076 | 222 | $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, NULL, NULL, 'Organization'); |
223 | if (!empty($organizations) && array_key_exists($membership->contact_id, $organizations)) { | |
6fe8deba DS |
224 | $this->_membershipContactID = $membership->contact_id; |
225 | $this->assign('membershipContactID', $this->_membershipContactID); | |
33260076 | 226 | $this->assign('membershipContactName', $organizations[$this->_membershipContactID]['name']); |
6fe8deba | 227 | $validMembership = TRUE; |
0db6c3e1 TO |
228 | } |
229 | else { | |
51e89def DS |
230 | $membershipType = new CRM_Member_BAO_MembershipType(); |
231 | $membershipType->id = $membership->membership_type_id; | |
232 | if ($membershipType->find(TRUE)) { | |
f9f0eff9 | 233 | // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values. |
f92d1e2a | 234 | // Convert to comma separated list. |
f9f0eff9 | 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); | |
40c655aa | 281 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() |
66af7c48 PN |
282 | && !CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id'])) |
283 | ) { | |
0799e1c4 E |
284 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
285 | } | |
a7488080 | 286 | if (empty($this->_values['is_active'])) { |
4b57bc9f | 287 | throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id); |
6a488035 TO |
288 | } |
289 | ||
8345c9d3 | 290 | $this->assignBillingType(); |
6a488035 TO |
291 | |
292 | // check for is_monetary status | |
293 | $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values); | |
294 | $isPayLater = CRM_Utils_Array::value('is_pay_later', $this->_values); | |
24cf7458 | 295 | if (!empty($this->_ccid)) { |
296 | $this->_values['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
297 | $this->_ccid, | |
298 | 'financial_type_id' | |
299 | ); | |
300 | if ($isPayLater) { | |
301 | $isPayLater = FALSE; | |
302 | $this->_values['is_pay_later'] = FALSE; | |
303 | } | |
37e877b2 | 304 | } |
6a488035 | 305 | |
6a488035 | 306 | if ($isMonetary && |
8cc574cf | 307 | (!$isPayLater || !empty($this->_values['payment_processor'])) |
6a488035 | 308 | ) { |
a6c15c46 EM |
309 | $this->_paymentProcessorIDs = explode( |
310 | CRM_Core_DAO::VALUE_SEPARATOR, | |
311 | CRM_Utils_Array::value('payment_processor', $this->_values) | |
1b9f9ca3 | 312 | ); |
f92d1e2a | 313 | |
cbcb5b49 | 314 | $this->assignPaymentProcessor($isPayLater); |
6a488035 TO |
315 | } |
316 | ||
317 | // get price info | |
318 | // CRM-5095 | |
9da8dc8c | 319 | CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page'); |
6a488035 TO |
320 | |
321 | // this avoids getting E_NOTICE errors in php | |
322 | $setNullFields = array( | |
323 | 'amount_block_is_active', | |
6a488035 TO |
324 | 'is_allow_other_amount', |
325 | 'footer_text', | |
326 | ); | |
327 | foreach ($setNullFields as $f) { | |
328 | if (!isset($this->_values[$f])) { | |
329 | $this->_values[$f] = NULL; | |
330 | } | |
331 | } | |
332 | ||
333 | //check if Membership Block is enabled, if Membership Fields are included in profile | |
334 | //get membership section for this contribution page | |
335 | $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id); | |
336 | $this->set('membershipBlock', $this->_membershipBlock); | |
337 | ||
4779abb3 | 338 | if (!empty($this->_values['custom_pre_id'])) { |
6a488035 TO |
339 | $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']); |
340 | } | |
341 | ||
4779abb3 | 342 | if (!empty($this->_values['custom_post_id'])) { |
6a488035 TO |
343 | $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']); |
344 | } | |
345 | ||
346 | if (((isset($postProfileType) && $postProfileType == 'Membership') || | |
347 | (isset($preProfileType) && $preProfileType == 'Membership') | |
348 | ) && | |
349 | !$this->_membershipBlock['is_active'] | |
350 | ) { | |
351 | 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.')); | |
352 | } | |
353 | ||
354 | $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id); | |
355 | ||
356 | if ($pledgeBlock) { | |
357 | $this->_values['pledge_block_id'] = CRM_Utils_Array::value('id', $pledgeBlock); | |
358 | $this->_values['max_reminders'] = CRM_Utils_Array::value('max_reminders', $pledgeBlock); | |
359 | $this->_values['initial_reminder_day'] = CRM_Utils_Array::value('initial_reminder_day', $pledgeBlock); | |
360 | $this->_values['additional_reminder_day'] = CRM_Utils_Array::value('additional_reminder_day', $pledgeBlock); | |
361 | ||
362 | //set pledge id in values | |
363 | $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this); | |
364 | ||
365 | //authenticate pledge user for pledge payment. | |
366 | if ($pledgeId) { | |
367 | $this->_values['pledge_id'] = $pledgeId; | |
368 | ||
369 | //lets override w/ pledge campaign. | |
370 | $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', | |
371 | $pledgeId, | |
372 | 'campaign_id' | |
373 | ); | |
374 | self::authenticatePledgeUser(); | |
375 | } | |
376 | } | |
377 | $this->set('values', $this->_values); | |
378 | $this->set('fields', $this->_fields); | |
379 | } | |
380 | ||
381 | // Handle PCP | |
382 | $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this); | |
383 | if ($pcpId) { | |
353ffa53 TO |
384 | $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values); |
385 | $this->_pcpId = $pcp['pcpId']; | |
6a488035 | 386 | $this->_pcpBlock = $pcp['pcpBlock']; |
353ffa53 | 387 | $this->_pcpInfo = $pcp['pcpInfo']; |
6a488035 TO |
388 | } |
389 | ||
390 | // Link (button) for users to create their own Personal Campaign page | |
391 | if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) { | |
392 | $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign', | |
393 | "action=add&reset=1&pageId={$this->_id}&component=contribute", | |
394 | FALSE, NULL, TRUE | |
395 | ); | |
396 | $this->assign('linkTextUrl', $linkTextUrl); | |
397 | $this->assign('linkText', $linkText); | |
398 | } | |
399 | ||
400 | //set pledge block if block id is set | |
a7488080 | 401 | if (!empty($this->_values['pledge_block_id'])) { |
6a488035 TO |
402 | $this->assign('pledgeBlock', TRUE); |
403 | } | |
404 | ||
f92d1e2a | 405 | // check if one of the (amount , membership) blocks is active or not. |
6a488035 TO |
406 | $this->_membershipBlock = $this->get('membershipBlock'); |
407 | ||
408 | if (!$this->_values['amount_block_is_active'] && | |
409 | !$this->_membershipBlock['is_active'] && | |
410 | !$this->_priceSetId | |
411 | ) { | |
412 | 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.')); | |
413 | } | |
414 | ||
415 | if ($this->_values['amount_block_is_active']) { | |
416 | $this->set('amount_block_is_active', $this->_values['amount_block_is_active']); | |
417 | } | |
418 | ||
419 | $this->_contributeMode = $this->get('contributeMode'); | |
420 | $this->assign('contributeMode', $this->_contributeMode); | |
421 | ||
422 | //assigning is_monetary and is_email_receipt to template | |
423 | $this->assign('is_monetary', $this->_values['is_monetary']); | |
424 | $this->assign('is_email_receipt', $this->_values['is_email_receipt']); | |
425 | $this->assign('bltID', $this->_bltID); | |
426 | ||
427 | //assign cancelSubscription URL to templates | |
428 | $this->assign('cancelSubscriptionUrl', | |
429 | CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values) | |
430 | ); | |
431 | ||
432 | // assigning title to template in case someone wants to use it, also setting CMS page title | |
433 | if ($this->_pcpId) { | |
434 | $this->assign('title', $this->_pcpInfo['title']); | |
435 | CRM_Utils_System::setTitle($this->_pcpInfo['title']); | |
436 | } | |
437 | else { | |
438 | $this->assign('title', $this->_values['title']); | |
439 | CRM_Utils_System::setTitle($this->_values['title']); | |
440 | } | |
441 | $this->_defaults = array(); | |
442 | ||
443 | $this->_amount = $this->get('amount'); | |
444 | ||
445 | //CRM-6907 | |
446 | $config = CRM_Core_Config::singleton(); | |
447 | $config->defaultCurrency = CRM_Utils_Array::value('currency', | |
448 | $this->_values, | |
449 | $config->defaultCurrency | |
450 | ); | |
451 | ||
452 | //lets allow user to override campaign. | |
453 | $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this); | |
454 | if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) { | |
455 | $this->_values['campaign_id'] = $campID; | |
456 | } | |
457 | ||
458 | //do check for cancel recurring and clean db, CRM-7696 | |
a3d827a7 | 459 | if (CRM_Utils_Request::retrieve('cancel', 'Boolean')) { |
6a488035 TO |
460 | self::cancelRecurring(); |
461 | } | |
8ae4d0d3 | 462 | |
463 | // check if billing block is required for pay later | |
464 | if (CRM_Utils_Array::value('is_pay_later', $this->_values)) { | |
465 | $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values); | |
466 | $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater); | |
467 | } | |
6a488035 TO |
468 | } |
469 | ||
470 | /** | |
fe482240 | 471 | * Set the default values. |
6a488035 | 472 | */ |
00be9182 | 473 | public function setDefaultValues() { |
6a488035 TO |
474 | return $this->_defaults; |
475 | } | |
476 | ||
477 | /** | |
fe482240 | 478 | * Assign the minimal set of variables to the template. |
6a488035 | 479 | */ |
00be9182 | 480 | public function assignToTemplate() { |
0b50eca0 | 481 | $this->set('name', $this->assignBillingName($this->_params)); |
6a488035 TO |
482 | |
483 | $this->assign('paymentProcessor', $this->_paymentProcessor); | |
484 | $vars = array( | |
353ffa53 TO |
485 | 'amount', |
486 | 'currencyID', | |
487 | 'credit_card_type', | |
488 | 'trxn_id', | |
489 | 'amount_level', | |
6a488035 TO |
490 | ); |
491 | ||
492 | $config = CRM_Core_Config::singleton(); | |
8cc574cf | 493 | if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) { |
6a488035 TO |
494 | $this->assign('is_recur_enabled', 1); |
495 | $vars = array_merge($vars, array( | |
353ffa53 TO |
496 | 'is_recur', |
497 | 'frequency_interval', | |
498 | 'frequency_unit', | |
499 | 'installments', | |
500 | )); | |
6a488035 TO |
501 | } |
502 | ||
503 | if (in_array('CiviPledge', $config->enableComponents) && | |
504 | CRM_Utils_Array::value('is_pledge', $this->_params) == 1 | |
505 | ) { | |
506 | $this->assign('pledge_enabled', 1); | |
507 | ||
508 | $vars = array_merge($vars, array( | |
509 | 'is_pledge', | |
353ffa53 TO |
510 | 'pledge_frequency_interval', |
511 | 'pledge_frequency_unit', | |
512 | 'pledge_installments', | |
513 | )); | |
6a488035 TO |
514 | } |
515 | ||
c039f658 | 516 | // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel |
517 | // function to get correct amount level consistently. Remove setting of the amount level in | |
518 | // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest | |
519 | // to cover all variants. | |
6a488035 TO |
520 | if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) { |
521 | $this->_params['amount_level'] = ''; | |
522 | } | |
523 | ||
524 | foreach ($vars as $v) { | |
3fb990f4 | 525 | if (isset($this->_params[$v])) { |
735fe42d PJ |
526 | if ($v == "amount" && $this->_params[$v] === 0) { |
527 | $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE); | |
3fb990f4 | 528 | } |
6a488035 TO |
529 | $this->assign($v, $this->_params[$v]); |
530 | } | |
531 | } | |
532 | ||
0b50eca0 | 533 | $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( |
534 | $this->_params, | |
535 | $this->_bltID | |
536 | )); | |
6a488035 | 537 | |
cb804cd9 | 538 | if (!empty($this->_params['onbehalf_profile_id']) && !empty($this->_params['onbehalf'])) { |
6a488035 TO |
539 | $this->assign('onBehalfName', $this->_params['organization_name']); |
540 | $locTypeId = array_keys($this->_params['onbehalf_location']['email']); | |
541 | $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']); | |
542 | } | |
543 | ||
544 | //fix for CRM-3767 | |
545 | $assignCCInfo = FALSE; | |
546 | if ($this->_amount > 0.0) { | |
547 | $assignCCInfo = TRUE; | |
548 | } | |
a7488080 | 549 | elseif (!empty($this->_params['selectMembership'])) { |
6a488035 TO |
550 | $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee'); |
551 | if ($memFee > 0.0) { | |
552 | $assignCCInfo = TRUE; | |
553 | } | |
554 | } | |
555 | ||
0f2b049e | 556 | // The concept of contributeMode is deprecated. |
557 | // The payment processor object can provide info about the fields it shows. | |
6a488035 | 558 | if ($this->_contributeMode == 'direct' && $assignCCInfo) { |
f92fc7eb CW |
559 | if ($this->_paymentProcessor && |
560 | $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT | |
561 | ) { | |
6a488035 TO |
562 | $this->assign('account_holder', $this->_params['account_holder']); |
563 | $this->assign('bank_identification_number', $this->_params['bank_identification_number']); | |
564 | $this->assign('bank_name', $this->_params['bank_name']); | |
565 | $this->assign('bank_account_number', $this->_params['bank_account_number']); | |
566 | } | |
567 | else { | |
35522279 | 568 | $date = CRM_Utils_Date::format(CRM_Utils_Array::value('credit_card_exp_date', $this->_params)); |
6a488035 TO |
569 | $date = CRM_Utils_Date::mysqlToIso($date); |
570 | $this->assign('credit_card_exp_date', $date); | |
571 | $this->assign('credit_card_number', | |
35522279 | 572 | CRM_Utils_System::mungeCreditCard(CRM_Utils_Array::value('credit_card_number', $this->_params)) |
6a488035 TO |
573 | ); |
574 | } | |
575 | } | |
576 | ||
577 | $this->assign('email', | |
578 | $this->controller->exportValue('Main', "email-{$this->_bltID}") | |
579 | ); | |
580 | ||
581 | // also assign the receipt_text | |
582 | if (isset($this->_values['receipt_text'])) { | |
583 | $this->assign('receipt_text', $this->_values['receipt_text']); | |
584 | } | |
585 | } | |
586 | ||
587 | /** | |
fe482240 | 588 | * Add the custom fields. |
6a488035 | 589 | * |
100fef9d CW |
590 | * @param int $id |
591 | * @param string $name | |
f4aaa82a EM |
592 | * @param bool $viewOnly |
593 | * @param null $profileContactType | |
f92d1e2a | 594 | * @param array $fieldTypes |
6a488035 | 595 | */ |
00be9182 | 596 | public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) { |
6a488035 | 597 | if ($id) { |
da8d9879 | 598 | $contactID = $this->getContactID(); |
6a488035 TO |
599 | |
600 | // we don't allow conflicting fields to be | |
601 | // configured via profile - CRM 2100 | |
602 | $fieldsToIgnore = array( | |
603 | 'receive_date' => 1, | |
604 | 'trxn_id' => 1, | |
605 | 'invoice_id' => 1, | |
606 | 'net_amount' => 1, | |
607 | 'fee_amount' => 1, | |
608 | 'non_deductible_amount' => 1, | |
609 | 'total_amount' => 1, | |
610 | 'amount_level' => 1, | |
611 | 'contribution_status_id' => 1, | |
612 | 'payment_instrument' => 1, | |
613 | 'check_number' => 1, | |
614 | 'financial_type' => 1, | |
615 | ); | |
616 | ||
423b9af4 | 617 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE, |
618 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL | |
619 | ); | |
6a488035 TO |
620 | |
621 | if ($fields) { | |
622 | // unset any email-* fields since we already collect it, CRM-2888 | |
623 | foreach (array_keys($fields) as $fieldName) { | |
c043358f | 624 | if (substr($fieldName, 0, 6) == 'email-' && !in_array($profileContactType, array('honor', 'onbehalf'))) { |
6a488035 TO |
625 | unset($fields[$fieldName]); |
626 | } | |
627 | } | |
628 | ||
629 | if (array_intersect_key($fields, $fieldsToIgnore)) { | |
630 | $fields = array_diff_key($fields, $fieldsToIgnore); | |
631 | CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert'); | |
632 | } | |
633 | ||
d7d630be | 634 | //remove common fields only if profile is not configured for onbehalf/honor |
635 | if (!in_array($profileContactType, array('honor', 'onbehalf'))) { | |
636 | $fields = array_diff_assoc($fields, $this->_fields); | |
637 | } | |
6a488035 TO |
638 | |
639 | CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID); | |
640 | $addCaptcha = FALSE; | |
f3726153 | 641 | // fetch file preview when not submitted yet, like in online contribution Confirm and ThankYou page |
c2292dbd | 642 | $viewOnlyFileValues = empty($profileContactType) ? array() : array($profileContactType => array()); |
6a488035 TO |
643 | foreach ($fields as $key => $field) { |
644 | if ($viewOnly && | |
645 | isset($field['data_type']) && | |
646 | $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL') | |
647 | ) { | |
c2292dbd | 648 | //retrieve file value from submitted values on basis of $profileContactType |
9b7bedc5 | 649 | $fileValue = CRM_Utils_Array::value($key, $this->_params); |
650 | if (!empty($profileContactType) && !empty($this->_params[$profileContactType])) { | |
651 | $fileValue = CRM_Utils_Array::value($key, $this->_params[$profileContactType]); | |
652 | } | |
c2292dbd | 653 | |
654 | if ($fileValue) { | |
655 | $path = CRM_Utils_Array::value('name', $fileValue); | |
656 | $fileType = CRM_Utils_Array::value('type', $fileValue); | |
657 | $fileValue = CRM_Utils_File::getFileURL($path, $fileType); | |
f3726153 | 658 | } |
6a488035 | 659 | |
c2292dbd | 660 | // format custom file value fetched from submitted value |
661 | if ($profileContactType) { | |
662 | $viewOnlyFileValues[$profileContactType][$key] = $fileValue; | |
663 | } | |
664 | else { | |
665 | $viewOnlyFileValues[$key] = $fileValue; | |
666 | } | |
1aedfc98 | 667 | |
668 | // On viewOnly use-case (as in online contribution Confirm page) we no longer need to set | |
669 | // required property because being required file is already uploaded while registration | |
670 | $field['is_required'] = FALSE; | |
c2292dbd | 671 | } |
133e2c99 | 672 | if ($profileContactType) { |
673 | //Since we are showing honoree name separately so we are removing it from honoree profile just for display | |
c043358f | 674 | if ($profileContactType == 'honor') { |
675 | $honoreeNamefields = array( | |
676 | 'prefix_id', | |
677 | 'first_name', | |
678 | 'last_name', | |
679 | 'suffix_id', | |
680 | 'organization_name', | |
681 | 'household_name', | |
682 | ); | |
683 | if (in_array($field['name'], $honoreeNamefields)) { | |
684 | unset($fields[$field['name']]); | |
685 | continue; | |
686 | } | |
133e2c99 | 687 | } |
6a488035 TO |
688 | if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) { |
689 | CRM_Core_BAO_UFGroup::buildProfile( | |
690 | $this, | |
691 | $field, | |
692 | CRM_Profile_Form::MODE_CREATE, | |
693 | $contactID, | |
133e2c99 | 694 | TRUE, |
695 | $profileContactType | |
6a488035 | 696 | ); |
133e2c99 | 697 | $this->_fields[$profileContactType][$key] = $field; |
6a488035 TO |
698 | } |
699 | else { | |
700 | unset($fields[$key]); | |
701 | } | |
702 | } | |
703 | else { | |
704 | CRM_Core_BAO_UFGroup::buildProfile( | |
705 | $this, | |
706 | $field, | |
707 | CRM_Profile_Form::MODE_CREATE, | |
708 | $contactID, | |
709 | TRUE | |
710 | ); | |
711 | $this->_fields[$key] = $field; | |
712 | } | |
71fc6ea4 DG |
713 | // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor |
714 | if ($field['add_captcha'] && !$this->_userID) { | |
6a488035 TO |
715 | $addCaptcha = TRUE; |
716 | } | |
717 | } | |
718 | ||
719 | $this->assign($name, $fields); | |
720 | ||
6d43e496 | 721 | if ($profileContactType && count($viewOnlyFileValues[$profileContactType])) { |
722 | $this->assign('viewOnlyPrefixFileValues', $viewOnlyFileValues); | |
723 | } | |
724 | elseif (count($viewOnlyFileValues)) { | |
f3726153 | 725 | $this->assign('viewOnlyFileValues', $viewOnlyFileValues); |
726 | } | |
727 | ||
6a488035 TO |
728 | if ($addCaptcha && !$viewOnly) { |
729 | $captcha = CRM_Utils_ReCAPTCHA::singleton(); | |
730 | $captcha->add($this); | |
731 | $this->assign('isCaptcha', TRUE); | |
732 | } | |
733 | } | |
734 | } | |
735 | } | |
736 | ||
4779abb3 | 737 | /** |
738 | * Add onbehalf/honoree profile fields and native module fields. | |
739 | * | |
740 | * @param int $id | |
741 | * @param CRM_Core_Form $form | |
742 | */ | |
bcb8cc84 | 743 | public function buildComponentForm($id, $form) { |
744 | if (empty($id)) { | |
745 | return; | |
746 | } | |
747 | ||
748 | $contactID = $this->getContactID(); | |
749 | ||
750 | foreach (array('soft_credit', 'on_behalf') as $module) { | |
bcb8cc84 | 751 | if ($module == 'soft_credit') { |
4779abb3 | 752 | if (empty($form->_values['honoree_profile_id'])) { |
753 | continue; | |
754 | } | |
bcb8cc84 | 755 | |
4779abb3 | 756 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['honoree_profile_id'], 'is_active')) { |
bcb8cc84 | 757 | CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of honoree and the selected honoree profile is either disabled or not found.')); |
758 | } | |
759 | ||
4779abb3 | 760 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']); |
bcb8cc84 | 761 | $requiredProfileFields = array( |
762 | 'Individual' => array('first_name', 'last_name'), | |
763 | 'Organization' => array('organization_name', 'email'), | |
764 | 'Household' => array('household_name', 'email'), | |
765 | ); | |
4779abb3 | 766 | $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_values['honoree_profile_id'], $requiredProfileFields[$profileContactType]); |
bcb8cc84 | 767 | if (!$validProfile) { |
768 | CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of honoree and the required fields of the selected honoree profile are disabled or doesn\'t exist.')); | |
769 | } | |
770 | ||
cb804cd9 | 771 | foreach (array('honor_block_title', 'honor_block_text') as $name) { |
4779abb3 | 772 | $form->assign($name, $form->_values[$name]); |
cb804cd9 | 773 | } |
774 | ||
775 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); | |
776 | ||
777 | // radio button for Honor Type | |
4779abb3 | 778 | foreach ($form->_values['soft_credit_types'] as $value) { |
cb804cd9 | 779 | $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value); |
780 | } | |
781 | $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE); | |
782 | ||
4779abb3 | 783 | $honoreeProfileFields = CRM_Core_BAO_UFGroup::getFields( |
784 | $this->_values['honoree_profile_id'], FALSE, | |
785 | NULL, NULL, | |
786 | NULL, FALSE, | |
787 | NULL, TRUE, | |
788 | NULL, CRM_Core_Permission::CREATE | |
cb804cd9 | 789 | ); |
cb804cd9 | 790 | $form->assign('honoreeProfileFields', $honoreeProfileFields); |
791 | ||
792 | // add the form elements | |
793 | foreach ($honoreeProfileFields as $name => $field) { | |
794 | // If soft credit type is not chosen then make omit requiredness from honoree profile fields | |
795 | if (count($form->_submitValues) && | |
796 | empty($form->_submitValues['soft_credit_type_id']) && | |
797 | !empty($field['is_required']) | |
798 | ) { | |
799 | $field['is_required'] = FALSE; | |
800 | } | |
c043358f | 801 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, NULL, FALSE, FALSE, NULL, 'honor'); |
cb804cd9 | 802 | } |
bcb8cc84 | 803 | } |
804 | else { | |
4779abb3 | 805 | if (empty($form->_values['onbehalf_profile_id'])) { |
806 | continue; | |
807 | } | |
bcb8cc84 | 808 | |
4779abb3 | 809 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['onbehalf_profile_id'], 'is_active')) { |
bcb8cc84 | 810 | CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of an organization and the selected onbehalf profile is either disabled or not found.')); |
811 | } | |
812 | ||
813 | $member = CRM_Member_BAO_Membership::getMembershipBlock($form->_id); | |
814 | if (empty($member['is_active'])) { | |
815 | $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.'); | |
4779abb3 | 816 | $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($form->_values['onbehalf_profile_id']); |
bcb8cc84 | 817 | foreach (array( |
818 | 'Individual', | |
819 | 'Organization', | |
820 | 'Household', | |
821 | ) as $contactType) { | |
822 | if (in_array($contactType, $onBehalfProfile) && | |
823 | (in_array('Membership', $onBehalfProfile) || | |
824 | in_array('Contribution', $onBehalfProfile) | |
825 | ) | |
826 | ) { | |
827 | CRM_Core_Error::fatal($msg); | |
828 | } | |
829 | } | |
830 | } | |
831 | ||
c043358f | 832 | if ($contactID) { |
33260076 | 833 | // retrieve all permissioned organizations of contact $contactID |
834 | $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($contactID, NULL, NULL, 'Organization'); | |
c043358f | 835 | |
33260076 | 836 | if (count($organizations)) { |
c043358f | 837 | // Related org url - pass checksum if needed |
f23093b2 | 838 | $args = array( |
4779abb3 | 839 | 'ufId' => $form->_values['onbehalf_profile_id'], |
f23093b2 | 840 | 'cid' => '', |
841 | ); | |
c043358f | 842 | if (!empty($_GET['cs'])) { |
843 | $args = array( | |
4779abb3 | 844 | 'ufId' => $form->_values['onbehalf_profile_id'], |
c043358f | 845 | 'uid' => $this->_contactID, |
846 | 'cs' => $_GET['cs'], | |
847 | 'cid' => '', | |
848 | ); | |
849 | } | |
850 | $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE); | |
851 | $form->assign('locDataURL', $locDataURL); | |
852 | } | |
33260076 | 853 | if (count($organizations) > 0) { |
854 | $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $organizations)); | |
c043358f | 855 | |
856 | $orgOptions = array( | |
857 | 0 => ts('Select an existing organization'), | |
858 | 1 => ts('Enter a new organization'), | |
859 | ); | |
860 | $form->addRadio('org_option', ts('options'), $orgOptions); | |
861 | $form->setDefaults(array('org_option' => 0)); | |
862 | } | |
bcb8cc84 | 863 | } |
864 | ||
c043358f | 865 | $form->assign('fieldSetTitle', ts('Organization Details')); |
c043358f | 866 | |
4779abb3 | 867 | if (CRM_Utils_Array::value('is_for_organization', $form->_values)) { |
868 | if ($form->_values['is_for_organization'] == 2) { | |
c043358f | 869 | $form->assign('onBehalfRequired', TRUE); |
bcb8cc84 | 870 | } |
871 | else { | |
872 | $form->addElement('checkbox', 'is_for_organization', | |
873 | $form->_values['for_organization'], | |
874 | NULL | |
875 | ); | |
876 | } | |
877 | } | |
c043358f | 878 | |
4779abb3 | 879 | $profileFields = CRM_Core_BAO_UFGroup::getFields( |
880 | $form->_values['onbehalf_profile_id'], | |
881 | FALSE, CRM_Core_Action::VIEW, NULL, | |
882 | NULL, FALSE, NULL, FALSE, NULL, | |
883 | CRM_Core_Permission::CREATE, NULL | |
c043358f | 884 | ); |
4779abb3 | 885 | |
c043358f | 886 | $form->assign('onBehalfOfFields', $profileFields); |
887 | if (!empty($form->_submitValues['onbehalf'])) { | |
888 | if (!empty($form->_submitValues['onbehalfof_id'])) { | |
889 | $form->assign('submittedOnBehalf', $form->_submitValues['onbehalfof_id']); | |
890 | } | |
891 | $form->assign('submittedOnBehalfInfo', json_encode($form->_submitValues['onbehalf'])); | |
892 | } | |
893 | ||
894 | $fieldTypes = array('Contact', 'Organization'); | |
895 | $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); | |
896 | $fieldTypes = array_merge($fieldTypes, $contactSubType); | |
897 | ||
898 | foreach ($profileFields as $name => $field) { | |
899 | if (in_array($field['field_type'], $fieldTypes)) { | |
900 | list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2); | |
901 | if (in_array($prefixName, array('organization_name', 'email')) && empty($field['is_required'])) { | |
902 | $field['is_required'] = 1; | |
903 | } | |
904 | if (count($form->_submitValues) && | |
905 | empty($form->_submitValues['is_for_organization']) && | |
4779abb3 | 906 | $form->_values['is_for_organization'] == 1 && |
c043358f | 907 | !empty($field['is_required']) |
908 | ) { | |
909 | $field['is_required'] = FALSE; | |
910 | } | |
4779abb3 | 911 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, NULL, NULL, FALSE, 'onbehalf', NULL, 'onbehalf'); |
c043358f | 912 | } |
913 | } | |
bcb8cc84 | 914 | } |
915 | } | |
916 | ||
917 | } | |
918 | ||
f4aaa82a | 919 | /** |
fe482240 | 920 | * Check template file exists. |
f92d1e2a EM |
921 | * |
922 | * @param string $suffix | |
f4aaa82a EM |
923 | * |
924 | * @return null|string | |
925 | */ | |
00be9182 | 926 | public function checkTemplateFileExists($suffix = NULL) { |
6a488035 TO |
927 | if ($this->_id) { |
928 | $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl"; | |
929 | $template = CRM_Core_Form::getTemplate(); | |
930 | if ($template->template_exists($templateFile)) { | |
931 | return $templateFile; | |
932 | } | |
933 | } | |
934 | return NULL; | |
935 | } | |
936 | ||
186c9c17 | 937 | /** |
fe482240 | 938 | * Use the form name to create the tpl file name. |
186c9c17 EM |
939 | * |
940 | * @return string | |
186c9c17 | 941 | */ |
00be9182 | 942 | public function getTemplateFileName() { |
6a488035 TO |
943 | $fileName = $this->checkTemplateFileExists(); |
944 | return $fileName ? $fileName : parent::getTemplateFileName(); | |
945 | } | |
946 | ||
186c9c17 | 947 | /** |
f92d1e2a EM |
948 | * Add the extra.tpl in. |
949 | * | |
186c9c17 | 950 | * Default extra tpl file basically just replaces .tpl with .extra.tpl |
f92d1e2a | 951 | * i.e. we do not override - why isn't this done at the CRM_Core_Form level? |
186c9c17 EM |
952 | * |
953 | * @return string | |
186c9c17 | 954 | */ |
00be9182 | 955 | public function overrideExtraTemplateFileName() { |
6a488035 TO |
956 | $fileName = $this->checkTemplateFileExists('extra.'); |
957 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); | |
958 | } | |
959 | ||
960 | /** | |
100fef9d | 961 | * Authenticate pledge user during online payment. |
6a488035 TO |
962 | */ |
963 | public function authenticatePledgeUser() { | |
964 | //get the userChecksum and contact id | |
965 | $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this); | |
966 | $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
967 | ||
968 | //get pledge status and contact id | |
353ffa53 TO |
969 | $pledgeValues = array(); |
970 | $pledgeParams = array('id' => $this->_values['pledge_id']); | |
6a488035 TO |
971 | $returnProperties = array('contact_id', 'status_id'); |
972 | CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties); | |
973 | ||
974 | //get all status | |
975 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
353ffa53 TO |
976 | $validStatus = array( |
977 | array_search('Pending', $allStatus), | |
6a488035 TO |
978 | array_search('In Progress', $allStatus), |
979 | array_search('Overdue', $allStatus), | |
980 | ); | |
981 | ||
982 | $validUser = FALSE; | |
983 | if ($this->_userID && | |
984 | $this->_userID == $pledgeValues['contact_id'] | |
985 | ) { | |
986 | //check for authenticated user. | |
987 | $validUser = TRUE; | |
988 | } | |
989 | elseif ($userChecksum && $pledgeValues['contact_id']) { | |
990 | //check for anonymous user. | |
991 | $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum); | |
992 | ||
993 | //make sure cid is same as pledge contact id | |
994 | if ($validUser && ($pledgeValues['contact_id'] != $contactID)) { | |
995 | $validUser = FALSE; | |
996 | } | |
997 | } | |
998 | ||
999 | if (!$validUser) { | |
1000 | 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.")); | |
1001 | } | |
1002 | ||
1003 | //check for valid pledge status. | |
1004 | if (!in_array($pledgeValues['status_id'], $validStatus)) { | |
1005 | 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)))); | |
1006 | } | |
1007 | } | |
1008 | ||
1009 | /** | |
f92d1e2a EM |
1010 | * Cancel recurring contributions. |
1011 | * | |
6a488035 TO |
1012 | * In case user cancel recurring contribution, |
1013 | * When we get the control back from payment gate way | |
1014 | * lets delete the recurring and related contribution. | |
389bcebf | 1015 | */ |
6a488035 | 1016 | public function cancelRecurring() { |
a3d827a7 | 1017 | $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean'); |
6a488035 | 1018 | if ($isCancel) { |
a3d827a7 CW |
1019 | $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean'); |
1020 | $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive'); | |
6a488035 TO |
1021 | //clean db for recurring contribution. |
1022 | if ($isRecur && $recurId) { | |
1023 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId); | |
1024 | } | |
a3d827a7 | 1025 | $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive'); |
6a488035 TO |
1026 | if ($contribId) { |
1027 | CRM_Contribute_BAO_Contribution::deleteContribution($contribId); | |
1028 | } | |
1029 | } | |
1030 | } | |
96025800 | 1031 | |
42e3a033 EM |
1032 | /** |
1033 | * Build Membership Block in Contribution Pages. | |
1034 | * | |
42e3a033 EM |
1035 | * @param int $cid |
1036 | * Contact checked for having a current membership for a particular membership. | |
a46bfec1 EM |
1037 | * @param bool $isContributionMainPage |
1038 | * Is this the main page? If so add form input fields. | |
1039 | * (or better yet don't have this functionality in a function shared with forms that don't share it). | |
42e3a033 EM |
1040 | * @param int $selectedMembershipTypeID |
1041 | * Selected membership id. | |
1042 | * @param bool $thankPage | |
1043 | * Thank you page. | |
1044 | * @param null $isTest | |
1045 | * | |
1046 | * @return bool | |
1047 | * Is this a separate membership payment | |
1048 | */ | |
1049 | protected function buildMembershipBlock( | |
1050 | $cid, | |
a46bfec1 | 1051 | $isContributionMainPage = FALSE, |
42e3a033 EM |
1052 | $selectedMembershipTypeID = NULL, |
1053 | $thankPage = FALSE, | |
1054 | $isTest = NULL | |
1055 | ) { | |
1056 | ||
1057 | $separateMembershipPayment = FALSE; | |
1058 | if ($this->_membershipBlock) { | |
1059 | $this->_currentMemberships = array(); | |
1060 | ||
42e3a033 EM |
1061 | $membershipTypeIds = $membershipTypes = $radio = array(); |
1062 | $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember) ? TRUE : FALSE; | |
1063 | ||
1064 | $allowAutoRenewMembership = $autoRenewOption = FALSE; | |
1065 | $autoRenewMembershipTypeOptions = array(); | |
1066 | ||
a46bfec1 | 1067 | $separateMembershipPayment = CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock); |
42e3a033 EM |
1068 | |
1069 | if ($membershipPriceset) { | |
1070 | foreach ($this->_priceSet['fields'] as $pField) { | |
1071 | if (empty($pField['options'])) { | |
1072 | continue; | |
1073 | } | |
1074 | foreach ($pField['options'] as $opId => $opValues) { | |
1075 | if (empty($opValues['membership_type_id'])) { | |
1076 | continue; | |
1077 | } | |
1078 | $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id']; | |
1079 | } | |
1080 | } | |
1081 | } | |
a46bfec1 EM |
1082 | elseif (!empty($this->_membershipBlock['membership_types'])) { |
1083 | $membershipTypeIds = explode(',', $this->_membershipBlock['membership_types']); | |
42e3a033 EM |
1084 | } |
1085 | ||
1086 | if (!empty($membershipTypeIds)) { | |
1087 | //set status message if wrong membershipType is included in membershipBlock | |
1088 | if (isset($this->_mid) && !$membershipPriceset) { | |
1089 | $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
1090 | $this->_mid, | |
1091 | 'membership_type_id' | |
1092 | ); | |
1093 | if (!in_array($membershipTypeID, $membershipTypeIds)) { | |
1094 | 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('Invalid Membership'), 'error'); | |
1095 | } | |
1096 | } | |
1097 | ||
4c7b8a7d | 1098 | $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIds); |
42e3a033 EM |
1099 | $this->_membershipTypeValues = $membershipTypeValues; |
1100 | $endDate = NULL; | |
1101 | foreach ($membershipTypeIds as $value) { | |
1102 | $memType = $membershipTypeValues[$value]; | |
1103 | if ($selectedMembershipTypeID != NULL) { | |
1104 | if ($memType['id'] == $selectedMembershipTypeID) { | |
1105 | $this->assign('minimum_fee', | |
1106 | CRM_Utils_Array::value('minimum_fee', $memType) | |
1107 | ); | |
1108 | $this->assign('membership_name', $memType['name']); | |
1109 | if (!$thankPage && $cid) { | |
1110 | $membership = new CRM_Member_DAO_Membership(); | |
1111 | $membership->contact_id = $cid; | |
1112 | $membership->membership_type_id = $memType['id']; | |
1113 | if ($membership->find(TRUE)) { | |
1114 | $this->assign('renewal_mode', TRUE); | |
1115 | $memType['current_membership'] = $membership->end_date; | |
1116 | $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; | |
1117 | } | |
1118 | } | |
1119 | $membershipTypes[] = $memType; | |
1120 | } | |
1121 | } | |
1122 | elseif ($memType['is_active']) { | |
1123 | $javascriptMethod = NULL; | |
08a4ce4e | 1124 | $allowAutoRenewOpt = (int) $memType['auto_renew']; |
42e3a033 EM |
1125 | if (is_array($this->_paymentProcessors)) { |
1126 | foreach ($this->_paymentProcessors as $id => $val) { | |
fb357d6e | 1127 | if ($id && !$val['is_recur']) { |
42e3a033 EM |
1128 | $allowAutoRenewOpt = 0; |
1129 | continue; | |
1130 | } | |
1131 | } | |
1132 | } | |
1133 | ||
1134 | $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );"); | |
1135 | $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array::value($value, CRM_Utils_Array::value('auto_renew', $this->_membershipBlock));; | |
1136 | ||
1137 | if ($allowAutoRenewOpt) { | |
1138 | $allowAutoRenewMembership = TRUE; | |
1139 | } | |
1140 | ||
1141 | //add membership type. | |
1142 | $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL, | |
1143 | $memType['id'], $javascriptMethod | |
1144 | ); | |
1145 | if ($cid) { | |
1146 | $membership = new CRM_Member_DAO_Membership(); | |
1147 | $membership->contact_id = $cid; | |
1148 | $membership->membership_type_id = $memType['id']; | |
1149 | ||
1150 | //show current membership, skip pending and cancelled membership records, | |
1151 | //because we take first membership record id for renewal | |
1152 | $membership->whereAdd('status_id != 5 AND status_id !=6'); | |
1153 | ||
1154 | if (!is_null($isTest)) { | |
1155 | $membership->is_test = $isTest; | |
1156 | } | |
1157 | ||
1158 | //CRM-4297 | |
1159 | $membership->orderBy('end_date DESC'); | |
1160 | ||
1161 | if ($membership->find(TRUE)) { | |
1162 | if (!$membership->end_date) { | |
1163 | unset($radio[$memType['id']]); | |
1164 | $this->assign('islifetime', TRUE); | |
1165 | continue; | |
1166 | } | |
1167 | $this->assign('renewal_mode', TRUE); | |
1168 | $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; | |
1169 | $memType['current_membership'] = $membership->end_date; | |
1170 | if (!$endDate) { | |
1171 | $endDate = $memType['current_membership']; | |
1172 | $this->_defaultMemTypeId = $memType['id']; | |
1173 | } | |
1174 | if ($memType['current_membership'] < $endDate) { | |
1175 | $endDate = $memType['current_membership']; | |
1176 | $this->_defaultMemTypeId = $memType['id']; | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | $membershipTypes[] = $memType; | |
1181 | } | |
1182 | } | |
1183 | } | |
1184 | ||
a46bfec1 EM |
1185 | $this->assign('membershipBlock', $this->_membershipBlock); |
1186 | $this->assign('showRadio', $isContributionMainPage); | |
1187 | $this->assign('membershipTypes', $membershipTypes); | |
1188 | $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership); | |
1189 | $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions)); | |
1190 | //give preference to user submitted auto_renew value. | |
1191 | $takeUserSubmittedAutoRenew = (!empty($_POST) || $this->isSubmitted()) ? TRUE : FALSE; | |
1192 | $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew); | |
1193 | ||
1194 | if ($isContributionMainPage) { | |
42e3a033 | 1195 | if (!$membershipPriceset) { |
a46bfec1 | 1196 | if (!$this->_membershipBlock['is_required']) { |
42e3a033 EM |
1197 | $this->assign('showRadioNoThanks', TRUE); |
1198 | $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL); | |
1199 | $this->addGroup($radio, 'selectMembership', NULL); | |
1200 | } | |
a46bfec1 | 1201 | elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) { |
42e3a033 EM |
1202 | $temp = array_keys($radio); |
1203 | $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership')); | |
1204 | $this->assign('singleMembership', TRUE); | |
1205 | $this->assign('showRadio', FALSE); | |
1206 | } | |
1207 | else { | |
1208 | $this->addGroup($radio, 'selectMembership', NULL); | |
1209 | } | |
1210 | ||
1211 | $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required'); | |
1212 | } | |
1213 | else { | |
1214 | $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId); | |
1215 | $this->assign('autoRenewOption', $autoRenewOption); | |
1216 | } | |
1217 | ||
fb357d6e | 1218 | if ((!$this->_values['is_pay_later'] || is_array($this->_paymentProcessors)) && ($allowAutoRenewMembership || $autoRenewOption)) { |
42e3a033 EM |
1219 | $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.')); |
1220 | } | |
1221 | ||
1222 | } | |
42e3a033 EM |
1223 | } |
1224 | ||
1225 | return $separateMembershipPayment; | |
1226 | } | |
1227 | ||
90102a32 EM |
1228 | /** |
1229 | * Determine if recurring parameters need to be added to the form parameters. | |
8cb12ff1 | 1230 | * |
90102a32 EM |
1231 | * - is_recur |
1232 | * - frequency_interval | |
1233 | * - frequency_unit | |
1234 | * | |
1235 | * For membership this is based on the membership type. | |
1236 | * | |
1237 | * This needs to be done before processing the pre-approval redirect where relevant on the main page or before any payment processing. | |
1238 | * | |
1239 | * Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout. | |
1240 | */ | |
1241 | protected function setRecurringMembershipParams() { | |
8cb12ff1 | 1242 | $selectedMembershipTypeID = CRM_Utils_Array::value('selectMembership', $this->_params); |
1243 | if ($selectedMembershipTypeID) { | |
fd359255 EM |
1244 | // @todo the price_x fields will ALWAYS allow us to determine the membership - so we should ignore |
1245 | // 'selectMembership' and calculate from the price_x fields so we have one method that always works | |
1246 | // this is lazy & only catches when selectMembership is set, but the worst of all worlds would be to fix | |
1247 | // this with an else (calculate for price set). | |
8cb12ff1 | 1248 | $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($this->_priceSetId); |
1249 | if (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_required']) | |
1250 | || (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_optional']) && | |
1251 | !empty($this->_params['is_recur'])) | |
1252 | ) { | |
fd359255 EM |
1253 | $this->_params['auto_renew'] = TRUE; |
1254 | } | |
1255 | } | |
8cb12ff1 | 1256 | if ((!empty($this->_params['selectMembership']) || !empty($this->_params['priceSetId'])) |
1257 | && !empty($this->_paymentProcessor['is_recur']) && | |
1258 | CRM_Utils_Array::value('auto_renew', $this->_params) | |
1259 | && empty($this->_params['is_recur']) && empty($this->_params['frequency_interval']) | |
90102a32 EM |
1260 | ) { |
1261 | ||
1262 | $this->_params['is_recur'] = $this->_values['is_recur'] = 1; | |
1263 | // check if price set is not quick config | |
1264 | if (!empty($this->_params['priceSetId']) && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config')) { | |
1265 | list($this->_params['frequency_interval'], $this->_params['frequency_unit']) = CRM_Price_BAO_PriceSet::getRecurDetails($this->_params['priceSetId']); | |
1266 | } | |
1267 | else { | |
1268 | // FIXME: set interval and unit based on selected membership type | |
1269 | $this->_params['frequency_interval'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
1270 | $this->_params['selectMembership'], 'duration_interval' | |
1271 | ); | |
1272 | $this->_params['frequency_unit'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
1273 | $this->_params['selectMembership'], 'duration_unit' | |
1274 | ); | |
1275 | } | |
1276 | } | |
1277 | } | |
1278 | ||
6a488035 | 1279 | } |