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 | ||
c59e7960 | 210 | /** |
211 | * Is the price set quick config. | |
212 | * @return bool | |
213 | */ | |
214 | public function isQuickConfig() { | |
77c21b32 | 215 | return self::$_quickConfig ?? FALSE; |
c59e7960 | 216 | } |
217 | ||
6a488035 | 218 | /** |
fe482240 | 219 | * Set variables up before form is built. |
6a488035 | 220 | * |
7fe37828 EM |
221 | * @throws \CRM_Contribute_Exception_InactiveContributionPageException |
222 | * @throws \Exception | |
6a488035 TO |
223 | */ |
224 | public function preProcess() { | |
6a488035 TO |
225 | |
226 | // current contribution page id | |
227 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
37e877b2 | 228 | $this->_ccid = CRM_Utils_Request::retrieve('ccid', 'Positive', $this); |
6a488035 | 229 | if (!$this->_id) { |
d420cf02 DL |
230 | // seems like the session is corrupted and/or we lost the id trail |
231 | // lets just bump this to a regular session error and redirect user to main page | |
232 | $this->controller->invalidKeyRedirect(); | |
6a488035 | 233 | } |
67c998f4 | 234 | $this->_emailExists = $this->get('emailExists'); |
d420cf02 | 235 | |
5b757295 | 236 | // this was used prior to the cleverer this_>getContactID - unsure now |
2dbdb9b9 | 237 | $this->_userID = CRM_Core_Session::getLoggedInContactID(); |
5b757295 | 238 | |
239 | $this->_contactID = $this->_membershipContactID = $this->getContactID(); | |
6a488035 | 240 | $this->_mid = NULL; |
5b757295 | 241 | if ($this->_contactID) { |
6a488035 TO |
242 | $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this); |
243 | if ($this->_mid) { | |
244 | $membership = new CRM_Member_DAO_Membership(); | |
245 | $membership->id = $this->_mid; | |
246 | ||
247 | if ($membership->find(TRUE)) { | |
248 | $this->_defaultMemTypeId = $membership->membership_type_id; | |
5b757295 | 249 | if ($membership->contact_id != $this->_contactID) { |
6fe8deba | 250 | $validMembership = FALSE; |
33260076 | 251 | $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, NULL, NULL, 'Organization'); |
252 | if (!empty($organizations) && array_key_exists($membership->contact_id, $organizations)) { | |
6fe8deba DS |
253 | $this->_membershipContactID = $membership->contact_id; |
254 | $this->assign('membershipContactID', $this->_membershipContactID); | |
33260076 | 255 | $this->assign('membershipContactName', $organizations[$this->_membershipContactID]['name']); |
6fe8deba | 256 | $validMembership = TRUE; |
0db6c3e1 TO |
257 | } |
258 | else { | |
51e89def DS |
259 | $membershipType = new CRM_Member_BAO_MembershipType(); |
260 | $membershipType->id = $membership->membership_type_id; | |
261 | if ($membershipType->find(TRUE)) { | |
f9f0eff9 | 262 | // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values. |
f92d1e2a | 263 | // Convert to comma separated list. |
f9f0eff9 | 264 | $inheritedRelTypes = implode(CRM_Utils_Array::explodePadded($membershipType->relationship_type_id), ','); |
51e89def DS |
265 | $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, $membershipType->relationship_type_id); |
266 | if (array_key_exists($membership->contact_id, $permContacts)) { | |
267 | $this->_membershipContactID = $membership->contact_id; | |
6fe8deba | 268 | $validMembership = TRUE; |
51e89def DS |
269 | } |
270 | } | |
6a488035 | 271 | } |
6fe8deba DS |
272 | if (!$validMembership) { |
273 | 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'); | |
274 | } | |
6a488035 TO |
275 | } |
276 | } | |
277 | else { | |
278 | 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'); | |
279 | } | |
280 | unset($membership); | |
281 | } | |
282 | } | |
283 | ||
284 | // we do not want to display recently viewed items, so turn off | |
285 | $this->assign('displayRecent', FALSE); | |
6a488035 TO |
286 | |
287 | // action | |
288 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
289 | $this->assign('action', $this->_action); | |
290 | ||
291 | // current mode | |
292 | $this->_mode = ($this->_action == 1024) ? 'test' : 'live'; | |
293 | ||
294 | $this->_values = $this->get('values'); | |
295 | $this->_fields = $this->get('fields'); | |
296 | $this->_bltID = $this->get('bltID'); | |
297 | $this->_paymentProcessor = $this->get('paymentProcessor'); | |
3dd22219 | 298 | |
6a488035 TO |
299 | $this->_priceSetId = $this->get('priceSetId'); |
300 | $this->_priceSet = $this->get('priceSet'); | |
301 | ||
302 | if (!$this->_values) { | |
303 | // get all the values from the dao object | |
be2fb01f CW |
304 | $this->_values = []; |
305 | $this->_fields = []; | |
6a488035 TO |
306 | |
307 | CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values); | |
40c655aa | 308 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() |
66af7c48 PN |
309 | && !CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id'])) |
310 | ) { | |
beb414cc | 311 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
0799e1c4 | 312 | } |
a7488080 | 313 | if (empty($this->_values['is_active'])) { |
4b57bc9f | 314 | throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id); |
6a488035 TO |
315 | } |
316 | ||
ed1c698a | 317 | $endDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('end_date', $this->_values)); |
318 | $now = date('YmdHis'); | |
319 | if ($endDate && $endDate < $now) { | |
6dabf459 | 320 | 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 | 321 | } |
322 | ||
323 | $startDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('start_date', $this->_values)); | |
324 | if ($startDate && $startDate > $now) { | |
6dabf459 | 325 | 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 | 326 | } |
327 | ||
8345c9d3 | 328 | $this->assignBillingType(); |
6a488035 TO |
329 | |
330 | // check for is_monetary status | |
9c1bc317 CW |
331 | $isMonetary = $this->_values['is_monetary'] ?? NULL; |
332 | $isPayLater = $this->_values['is_pay_later'] ?? NULL; | |
24cf7458 | 333 | if (!empty($this->_ccid)) { |
334 | $this->_values['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
335 | $this->_ccid, | |
336 | 'financial_type_id' | |
337 | ); | |
338 | if ($isPayLater) { | |
339 | $isPayLater = FALSE; | |
340 | $this->_values['is_pay_later'] = FALSE; | |
341 | } | |
37e877b2 | 342 | } |
bf7ae813 | 343 | if ($isPayLater) { |
344 | $this->setPayLaterLabel($this->_values['pay_later_text']); | |
345 | } | |
6a488035 | 346 | |
427ae6c5 JP |
347 | if ($isMonetary) { |
348 | $this->_paymentProcessorIDs = array_filter(explode( | |
a6c15c46 EM |
349 | CRM_Core_DAO::VALUE_SEPARATOR, |
350 | CRM_Utils_Array::value('payment_processor', $this->_values) | |
427ae6c5 | 351 | )); |
f92d1e2a | 352 | |
cbcb5b49 | 353 | $this->assignPaymentProcessor($isPayLater); |
6a488035 TO |
354 | } |
355 | ||
356 | // get price info | |
357 | // CRM-5095 | |
d1d16083 | 358 | $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id); |
359 | CRM_Price_BAO_PriceSet::initSet($this, 'civicrm_contribution_page', FALSE, $priceSetId); | |
6a488035 TO |
360 | |
361 | // this avoids getting E_NOTICE errors in php | |
be2fb01f | 362 | $setNullFields = [ |
6a488035 | 363 | 'amount_block_is_active', |
6a488035 TO |
364 | 'is_allow_other_amount', |
365 | 'footer_text', | |
be2fb01f | 366 | ]; |
6a488035 TO |
367 | foreach ($setNullFields as $f) { |
368 | if (!isset($this->_values[$f])) { | |
369 | $this->_values[$f] = NULL; | |
370 | } | |
371 | } | |
372 | ||
373 | //check if Membership Block is enabled, if Membership Fields are included in profile | |
374 | //get membership section for this contribution page | |
375 | $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id); | |
376 | $this->set('membershipBlock', $this->_membershipBlock); | |
377 | ||
4779abb3 | 378 | if (!empty($this->_values['custom_pre_id'])) { |
6a488035 TO |
379 | $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']); |
380 | } | |
381 | ||
4779abb3 | 382 | if (!empty($this->_values['custom_post_id'])) { |
6a488035 TO |
383 | $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']); |
384 | } | |
385 | ||
9ac53857 | 386 | if (((isset($postProfileType) && $postProfileType === 'Membership') || |
387 | (isset($preProfileType) && $preProfileType === 'Membership') | |
6a488035 TO |
388 | ) && |
389 | !$this->_membershipBlock['is_active'] | |
390 | ) { | |
9ac53857 | 391 | 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 |
392 | } |
393 | ||
394 | $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id); | |
395 | ||
396 | if ($pledgeBlock) { | |
9c1bc317 CW |
397 | $this->_values['pledge_block_id'] = $pledgeBlock['id'] ?? NULL; |
398 | $this->_values['max_reminders'] = $pledgeBlock['max_reminders'] ?? NULL; | |
399 | $this->_values['initial_reminder_day'] = $pledgeBlock['initial_reminder_day'] ?? NULL; | |
400 | $this->_values['additional_reminder_day'] = $pledgeBlock['additional_reminder_day'] ?? NULL; | |
6a488035 TO |
401 | |
402 | //set pledge id in values | |
403 | $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this); | |
404 | ||
405 | //authenticate pledge user for pledge payment. | |
406 | if ($pledgeId) { | |
407 | $this->_values['pledge_id'] = $pledgeId; | |
408 | ||
409 | //lets override w/ pledge campaign. | |
410 | $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', | |
411 | $pledgeId, | |
412 | 'campaign_id' | |
413 | ); | |
414 | self::authenticatePledgeUser(); | |
415 | } | |
416 | } | |
417 | $this->set('values', $this->_values); | |
418 | $this->set('fields', $this->_fields); | |
419 | } | |
420 | ||
421 | // Handle PCP | |
422 | $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this); | |
423 | if ($pcpId) { | |
353ffa53 TO |
424 | $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values); |
425 | $this->_pcpId = $pcp['pcpId']; | |
6a488035 | 426 | $this->_pcpBlock = $pcp['pcpBlock']; |
353ffa53 | 427 | $this->_pcpInfo = $pcp['pcpInfo']; |
6a488035 TO |
428 | } |
429 | ||
430 | // Link (button) for users to create their own Personal Campaign page | |
431 | if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) { | |
432 | $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign', | |
433 | "action=add&reset=1&pageId={$this->_id}&component=contribute", | |
434 | FALSE, NULL, TRUE | |
435 | ); | |
436 | $this->assign('linkTextUrl', $linkTextUrl); | |
437 | $this->assign('linkText', $linkText); | |
438 | } | |
439 | ||
440 | //set pledge block if block id is set | |
a7488080 | 441 | if (!empty($this->_values['pledge_block_id'])) { |
6a488035 TO |
442 | $this->assign('pledgeBlock', TRUE); |
443 | } | |
444 | ||
f92d1e2a | 445 | // check if one of the (amount , membership) blocks is active or not. |
6a488035 TO |
446 | $this->_membershipBlock = $this->get('membershipBlock'); |
447 | ||
448 | if (!$this->_values['amount_block_is_active'] && | |
449 | !$this->_membershipBlock['is_active'] && | |
450 | !$this->_priceSetId | |
451 | ) { | |
9ac53857 | 452 | 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 TO |
453 | } |
454 | ||
455 | if ($this->_values['amount_block_is_active']) { | |
456 | $this->set('amount_block_is_active', $this->_values['amount_block_is_active']); | |
457 | } | |
458 | ||
459 | $this->_contributeMode = $this->get('contributeMode'); | |
460 | $this->assign('contributeMode', $this->_contributeMode); | |
461 | ||
462 | //assigning is_monetary and is_email_receipt to template | |
463 | $this->assign('is_monetary', $this->_values['is_monetary']); | |
464 | $this->assign('is_email_receipt', $this->_values['is_email_receipt']); | |
465 | $this->assign('bltID', $this->_bltID); | |
466 | ||
467 | //assign cancelSubscription URL to templates | |
468 | $this->assign('cancelSubscriptionUrl', | |
469 | CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values) | |
470 | ); | |
471 | ||
6489e3de SL |
472 | $title = !empty($this->_values['frontend_title']) ? $this->_values['frontend_title'] : $this->_values['title']; |
473 | ||
474 | $this->setTitle(($this->_pcpId ? $this->_pcpInfo['title'] : $title)); | |
be2fb01f | 475 | $this->_defaults = []; |
6a488035 TO |
476 | |
477 | $this->_amount = $this->get('amount'); | |
d02c6418 | 478 | // Assigning this to the template means it will be passed through to the payment form. |
479 | // This can, for example, by used by payment processors using client side encryption | |
480 | $this->assign('currency', $this->getCurrency()); | |
6a488035 | 481 | |
85939a77 | 482 | CRM_Contribute_BAO_Contribution_Utils::overrideDefaultCurrency($this->_values); |
6a488035 TO |
483 | |
484 | //lets allow user to override campaign. | |
485 | $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this); | |
486 | if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) { | |
487 | $this->_values['campaign_id'] = $campID; | |
488 | } | |
489 | ||
490 | //do check for cancel recurring and clean db, CRM-7696 | |
a3d827a7 | 491 | if (CRM_Utils_Request::retrieve('cancel', 'Boolean')) { |
6a488035 TO |
492 | self::cancelRecurring(); |
493 | } | |
8ae4d0d3 | 494 | |
495 | // check if billing block is required for pay later | |
f3acfdd9 | 496 | if (!empty($this->_values['is_pay_later'])) { |
9c1bc317 | 497 | $this->_isBillingAddressRequiredForPayLater = $this->_values['is_billing_required'] ?? NULL; |
8ae4d0d3 | 498 | $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater); |
499 | } | |
6a488035 TO |
500 | } |
501 | ||
502 | /** | |
fe482240 | 503 | * Set the default values. |
6a488035 | 504 | */ |
00be9182 | 505 | public function setDefaultValues() { |
6a488035 TO |
506 | return $this->_defaults; |
507 | } | |
508 | ||
509 | /** | |
fe482240 | 510 | * Assign the minimal set of variables to the template. |
6a488035 | 511 | */ |
00be9182 | 512 | public function assignToTemplate() { |
0b50eca0 | 513 | $this->set('name', $this->assignBillingName($this->_params)); |
6a488035 TO |
514 | |
515 | $this->assign('paymentProcessor', $this->_paymentProcessor); | |
be2fb01f | 516 | $vars = [ |
353ffa53 TO |
517 | 'amount', |
518 | 'currencyID', | |
519 | 'credit_card_type', | |
520 | 'trxn_id', | |
521 | 'amount_level', | |
be2fb01f | 522 | ]; |
6a488035 TO |
523 | |
524 | $config = CRM_Core_Config::singleton(); | |
8cc574cf | 525 | if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) { |
6a488035 | 526 | $this->assign('is_recur_enabled', 1); |
be2fb01f | 527 | $vars = array_merge($vars, [ |
353ffa53 TO |
528 | 'is_recur', |
529 | 'frequency_interval', | |
530 | 'frequency_unit', | |
531 | 'installments', | |
be2fb01f | 532 | ]); |
6a488035 TO |
533 | } |
534 | ||
535 | if (in_array('CiviPledge', $config->enableComponents) && | |
536 | CRM_Utils_Array::value('is_pledge', $this->_params) == 1 | |
537 | ) { | |
538 | $this->assign('pledge_enabled', 1); | |
539 | ||
be2fb01f | 540 | $vars = array_merge($vars, [ |
6a488035 | 541 | 'is_pledge', |
353ffa53 TO |
542 | 'pledge_frequency_interval', |
543 | 'pledge_frequency_unit', | |
544 | 'pledge_installments', | |
be2fb01f | 545 | ]); |
6a488035 TO |
546 | } |
547 | ||
c039f658 | 548 | // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel |
549 | // function to get correct amount level consistently. Remove setting of the amount level in | |
550 | // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest | |
551 | // to cover all variants. | |
6a488035 TO |
552 | if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) { |
553 | $this->_params['amount_level'] = ''; | |
554 | } | |
555 | ||
556 | foreach ($vars as $v) { | |
3fb990f4 | 557 | if (isset($this->_params[$v])) { |
735fe42d PJ |
558 | if ($v == "amount" && $this->_params[$v] === 0) { |
559 | $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE); | |
3fb990f4 | 560 | } |
6a488035 TO |
561 | $this->assign($v, $this->_params[$v]); |
562 | } | |
563 | } | |
564 | ||
0b50eca0 | 565 | $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( |
566 | $this->_params, | |
567 | $this->_bltID | |
568 | )); | |
6a488035 | 569 | |
cb804cd9 | 570 | if (!empty($this->_params['onbehalf_profile_id']) && !empty($this->_params['onbehalf'])) { |
6a488035 TO |
571 | $this->assign('onBehalfName', $this->_params['organization_name']); |
572 | $locTypeId = array_keys($this->_params['onbehalf_location']['email']); | |
573 | $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']); | |
574 | } | |
a050fa6b | 575 | $this->assignPaymentFields(); |
d90e6c3b | 576 | $this->assignEmailField(); |
6a488035 TO |
577 | |
578 | // also assign the receipt_text | |
579 | if (isset($this->_values['receipt_text'])) { | |
580 | $this->assign('receipt_text', $this->_values['receipt_text']); | |
581 | } | |
582 | } | |
583 | ||
d90e6c3b JP |
584 | /** |
585 | * Assign email variable in the template. | |
586 | */ | |
587 | public function assignEmailField() { | |
588 | //If email exist in a profile, the default billing email field is not loaded on the page. | |
589 | //Hence, assign the existing location type email by iterating through the params. | |
238ea0e4 JP |
590 | if ($this->_emailExists && empty($this->_params["email-{$this->_bltID}"])) { |
591 | foreach ($this->_params as $key => $val) { | |
9ac53857 | 592 | if (substr($key, 0, 6) === 'email-') { |
238ea0e4 | 593 | $this->assign('email', $this->_params[$key]); |
d90e6c3b | 594 | break; |
238ea0e4 JP |
595 | } |
596 | } | |
597 | } | |
598 | else { | |
599 | $this->assign('email', CRM_Utils_Array::value("email-{$this->_bltID}", $this->_params)); | |
600 | } | |
6a488035 TO |
601 | } |
602 | ||
603 | /** | |
fe482240 | 604 | * Add the custom fields. |
6a488035 | 605 | * |
100fef9d CW |
606 | * @param int $id |
607 | * @param string $name | |
f4aaa82a EM |
608 | * @param bool $viewOnly |
609 | * @param null $profileContactType | |
f92d1e2a | 610 | * @param array $fieldTypes |
6a488035 | 611 | */ |
00be9182 | 612 | public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) { |
6a488035 | 613 | if ($id) { |
da8d9879 | 614 | $contactID = $this->getContactID(); |
6a488035 TO |
615 | |
616 | // we don't allow conflicting fields to be | |
617 | // configured via profile - CRM 2100 | |
be2fb01f | 618 | $fieldsToIgnore = [ |
6a488035 TO |
619 | 'receive_date' => 1, |
620 | 'trxn_id' => 1, | |
621 | 'invoice_id' => 1, | |
622 | 'net_amount' => 1, | |
623 | 'fee_amount' => 1, | |
624 | 'non_deductible_amount' => 1, | |
625 | 'total_amount' => 1, | |
626 | 'amount_level' => 1, | |
627 | 'contribution_status_id' => 1, | |
7bc1d4da | 628 | // @todo replace payment_instrument with payment instrument id. |
629 | // both are available now but the id field is the most consistent. | |
6a488035 | 630 | 'payment_instrument' => 1, |
7bc1d4da | 631 | 'payment_instrument_id' => 1, |
632 | 'contribution_check_number' => 1, | |
6a488035 | 633 | 'financial_type' => 1, |
be2fb01f | 634 | ]; |
6a488035 | 635 | |
423b9af4 | 636 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE, |
637 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL | |
638 | ); | |
6a488035 TO |
639 | |
640 | if ($fields) { | |
0c7c9ff7 KC |
641 | // determine if email exists in profile so we know if we need to manually insert CRM-2888, CRM-15067 |
642 | foreach ($fields as $key => $field) { | |
643 | if (substr($key, 0, 6) == 'email-' && | |
be2fb01f | 644 | !in_array($profileContactType, ['honor', 'onbehalf']) |
0c7c9ff7 KC |
645 | ) { |
646 | $this->_emailExists = TRUE; | |
67c998f4 | 647 | $this->set('emailExists', TRUE); |
6a488035 TO |
648 | } |
649 | } | |
650 | ||
651 | if (array_intersect_key($fields, $fieldsToIgnore)) { | |
652 | $fields = array_diff_key($fields, $fieldsToIgnore); | |
653 | CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert'); | |
654 | } | |
655 | ||
d7d630be | 656 | //remove common fields only if profile is not configured for onbehalf/honor |
be2fb01f | 657 | if (!in_array($profileContactType, ['honor', 'onbehalf'])) { |
94e01524 | 658 | $fields = array_diff_key($fields, $this->_fields); |
d7d630be | 659 | } |
6a488035 TO |
660 | |
661 | CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID); | |
662 | $addCaptcha = FALSE; | |
f3726153 | 663 | // fetch file preview when not submitted yet, like in online contribution Confirm and ThankYou page |
be2fb01f | 664 | $viewOnlyFileValues = empty($profileContactType) ? [] : [$profileContactType => []]; |
6a488035 TO |
665 | foreach ($fields as $key => $field) { |
666 | if ($viewOnly && | |
667 | isset($field['data_type']) && | |
668 | $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL') | |
669 | ) { | |
c2292dbd | 670 | //retrieve file value from submitted values on basis of $profileContactType |
9c1bc317 | 671 | $fileValue = $this->_params[$key] ?? NULL; |
9b7bedc5 | 672 | if (!empty($profileContactType) && !empty($this->_params[$profileContactType])) { |
9c1bc317 | 673 | $fileValue = $this->_params[$profileContactType][$key] ?? NULL; |
9b7bedc5 | 674 | } |
c2292dbd | 675 | |
676 | if ($fileValue) { | |
9c1bc317 CW |
677 | $path = $fileValue['name'] ?? NULL; |
678 | $fileType = $fileValue['type'] ?? NULL; | |
c2292dbd | 679 | $fileValue = CRM_Utils_File::getFileURL($path, $fileType); |
f3726153 | 680 | } |
6a488035 | 681 | |
c2292dbd | 682 | // format custom file value fetched from submitted value |
683 | if ($profileContactType) { | |
684 | $viewOnlyFileValues[$profileContactType][$key] = $fileValue; | |
685 | } | |
686 | else { | |
687 | $viewOnlyFileValues[$key] = $fileValue; | |
688 | } | |
1aedfc98 | 689 | |
690 | // On viewOnly use-case (as in online contribution Confirm page) we no longer need to set | |
691 | // required property because being required file is already uploaded while registration | |
692 | $field['is_required'] = FALSE; | |
c2292dbd | 693 | } |
133e2c99 | 694 | if ($profileContactType) { |
695 | //Since we are showing honoree name separately so we are removing it from honoree profile just for display | |
c043358f | 696 | if ($profileContactType == 'honor') { |
be2fb01f | 697 | $honoreeNamefields = [ |
c043358f | 698 | 'prefix_id', |
699 | 'first_name', | |
700 | 'last_name', | |
701 | 'suffix_id', | |
702 | 'organization_name', | |
703 | 'household_name', | |
be2fb01f | 704 | ]; |
c043358f | 705 | if (in_array($field['name'], $honoreeNamefields)) { |
706 | unset($fields[$field['name']]); | |
707 | continue; | |
708 | } | |
133e2c99 | 709 | } |
6a488035 TO |
710 | if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) { |
711 | CRM_Core_BAO_UFGroup::buildProfile( | |
712 | $this, | |
713 | $field, | |
714 | CRM_Profile_Form::MODE_CREATE, | |
715 | $contactID, | |
133e2c99 | 716 | TRUE, |
717 | $profileContactType | |
6a488035 | 718 | ); |
133e2c99 | 719 | $this->_fields[$profileContactType][$key] = $field; |
6a488035 TO |
720 | } |
721 | else { | |
722 | unset($fields[$key]); | |
723 | } | |
724 | } | |
725 | else { | |
726 | CRM_Core_BAO_UFGroup::buildProfile( | |
727 | $this, | |
728 | $field, | |
729 | CRM_Profile_Form::MODE_CREATE, | |
730 | $contactID, | |
731 | TRUE | |
732 | ); | |
733 | $this->_fields[$key] = $field; | |
734 | } | |
71fc6ea4 DG |
735 | // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor |
736 | if ($field['add_captcha'] && !$this->_userID) { | |
6a488035 TO |
737 | $addCaptcha = TRUE; |
738 | } | |
739 | } | |
740 | ||
741 | $this->assign($name, $fields); | |
742 | ||
6d43e496 | 743 | if ($profileContactType && count($viewOnlyFileValues[$profileContactType])) { |
744 | $this->assign('viewOnlyPrefixFileValues', $viewOnlyFileValues); | |
745 | } | |
746 | elseif (count($viewOnlyFileValues)) { | |
f3726153 | 747 | $this->assign('viewOnlyFileValues', $viewOnlyFileValues); |
748 | } | |
749 | ||
6a488035 | 750 | if ($addCaptcha && !$viewOnly) { |
96ea60e0 | 751 | $this->enableCaptchaOnForm(); |
6a488035 TO |
752 | } |
753 | } | |
754 | } | |
755 | } | |
756 | ||
96ea60e0 AP |
757 | /** |
758 | * Enable ReCAPTCHA on Contribution form | |
759 | */ | |
760 | protected function enableCaptchaOnForm() { | |
268ff0e8 | 761 | CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this); |
ce287b85 AP |
762 | } |
763 | ||
9ac53857 | 764 | /** |
765 | * Assign payment field information to the template. | |
766 | * | |
767 | * @throws \CRM_Core_Exception | |
768 | * @throws \CiviCRM_API3_Exception | |
769 | */ | |
a050fa6b MWMC |
770 | public function assignPaymentFields() { |
771 | //fix for CRM-3767 | |
772 | $isMonetary = FALSE; | |
773 | if ($this->_amount > 0.0) { | |
774 | $isMonetary = TRUE; | |
775 | } | |
776 | elseif (!empty($this->_params['selectMembership'])) { | |
777 | $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee'); | |
778 | if ($memFee > 0.0) { | |
779 | $isMonetary = TRUE; | |
780 | } | |
781 | } | |
782 | ||
783 | // The concept of contributeMode is deprecated. | |
784 | // The payment processor object can provide info about the fields it shows. | |
785 | if ($isMonetary && is_a($this->_paymentProcessor['object'], 'CRM_Core_Payment')) { | |
9ac53857 | 786 | /** @var \CRM_Core_Payment $paymentProcessorObject */ |
a050fa6b MWMC |
787 | $paymentProcessorObject = $this->_paymentProcessor['object']; |
788 | ||
789 | $paymentFields = $paymentProcessorObject->getPaymentFormFields(); | |
790 | foreach ($paymentFields as $index => $paymentField) { | |
791 | if (!isset($this->_params[$paymentField])) { | |
792 | unset($paymentFields[$index]); | |
793 | continue; | |
794 | } | |
795 | if ($paymentField === 'credit_card_exp_date') { | |
796 | $date = CRM_Utils_Date::format(CRM_Utils_Array::value('credit_card_exp_date', $this->_params)); | |
797 | $date = CRM_Utils_Date::mysqlToIso($date); | |
798 | $this->assign('credit_card_exp_date', $date); | |
799 | } | |
800 | elseif ($paymentField === 'credit_card_number') { | |
801 | $this->assign('credit_card_number', | |
802 | CRM_Utils_System::mungeCreditCard(CRM_Utils_Array::value('credit_card_number', $this->_params)) | |
803 | ); | |
804 | } | |
805 | elseif ($paymentField === 'credit_card_type') { | |
806 | $this->assign('credit_card_type', CRM_Core_PseudoConstant::getLabel( | |
807 | 'CRM_Core_BAO_FinancialTrxn', | |
808 | 'card_type_id', | |
809 | CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'card_type_id', $this->_params['credit_card_type']) | |
810 | )); | |
811 | } | |
812 | else { | |
813 | $this->assign($paymentField, $this->_params[$paymentField]); | |
814 | } | |
815 | } | |
816 | $this->assign('paymentFieldsetLabel', CRM_Core_Payment_Form::getPaymentLabel($paymentProcessorObject)); | |
817 | $this->assign('paymentFields', $paymentFields); | |
818 | ||
819 | } | |
820 | } | |
821 | ||
ce287b85 AP |
822 | /** |
823 | * Display ReCAPTCHA warning on Contribution form | |
824 | */ | |
825 | protected function displayCaptchaWarning() { | |
826 | if (CRM_Core_Permission::check("administer CiviCRM")) { | |
268ff0e8 | 827 | if (!CRM_Utils_ReCAPTCHA::hasSettingsAvailable()) { |
ce287b85 AP |
828 | $this->assign('displayCaptchaWarning', TRUE); |
829 | } | |
8237b510 | 830 | } |
96ea60e0 AP |
831 | } |
832 | ||
ce287b85 AP |
833 | /** |
834 | * Check if ReCAPTCHA has to be added on Contribution form forcefully. | |
835 | */ | |
836 | protected function hasToAddForcefully() { | |
268ff0e8 | 837 | return CRM_Utils_ReCAPTCHA::hasToAddForcefully(); |
ce287b85 AP |
838 | } |
839 | ||
4779abb3 | 840 | /** |
841 | * Add onbehalf/honoree profile fields and native module fields. | |
842 | * | |
843 | * @param int $id | |
844 | * @param CRM_Core_Form $form | |
9ac53857 | 845 | * |
846 | * @throws \CRM_Core_Exception | |
4779abb3 | 847 | */ |
bcb8cc84 | 848 | public function buildComponentForm($id, $form) { |
849 | if (empty($id)) { | |
850 | return; | |
851 | } | |
852 | ||
853 | $contactID = $this->getContactID(); | |
854 | ||
be2fb01f | 855 | foreach (['soft_credit', 'on_behalf'] as $module) { |
9ac53857 | 856 | if ($module === 'soft_credit') { |
4779abb3 | 857 | if (empty($form->_values['honoree_profile_id'])) { |
858 | continue; | |
859 | } | |
bcb8cc84 | 860 | |
4779abb3 | 861 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['honoree_profile_id'], 'is_active')) { |
9ac53857 | 862 | 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 | 863 | } |
864 | ||
4779abb3 | 865 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']); |
be2fb01f CW |
866 | $requiredProfileFields = [ |
867 | 'Individual' => ['first_name', 'last_name'], | |
868 | 'Organization' => ['organization_name', 'email'], | |
869 | 'Household' => ['household_name', 'email'], | |
870 | ]; | |
4779abb3 | 871 | $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_values['honoree_profile_id'], $requiredProfileFields[$profileContactType]); |
bcb8cc84 | 872 | if (!$validProfile) { |
9ac53857 | 873 | 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 | 874 | } |
875 | ||
be2fb01f | 876 | foreach (['honor_block_title', 'honor_block_text'] as $name) { |
4779abb3 | 877 | $form->assign($name, $form->_values[$name]); |
cb804cd9 | 878 | } |
879 | ||
880 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); | |
881 | ||
882 | // radio button for Honor Type | |
4779abb3 | 883 | foreach ($form->_values['soft_credit_types'] as $value) { |
cb804cd9 | 884 | $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value); |
885 | } | |
886 | $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE); | |
887 | ||
4779abb3 | 888 | $honoreeProfileFields = CRM_Core_BAO_UFGroup::getFields( |
889 | $this->_values['honoree_profile_id'], FALSE, | |
890 | NULL, NULL, | |
891 | NULL, FALSE, | |
892 | NULL, TRUE, | |
893 | NULL, CRM_Core_Permission::CREATE | |
cb804cd9 | 894 | ); |
cb804cd9 | 895 | $form->assign('honoreeProfileFields', $honoreeProfileFields); |
896 | ||
897 | // add the form elements | |
898 | foreach ($honoreeProfileFields as $name => $field) { | |
899 | // If soft credit type is not chosen then make omit requiredness from honoree profile fields | |
900 | if (count($form->_submitValues) && | |
901 | empty($form->_submitValues['soft_credit_type_id']) && | |
902 | !empty($field['is_required']) | |
903 | ) { | |
904 | $field['is_required'] = FALSE; | |
905 | } | |
c043358f | 906 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, NULL, FALSE, FALSE, NULL, 'honor'); |
cb804cd9 | 907 | } |
bcb8cc84 | 908 | } |
909 | else { | |
4779abb3 | 910 | if (empty($form->_values['onbehalf_profile_id'])) { |
911 | continue; | |
912 | } | |
bcb8cc84 | 913 | |
4779abb3 | 914 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['onbehalf_profile_id'], 'is_active')) { |
9ac53857 | 915 | 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 | 916 | } |
917 | ||
918 | $member = CRM_Member_BAO_Membership::getMembershipBlock($form->_id); | |
919 | if (empty($member['is_active'])) { | |
920 | $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.'); | |
4779abb3 | 921 | $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($form->_values['onbehalf_profile_id']); |
0c7c9ff7 | 922 | foreach ( |
be2fb01f | 923 | [ |
bcb8cc84 | 924 | 'Individual', |
925 | 'Organization', | |
926 | 'Household', | |
be2fb01f | 927 | ] as $contactType |
0c7c9ff7 | 928 | ) { |
bcb8cc84 | 929 | if (in_array($contactType, $onBehalfProfile) && |
930 | (in_array('Membership', $onBehalfProfile) || | |
931 | in_array('Contribution', $onBehalfProfile) | |
932 | ) | |
933 | ) { | |
9ac53857 | 934 | CRM_Core_Error::statusBounce($msg); |
bcb8cc84 | 935 | } |
936 | } | |
937 | } | |
938 | ||
c043358f | 939 | if ($contactID) { |
33260076 | 940 | // retrieve all permissioned organizations of contact $contactID |
941 | $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($contactID, NULL, NULL, 'Organization'); | |
c043358f | 942 | |
33260076 | 943 | if (count($organizations)) { |
c043358f | 944 | // Related org url - pass checksum if needed |
be2fb01f | 945 | $args = [ |
4779abb3 | 946 | 'ufId' => $form->_values['onbehalf_profile_id'], |
f23093b2 | 947 | 'cid' => '', |
be2fb01f | 948 | ]; |
c043358f | 949 | if (!empty($_GET['cs'])) { |
be2fb01f | 950 | $args = [ |
4779abb3 | 951 | 'ufId' => $form->_values['onbehalf_profile_id'], |
c043358f | 952 | 'uid' => $this->_contactID, |
953 | 'cs' => $_GET['cs'], | |
954 | 'cid' => '', | |
be2fb01f | 955 | ]; |
c043358f | 956 | } |
957 | $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE); | |
958 | $form->assign('locDataURL', $locDataURL); | |
959 | } | |
33260076 | 960 | if (count($organizations) > 0) { |
961 | $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $organizations)); | |
c043358f | 962 | |
be2fb01f | 963 | $orgOptions = [ |
c043358f | 964 | 0 => ts('Select an existing organization'), |
965 | 1 => ts('Enter a new organization'), | |
be2fb01f | 966 | ]; |
c043358f | 967 | $form->addRadio('org_option', ts('options'), $orgOptions); |
be2fb01f | 968 | $form->setDefaults(['org_option' => 0]); |
c043358f | 969 | } |
bcb8cc84 | 970 | } |
971 | ||
6dabf459 | 972 | $form->assign('fieldSetTitle', CRM_Core_BAO_UFGroup::getTitle($form->_values['onbehalf_profile_id'])); |
c043358f | 973 | |
f3acfdd9 | 974 | if (!empty($form->_values['is_for_organization'])) { |
4779abb3 | 975 | if ($form->_values['is_for_organization'] == 2) { |
c043358f | 976 | $form->assign('onBehalfRequired', TRUE); |
bcb8cc84 | 977 | } |
978 | else { | |
979 | $form->addElement('checkbox', 'is_for_organization', | |
980 | $form->_values['for_organization'], | |
981 | NULL | |
982 | ); | |
983 | } | |
984 | } | |
c043358f | 985 | |
4779abb3 | 986 | $profileFields = CRM_Core_BAO_UFGroup::getFields( |
987 | $form->_values['onbehalf_profile_id'], | |
988 | FALSE, CRM_Core_Action::VIEW, NULL, | |
989 | NULL, FALSE, NULL, FALSE, NULL, | |
990 | CRM_Core_Permission::CREATE, NULL | |
c043358f | 991 | ); |
4779abb3 | 992 | |
c043358f | 993 | $form->assign('onBehalfOfFields', $profileFields); |
994 | if (!empty($form->_submitValues['onbehalf'])) { | |
995 | if (!empty($form->_submitValues['onbehalfof_id'])) { | |
996 | $form->assign('submittedOnBehalf', $form->_submitValues['onbehalfof_id']); | |
997 | } | |
d425f39b | 998 | $form->assign('submittedOnBehalfInfo', json_encode(str_replace('"', '\"', $form->_submitValues['onbehalf']), JSON_HEX_APOS)); |
c043358f | 999 | } |
1000 | ||
be2fb01f | 1001 | $fieldTypes = ['Contact', 'Organization']; |
a6a083d8 | 1002 | if (!empty($form->_membershipBlock)) { |
be2fb01f | 1003 | $fieldTypes = array_merge($fieldTypes, ['Membership']); |
a6a083d8 | 1004 | } |
c043358f | 1005 | $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); |
1006 | $fieldTypes = array_merge($fieldTypes, $contactSubType); | |
1007 | ||
1008 | foreach ($profileFields as $name => $field) { | |
1009 | if (in_array($field['field_type'], $fieldTypes)) { | |
1010 | list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2); | |
be2fb01f | 1011 | if (in_array($prefixName, ['organization_name', 'email']) && empty($field['is_required'])) { |
c043358f | 1012 | $field['is_required'] = 1; |
1013 | } | |
1014 | if (count($form->_submitValues) && | |
1015 | empty($form->_submitValues['is_for_organization']) && | |
4779abb3 | 1016 | $form->_values['is_for_organization'] == 1 && |
c043358f | 1017 | !empty($field['is_required']) |
1018 | ) { | |
1019 | $field['is_required'] = FALSE; | |
1020 | } | |
4779abb3 | 1021 | CRM_Core_BAO_UFGroup::buildProfile($form, $field, NULL, NULL, FALSE, 'onbehalf', NULL, 'onbehalf'); |
c043358f | 1022 | } |
1023 | } | |
bcb8cc84 | 1024 | } |
1025 | } | |
1026 | ||
1027 | } | |
1028 | ||
f4aaa82a | 1029 | /** |
fe482240 | 1030 | * Check template file exists. |
f92d1e2a EM |
1031 | * |
1032 | * @param string $suffix | |
f4aaa82a EM |
1033 | * |
1034 | * @return null|string | |
1035 | */ | |
00be9182 | 1036 | public function checkTemplateFileExists($suffix = NULL) { |
6a488035 TO |
1037 | if ($this->_id) { |
1038 | $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl"; | |
1039 | $template = CRM_Core_Form::getTemplate(); | |
1040 | if ($template->template_exists($templateFile)) { | |
1041 | return $templateFile; | |
1042 | } | |
1043 | } | |
1044 | return NULL; | |
1045 | } | |
1046 | ||
186c9c17 | 1047 | /** |
fe482240 | 1048 | * Use the form name to create the tpl file name. |
186c9c17 EM |
1049 | * |
1050 | * @return string | |
186c9c17 | 1051 | */ |
00be9182 | 1052 | public function getTemplateFileName() { |
6a488035 | 1053 | $fileName = $this->checkTemplateFileExists(); |
9ac53857 | 1054 | return $fileName ?: parent::getTemplateFileName(); |
6a488035 TO |
1055 | } |
1056 | ||
186c9c17 | 1057 | /** |
f92d1e2a EM |
1058 | * Add the extra.tpl in. |
1059 | * | |
186c9c17 | 1060 | * Default extra tpl file basically just replaces .tpl with .extra.tpl |
f92d1e2a | 1061 | * i.e. we do not override - why isn't this done at the CRM_Core_Form level? |
186c9c17 EM |
1062 | * |
1063 | * @return string | |
186c9c17 | 1064 | */ |
00be9182 | 1065 | public function overrideExtraTemplateFileName() { |
6a488035 TO |
1066 | $fileName = $this->checkTemplateFileExists('extra.'); |
1067 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); | |
1068 | } | |
1069 | ||
1070 | /** | |
100fef9d | 1071 | * Authenticate pledge user during online payment. |
9ac53857 | 1072 | * |
1073 | * @throws \CRM_Core_Exception | |
6a488035 TO |
1074 | */ |
1075 | public function authenticatePledgeUser() { | |
1076 | //get the userChecksum and contact id | |
1077 | $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this); | |
1078 | $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
1079 | ||
1080 | //get pledge status and contact id | |
be2fb01f CW |
1081 | $pledgeValues = []; |
1082 | $pledgeParams = ['id' => $this->_values['pledge_id']]; | |
1083 | $returnProperties = ['contact_id', 'status_id']; | |
6a488035 TO |
1084 | CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties); |
1085 | ||
1086 | //get all status | |
1087 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
be2fb01f | 1088 | $validStatus = [ |
353ffa53 | 1089 | array_search('Pending', $allStatus), |
6a488035 TO |
1090 | array_search('In Progress', $allStatus), |
1091 | array_search('Overdue', $allStatus), | |
be2fb01f | 1092 | ]; |
6a488035 TO |
1093 | |
1094 | $validUser = FALSE; | |
1095 | if ($this->_userID && | |
1096 | $this->_userID == $pledgeValues['contact_id'] | |
1097 | ) { | |
1098 | //check for authenticated user. | |
1099 | $validUser = TRUE; | |
1100 | } | |
1101 | elseif ($userChecksum && $pledgeValues['contact_id']) { | |
1102 | //check for anonymous user. | |
1103 | $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum); | |
1104 | ||
1105 | //make sure cid is same as pledge contact id | |
1106 | if ($validUser && ($pledgeValues['contact_id'] != $contactID)) { | |
1107 | $validUser = FALSE; | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | if (!$validUser) { | |
9ac53857 | 1112 | 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 |
1113 | } |
1114 | ||
1115 | //check for valid pledge status. | |
1116 | if (!in_array($pledgeValues['status_id'], $validStatus)) { | |
9ac53857 | 1117 | 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 |
1118 | } |
1119 | } | |
1120 | ||
1121 | /** | |
f92d1e2a EM |
1122 | * Cancel recurring contributions. |
1123 | * | |
6a488035 TO |
1124 | * In case user cancel recurring contribution, |
1125 | * When we get the control back from payment gate way | |
1126 | * lets delete the recurring and related contribution. | |
9ac53857 | 1127 | * |
1128 | * @throws \CRM_Core_Exception | |
389bcebf | 1129 | */ |
6a488035 | 1130 | public function cancelRecurring() { |
a3d827a7 | 1131 | $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean'); |
6a488035 | 1132 | if ($isCancel) { |
a3d827a7 CW |
1133 | $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean'); |
1134 | $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive'); | |
6a488035 TO |
1135 | //clean db for recurring contribution. |
1136 | if ($isRecur && $recurId) { | |
1137 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId); | |
1138 | } | |
a3d827a7 | 1139 | $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive'); |
6a488035 TO |
1140 | if ($contribId) { |
1141 | CRM_Contribute_BAO_Contribution::deleteContribution($contribId); | |
1142 | } | |
1143 | } | |
1144 | } | |
96025800 | 1145 | |
42e3a033 EM |
1146 | /** |
1147 | * Build Membership Block in Contribution Pages. | |
1148 | * | |
42e3a033 EM |
1149 | * @param int $cid |
1150 | * Contact checked for having a current membership for a particular membership. | |
a46bfec1 EM |
1151 | * @param bool $isContributionMainPage |
1152 | * Is this the main page? If so add form input fields. | |
1153 | * (or better yet don't have this functionality in a function shared with forms that don't share it). | |
376b971e | 1154 | * @param int|array $selectedMembershipTypeID |
42e3a033 EM |
1155 | * Selected membership id. |
1156 | * @param bool $thankPage | |
1157 | * Thank you page. | |
1158 | * @param null $isTest | |
1159 | * | |
1160 | * @return bool | |
1161 | * Is this a separate membership payment | |
9ac53857 | 1162 | * |
1163 | * @throws \CiviCRM_API3_Exception | |
1164 | * @throws \CRM_Core_Exception | |
42e3a033 EM |
1165 | */ |
1166 | protected function buildMembershipBlock( | |
1167 | $cid, | |
a46bfec1 | 1168 | $isContributionMainPage = FALSE, |
42e3a033 EM |
1169 | $selectedMembershipTypeID = NULL, |
1170 | $thankPage = FALSE, | |
1171 | $isTest = NULL | |
1172 | ) { | |
1173 | ||
1174 | $separateMembershipPayment = FALSE; | |
1175 | if ($this->_membershipBlock) { | |
be2fb01f | 1176 | $this->_currentMemberships = []; |
42e3a033 | 1177 | |
be2fb01f | 1178 | $membershipTypeIds = $membershipTypes = $radio = []; |
3ec3dcbe | 1179 | $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember); |
42e3a033 EM |
1180 | |
1181 | $allowAutoRenewMembership = $autoRenewOption = FALSE; | |
be2fb01f | 1182 | $autoRenewMembershipTypeOptions = []; |
42e3a033 | 1183 | |
9c1bc317 | 1184 | $separateMembershipPayment = $this->_membershipBlock['is_separate_payment'] ?? NULL; |
42e3a033 EM |
1185 | |
1186 | if ($membershipPriceset) { | |
1187 | foreach ($this->_priceSet['fields'] as $pField) { | |
1188 | if (empty($pField['options'])) { | |
1189 | continue; | |
1190 | } | |
1191 | foreach ($pField['options'] as $opId => $opValues) { | |
1192 | if (empty($opValues['membership_type_id'])) { | |
1193 | continue; | |
1194 | } | |
1195 | $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id']; | |
1196 | } | |
1197 | } | |
1198 | } | |
a46bfec1 EM |
1199 | elseif (!empty($this->_membershipBlock['membership_types'])) { |
1200 | $membershipTypeIds = explode(',', $this->_membershipBlock['membership_types']); | |
42e3a033 EM |
1201 | } |
1202 | ||
1203 | if (!empty($membershipTypeIds)) { | |
1204 | //set status message if wrong membershipType is included in membershipBlock | |
1205 | if (isset($this->_mid) && !$membershipPriceset) { | |
1206 | $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
1207 | $this->_mid, | |
1208 | 'membership_type_id' | |
1209 | ); | |
1210 | if (!in_array($membershipTypeID, $membershipTypeIds)) { | |
1211 | 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'); | |
1212 | } | |
1213 | } | |
1214 | ||
4c7b8a7d | 1215 | $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIds); |
42e3a033 EM |
1216 | $this->_membershipTypeValues = $membershipTypeValues; |
1217 | $endDate = NULL; | |
b974584b MWMC |
1218 | |
1219 | // Check if we support auto-renew on this contribution page | |
1220 | // FIXME: If any of the payment processors do NOT support recurring you cannot setup an | |
1221 | // auto-renew payment even if that processor is not selected. | |
1222 | $allowAutoRenewOpt = TRUE; | |
1223 | if (is_array($this->_paymentProcessors)) { | |
1224 | foreach ($this->_paymentProcessors as $id => $val) { | |
1225 | if ($id && !$val['is_recur']) { | |
1226 | $allowAutoRenewOpt = FALSE; | |
1227 | } | |
1228 | } | |
1229 | } | |
42e3a033 EM |
1230 | foreach ($membershipTypeIds as $value) { |
1231 | $memType = $membershipTypeValues[$value]; | |
1232 | if ($selectedMembershipTypeID != NULL) { | |
1233 | if ($memType['id'] == $selectedMembershipTypeID) { | |
1234 | $this->assign('minimum_fee', | |
1235 | CRM_Utils_Array::value('minimum_fee', $memType) | |
1236 | ); | |
1237 | $this->assign('membership_name', $memType['name']); | |
1238 | if (!$thankPage && $cid) { | |
1239 | $membership = new CRM_Member_DAO_Membership(); | |
1240 | $membership->contact_id = $cid; | |
1241 | $membership->membership_type_id = $memType['id']; | |
1242 | if ($membership->find(TRUE)) { | |
1243 | $this->assign('renewal_mode', TRUE); | |
1244 | $memType['current_membership'] = $membership->end_date; | |
1245 | $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; | |
1246 | } | |
1247 | } | |
1248 | $membershipTypes[] = $memType; | |
1249 | } | |
1250 | } | |
1251 | elseif ($memType['is_active']) { | |
42e3a033 EM |
1252 | |
1253 | if ($allowAutoRenewOpt) { | |
be2fb01f | 1254 | $javascriptMethod = ['onclick' => "return showHideAutoRenew( this.value );"]; |
52b04786 | 1255 | $isAvailableAutoRenew = $this->_membershipBlock['auto_renew'][$value] ?? 1; |
1256 | $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $memType['auto_renew'] * $isAvailableAutoRenew; | |
42e3a033 EM |
1257 | $allowAutoRenewMembership = TRUE; |
1258 | } | |
b974584b MWMC |
1259 | else { |
1260 | $javascriptMethod = NULL; | |
1261 | $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = 0; | |
1262 | } | |
42e3a033 EM |
1263 | |
1264 | //add membership type. | |
1265 | $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL, | |
1266 | $memType['id'], $javascriptMethod | |
1267 | ); | |
1268 | if ($cid) { | |
1269 | $membership = new CRM_Member_DAO_Membership(); | |
1270 | $membership->contact_id = $cid; | |
1271 | $membership->membership_type_id = $memType['id']; | |
1272 | ||
1273 | //show current membership, skip pending and cancelled membership records, | |
1274 | //because we take first membership record id for renewal | |
1275 | $membership->whereAdd('status_id != 5 AND status_id !=6'); | |
1276 | ||
1277 | if (!is_null($isTest)) { | |
1278 | $membership->is_test = $isTest; | |
1279 | } | |
1280 | ||
1281 | //CRM-4297 | |
1282 | $membership->orderBy('end_date DESC'); | |
1283 | ||
1284 | if ($membership->find(TRUE)) { | |
1285 | if (!$membership->end_date) { | |
1286 | unset($radio[$memType['id']]); | |
1287 | $this->assign('islifetime', TRUE); | |
1288 | continue; | |
1289 | } | |
1290 | $this->assign('renewal_mode', TRUE); | |
1291 | $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; | |
1292 | $memType['current_membership'] = $membership->end_date; | |
1293 | if (!$endDate) { | |
1294 | $endDate = $memType['current_membership']; | |
1295 | $this->_defaultMemTypeId = $memType['id']; | |
1296 | } | |
1297 | if ($memType['current_membership'] < $endDate) { | |
1298 | $endDate = $memType['current_membership']; | |
1299 | $this->_defaultMemTypeId = $memType['id']; | |
1300 | } | |
1301 | } | |
1302 | } | |
1303 | $membershipTypes[] = $memType; | |
1304 | } | |
1305 | } | |
1306 | } | |
1307 | ||
a46bfec1 EM |
1308 | $this->assign('membershipBlock', $this->_membershipBlock); |
1309 | $this->assign('showRadio', $isContributionMainPage); | |
1310 | $this->assign('membershipTypes', $membershipTypes); | |
1311 | $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership); | |
1312 | $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions)); | |
1313 | //give preference to user submitted auto_renew value. | |
3ec3dcbe | 1314 | $takeUserSubmittedAutoRenew = (!empty($_POST) || $this->isSubmitted()); |
a46bfec1 EM |
1315 | $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew); |
1316 | ||
c843169c MW |
1317 | // Assign autorenew option (0:hide,1:optional,2:required) so we can use it in confirmation etc. |
1318 | $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId); | |
376b971e JP |
1319 | //$selectedMembershipTypeID is retrieved as an array for membership priceset if multiple |
1320 | //options for different organisation is selected on the contribution page. | |
1321 | if (is_numeric($selectedMembershipTypeID) && isset($membershipTypeValues[$selectedMembershipTypeID]['auto_renew'])) { | |
c843169c MW |
1322 | $this->assign('autoRenewOption', $membershipTypeValues[$selectedMembershipTypeID]['auto_renew']); |
1323 | } | |
1324 | else { | |
1325 | $this->assign('autoRenewOption', $autoRenewOption); | |
1326 | } | |
1327 | ||
a46bfec1 | 1328 | if ($isContributionMainPage) { |
42e3a033 | 1329 | if (!$membershipPriceset) { |
a46bfec1 | 1330 | if (!$this->_membershipBlock['is_required']) { |
42e3a033 EM |
1331 | $this->assign('showRadioNoThanks', TRUE); |
1332 | $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL); | |
1333 | $this->addGroup($radio, 'selectMembership', NULL); | |
1334 | } | |
a46bfec1 | 1335 | elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) { |
42e3a033 | 1336 | $temp = array_keys($radio); |
be2fb01f | 1337 | $this->add('hidden', 'selectMembership', $temp[0], ['id' => 'selectMembership']); |
42e3a033 EM |
1338 | $this->assign('singleMembership', TRUE); |
1339 | $this->assign('showRadio', FALSE); | |
1340 | } | |
1341 | else { | |
1342 | $this->addGroup($radio, 'selectMembership', NULL); | |
1343 | } | |
1344 | ||
1345 | $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required'); | |
1346 | } | |
42e3a033 | 1347 | |
fb357d6e | 1348 | if ((!$this->_values['is_pay_later'] || is_array($this->_paymentProcessors)) && ($allowAutoRenewMembership || $autoRenewOption)) { |
c843169c MW |
1349 | if ($autoRenewOption == 2) { |
1350 | $this->addElement('hidden', 'auto_renew', ts('Please renew my membership automatically.')); | |
1351 | } | |
1352 | else { | |
1353 | $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.')); | |
1354 | } | |
42e3a033 EM |
1355 | } |
1356 | ||
1357 | } | |
42e3a033 EM |
1358 | } |
1359 | ||
1360 | return $separateMembershipPayment; | |
1361 | } | |
1362 | ||
90102a32 EM |
1363 | /** |
1364 | * Determine if recurring parameters need to be added to the form parameters. | |
8cb12ff1 | 1365 | * |
90102a32 EM |
1366 | * - is_recur |
1367 | * - frequency_interval | |
1368 | * - frequency_unit | |
1369 | * | |
1370 | * For membership this is based on the membership type. | |
1371 | * | |
1372 | * This needs to be done before processing the pre-approval redirect where relevant on the main page or before any payment processing. | |
1373 | * | |
1374 | * Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout. | |
1375 | */ | |
1376 | protected function setRecurringMembershipParams() { | |
9c1bc317 | 1377 | $selectedMembershipTypeID = $this->_params['selectMembership'] ?? NULL; |
8cb12ff1 | 1378 | if ($selectedMembershipTypeID) { |
fd359255 EM |
1379 | // @todo the price_x fields will ALWAYS allow us to determine the membership - so we should ignore |
1380 | // 'selectMembership' and calculate from the price_x fields so we have one method that always works | |
1381 | // this is lazy & only catches when selectMembership is set, but the worst of all worlds would be to fix | |
1382 | // this with an else (calculate for price set). | |
8cb12ff1 | 1383 | $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($this->_priceSetId); |
1384 | if (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_required']) | |
1385 | || (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_optional']) && | |
1386 | !empty($this->_params['is_recur'])) | |
1387 | ) { | |
fd359255 EM |
1388 | $this->_params['auto_renew'] = TRUE; |
1389 | } | |
1390 | } | |
8cb12ff1 | 1391 | if ((!empty($this->_params['selectMembership']) || !empty($this->_params['priceSetId'])) |
1392 | && !empty($this->_paymentProcessor['is_recur']) && | |
b99f3e96 | 1393 | !empty($this->_params['auto_renew']) |
8cb12ff1 | 1394 | && empty($this->_params['is_recur']) && empty($this->_params['frequency_interval']) |
90102a32 EM |
1395 | ) { |
1396 | ||
1397 | $this->_params['is_recur'] = $this->_values['is_recur'] = 1; | |
1398 | // check if price set is not quick config | |
1399 | if (!empty($this->_params['priceSetId']) && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config')) { | |
1400 | list($this->_params['frequency_interval'], $this->_params['frequency_unit']) = CRM_Price_BAO_PriceSet::getRecurDetails($this->_params['priceSetId']); | |
1401 | } | |
1402 | else { | |
1403 | // FIXME: set interval and unit based on selected membership type | |
1404 | $this->_params['frequency_interval'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
1405 | $this->_params['selectMembership'], 'duration_interval' | |
1406 | ); | |
1407 | $this->_params['frequency_unit'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
1408 | $this->_params['selectMembership'], 'duration_unit' | |
1409 | ); | |
1410 | } | |
1411 | } | |
1412 | } | |
1413 | ||
e364c762 | 1414 | /** |
1415 | * Get the payment processor object for the submission, returning the manual one for offline payments. | |
1416 | * | |
1417 | * @return CRM_Core_Payment | |
1418 | */ | |
1419 | protected function getPaymentProcessorObject() { | |
1420 | if (!empty($this->_paymentProcessor)) { | |
1421 | return $this->_paymentProcessor['object']; | |
1422 | } | |
1423 | return new CRM_Core_Payment_Manual(); | |
1424 | } | |
1425 | ||
ec57bf87 | 1426 | /** |
1427 | * Get the amount for the main contribution. | |
1428 | * | |
1429 | * The goal is to expand this function so that all the argy-bargy of figuring out the amount | |
1430 | * winds up here as the main spaghetti shrinks. | |
1431 | * | |
1432 | * If there is a separate membership contribution this is the 'other one'. Otherwise there | |
1433 | * is only one. | |
1434 | * | |
1435 | * @param $params | |
1436 | * | |
1437 | * @return float | |
1438 | * | |
1439 | * @throws \CiviCRM_API3_Exception | |
1440 | */ | |
1441 | protected function getMainContributionAmount($params) { | |
1442 | if (!empty($params['selectMembership'])) { | |
1443 | if (empty($params['amount']) && !$this->_separateMembershipPayment) { | |
1444 | return CRM_Member_BAO_MembershipType::getMembershipType($params['selectMembership'])['minimum_fee'] ?? 0; | |
1445 | } | |
1446 | } | |
1447 | return $params['amount'] ?? 0; | |
1448 | } | |
1449 | ||
bdcbbfea | 1450 | /** |
1451 | * Wrapper for processAmount that also sets autorenew. | |
1452 | * | |
1453 | * @param $fields | |
1454 | * This is the output of the function CRM_Price_BAO_PriceSet::getSetDetail($priceSetID, FALSE, FALSE); | |
1455 | * And, it would make sense to introduce caching into that function and call it from here rather than | |
1456 | * require the $fields array which is passed from pillar to post around the form in order to pass it in here. | |
1457 | * @param array $params | |
1458 | * Params reflecting form input e.g with fields 'price_5' => 7, 'price_8' => array(7, 8) | |
1459 | * @param $lineItems | |
1460 | * Line item array to be altered. | |
1461 | * @param int $priceSetID | |
1462 | */ | |
1463 | public function processAmountAndGetAutoRenew($fields, &$params, &$lineItems, $priceSetID = NULL) { | |
1464 | CRM_Price_BAO_PriceSet::processAmount($fields, $params, $lineItems, $priceSetID); | |
1465 | $autoRenew = []; | |
1466 | $autoRenew[0] = $autoRenew[1] = $autoRenew[2] = 0; | |
1467 | foreach ($lineItems as $lineItem) { | |
1468 | if (!empty($lineItem['auto_renew']) && | |
1469 | is_numeric($lineItem['auto_renew']) | |
1470 | ) { | |
1471 | $autoRenew[$lineItem['auto_renew']] += $lineItem['line_total']; | |
1472 | } | |
1473 | } | |
1474 | if (count($autoRenew) > 1) { | |
1475 | $params['autoRenew'] = $autoRenew; | |
1476 | } | |
1477 | } | |
1478 | ||
6a488035 | 1479 | } |