Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
f92d1e2a | 19 | * This class generates form components for processing a contribution. |
6a488035 TO |
20 | */ |
21 | class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { | |
573aa07e | 22 | use CRM_Financial_Form_FrontEndPaymentFormTrait; |
6a488035 TO |
23 | |
24 | /** | |
f92d1e2a | 25 | * The id of the contribution page that we are processing. |
6a488035 TO |
26 | * |
27 | * @var int | |
6a488035 TO |
28 | */ |
29 | public $_id; | |
30 | ||
31 | /** | |
100fef9d | 32 | * The mode that we are in |
6a488035 TO |
33 | * |
34 | * @var string | |
35 | * @protect | |
36 | */ | |
37 | public $_mode; | |
38 | ||
39 | /** | |
100fef9d | 40 | * The contact id related to a membership |
6a488035 TO |
41 | * |
42 | * @var int | |
6a488035 TO |
43 | */ |
44 | public $_membershipContactID; | |
45 | ||
46 | /** | |
100fef9d | 47 | * The values for the contribution db object |
6a488035 TO |
48 | * |
49 | * @var array | |
6a488035 TO |
50 | */ |
51 | public $_values; | |
52 | ||
53 | /** | |
100fef9d | 54 | * The paymentProcessor attributes for this page |
6a488035 TO |
55 | * |
56 | * @var array | |
6a488035 TO |
57 | */ |
58 | public $_paymentProcessor; | |
1b9f9ca3 | 59 | |
6a488035 TO |
60 | public $_paymentObject = NULL; |
61 | ||
62 | /** | |
63 | * The membership block for this page | |
64 | * | |
65 | * @var array | |
6a488035 TO |
66 | */ |
67 | public $_membershipBlock = NULL; | |
68 | ||
f64a217a EM |
69 | /** |
70 | * Does this form support a separate membership payment | |
71 | * @var bool | |
72 | */ | |
73 | protected $_separateMembershipPayment; | |
6a488035 TO |
74 | |
75 | /** | |
76 | * The params submitted by the form and computed by the app | |
77 | * | |
78 | * @var array | |
6a488035 | 79 | */ |
be2fb01f | 80 | public $_params = []; |
6a488035 TO |
81 | |
82 | /** | |
83 | * The fields involved in this contribution page | |
84 | * | |
85 | * @var array | |
6a488035 | 86 | */ |
be2fb01f | 87 | public $_fields = []; |
6a488035 TO |
88 | |
89 | /** | |
f92d1e2a | 90 | * The billing location id for this contribution page. |
6a488035 TO |
91 | * |
92 | * @var int | |
6a488035 TO |
93 | */ |
94 | public $_bltID; | |
95 | ||
96 | /** | |
97 | * Cache the amount to make things easier | |
98 | * | |
99 | * @var float | |
6a488035 TO |
100 | */ |
101 | public $_amount; | |
102 | ||
103 | /** | |
100fef9d | 104 | * Pcp id |
6a488035 | 105 | * |
9f266042 | 106 | * @var int |
6a488035 TO |
107 | */ |
108 | public $_pcpId; | |
109 | ||
110 | /** | |
100fef9d | 111 | * Pcp block |
6a488035 TO |
112 | * |
113 | * @var array | |
6a488035 TO |
114 | */ |
115 | public $_pcpBlock; | |
116 | ||
117 | /** | |
100fef9d | 118 | * Pcp info |
6a488035 TO |
119 | * |
120 | * @var array | |
6a488035 TO |
121 | */ |
122 | public $_pcpInfo; | |
123 | ||
5b757295 | 124 | /** |
125 | * The contact id of the person for whom membership is being added or renewed based on the cid in the url, | |
126 | * checksum, or session | |
0e5e0c2e | 127 | * @var int |
5b757295 | 128 | */ |
0e5e0c2e | 129 | public $_contactID; |
5b757295 | 130 | |
6a488035 TO |
131 | protected $_userID; |
132 | ||
133 | /** | |
100fef9d | 134 | * The Membership ID for membership renewal |
6a488035 TO |
135 | * |
136 | * @var int | |
6a488035 TO |
137 | */ |
138 | public $_membershipId; | |
139 | ||
140 | /** | |
141 | * Price Set ID, if the new price set method is used | |
142 | * | |
143 | * @var int | |
6a488035 TO |
144 | */ |
145 | public $_priceSetId; | |
146 | ||
147 | /** | |
148 | * Array of fields for the price set | |
149 | * | |
150 | * @var array | |
6a488035 TO |
151 | */ |
152 | public $_priceSet; | |
153 | ||
154 | public $_action; | |
155 | ||
dbddfb08 | 156 | /** |
415e7323 | 157 | * Contribution mode. |
158 | * | |
159 | * In general we are trying to deprecate this parameter but some templates and processors still | |
160 | * require it to denote whether the processor redirects offsite (notify) or not. | |
161 | * | |
162 | * The intent is that this knowledge should not be required and all contributions should | |
163 | * be created in a pending state and updated based on the payment result without needing to be | |
164 | * aware of the processor workings. | |
165 | * | |
dbddfb08 | 166 | * @var string |
415e7323 | 167 | * |
168 | * @deprecated | |
dbddfb08 EM |
169 | */ |
170 | public $_contributeMode; | |
171 | ||
172 | /** | |
100fef9d | 173 | * Contribution page supports memberships |
b67daa72 | 174 | * @var bool |
dbddfb08 EM |
175 | */ |
176 | public $_useForMember; | |
8ae4d0d3 | 177 | |
0f2b049e | 178 | /** |
1330f57a | 179 | * @var bool |
0f2b049e | 180 | * @deprecated |
0f2b049e | 181 | */ |
8ae4d0d3 | 182 | public $_isBillingAddressRequiredForPayLater; |
353ffa53 | 183 | |
0c7c9ff7 KC |
184 | /** |
185 | * Flag if email field exists in embedded profile | |
186 | * | |
187 | * @var bool | |
188 | */ | |
189 | public $_emailExists = FALSE; | |
190 | ||
18135422 | 191 | /** |
415e7323 | 192 | * Is this a backoffice form. |
193 | * | |
194 | * Processors may display different options to backoffice users. | |
195 | * | |
18135422 | 196 | * @var bool |
197 | */ | |
198 | public $isBackOffice = FALSE; | |
199 | ||
200 | /** | |
201 | * Payment instrument if for the transaction. | |
202 | * | |
203 | * This will generally be drawn from the payment processor and is ignored for | |
204 | * front end forms. | |
205 | * | |
206 | * @var int | |
207 | */ | |
208 | public $paymentInstrumentID; | |
209 | ||
36601c73 MW |
210 | /** |
211 | * The contribution ID - is an option in the URL if you are making a payment against an existing contribution (an | |
212 | * "invoice payment"). | |
213 | * | |
214 | * @var int | |
215 | */ | |
216 | public $_ccid; | |
217 | ||
c59e7960 | 218 | /** |
219 | * Is the price set quick config. | |
220 | * @return bool | |
221 | */ | |
222 | public function isQuickConfig() { | |
77c21b32 | 223 | return self::$_quickConfig ?? FALSE; |
c59e7960 | 224 | } |
225 | ||
6a488035 | 226 | /** |
fe482240 | 227 | * Set variables up before form is built. |
6a488035 | 228 | * |
7fe37828 EM |
229 | * @throws \CRM_Contribute_Exception_InactiveContributionPageException |
230 | * @throws \Exception | |
6a488035 TO |
231 | */ |
232 | public function preProcess() { | |
6a488035 TO |
233 | |
234 | // current contribution page id | |
235 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
37e877b2 | 236 | $this->_ccid = CRM_Utils_Request::retrieve('ccid', 'Positive', $this); |
6a488035 | 237 | if (!$this->_id) { |
d420cf02 DL |
238 | // seems like the session is corrupted and/or we lost the id trail |
239 | // lets just bump this to a regular session error and redirect user to main page | |
240 | $this->controller->invalidKeyRedirect(); | |
6a488035 | 241 | } |
67c998f4 | 242 | $this->_emailExists = $this->get('emailExists'); |
d420cf02 | 243 | |
5b757295 | 244 | // this was used prior to the cleverer this_>getContactID - unsure now |
2dbdb9b9 | 245 | $this->_userID = CRM_Core_Session::getLoggedInContactID(); |
5b757295 | 246 | |
247 | $this->_contactID = $this->_membershipContactID = $this->getContactID(); | |
6a488035 | 248 | $this->_mid = NULL; |
5b757295 | 249 | if ($this->_contactID) { |
6a488035 TO |
250 | $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this); |
251 | if ($this->_mid) { | |
252 | $membership = new CRM_Member_DAO_Membership(); | |
253 | $membership->id = $this->_mid; | |
254 | ||
255 | if ($membership->find(TRUE)) { | |
256 | $this->_defaultMemTypeId = $membership->membership_type_id; | |
5b757295 | 257 | if ($membership->contact_id != $this->_contactID) { |
6fe8deba | 258 | $validMembership = FALSE; |
33260076 | 259 | $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, NULL, NULL, 'Organization'); |
260 | if (!empty($organizations) && array_key_exists($membership->contact_id, $organizations)) { | |
6fe8deba DS |
261 | $this->_membershipContactID = $membership->contact_id; |
262 | $this->assign('membershipContactID', $this->_membershipContactID); | |
33260076 | 263 | $this->assign('membershipContactName', $organizations[$this->_membershipContactID]['name']); |
6fe8deba | 264 | $validMembership = TRUE; |
0db6c3e1 TO |
265 | } |
266 | else { | |
51e89def DS |
267 | $membershipType = new CRM_Member_BAO_MembershipType(); |
268 | $membershipType->id = $membership->membership_type_id; | |
269 | if ($membershipType->find(TRUE)) { | |
f9f0eff9 | 270 | // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values. |
f92d1e2a | 271 | // Convert to comma separated list. |
f9f0eff9 | 272 | $inheritedRelTypes = implode(CRM_Utils_Array::explodePadded($membershipType->relationship_type_id), ','); |
51e89def DS |
273 | $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, $membershipType->relationship_type_id); |
274 | if (array_key_exists($membership->contact_id, $permContacts)) { | |
275 | $this->_membershipContactID = $membership->contact_id; | |
6fe8deba | 276 | $validMembership = TRUE; |
51e89def DS |
277 | } |
278 | } | |
6a488035 | 279 | } |
6fe8deba DS |
280 | if (!$validMembership) { |
281 | CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert'); | |
282 | } | |
6a488035 TO |
283 | } |
284 | } | |
285 | else { | |
286 | CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert'); | |
287 | } | |
288 | unset($membership); | |
289 | } | |
290 | } | |
291 | ||
292 | // we do not want to display recently viewed items, so turn off | |
293 | $this->assign('displayRecent', FALSE); | |
6a488035 TO |
294 | |
295 | // action | |
296 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
297 | $this->assign('action', $this->_action); | |
298 | ||
299 | // current mode | |
300 | $this->_mode = ($this->_action == 1024) ? 'test' : 'live'; | |
301 | ||
302 | $this->_values = $this->get('values'); | |
303 | $this->_fields = $this->get('fields'); | |
304 | $this->_bltID = $this->get('bltID'); | |
305 | $this->_paymentProcessor = $this->get('paymentProcessor'); | |
3dd22219 | 306 | |
6a488035 TO |
307 | $this->_priceSetId = $this->get('priceSetId'); |
308 | $this->_priceSet = $this->get('priceSet'); | |
309 | ||
310 | if (!$this->_values) { | |
311 | // get all the values from the dao object | |
be2fb01f CW |
312 | $this->_values = []; |
313 | $this->_fields = []; | |
6a488035 TO |
314 | |
315 | CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values); | |
40c655aa | 316 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() |
66af7c48 PN |
317 | && !CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id'])) |
318 | ) { | |
beb414cc | 319 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
0799e1c4 | 320 | } |
a7488080 | 321 | if (empty($this->_values['is_active'])) { |
4b57bc9f | 322 | throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id); |
6a488035 TO |
323 | } |
324 | ||
ed1c698a | 325 | $endDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('end_date', $this->_values)); |
326 | $now = date('YmdHis'); | |
327 | if ($endDate && $endDate < $now) { | |
6dabf459 | 328 | throw new CRM_Contribute_Exception_PastContributionPageException(ts('The page you requested has past its end date on %1', [1 => CRM_Utils_Date::customFormat($endDate)]), $this->_id); |
ed1c698a | 329 | } |
330 | ||
331 | $startDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('start_date', $this->_values)); | |
332 | if ($startDate && $startDate > $now) { | |
6dabf459 | 333 | throw new CRM_Contribute_Exception_FutureContributionPageException(ts('The page you requested will be active from %1', [1 => CRM_Utils_Date::customFormat($startDate)]), $this->_id); |
ed1c698a | 334 | } |
335 | ||
8345c9d3 | 336 | $this->assignBillingType(); |
6a488035 TO |
337 | |
338 | // check for is_monetary status | |
9c1bc317 | 339 | $isPayLater = $this->_values['is_pay_later'] ?? NULL; |
24cf7458 | 340 | if (!empty($this->_ccid)) { |
341 | $this->_values['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
342 | $this->_ccid, | |
343 | 'financial_type_id' | |
344 | ); | |
345 | if ($isPayLater) { | |
346 | $isPayLater = FALSE; | |
347 | $this->_values['is_pay_later'] = FALSE; | |
348 | } | |
37e877b2 | 349 | } |
bf7ae813 | 350 | if ($isPayLater) { |
351 | $this->setPayLaterLabel($this->_values['pay_later_text']); | |
352 | } | |
6a488035 | 353 | |
8cfdb972 EM |
354 | $this->_paymentProcessorIDs = array_filter(explode( |
355 | CRM_Core_DAO::VALUE_SEPARATOR, | |
356 | CRM_Utils_Array::value('payment_processor', $this->_values) | |
357 | )); | |
f92d1e2a | 358 | |
8cfdb972 | 359 | $this->assignPaymentProcessor($isPayLater); |
6a488035 TO |
360 | |
361 | // get price info | |
362 | // CRM-5095 | |
d1d16083 | 363 | $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id); |
364 | CRM_Price_BAO_PriceSet::initSet($this, 'civicrm_contribution_page', FALSE, $priceSetId); | |
6a488035 TO |
365 | |
366 | // this avoids getting E_NOTICE errors in php | |
be2fb01f | 367 | $setNullFields = [ |
6a488035 TO |
368 | 'is_allow_other_amount', |
369 | 'footer_text', | |
be2fb01f | 370 | ]; |
6a488035 TO |
371 | foreach ($setNullFields as $f) { |
372 | if (!isset($this->_values[$f])) { | |
373 | $this->_values[$f] = NULL; | |
374 | } | |
375 | } | |
376 | ||
377 | //check if Membership Block is enabled, if Membership Fields are included in profile | |
378 | //get membership section for this contribution page | |
379 | $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id); | |
380 | $this->set('membershipBlock', $this->_membershipBlock); | |
381 | ||
4779abb3 | 382 | if (!empty($this->_values['custom_pre_id'])) { |
6a488035 TO |
383 | $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']); |
384 | } | |
385 | ||
4779abb3 | 386 | if (!empty($this->_values['custom_post_id'])) { |
6a488035 TO |
387 | $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']); |
388 | } | |
389 | ||
9ac53857 | 390 | if (((isset($postProfileType) && $postProfileType === 'Membership') || |
391 | (isset($preProfileType) && $preProfileType === 'Membership') | |
6a488035 TO |
392 | ) && |
393 | !$this->_membershipBlock['is_active'] | |
394 | ) { | |
9ac53857 | 395 | CRM_Core_Error::statusBounce(ts('This page includes a Profile with Membership fields - but the Membership Block is NOT enabled. Please notify the site administrator.')); |
6a488035 TO |
396 | } |
397 | ||
398 | $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id); | |
399 | ||
400 | if ($pledgeBlock) { | |
9c1bc317 CW |
401 | $this->_values['pledge_block_id'] = $pledgeBlock['id'] ?? NULL; |
402 | $this->_values['max_reminders'] = $pledgeBlock['max_reminders'] ?? NULL; | |
403 | $this->_values['initial_reminder_day'] = $pledgeBlock['initial_reminder_day'] ?? NULL; | |
404 | $this->_values['additional_reminder_day'] = $pledgeBlock['additional_reminder_day'] ?? NULL; | |
6a488035 TO |
405 | |
406 | //set pledge id in values | |
407 | $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this); | |
408 | ||
409 | //authenticate pledge user for pledge payment. | |
410 | if ($pledgeId) { | |
411 | $this->_values['pledge_id'] = $pledgeId; | |
412 | ||
413 | //lets override w/ pledge campaign. | |
414 | $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', | |
415 | $pledgeId, | |
416 | 'campaign_id' | |
417 | ); | |
418 | self::authenticatePledgeUser(); | |
419 | } | |
420 | } | |
421 | $this->set('values', $this->_values); | |
422 | $this->set('fields', $this->_fields); | |
423 | } | |
424 | ||
425 | // Handle PCP | |
426 | $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this); | |
427 | if ($pcpId) { | |
353ffa53 TO |
428 | $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values); |
429 | $this->_pcpId = $pcp['pcpId']; | |
6a488035 | 430 | $this->_pcpBlock = $pcp['pcpBlock']; |
353ffa53 | 431 | $this->_pcpInfo = $pcp['pcpInfo']; |
6a488035 TO |
432 | } |
433 | ||
434 | // Link (button) for users to create their own Personal Campaign page | |
435 | if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) { | |
436 | $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign', | |
437 | "action=add&reset=1&pageId={$this->_id}&component=contribute", | |
438 | FALSE, NULL, TRUE | |
439 | ); | |
440 | $this->assign('linkTextUrl', $linkTextUrl); | |
441 | $this->assign('linkText', $linkText); | |
442 | } | |
443 | ||
444 | //set pledge block if block id is set | |
a7488080 | 445 | if (!empty($this->_values['pledge_block_id'])) { |
6a488035 TO |
446 | $this->assign('pledgeBlock', TRUE); |
447 | } | |
448 | ||
f92d1e2a | 449 | // check if one of the (amount , membership) blocks is active or not. |
6a488035 TO |
450 | $this->_membershipBlock = $this->get('membershipBlock'); |
451 | ||
1bec3b51 | 452 | if (!$this->isFormSupportsNonMembershipContributions() && |
6a488035 TO |
453 | !$this->_membershipBlock['is_active'] && |
454 | !$this->_priceSetId | |
455 | ) { | |
9ac53857 | 456 | CRM_Core_Error::statusBounce(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.')); |
6a488035 | 457 | } |
1bec3b51 EM |
458 | // This can probably go as nothing it 'getting it' anymore since the values data is loaded |
459 | // on every form, rather than being passed from form to form. | |
460 | $this->set('amount_block_is_active', $this->isFormSupportsNonMembershipContributions()); | |
6a488035 TO |
461 | |
462 | $this->_contributeMode = $this->get('contributeMode'); | |
463 | $this->assign('contributeMode', $this->_contributeMode); | |
464 | ||
465 | //assigning is_monetary and is_email_receipt to template | |
466 | $this->assign('is_monetary', $this->_values['is_monetary']); | |
467 | $this->assign('is_email_receipt', $this->_values['is_email_receipt']); | |
468 | $this->assign('bltID', $this->_bltID); | |
469 | ||
470 | //assign cancelSubscription URL to templates | |
471 | $this->assign('cancelSubscriptionUrl', | |
472 | CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values) | |
473 | ); | |
474 | ||
6489e3de SL |
475 | $title = !empty($this->_values['frontend_title']) ? $this->_values['frontend_title'] : $this->_values['title']; |
476 | ||
477 | $this->setTitle(($this->_pcpId ? $this->_pcpInfo['title'] : $title)); | |
be2fb01f | 478 | $this->_defaults = []; |
6a488035 TO |
479 | |
480 | $this->_amount = $this->get('amount'); | |
d02c6418 | 481 | // Assigning this to the template means it will be passed through to the payment form. |
482 | // This can, for example, by used by payment processors using client side encryption | |
483 | $this->assign('currency', $this->getCurrency()); | |
6a488035 | 484 | |
85939a77 | 485 | CRM_Contribute_BAO_Contribution_Utils::overrideDefaultCurrency($this->_values); |
6a488035 TO |
486 | |
487 | //lets allow user to override campaign. | |
488 | $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this); | |
489 | if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) { | |
490 | $this->_values['campaign_id'] = $campID; | |
491 | } | |
492 | ||
493 | //do check for cancel recurring and clean db, CRM-7696 | |
a3d827a7 | 494 | if (CRM_Utils_Request::retrieve('cancel', 'Boolean')) { |
6a488035 TO |
495 | self::cancelRecurring(); |
496 | } | |
8ae4d0d3 | 497 | |
498 | // check if billing block is required for pay later | |
f3acfdd9 | 499 | if (!empty($this->_values['is_pay_later'])) { |
9c1bc317 | 500 | $this->_isBillingAddressRequiredForPayLater = $this->_values['is_billing_required'] ?? NULL; |
8ae4d0d3 | 501 | $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater); |
502 | } | |
6a488035 TO |
503 | } |
504 | ||
505 | /** | |
fe482240 | 506 | * Set the default values. |
6a488035 | 507 | */ |
00be9182 | 508 | public function setDefaultValues() { |
6a488035 TO |
509 | return $this->_defaults; |
510 | } | |
511 | ||
512 | /** | |
fe482240 | 513 | * Assign the minimal set of variables to the template. |
6a488035 | 514 | */ |
00be9182 | 515 | public function assignToTemplate() { |
0b50eca0 | 516 | $this->set('name', $this->assignBillingName($this->_params)); |
6a488035 TO |
517 | |
518 | $this->assign('paymentProcessor', $this->_paymentProcessor); | |
be2fb01f | 519 | $vars = [ |
353ffa53 TO |
520 | 'amount', |
521 | 'currencyID', | |
522 | 'credit_card_type', | |
523 | 'trxn_id', | |
524 | 'amount_level', | |
be2fb01f | 525 | ]; |
6a488035 | 526 | |
8cc574cf | 527 | if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) { |
6a488035 | 528 | $this->assign('is_recur_enabled', 1); |
be2fb01f | 529 | $vars = array_merge($vars, [ |
353ffa53 TO |
530 | 'is_recur', |
531 | 'frequency_interval', | |
532 | 'frequency_unit', | |
533 | 'installments', | |
be2fb01f | 534 | ]); |
6a488035 TO |
535 | } |
536 | ||
e46f73c7 CW |
537 | if (CRM_Core_Component::isEnabled('CiviPledge') && |
538 | !empty($this->_params['is_pledge']) | |
6a488035 | 539 | ) { |
e46f73c7 | 540 | // TODO: Assigned variable appears to be unused |
6a488035 TO |
541 | $this->assign('pledge_enabled', 1); |
542 | ||
be2fb01f | 543 | $vars = array_merge($vars, [ |
6a488035 | 544 | 'is_pledge', |
353ffa53 TO |
545 | 'pledge_frequency_interval', |
546 | 'pledge_frequency_unit', | |
547 | 'pledge_installments', | |
be2fb01f | 548 | ]); |
6a488035 TO |
549 | } |
550 | ||
c039f658 | 551 | // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel |
552 | // function to get correct amount level consistently. Remove setting of the amount level in | |
553 | // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest | |
554 | // to cover all variants. | |
6a488035 TO |
555 | if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) { |
556 | $this->_params['amount_level'] = ''; | |
557 | } | |
558 | ||
559 | foreach ($vars as $v) { | |
3fb990f4 | 560 | if (isset($this->_params[$v])) { |
735fe42d PJ |
561 | if ($v == "amount" && $this->_params[$v] === 0) { |
562 | $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE); | |
3fb990f4 | 563 | } |
6a488035 TO |
564 | $this->assign($v, $this->_params[$v]); |
565 | } | |
566 | } | |
567 | ||
0b50eca0 | 568 | $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( |
569 | $this->_params, | |
570 | $this->_bltID | |
571 | )); | |
6a488035 | 572 | |
cb804cd9 | 573 | if (!empty($this->_params['onbehalf_profile_id']) && !empty($this->_params['onbehalf'])) { |
6a488035 TO |
574 | $this->assign('onBehalfName', $this->_params['organization_name']); |
575 | $locTypeId = array_keys($this->_params['onbehalf_location']['email']); | |
576 | $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']); | |
577 | } | |
a050fa6b | 578 | $this->assignPaymentFields(); |
d90e6c3b | 579 | $this->assignEmailField(); |
6a488035 TO |
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 | ||
d90e6c3b JP |
587 | /** |
588 | * Assign email variable in the template. | |
589 | */ | |
590 | public function assignEmailField() { | |
591 | //If email exist in a profile, the default billing email field is not loaded on the page. | |
592 | //Hence, assign the existing location type email by iterating through the params. | |
238ea0e4 JP |
593 | if ($this->_emailExists && empty($this->_params["email-{$this->_bltID}"])) { |
594 | foreach ($this->_params as $key => $val) { | |
9ac53857 | 595 | if (substr($key, 0, 6) === 'email-') { |
238ea0e4 | 596 | $this->assign('email', $this->_params[$key]); |
d90e6c3b | 597 | break; |
238ea0e4 JP |
598 | } |
599 | } | |
600 | } | |
601 | else { | |
602 | $this->assign('email', CRM_Utils_Array::value("email-{$this->_bltID}", $this->_params)); | |
603 | } | |
6a488035 TO |
604 | } |
605 | ||
606 | /** | |
fe482240 | 607 | * Add the custom fields. |
6a488035 | 608 | * |
100fef9d CW |
609 | * @param int $id |
610 | * @param string $name | |
f4aaa82a EM |
611 | * @param bool $viewOnly |
612 | * @param null $profileContactType | |
f92d1e2a | 613 | * @param array $fieldTypes |
6a488035 | 614 | */ |
00be9182 | 615 | public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) { |
6a488035 | 616 | if ($id) { |
da8d9879 | 617 | $contactID = $this->getContactID(); |
6a488035 TO |
618 | |
619 | // we don't allow conflicting fields to be | |
620 | // configured via profile - CRM 2100 | |
be2fb01f | 621 | $fieldsToIgnore = [ |
6a488035 TO |
622 | 'receive_date' => 1, |
623 | 'trxn_id' => 1, | |
624 | 'invoice_id' => 1, | |
625 | 'net_amount' => 1, | |
626 | 'fee_amount' => 1, | |
627 | 'non_deductible_amount' => 1, | |
628 | 'total_amount' => 1, | |
629 | 'amount_level' => 1, | |
630 | 'contribution_status_id' => 1, | |
7bc1d4da | 631 | // @todo replace payment_instrument with payment instrument id. |
632 | // both are available now but the id field is the most consistent. | |
6a488035 | 633 | 'payment_instrument' => 1, |
7bc1d4da | 634 | 'payment_instrument_id' => 1, |
635 | 'contribution_check_number' => 1, | |
6a488035 | 636 | 'financial_type' => 1, |
be2fb01f | 637 | ]; |
6a488035 | 638 | |
423b9af4 | 639 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE, |
640 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL | |
641 | ); | |
6a488035 TO |
642 | |
643 | if ($fields) { | |
0c7c9ff7 KC |
644 | // determine if email exists in profile so we know if we need to manually insert CRM-2888, CRM-15067 |
645 | foreach ($fields as $key => $field) { | |
646 | if (substr($key, 0, 6) == 'email-' && | |
be2fb01f | 647 | !in_array($profileContactType, ['honor', 'onbehalf']) |
0c7c9ff7 KC |
648 | ) { |
649 | $this->_emailExists = TRUE; | |
67c998f4 | 650 | $this->set('emailExists', TRUE); |
6a488035 TO |
651 | } |
652 | } | |
653 | ||
654 | if (array_intersect_key($fields, $fieldsToIgnore)) { | |
655 | $fields = array_diff_key($fields, $fieldsToIgnore); | |
656 | CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert'); | |
657 | } | |
658 | ||
d7d630be | 659 | //remove common fields only if profile is not configured for onbehalf/honor |
be2fb01f | 660 | if (!in_array($profileContactType, ['honor', 'onbehalf'])) { |
94e01524 | 661 | $fields = array_diff_key($fields, $this->_fields); |
d7d630be | 662 | } |
6a488035 TO |
663 | |
664 | CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID); | |
665 | $addCaptcha = FALSE; | |
f3726153 | 666 | // fetch file preview when not submitted yet, like in online contribution Confirm and ThankYou page |
be2fb01f | 667 | $viewOnlyFileValues = empty($profileContactType) ? [] : [$profileContactType => []]; |
6a488035 TO |
668 | foreach ($fields as $key => $field) { |
669 | if ($viewOnly && | |
670 | isset($field['data_type']) && | |
671 | $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL') | |
672 | ) { | |
c2292dbd | 673 | //retrieve file value from submitted values on basis of $profileContactType |
9c1bc317 | 674 | $fileValue = $this->_params[$key] ?? NULL; |
9b7bedc5 | 675 | if (!empty($profileContactType) && !empty($this->_params[$profileContactType])) { |
9c1bc317 | 676 | $fileValue = $this->_params[$profileContactType][$key] ?? NULL; |
9b7bedc5 | 677 | } |
c2292dbd | 678 | |
679 | if ($fileValue) { | |
9c1bc317 CW |
680 | $path = $fileValue['name'] ?? NULL; |
681 | $fileType = $fileValue['type'] ?? NULL; | |
c2292dbd | 682 | $fileValue = CRM_Utils_File::getFileURL($path, $fileType); |
f3726153 | 683 | } |
6a488035 | 684 | |
c2292dbd | 685 | // format custom file value fetched from submitted value |
686 | if ($profileContactType) { | |
687 | $viewOnlyFileValues[$profileContactType][$key] = $fileValue; | |
688 | } | |
689 | else { | |
690 | $viewOnlyFileValues[$key] = $fileValue; | |
691 | } | |
1aedfc98 | 692 | |
693 | // On viewOnly use-case (as in online contribution Confirm page) we no longer need to set | |
694 | // required property because being required file is already uploaded while registration | |
695 | $field['is_required'] = FALSE; | |
c2292dbd | 696 | } |
133e2c99 | 697 | if ($profileContactType) { |
698 | //Since we are showing honoree name separately so we are removing it from honoree profile just for display | |
c043358f | 699 | if ($profileContactType == 'honor') { |
be2fb01f | 700 | $honoreeNamefields = [ |
c043358f | 701 | 'prefix_id', |
702 | 'first_name', | |
703 | 'last_name', | |
704 | 'suffix_id', | |
705 | 'organization_name', | |
706 | 'household_name', | |
be2fb01f | 707 | ]; |
c043358f | 708 | if (in_array($field['name'], $honoreeNamefields)) { |
709 | unset($fields[$field['name']]); | |
710 | continue; | |
711 | } | |
133e2c99 | 712 | } |
6a488035 TO |
713 | if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) { |
714 | CRM_Core_BAO_UFGroup::buildProfile( | |
715 | $this, | |
716 | $field, | |
717 | CRM_Profile_Form::MODE_CREATE, | |
718 | $contactID, | |
133e2c99 | 719 | TRUE, |
720 | $profileContactType | |
6a488035 | 721 | ); |
133e2c99 | 722 | $this->_fields[$profileContactType][$key] = $field; |
6a488035 TO |
723 | } |
724 | else { | |
725 | unset($fields[$key]); | |
726 | } | |
727 | } | |
728 | else { | |
729 | CRM_Core_BAO_UFGroup::buildProfile( | |
730 | $this, | |
731 | $field, | |
732 | CRM_Profile_Form::MODE_CREATE, | |
733 | $contactID, | |
734 | TRUE | |
735 | ); | |
736 | $this->_fields[$key] = $field; | |
737 | } | |
71fc6ea4 DG |
738 | // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor |
739 | if ($field['add_captcha'] && !$this->_userID) { | |
6a488035 TO |
740 | $addCaptcha = TRUE; |
741 | } | |
742 | } | |
743 | ||
744 | $this->assign($name, $fields); | |
745 | ||
6d43e496 | 746 | if ($profileContactType && count($viewOnlyFileValues[$profileContactType])) { |
747 | $this->assign('viewOnlyPrefixFileValues', $viewOnlyFileValues); | |
748 | } | |
749 | elseif (count($viewOnlyFileValues)) { | |
f3726153 | 750 | $this->assign('viewOnlyFileValues', $viewOnlyFileValues); |
751 | } | |
752 | ||
6a488035 | 753 | if ($addCaptcha && !$viewOnly) { |
cc50ef3a | 754 | CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this); |
6a488035 TO |
755 | } |
756 | } | |
757 | } | |
758 | } | |
759 | ||
9ac53857 | 760 | /** |
761 | * Assign payment field information to the template. | |
762 | * | |
763 | * @throws \CRM_Core_Exception | |
764 | * @throws \CiviCRM_API3_Exception | |
765 | */ | |
a050fa6b MWMC |
766 | public function assignPaymentFields() { |
767 | //fix for CRM-3767 | |
768 | $isMonetary = FALSE; | |
769 | if ($this->_amount > 0.0) { | |
770 | $isMonetary = TRUE; | |
771 | } | |
772 | elseif (!empty($this->_params['selectMembership'])) { | |
773 | $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee'); | |
774 | if ($memFee > 0.0) { | |
775 | $isMonetary = TRUE; | |
776 | } | |
777 | } | |
778 | ||
779 | // The concept of contributeMode is deprecated. | |
780 | // The payment processor object can provide info about the fields it shows. | |
781 | if ($isMonetary && is_a($this->_paymentProcessor['object'], 'CRM_Core_Payment')) { | |
9ac53857 | 782 | /** @var \CRM_Core_Payment $paymentProcessorObject */ |
a050fa6b MWMC |
783 | $paymentProcessorObject = $this->_paymentProcessor['object']; |
784 | ||
785 | $paymentFields = $paymentProcessorObject->getPaymentFormFields(); | |
786 | foreach ($paymentFields as $index => $paymentField) { | |
787 | if (!isset($this->_params[$paymentField])) { | |
788 | unset($paymentFields[$index]); | |
789 | continue; | |
790 | } | |
791 | if ($paymentField === 'credit_card_exp_date') { | |
792 | $date = CRM_Utils_Date::format(CRM_Utils_Array::value('credit_card_exp_date', $this->_params)); | |
793 | $date = CRM_Utils_Date::mysqlToIso($date); | |
794 | $this->assign('credit_card_exp_date', $date); | |
795 | } | |
796 | elseif ($paymentField === 'credit_card_number') { | |
797 | $this->assign('credit_card_number', | |
798 | CRM_Utils_System::mungeCreditCard(CRM_Utils_Array::value('credit_card_number', $this->_params)) | |
799 | ); | |
800 | } | |
801 | elseif ($paymentField === 'credit_card_type') { | |
802 | $this->assign('credit_card_type', CRM_Core_PseudoConstant::getLabel( | |
803 | 'CRM_Core_BAO_FinancialTrxn', | |
804 | 'card_type_id', | |
805 | CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'card_type_id', $this->_params['credit_card_type']) | |
806 | )); | |
807 | } | |
808 | else { | |
809 | $this->assign($paymentField, $this->_params[$paymentField]); | |
810 | } | |
811 | } | |
812 | $this->assign('paymentFieldsetLabel', CRM_Core_Payment_Form::getPaymentLabel($paymentProcessorObject)); | |
813 | $this->assign('paymentFields', $paymentFields); | |
814 | ||
815 | } | |
816 | } | |
817 | ||
4779abb3 | 818 | /** |
819 | * Add onbehalf/honoree profile fields and native module fields. | |
820 | * | |
821 | * @param int $id | |
822 | * @param CRM_Core_Form $form | |
9ac53857 | 823 | * |
5903c058 | 824 | * @throws \API_Exception |
9ac53857 | 825 | * @throws \CRM_Core_Exception |
5903c058 | 826 | * @throws \CiviCRM_API3_Exception |
4779abb3 | 827 | */ |
5903c058 | 828 | public function buildComponentForm($id, $form): void { |
bcb8cc84 | 829 | if (empty($id)) { |
830 | return; | |
831 | } | |
832 | ||
833 | $contactID = $this->getContactID(); | |
834 | ||
be2fb01f | 835 | foreach (['soft_credit', 'on_behalf'] as $module) { |
9ac53857 | 836 | if ($module === 'soft_credit') { |
4779abb3 | 837 | if (empty($form->_values['honoree_profile_id'])) { |
838 | continue; | |
839 | } | |
bcb8cc84 | 840 | |
4779abb3 | 841 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['honoree_profile_id'], 'is_active')) { |
9ac53857 | 842 | CRM_Core_Error::statusBounce(ts('This contribution page has been configured for contribution on behalf of honoree and the selected honoree profile is either disabled or not found.')); |
bcb8cc84 | 843 | } |
844 | ||
4779abb3 | 845 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']); |
be2fb01f CW |
846 | $requiredProfileFields = [ |
847 | 'Individual' => ['first_name', 'last_name'], | |
848 | 'Organization' => ['organization_name', 'email'], | |
849 | 'Household' => ['household_name', 'email'], | |
850 | ]; | |
4779abb3 | 851 | $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_values['honoree_profile_id'], $requiredProfileFields[$profileContactType]); |
bcb8cc84 | 852 | if (!$validProfile) { |
9ac53857 | 853 | CRM_Core_Error::statusBounce(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.')); |
bcb8cc84 | 854 | } |
855 | ||
be2fb01f | 856 | foreach (['honor_block_title', 'honor_block_text'] as $name) { |
4779abb3 | 857 | $form->assign($name, $form->_values[$name]); |
cb804cd9 | 858 | } |
859 | ||
860 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); | |
861 | ||
862 | // radio button for Honor Type | |
4779abb3 | 863 | foreach ($form->_values['soft_credit_types'] as $value) { |
ca5603d5 | 864 | $honorTypes[$value] = $softCreditTypes[$value]; |
cb804cd9 | 865 | } |
ca5603d5 | 866 | $form->addRadio('soft_credit_type_id', NULL, $honorTypes, ['allowClear' => TRUE]); |
cb804cd9 | 867 | |
4779abb3 | 868 | $honoreeProfileFields = CRM_Core_BAO_UFGroup::getFields( |
869 | $this->_values['honoree_profile_id'], FALSE, | |
870 | NULL, NULL, | |
871 | NULL, FALSE, | |
872 | NULL, TRUE, | |
873 | NULL, CRM_Core_Permission::CREATE | |
cb804cd9 | 874 | ); |
cb804cd9 | 875 | $form->assign('honoreeProfileFields', $honoreeProfileFields); |
876 | ||
877 | // add the form elements | |
878 | foreach ($honoreeProfileFields as $name => $field) { | |
879 | // If soft credit type is not chosen then make omit requiredness from honoree profile fields | |
880 | if (count($form->_submitValues) && | |
881 | empty($form->_submitValues['soft_credit_type_id']) && | |
882 | !empty($field['is_required']) | |
883 | ) { | |
884 | $field['is_required'] = FALSE; | |
885 | } | |
c043358f | 886 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, NULL, FALSE, FALSE, NULL, 'honor'); |
cb804cd9 | 887 | } |
bcb8cc84 | 888 | } |
889 | else { | |
4779abb3 | 890 | if (empty($form->_values['onbehalf_profile_id'])) { |
891 | continue; | |
892 | } | |
bcb8cc84 | 893 | |
4779abb3 | 894 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['onbehalf_profile_id'], 'is_active')) { |
9ac53857 | 895 | CRM_Core_Error::statusBounce(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.')); |
bcb8cc84 | 896 | } |
897 | ||
898 | $member = CRM_Member_BAO_Membership::getMembershipBlock($form->_id); | |
899 | if (empty($member['is_active'])) { | |
900 | $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.'); | |
4779abb3 | 901 | $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($form->_values['onbehalf_profile_id']); |
0c7c9ff7 | 902 | foreach ( |
be2fb01f | 903 | [ |
bcb8cc84 | 904 | 'Individual', |
905 | 'Organization', | |
906 | 'Household', | |
be2fb01f | 907 | ] as $contactType |
0c7c9ff7 | 908 | ) { |
bcb8cc84 | 909 | if (in_array($contactType, $onBehalfProfile) && |
910 | (in_array('Membership', $onBehalfProfile) || | |
911 | in_array('Contribution', $onBehalfProfile) | |
912 | ) | |
913 | ) { | |
9ac53857 | 914 | CRM_Core_Error::statusBounce($msg); |
bcb8cc84 | 915 | } |
916 | } | |
917 | } | |
918 | ||
c043358f | 919 | if ($contactID) { |
33260076 | 920 | // retrieve all permissioned organizations of contact $contactID |
921 | $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($contactID, NULL, NULL, 'Organization'); | |
c043358f | 922 | |
33260076 | 923 | if (count($organizations)) { |
c043358f | 924 | // Related org url - pass checksum if needed |
be2fb01f | 925 | $args = [ |
4779abb3 | 926 | 'ufId' => $form->_values['onbehalf_profile_id'], |
f23093b2 | 927 | 'cid' => '', |
be2fb01f | 928 | ]; |
c043358f | 929 | if (!empty($_GET['cs'])) { |
be2fb01f | 930 | $args = [ |
4779abb3 | 931 | 'ufId' => $form->_values['onbehalf_profile_id'], |
c043358f | 932 | 'uid' => $this->_contactID, |
933 | 'cs' => $_GET['cs'], | |
934 | 'cid' => '', | |
be2fb01f | 935 | ]; |
c043358f | 936 | } |
937 | $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE); | |
938 | $form->assign('locDataURL', $locDataURL); | |
939 | } | |
33260076 | 940 | if (count($organizations) > 0) { |
941 | $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $organizations)); | |
c043358f | 942 | |
be2fb01f | 943 | $orgOptions = [ |
c043358f | 944 | 0 => ts('Select an existing organization'), |
945 | 1 => ts('Enter a new organization'), | |
be2fb01f | 946 | ]; |
c043358f | 947 | $form->addRadio('org_option', ts('options'), $orgOptions); |
be2fb01f | 948 | $form->setDefaults(['org_option' => 0]); |
c043358f | 949 | } |
bcb8cc84 | 950 | } |
951 | ||
5903c058 | 952 | $form->assign('fieldSetTitle', CRM_Core_BAO_UFGroup::getFrontEndTitle($form->_values['onbehalf_profile_id'])); |
c043358f | 953 | |
f3acfdd9 | 954 | if (!empty($form->_values['is_for_organization'])) { |
4779abb3 | 955 | if ($form->_values['is_for_organization'] == 2) { |
c043358f | 956 | $form->assign('onBehalfRequired', TRUE); |
bcb8cc84 | 957 | } |
958 | else { | |
959 | $form->addElement('checkbox', 'is_for_organization', | |
960 | $form->_values['for_organization'], | |
961 | NULL | |
962 | ); | |
963 | } | |
964 | } | |
c043358f | 965 | |
4779abb3 | 966 | $profileFields = CRM_Core_BAO_UFGroup::getFields( |
967 | $form->_values['onbehalf_profile_id'], | |
968 | FALSE, CRM_Core_Action::VIEW, NULL, | |
969 | NULL, FALSE, NULL, FALSE, NULL, | |
970 | CRM_Core_Permission::CREATE, NULL | |
c043358f | 971 | ); |
4779abb3 | 972 | |
c043358f | 973 | $form->assign('onBehalfOfFields', $profileFields); |
974 | if (!empty($form->_submitValues['onbehalf'])) { | |
975 | if (!empty($form->_submitValues['onbehalfof_id'])) { | |
976 | $form->assign('submittedOnBehalf', $form->_submitValues['onbehalfof_id']); | |
977 | } | |
d425f39b | 978 | $form->assign('submittedOnBehalfInfo', json_encode(str_replace('"', '\"', $form->_submitValues['onbehalf']), JSON_HEX_APOS)); |
c043358f | 979 | } |
980 | ||
be2fb01f | 981 | $fieldTypes = ['Contact', 'Organization']; |
a6a083d8 | 982 | if (!empty($form->_membershipBlock)) { |
be2fb01f | 983 | $fieldTypes = array_merge($fieldTypes, ['Membership']); |
a6a083d8 | 984 | } |
c043358f | 985 | $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); |
986 | $fieldTypes = array_merge($fieldTypes, $contactSubType); | |
987 | ||
988 | foreach ($profileFields as $name => $field) { | |
989 | if (in_array($field['field_type'], $fieldTypes)) { | |
5903c058 | 990 | [$prefixName, $index] = CRM_Utils_System::explode('-', $name, 2); |
be2fb01f | 991 | if (in_array($prefixName, ['organization_name', 'email']) && empty($field['is_required'])) { |
c043358f | 992 | $field['is_required'] = 1; |
993 | } | |
994 | if (count($form->_submitValues) && | |
995 | empty($form->_submitValues['is_for_organization']) && | |
4779abb3 | 996 | $form->_values['is_for_organization'] == 1 && |
c043358f | 997 | !empty($field['is_required']) |
998 | ) { | |
999 | $field['is_required'] = FALSE; | |
1000 | } | |
4779abb3 | 1001 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, NULL, NULL, FALSE, 'onbehalf', NULL, 'onbehalf'); |
c043358f | 1002 | } |
1003 | } | |
bcb8cc84 | 1004 | } |
1005 | } | |
1006 | ||
1007 | } | |
1008 | ||
f4aaa82a | 1009 | /** |
fe482240 | 1010 | * Check template file exists. |
f92d1e2a | 1011 | * |
d7f0cce6 | 1012 | * @param string|null $suffix |
f4aaa82a | 1013 | * |
d7f0cce6 BT |
1014 | * @return string|null |
1015 | * Template file path, else null | |
f4aaa82a | 1016 | */ |
00be9182 | 1017 | public function checkTemplateFileExists($suffix = NULL) { |
6a488035 TO |
1018 | if ($this->_id) { |
1019 | $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl"; | |
1020 | $template = CRM_Core_Form::getTemplate(); | |
1021 | if ($template->template_exists($templateFile)) { | |
1022 | return $templateFile; | |
1023 | } | |
1024 | } | |
1025 | return NULL; | |
1026 | } | |
1027 | ||
186c9c17 | 1028 | /** |
fe482240 | 1029 | * Use the form name to create the tpl file name. |
186c9c17 EM |
1030 | * |
1031 | * @return string | |
186c9c17 | 1032 | */ |
00be9182 | 1033 | public function getTemplateFileName() { |
6a488035 | 1034 | $fileName = $this->checkTemplateFileExists(); |
9ac53857 | 1035 | return $fileName ?: parent::getTemplateFileName(); |
6a488035 TO |
1036 | } |
1037 | ||
186c9c17 | 1038 | /** |
f92d1e2a EM |
1039 | * Add the extra.tpl in. |
1040 | * | |
186c9c17 | 1041 | * Default extra tpl file basically just replaces .tpl with .extra.tpl |
f92d1e2a | 1042 | * i.e. we do not override - why isn't this done at the CRM_Core_Form level? |
186c9c17 EM |
1043 | * |
1044 | * @return string | |
186c9c17 | 1045 | */ |
00be9182 | 1046 | public function overrideExtraTemplateFileName() { |
6a488035 TO |
1047 | $fileName = $this->checkTemplateFileExists('extra.'); |
1048 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); | |
1049 | } | |
1050 | ||
1051 | /** | |
100fef9d | 1052 | * Authenticate pledge user during online payment. |
9ac53857 | 1053 | * |
1054 | * @throws \CRM_Core_Exception | |
6a488035 TO |
1055 | */ |
1056 | public function authenticatePledgeUser() { | |
1057 | //get the userChecksum and contact id | |
1058 | $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this); | |
1059 | $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
1060 | ||
1061 | //get pledge status and contact id | |
be2fb01f CW |
1062 | $pledgeValues = []; |
1063 | $pledgeParams = ['id' => $this->_values['pledge_id']]; | |
1064 | $returnProperties = ['contact_id', 'status_id']; | |
6a488035 TO |
1065 | CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties); |
1066 | ||
1067 | //get all status | |
1068 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
be2fb01f | 1069 | $validStatus = [ |
353ffa53 | 1070 | array_search('Pending', $allStatus), |
6a488035 TO |
1071 | array_search('In Progress', $allStatus), |
1072 | array_search('Overdue', $allStatus), | |
be2fb01f | 1073 | ]; |
6a488035 TO |
1074 | |
1075 | $validUser = FALSE; | |
1076 | if ($this->_userID && | |
1077 | $this->_userID == $pledgeValues['contact_id'] | |
1078 | ) { | |
1079 | //check for authenticated user. | |
1080 | $validUser = TRUE; | |
1081 | } | |
1082 | elseif ($userChecksum && $pledgeValues['contact_id']) { | |
1083 | //check for anonymous user. | |
1084 | $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum); | |
1085 | ||
1086 | //make sure cid is same as pledge contact id | |
1087 | if ($validUser && ($pledgeValues['contact_id'] != $contactID)) { | |
1088 | $validUser = FALSE; | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | if (!$validUser) { | |
9ac53857 | 1093 | CRM_Core_Error::statusBounce(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.")); |
6a488035 TO |
1094 | } |
1095 | ||
1096 | //check for valid pledge status. | |
1097 | if (!in_array($pledgeValues['status_id'], $validStatus)) { | |
9ac53857 | 1098 | CRM_Core_Error::statusBounce(ts('Oops. You cannot make a payment for this pledge - pledge status is %1.', [1 => CRM_Utils_Array::value($pledgeValues['status_id'], $allStatus)])); |
6a488035 TO |
1099 | } |
1100 | } | |
1101 | ||
1102 | /** | |
f92d1e2a EM |
1103 | * Cancel recurring contributions. |
1104 | * | |
6a488035 TO |
1105 | * In case user cancel recurring contribution, |
1106 | * When we get the control back from payment gate way | |
1107 | * lets delete the recurring and related contribution. | |
9ac53857 | 1108 | * |
1109 | * @throws \CRM_Core_Exception | |
389bcebf | 1110 | */ |
6a488035 | 1111 | public function cancelRecurring() { |
a3d827a7 | 1112 | $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean'); |
6a488035 | 1113 | if ($isCancel) { |
a3d827a7 CW |
1114 | $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean'); |
1115 | $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive'); | |
6a488035 TO |
1116 | //clean db for recurring contribution. |
1117 | if ($isRecur && $recurId) { | |
1118 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId); | |
1119 | } | |
a3d827a7 | 1120 | $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive'); |
6a488035 TO |
1121 | if ($contribId) { |
1122 | CRM_Contribute_BAO_Contribution::deleteContribution($contribId); | |
1123 | } | |
1124 | } | |
1125 | } | |
96025800 | 1126 | |
90102a32 EM |
1127 | /** |
1128 | * Determine if recurring parameters need to be added to the form parameters. | |
8cb12ff1 | 1129 | * |
90102a32 EM |
1130 | * - is_recur |
1131 | * - frequency_interval | |
1132 | * - frequency_unit | |
1133 | * | |
1134 | * For membership this is based on the membership type. | |
1135 | * | |
1136 | * This needs to be done before processing the pre-approval redirect where relevant on the main page or before any payment processing. | |
1137 | * | |
1138 | * Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout. | |
1139 | */ | |
1140 | protected function setRecurringMembershipParams() { | |
c656704b JG |
1141 | $priceFieldId = array_key_first($this->_values['fee']); |
1142 | // Why is this an array in CRM_Contribute_Form_Contribution_Main::submit and a string in CRM_Contribute_Form_Contribution_Confirm::preProcess()? | |
1143 | if (is_array($this->_params["price_{$priceFieldId}"])) { | |
1144 | $priceFieldValue = array_key_first($this->_params["price_{$priceFieldId}"]); | |
1145 | } | |
1146 | else { | |
1147 | $priceFieldValue = $this->_params["price_{$priceFieldId}"]; | |
1148 | } | |
1149 | $selectedMembershipTypeID = $this->_values['fee'][$priceFieldId]['options'][$priceFieldValue]['membership_type_id'] ?? NULL; | |
1150 | if (!$selectedMembershipTypeID) { | |
1151 | return; | |
fd359255 | 1152 | } |
90102a32 | 1153 | |
c656704b JG |
1154 | $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($this->_priceSetId); |
1155 | if (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_required']) | |
1156 | || (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_optional']) && | |
1157 | !empty($this->_params['is_recur'])) | |
1158 | && !empty($this->_paymentProcessor['is_recur']) | |
1159 | ) { | |
1160 | $this->_params['auto_renew'] = TRUE; | |
90102a32 | 1161 | $this->_params['is_recur'] = $this->_values['is_recur'] = 1; |
c656704b JG |
1162 | $membershipTypeDetails = \Civi\Api4\MembershipType::get(FALSE) |
1163 | ->addWhere('id', '=', $selectedMembershipTypeID) | |
1164 | ->execute() | |
1165 | ->first(); | |
1166 | $this->_params['frequency_interval'] = $this->_params['frequency_interval'] ?? $this->_values['fee'][$priceFieldId]['options'][$priceFieldValue]['membership_num_terms']; | |
1167 | $this->_params['frequency_unit'] = $this->_params['frequency_unit'] ?? $membershipTypeDetails['duration_unit']; | |
90102a32 EM |
1168 | } |
1169 | } | |
1170 | ||
e364c762 | 1171 | /** |
1172 | * Get the payment processor object for the submission, returning the manual one for offline payments. | |
1173 | * | |
1174 | * @return CRM_Core_Payment | |
1175 | */ | |
1176 | protected function getPaymentProcessorObject() { | |
1177 | if (!empty($this->_paymentProcessor)) { | |
1178 | return $this->_paymentProcessor['object']; | |
1179 | } | |
1180 | return new CRM_Core_Payment_Manual(); | |
1181 | } | |
1182 | ||
ec57bf87 | 1183 | /** |
1184 | * Get the amount for the main contribution. | |
1185 | * | |
1186 | * The goal is to expand this function so that all the argy-bargy of figuring out the amount | |
1187 | * winds up here as the main spaghetti shrinks. | |
1188 | * | |
1189 | * If there is a separate membership contribution this is the 'other one'. Otherwise there | |
1190 | * is only one. | |
1191 | * | |
1192 | * @param $params | |
1193 | * | |
1194 | * @return float | |
1195 | * | |
1196 | * @throws \CiviCRM_API3_Exception | |
1197 | */ | |
1198 | protected function getMainContributionAmount($params) { | |
1199 | if (!empty($params['selectMembership'])) { | |
1200 | if (empty($params['amount']) && !$this->_separateMembershipPayment) { | |
1201 | return CRM_Member_BAO_MembershipType::getMembershipType($params['selectMembership'])['minimum_fee'] ?? 0; | |
1202 | } | |
1203 | } | |
1204 | return $params['amount'] ?? 0; | |
1205 | } | |
1206 | ||
bdcbbfea | 1207 | /** |
1208 | * Wrapper for processAmount that also sets autorenew. | |
1209 | * | |
1210 | * @param $fields | |
1211 | * This is the output of the function CRM_Price_BAO_PriceSet::getSetDetail($priceSetID, FALSE, FALSE); | |
1212 | * And, it would make sense to introduce caching into that function and call it from here rather than | |
1213 | * require the $fields array which is passed from pillar to post around the form in order to pass it in here. | |
1214 | * @param array $params | |
1215 | * Params reflecting form input e.g with fields 'price_5' => 7, 'price_8' => array(7, 8) | |
1216 | * @param $lineItems | |
1217 | * Line item array to be altered. | |
1218 | * @param int $priceSetID | |
1219 | */ | |
1220 | public function processAmountAndGetAutoRenew($fields, &$params, &$lineItems, $priceSetID = NULL) { | |
1221 | CRM_Price_BAO_PriceSet::processAmount($fields, $params, $lineItems, $priceSetID); | |
1222 | $autoRenew = []; | |
1223 | $autoRenew[0] = $autoRenew[1] = $autoRenew[2] = 0; | |
1224 | foreach ($lineItems as $lineItem) { | |
1225 | if (!empty($lineItem['auto_renew']) && | |
1226 | is_numeric($lineItem['auto_renew']) | |
1227 | ) { | |
1228 | $autoRenew[$lineItem['auto_renew']] += $lineItem['line_total']; | |
1229 | } | |
1230 | } | |
1231 | if (count($autoRenew) > 1) { | |
1232 | $params['autoRenew'] = $autoRenew; | |
1233 | } | |
1234 | } | |
1235 | ||
1bec3b51 EM |
1236 | /** |
1237 | * Is payment for (non membership) contributions enabled on this form. | |
1238 | * | |
1239 | * This would be true in a case of contributions only or where both | |
1240 | * memberships and non-membership contributions are enabled (whether they | |
1241 | * are using quick config price sets or explicit price sets). | |
1242 | * | |
1243 | * The value is a database value in the config for the contribution page. It | |
1244 | * is loaded into values in ContributionBase::preProcess (called by this). | |
1245 | * | |
1246 | * @internal function is public to support validate but is for core use only. | |
1247 | * | |
1248 | * @return bool | |
1249 | */ | |
1250 | public function isFormSupportsNonMembershipContributions(): bool { | |
1251 | return (bool) ($this->_values['amount_block_is_active'] ?? FALSE); | |
1252 | } | |
1253 | ||
6a488035 | 1254 | } |