Merge pull request #17197 from eileenmcnaughton/act_r_test
[civicrm-core.git] / CRM / Contribute / BAO / ContributionPage.php
CommitLineData
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/**
19 * This class contains Contribution Page related functions.
20 */
21class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_ContributionPage {
22
23 /**
7bc789a5 24 * Creates a contribution page.
6a488035 25 *
014c4014 26 * @param array $params
6a488035 27 *
16b10e64 28 * @return CRM_Contribute_DAO_ContributionPage
6a488035 29 */
7bc789a5 30 public static function create($params) {
157b21d8 31 $financialTypeId = NULL;
a7488080 32 if (!empty($params['id']) && !CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $params['id'], NULL, 1)) {
157b21d8
PN
33 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $params['id'], 'financial_type_id');
34 }
7bc789a5 35
36 if (isset($params['payment_processor']) && is_array($params['payment_processor'])) {
37 $params['payment_processor'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['payment_processor']);
38 }
d0a0de8d
EM
39 $hook = empty($params['id']) ? 'create' : 'edit';
40 CRM_Utils_Hook::pre($hook, 'ContributionPage', CRM_Utils_Array::value('id', $params), $params);
6a488035
TO
41 $dao = new CRM_Contribute_DAO_ContributionPage();
42 $dao->copyValues($params);
43 $dao->save();
8cc574cf 44 if ($financialTypeId && !empty($params['financial_type_id']) && $financialTypeId != $params['financial_type_id']) {
e1c77163 45 CRM_Price_BAO_PriceFieldValue::updateFinancialType($params['id'], 'civicrm_contribution_page', $params['financial_type_id']);
157b21d8 46 }
d0a0de8d 47 CRM_Utils_Hook::post($hook, 'ContributionPage', $dao->id, $dao);
6a488035
TO
48 return $dao;
49 }
50
51 /**
fe482240 52 * Update the is_active flag in the db.
6a488035 53 *
2c4442ca 54 * @deprecated - this bypasses hooks.
55 *
014c4014
TO
56 * @param int $id
57 * Id of the database record.
58 * @param bool $is_active
59 * Value we want to set the is_active field.
6a488035 60 *
8a4fede3 61 * @return bool
62 * true if we found and updated the object, else false
6a488035 63 */
00be9182 64 public static function setIsActive($id, $is_active) {
6a488035
TO
65 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'is_active', $is_active);
66 }
67
186c9c17 68 /**
2c4442ca 69 * Load values for a contribution page.
70 *
100fef9d 71 * @param int $id
2c4442ca 72 * @param array $values
186c9c17 73 */
00be9182 74 public static function setValues($id, &$values) {
be2fb01f 75 $modules = ['CiviContribute', 'soft_credit', 'on_behalf'];
4779abb3 76 $values['custom_pre_id'] = $values['custom_post_id'] = NULL;
6a488035 77
be2fb01f 78 $params = ['id' => $id];
6a488035 79 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $values);
8ef12e64 80
6a488035 81 // get the profile ids
be2fb01f 82 $ufJoinParams = [
6a488035
TO
83 'entity_table' => 'civicrm_contribution_page',
84 'entity_id' => $id,
be2fb01f 85 ];
6a488035 86
4779abb3 87 // retrieve profile id as also unserialize module_data corresponding to each $module
88 foreach ($modules as $module) {
89 $ufJoinParams['module'] = $module;
90 $ufJoin = new CRM_Core_DAO_UFJoin();
91 $ufJoin->copyValues($ufJoinParams);
92 if ($module == 'CiviContribute') {
93 $ufJoin->orderBy('weight asc');
94 $ufJoin->find();
95 while ($ufJoin->fetch()) {
96 if ($ufJoin->weight == 1) {
97 $values['custom_pre_id'] = $ufJoin->uf_group_id;
98 }
99 else {
100 $values['custom_post_id'] = $ufJoin->uf_group_id;
101 }
102 }
103 }
104 else {
105 $ufJoin->find(TRUE);
106 if (!$ufJoin->is_active) {
107 continue;
108 }
109 $params = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoin->module_data, TRUE, $module);
110 $values = array_merge($params, $values);
111 if ($module == 'soft_credit') {
112 $values['honoree_profile_id'] = $ufJoin->uf_group_id;
113 $values['honor_block_is_active'] = $ufJoin->is_active;
114 }
115 else {
116 $values['onbehalf_profile_id'] = $ufJoin->uf_group_id;
117 }
118 }
6a488035 119 }
874c9be7 120 }
6a488035
TO
121
122 /**
fe482240 123 * Send the emails.
6a488035 124 *
014c4014
TO
125 * @param int $contactID
126 * Contact id.
127 * @param array $values
128 * Associated array of fields.
129 * @param bool $isTest
130 * If in test mode.
131 * @param bool $returnMessageText
132 * Return the message text instead of sending the mail.
6a488035 133 *
2c4442ca 134 * @param array $fieldTypes
7b6485a1 135 *
136 * @throws \CRM_Core_Exception
6a488035 137 */
00be9182 138 public static function sendMail($contactID, $values, $isTest = FALSE, $returnMessageText = FALSE, $fieldTypes = NULL) {
be2fb01f
CW
139 $gIds = [];
140 $params = ['custom_pre_id' => [], 'custom_post_id' => []];
6a488035 141 $email = NULL;
6e10d414 142
143 // We are trying to fight the good fight against leaky variables (CRM-17519) so let's get really explicit
144 // about ensuring the variables we want for the template are defined.
145 // @todo add to this until all tpl params are explicit in this function and not waltzing around the codebase.
858f7096 146 // Next stage is to remove this & ensure there are no e-notices - ie. all are set before they hit this fn.
be2fb01f 147 $valuesRequiredForTemplate = [
858f7096 148 'customPre',
149 'customPost',
150 'customPre_grouptitle',
151 'customPost_grouptitle',
152 'useForMember',
153 'membership_assign',
154 'amount',
d9924163 155 'receipt_date',
c2358f41 156 'is_pay_later',
be2fb01f 157 ];
858f7096 158
6e10d414 159 foreach ($valuesRequiredForTemplate as $valueRequiredForTemplate) {
160 if (!isset($values[$valueRequiredForTemplate])) {
161 $values[$valueRequiredForTemplate] = NULL;
162 }
163 }
164
6a488035
TO
165 if (isset($values['custom_pre_id'])) {
166 $preProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_pre_id']);
8cc574cf 167 if ($preProfileType == 'Membership' && !empty($values['membership_id'])) {
be2fb01f
CW
168 $params['custom_pre_id'] = [
169 [
6a488035
TO
170 'member_id',
171 '=',
172 $values['membership_id'],
173 0,
174 0,
be2fb01f
CW
175 ],
176 ];
6a488035 177 }
8cc574cf 178 elseif ($preProfileType == 'Contribution' && !empty($values['contribution_id'])) {
be2fb01f
CW
179 $params['custom_pre_id'] = [
180 [
6a488035
TO
181 'contribution_id',
182 '=',
183 $values['contribution_id'],
184 0,
185 0,
be2fb01f
CW
186 ],
187 ];
6a488035
TO
188 }
189
190 $gIds['custom_pre_id'] = $values['custom_pre_id'];
191 }
192
193 if (isset($values['custom_post_id'])) {
194 $postProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_post_id']);
8cc574cf 195 if ($postProfileType == 'Membership' && !empty($values['membership_id'])) {
be2fb01f
CW
196 $params['custom_post_id'] = [
197 [
6a488035
TO
198 'member_id',
199 '=',
200 $values['membership_id'],
201 0,
202 0,
be2fb01f
CW
203 ],
204 ];
6a488035 205 }
8cc574cf 206 elseif ($postProfileType == 'Contribution' && !empty($values['contribution_id'])) {
be2fb01f
CW
207 $params['custom_post_id'] = [
208 [
6a488035
TO
209 'contribution_id',
210 '=',
211 $values['contribution_id'],
212 0,
213 0,
be2fb01f
CW
214 ],
215 ];
6a488035
TO
216 }
217
218 $gIds['custom_post_id'] = $values['custom_post_id'];
219 }
220
a7488080
CW
221 if (!empty($values['is_for_organization'])) {
222 if (!empty($values['membership_id'])) {
be2fb01f
CW
223 $params['onbehalf_profile'] = [
224 [
6a488035
TO
225 'member_id',
226 '=',
227 $values['membership_id'],
228 0,
229 0,
be2fb01f
CW
230 ],
231 ];
6a488035 232 }
a7488080 233 elseif (!empty($values['contribution_id'])) {
be2fb01f
CW
234 $params['onbehalf_profile'] = [
235 [
6a488035
TO
236 'contribution_id',
237 '=',
238 $values['contribution_id'],
239 0,
240 0,
be2fb01f
CW
241 ],
242 ];
6a488035
TO
243 }
244 }
245
246 //check whether it is a test drive
247 if ($isTest && !empty($params['custom_pre_id'])) {
be2fb01f 248 $params['custom_pre_id'][] = [
6a488035
TO
249 'contribution_test',
250 '=',
251 1,
252 0,
253 0,
be2fb01f 254 ];
6a488035
TO
255 }
256
257 if ($isTest && !empty($params['custom_post_id'])) {
be2fb01f 258 $params['custom_post_id'][] = [
6a488035
TO
259 'contribution_test',
260 '=',
261 1,
262 0,
263 0,
be2fb01f 264 ];
6a488035 265 }
8ef12e64 266
6a488035
TO
267 if (!$returnMessageText && !empty($gIds)) {
268 //send notification email if field values are set (CRM-1941)
269 foreach ($gIds as $key => $gId) {
270 if ($gId) {
271 $email = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify');
272 if ($email) {
481a74f4 273 $val = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $contactID, CRM_Utils_Array::value($key, $params), TRUE);
6a488035
TO
274 CRM_Core_BAO_UFGroup::commonSendMail($contactID, $val);
275 }
276 }
277 }
278 }
279
8cc574cf 280 if (!empty($values['is_email_receipt']) || !empty($values['onbehalf_dupe_alert']) ||
6a488035
TO
281 $returnMessageText
282 ) {
283 $template = CRM_Core_Smarty::singleton();
284
6a488035 285 if (!array_key_exists('related_contact', $values)) {
99c5b751 286 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE, CRM_Core_BAO_LocationType::getBilling());
6a488035
TO
287 }
288 // get primary location email if no email exist( for billing location).
289 if (!$email) {
290 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
291 }
292 if (empty($displayName)) {
293 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
294 }
295
296 //for display profile need to get individual contact id,
297 //hence get it from related_contact if on behalf of org true CRM-3767
298 //CRM-5001 Contribution/Membership:: On Behalf of Organization,
299 //If profile GROUP contain the Individual type then consider the
300 //profile is of Individual ( including the custom data of membership/contribution )
301 //IF Individual type not present in profile then it is consider as Organization data.
302 $userID = $contactID;
303 if ($preID = CRM_Utils_Array::value('custom_pre_id', $values)) {
a7488080 304 if (!empty($values['related_contact'])) {
6a488035 305 $preProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($preID);
da5d91d5 306 if (in_array('Individual', $preProfileTypes) || in_array('Contact', $preProfileTypes)) {
6a488035 307 //Take Individual contact ID
9c1bc317 308 $userID = $values['related_contact'] ?? NULL;
6a488035
TO
309 }
310 }
6e10d414 311 list($values['customPre_grouptitle'], $values['customPre']) = self::getProfileNameAndFields($preID, $userID, $params['custom_pre_id']);
6a488035
TO
312 }
313 $userID = $contactID;
314 if ($postID = CRM_Utils_Array::value('custom_post_id', $values)) {
a7488080 315 if (!empty($values['related_contact'])) {
6a488035
TO
316 $postProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($postID);
317 if (in_array('Individual', $postProfileTypes) || in_array('Contact', $postProfileTypes)) {
318 //Take Individual contact ID
9c1bc317 319 $userID = $values['related_contact'] ?? NULL;
6a488035
TO
320 }
321 }
6e10d414 322 list($values['customPost_grouptitle'], $values['customPost']) = self::getProfileNameAndFields($postID, $userID, $params['custom_post_id']);
6a488035 323 }
1421174e 324 if (isset($values['honor'])) {
325 $honorValues = $values['honor'];
be2fb01f 326 $template->_values = ['honoree_profile_id' => $values['honoree_profile_id']];
1421174e 327 CRM_Contribute_BAO_ContributionSoft::formatHonoreeProfileFields(
328 $template,
329 $honorValues['honor_profile_values'],
1421174e 330 $honorValues['honor_id']
331 );
332 }
6a488035 333
77c21b32 334 $title = $values['title'] ?? CRM_Contribute_BAO_Contribution_Utils::getContributionPageTitle($values['contribution_page_id']);
6a488035 335
557e8899 336 // Set email variables explicitly to avoid leaky smarty variables.
337 // All of these will be assigned to the template, replacing any that might be assigned elsewhere.
be2fb01f 338 $tplParams = [
6a488035 339 'email' => $email,
6b409353 340 'receiptFromEmail' => $values['receipt_from_email'] ?? NULL,
6a488035
TO
341 'contactID' => $contactID,
342 'displayName' => $displayName,
6b409353
CW
343 'contributionID' => $values['contribution_id'] ?? NULL,
344 'contributionOtherID' => $values['contribution_other_id'] ?? NULL,
6a488035 345 // CRM-5095
6b409353 346 'lineItem' => $values['lineItem'] ?? NULL,
6a488035 347 // CRM-5095
6b409353 348 'priceSetID' => $values['priceSetID'] ?? NULL,
6a488035 349 'title' => $title,
6b409353
CW
350 'isShare' => $values['is_share'] ?? NULL,
351 'thankyou_title' => $values['thankyou_title'] ?? NULL,
6e10d414 352 'customPre' => $values['customPre'],
353 'customPre_grouptitle' => $values['customPre_grouptitle'],
354 'customPost' => $values['customPost'],
355 'customPost_grouptitle' => $values['customPost_grouptitle'],
557e8899 356 'useForMember' => $values['useForMember'],
858f7096 357 'membership_assign' => $values['membership_assign'],
358 'amount' => $values['amount'],
c2358f41 359 'is_pay_later' => $values['is_pay_later'],
76e8d9c4 360 'receipt_date' => !$values['receipt_date'] ? NULL : date('YmdHis', strtotime($values['receipt_date'])),
6b409353
CW
361 'pay_later_receipt' => $values['pay_later_receipt'] ?? NULL,
362 'honor_block_is_active' => $values['honor_block_is_active'] ?? NULL,
363 'contributionStatus' => $values['contribution_status'] ?? NULL,
be2fb01f 364 ];
6a488035 365
91bb24a7 366 if ($contributionTypeId = CRM_Utils_Array::value('financial_type_id', $values)) {
b956b310 367 $tplParams['financialTypeId'] = $contributionTypeId;
368 $tplParams['financialTypeName'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
91bb24a7 369 $contributionTypeId);
b956b310 370 // Legacy support
371 $tplParams['contributionTypeName'] = $tplParams['financialTypeName'];
372 $tplParams['contributionTypeId'] = $contributionTypeId;
6a488035
TO
373 }
374
375 if ($contributionPageId = CRM_Utils_Array::value('id', $values)) {
376 $tplParams['contributionPageId'] = $contributionPageId;
377 }
378
379 // address required during receipt processing (pdf and email receipt)
380 if ($displayAddress = CRM_Utils_Array::value('address', $values)) {
381 $tplParams['address'] = $displayAddress;
382 }
383
384 // CRM-6976
9c1bc317 385 $originalCCReceipt = $values['cc_receipt'] ?? NULL;
6a488035
TO
386
387 // cc to related contacts of contributor OR the one who
388 // signs up. Is used for cases like - on behalf of
389 // contribution / signup ..etc
390 if (array_key_exists('related_contact', $values)) {
391 list($ccDisplayName, $ccEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($values['related_contact']);
392 $ccMailId = "{$ccDisplayName} <{$ccEmail}>";
393
fdd2a6db 394 //@todo - this is the only place in this function where $values is altered - but I can't find any evidence it is used
0d8afee2 395 $values['cc_receipt'] = !empty($values['cc_receipt']) ? ($values['cc_receipt'] . ',' . $ccMailId) : $ccMailId;
6a488035
TO
396
397 // reset primary-email in the template
398 $tplParams['email'] = $ccEmail;
399
400 $tplParams['onBehalfName'] = $displayName;
401 $tplParams['onBehalfEmail'] = $email;
402
4779abb3 403 if (!empty($values['onbehalf_profile_id'])) {
404 self::buildCustomDisplay($values['onbehalf_profile_id'], 'onBehalfProfile', $contactID, $template, $params['onbehalf_profile'], $fieldTypes);
405 }
6a488035
TO
406 }
407
408 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
be2fb01f 409 $sendTemplateParams = [
12dac866 410 'groupName' => !empty($values['isMembership']) ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
411 'valueName' => !empty($values['isMembership']) ? 'membership_online_receipt' : 'contribution_online_receipt',
6a488035
TO
412 'contactId' => $contactID,
413 'tplParams' => $tplParams,
414 'isTest' => $isTest,
415 'PDFFilename' => 'receipt.pdf',
be2fb01f 416 ];
6a488035
TO
417
418 if ($returnMessageText) {
c6327d7d 419 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
be2fb01f 420 return [
6a488035
TO
421 'subject' => $subject,
422 'body' => $message,
423 'to' => $displayName,
424 'html' => $html,
be2fb01f 425 ];
6a488035
TO
426 }
427
ec7e3954
E
428 if (empty($values['receipt_from_name']) && empty($values['receipt_from_name'])) {
429 list($values['receipt_from_name'], $values['receipt_from_email']) = CRM_Core_BAO_Domain::getNameAndEmail();
430 }
431
6a488035
TO
432 if ($values['is_email_receipt']) {
433 $sendTemplateParams['from'] = CRM_Utils_Array::value('receipt_from_name', $values) . ' <' . $values['receipt_from_email'] . '>';
434 $sendTemplateParams['toName'] = $displayName;
435 $sendTemplateParams['toEmail'] = $email;
9c1bc317
CW
436 $sendTemplateParams['cc'] = $values['cc_receipt'] ?? NULL;
437 $sendTemplateParams['bcc'] = $values['bcc_receipt'] ?? NULL;
9161952c 438 //send email with pdf invoice
481a74f4 439 $template = CRM_Core_Smarty::singleton();
9161952c 440 $taxAmt = $template->get_template_vars('dataArray');
e3eb0205
SL
441 $isEmailPDF = Civi::settings()->get('invoice_is_email_pdf');
442 $invoicing = Civi::settings()->get('invoicing');
443 if ($invoicing && !empty($isEmailPDF)) {
874c9be7 444 $sendTemplateParams['isEmailPdf'] = TRUE;
9161952c
PD
445 $sendTemplateParams['contributionId'] = $values['contribution_id'];
446 }
c6327d7d 447 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
448 }
449
450 // send duplicate alert, if dupe match found during on-behalf-of processing.
a7488080 451 if (!empty($values['onbehalf_dupe_alert'])) {
6a488035
TO
452 $sendTemplateParams['groupName'] = 'msg_tpl_workflow_contribution';
453 $sendTemplateParams['valueName'] = 'contribution_dupalert';
454 $sendTemplateParams['from'] = ts('Automatically Generated') . " <{$values['receipt_from_email']}>";
9c1bc317
CW
455 $sendTemplateParams['toName'] = $values['receipt_from_name'] ?? NULL;
456 $sendTemplateParams['toEmail'] = $values['receipt_from_email'] ?? NULL;
6a488035
TO
457 $sendTemplateParams['tplParams']['onBehalfID'] = $contactID;
458 $sendTemplateParams['tplParams']['receiptMessage'] = $message;
459
460 // fix cc and reset back to original, CRM-6976
461 $sendTemplateParams['cc'] = $originalCCReceipt;
462
c6327d7d 463 CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
464 }
465 }
466 }
8ef12e64 467
6e10d414 468 /**
469 * Get the profile title and fields.
470 *
471 * @param int $gid
472 * @param int $cid
473 * @param array $params
474 * @param array $fieldTypes
475 *
476 * @return array
7b6485a1 477 *
478 * @throws \CRM_Core_Exception
6e10d414 479 */
7b6485a1 480 protected static function getProfileNameAndFields($gid, $cid, $params, $fieldTypes = []) {
6e10d414 481 $groupTitle = NULL;
be2fb01f 482 $values = [];
6e10d414 483 if ($gid) {
484 if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $cid)) {
485 $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL);
486 foreach ($fields as $k => $v) {
487 if (!$groupTitle) {
7b6485a1 488 $groupTitle = $v['groupTitle'];
6e10d414 489 }
490 // suppress all file fields from display and formatting fields
491 if (
7b6485a1 492 $v['data_type'] === 'File' || $v['name'] === 'image_URL' || $v['field_type'] === 'Formatting') {
6e10d414 493 unset($fields[$k]);
494 }
495
496 if (!empty($fieldTypes) && (!in_array($v['field_type'], $fieldTypes))) {
497 unset($fields[$k]);
498 }
499 }
500
501 CRM_Core_BAO_UFGroup::getValues($cid, $fields, $values, FALSE, $params);
502 }
503 }
be2fb01f 504 return [$groupTitle, $values];
6e10d414 505 }
506
186c9c17 507 /**
84f0ed41 508 * Send the emails for Recurring Contribution Notification.
6a488035 509 *
014c4014
TO
510 * @param string $type
511 * TxnType.
512 * @param int $contactID
513 * Contact id for contributor.
514 * @param int $pageID
515 * Contribution page id.
516 * @param object $recur
517 * Object of recurring contribution table.
2a6da8d7 518 * @param bool|object $autoRenewMembership is it a auto renew membership.
6a488035 519 */
00be9182 520 public static function recurringNotify($type, $contactID, $pageID, $recur, $autoRenewMembership = FALSE) {
be2fb01f 521 $value = [];
ddf80fb9 522 $isEmailReceipt = FALSE;
6a488035 523 if ($pageID) {
be2fb01f 524 CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', $pageID, $value, [
91bb24a7 525 'title',
526 'is_email_receipt',
527 'receipt_from_name',
528 'receipt_from_email',
529 'cc_receipt',
530 'bcc_receipt',
be2fb01f 531 ]);
9c1bc317 532 $isEmailReceipt = $value[$pageID]['is_email_receipt'] ?? NULL;
6a488035 533 }
ab30e033
EM
534 elseif ($recur->id) {
535 // This means we are coming from back-office - ie. no page ID, but recurring.
536 // Ideally this information would be passed into the function clearly rather than guessing by convention.
537 $isEmailReceipt = TRUE;
6a488035 538 }
ab30e033
EM
539
540 if ($isEmailReceipt) {
6a488035
TO
541 if ($pageID) {
542 $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>';
543
544 $receiptFromName = $value[$pageID]['receipt_from_name'];
545 $receiptFromEmail = $value[$pageID]['receipt_from_email'];
546 }
547 else {
91bb24a7 548 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
549 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
550 $receiptFromName = $domainValues[0];
6a488035
TO
551 $receiptFromEmail = $domainValues[1];
552 }
553
554 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE);
be2fb01f 555 $templatesParams = [
6a488035
TO
556 'groupName' => 'msg_tpl_workflow_contribution',
557 'valueName' => 'contribution_recurring_notify',
558 'contactId' => $contactID,
be2fb01f 559 'tplParams' => [
6a488035
TO
560 'recur_frequency_interval' => $recur->frequency_interval,
561 'recur_frequency_unit' => $recur->frequency_unit,
562 'recur_installments' => $recur->installments,
563 'recur_start_date' => $recur->start_date,
564 'recur_end_date' => $recur->end_date,
565 'recur_amount' => $recur->amount,
566 'recur_txnType' => $type,
567 'displayName' => $displayName,
568 'receipt_from_name' => $receiptFromName,
569 'receipt_from_email' => $receiptFromEmail,
570 'auto_renew_membership' => $autoRenewMembership,
be2fb01f 571 ],
6a488035
TO
572 'from' => $receiptFrom,
573 'toName' => $displayName,
574 'toEmail' => $email,
be2fb01f 575 ];
1421174e 576 //CRM-13811
039134ba 577 if ($pageID) {
9c1bc317
CW
578 $templatesParams['cc'] = $value[$pageID]['cc_receipt'] ?? NULL;
579 $templatesParams['bcc'] = $value[$pageID]['bcc_receipt'] ?? NULL;
039134ba 580 }
6a488035
TO
581 if ($recur->id) {
582 // in some cases its just recurringNotify() thats called for the first time and these urls don't get set.
583 // like in PaypalPro, & therefore we set it here additionally.
91bb24a7 584 $template = CRM_Core_Smarty::singleton();
6a488035 585 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recur->id, 'recur', 'obj');
3e473c0b 586 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'cancel');
6a488035
TO
587 $template->assign('cancelSubscriptionUrl', $url);
588
589 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'billing');
590 $template->assign('updateSubscriptionBillingUrl', $url);
591
592 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'update');
593 $template->assign('updateSubscriptionUrl', $url);
594 }
595
84f0ed41 596 list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($templatesParams);
6a488035
TO
597
598 if ($sent) {
599 CRM_Core_Error::debug_log_message('Success: mail sent for recurring notification.');
600 }
601 else {
602 CRM_Core_Error::debug_log_message('Failure: mail not sent for recurring notification.');
603 }
604 }
605 }
606
607 /**
c7e7164f 608 * Add the custom fields for contribution page (ie profile).
609 *
610 * @deprecated assigning values to smarty like this is risky because
611 * - it is hard to debug since $name is used in the assign
612 * - it is potentially 'leaky' - it's better to do this on the form
613 * or close to where it is used / required. See CRM-17519 for leakage e.g.
6a488035 614 *
014c4014
TO
615 * @param int $gid
616 * Uf group id.
6a488035 617 * @param string $name
014c4014
TO
618 * @param int $cid
619 * Contact id.
2a6da8d7 620 * @param $template
014c4014
TO
621 * @param array $params
622 * Params to build component whereclause.
2a6da8d7 623 *
6e10d414 624 * @param array|null $fieldTypes
6a488035 625 */
6e10d414 626 public static function buildCustomDisplay($gid, $name, $cid, &$template, &$params, $fieldTypes = NULL) {
627 list($groupTitle, $values) = self::getProfileNameAndFields($gid, $cid, $params, $fieldTypes);
628 if (!empty($values)) {
629 $template->assign($name, $values);
6a488035 630 }
6e10d414 631 $template->assign($name . "_grouptitle", $groupTitle);
6a488035
TO
632 }
633
634 /**
2c4442ca 635 * Make a copy of a contribution page, including all the blocks in the page.
6a488035 636 *
014c4014
TO
637 * @param int $id
638 * The contribution page id to copy.
6a488035 639 *
72b3a70c 640 * @return CRM_Contribute_DAO_ContributionPage
6a488035 641 */
00be9182 642 public static function copy($id) {
e6df7827
J
643 $session = CRM_Core_Session::singleton();
644
be2fb01f
CW
645 $fieldsFix = [
646 'prefix' => [
6a488035 647 'title' => ts('Copy of') . ' ',
be2fb01f 648 ],
e6df7827
J
649 'replace' => [
650 'created_id' => $session->get('userID'),
651 'created_date' => date('YmdHis'),
652 ],
be2fb01f 653 ];
3fec1adc 654 $copy = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', [
353ffa53 655 'id' => $id,
be2fb01f 656 ], NULL, $fieldsFix);
6a488035
TO
657
658 //copying all the blocks pertaining to the contribution page
3fec1adc 659 $copyPledgeBlock = CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', [
353ffa53
TO
660 'entity_id' => $id,
661 'entity_table' => 'civicrm_contribution_page',
be2fb01f 662 ], [
353ffa53 663 'entity_id' => $copy->id,
be2fb01f 664 ]);
6a488035 665
3fec1adc 666 $copyMembershipBlock = CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', [
353ffa53
TO
667 'entity_id' => $id,
668 'entity_table' => 'civicrm_contribution_page',
be2fb01f 669 ], [
353ffa53 670 'entity_id' => $copy->id,
be2fb01f 671 ]);
6a488035 672
3fec1adc 673 $copyUFJoin = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', [
353ffa53
TO
674 'entity_id' => $id,
675 'entity_table' => 'civicrm_contribution_page',
be2fb01f 676 ], [
353ffa53 677 'entity_id' => $copy->id,
be2fb01f 678 ]);
6a488035 679
3fec1adc 680 $copyWidget = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', [
353ffa53 681 'contribution_page_id' => $id,
be2fb01f 682 ], [
353ffa53 683 'contribution_page_id' => $copy->id,
be2fb01f 684 ]);
6a488035 685
c6914066 686 //copy price sets
9da8dc8c 687 CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_contribution_page', $id, $copy->id);
c6914066 688
3fec1adc 689 $copyTellFriend = CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', [
353ffa53
TO
690 'entity_id' => $id,
691 'entity_table' => 'civicrm_contribution_page',
be2fb01f 692 ], [
353ffa53 693 'entity_id' => $copy->id,
be2fb01f 694 ]);
6a488035 695
3fec1adc 696 $copyPersonalCampaignPages = CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', [
353ffa53
TO
697 'entity_id' => $id,
698 'entity_table' => 'civicrm_contribution_page',
be2fb01f 699 ], [
353ffa53 700 'entity_id' => $copy->id,
b7ff0a0a 701 'target_entity_id' => $copy->id,
be2fb01f 702 ]);
6a488035 703
3fec1adc 704 $copyPremium = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', [
353ffa53
TO
705 'entity_id' => $id,
706 'entity_table' => 'civicrm_contribution_page',
be2fb01f 707 ], [
353ffa53 708 'entity_id' => $copy->id,
be2fb01f 709 ]);
6a488035
TO
710 $premiumQuery = "
711SELECT id
712FROM civicrm_premiums
713WHERE entity_table = 'civicrm_contribution_page'
714 AND entity_id ={$id}";
715
e03e1641 716 $premiumDao = CRM_Core_DAO::executeQuery($premiumQuery);
6a488035
TO
717 while ($premiumDao->fetch()) {
718 if ($premiumDao->id) {
be2fb01f 719 CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_PremiumsProduct', [
91bb24a7 720 'premiums_id' => $premiumDao->id,
be2fb01f 721 ], [
91bb24a7 722 'premiums_id' => $copyPremium->id,
be2fb01f 723 ]);
6a488035
TO
724 }
725 }
726
727 $copy->save();
728
729 CRM_Utils_Hook::copy('ContributionPage', $copy);
730
731 return $copy;
732 }
733
6a488035 734 /**
100fef9d 735 * Get info for all sections enable/disable.
6a488035 736 *
2a6da8d7 737 * @param array $contribPageIds
a6c01b45
CW
738 * @return array
739 * info regarding all sections.
6a488035 740 */
be2fb01f
CW
741 public static function getSectionInfo($contribPageIds = []) {
742 $info = [];
6a488035
TO
743 $whereClause = NULL;
744 if (is_array($contribPageIds) && !empty($contribPageIds)) {
745 $whereClause = 'WHERE civicrm_contribution_page.id IN ( ' . implode(', ', $contribPageIds) . ' )';
746 }
747
be2fb01f 748 $sections = [
6a488035
TO
749 'settings',
750 'amount',
751 'membership',
752 'custom',
753 'thankyou',
754 'friend',
755 'pcp',
756 'widget',
757 'premium',
be2fb01f 758 ];
6a488035
TO
759 $query = "
760 SELECT civicrm_contribution_page.id as id,
8ef12e64 761 civicrm_contribution_page.financial_type_id as settings,
6a488035
TO
762 amount_block_is_active as amount,
763 civicrm_membership_block.id as membership,
764 civicrm_uf_join.id as custom,
765 civicrm_contribution_page.thankyou_title as thankyou,
766 civicrm_tell_friend.id as friend,
767 civicrm_pcp_block.id as pcp,
768 civicrm_contribution_widget.id as widget,
769 civicrm_premiums.id as premium
770 FROM civicrm_contribution_page
771LEFT JOIN civicrm_membership_block ON ( civicrm_membership_block.entity_id = civicrm_contribution_page.id
772 AND civicrm_membership_block.entity_table = 'civicrm_contribution_page'
773 AND civicrm_membership_block.is_active = 1 )
774LEFT JOIN civicrm_uf_join ON ( civicrm_uf_join.entity_id = civicrm_contribution_page.id
775 AND civicrm_uf_join.entity_table = 'civicrm_contribution_page'
776 AND module = 'CiviContribute'
777 AND civicrm_uf_join.is_active = 1 )
778LEFT JOIN civicrm_tell_friend ON ( civicrm_tell_friend.entity_id = civicrm_contribution_page.id
779 AND civicrm_tell_friend.entity_table = 'civicrm_contribution_page'
780 AND civicrm_tell_friend.is_active = 1)
781LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_contribution_page.id
782 AND civicrm_pcp_block.entity_table = 'civicrm_contribution_page'
783 AND civicrm_pcp_block.is_active = 1 )
784LEFT JOIN civicrm_contribution_widget ON ( civicrm_contribution_widget.contribution_page_id = civicrm_contribution_page.id
785 AND civicrm_contribution_widget.is_active = 1 )
786LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm_contribution_page.id
787 AND civicrm_premiums.entity_table = 'civicrm_contribution_page'
788 AND civicrm_premiums.premiums_active = 1 )
789 $whereClause";
790
791 $contributionPage = CRM_Core_DAO::executeQuery($query);
792 while ($contributionPage->fetch()) {
793 if (!isset($info[$contributionPage->id]) || !is_array($info[$contributionPage->id])) {
794 $info[$contributionPage->id] = array_fill_keys(array_values($sections), FALSE);
795 }
796 foreach ($sections as $section) {
797 if ($contributionPage->$section) {
798 $info[$contributionPage->id][$section] = TRUE;
799 }
800 }
801 }
802
803 return $info;
804 }
e1462487
CW
805
806 /**
807 * Get options for a given field.
808 * @see CRM_Core_DAO::buildOptions
809 *
014c4014 810 * @param string $fieldName
353ffa53
TO
811 * @param string $context : @see CRM_Core_DAO::buildOptionsContext
812 * @param array $props : whatever is known about this dao object
e1462487
CW
813 *
814 * @return array|bool
815 */
be2fb01f
CW
816 public static function buildOptions($fieldName, $context = NULL, $props = []) {
817 $params = [];
e1462487
CW
818 // Special logic for fields whose options depend on context or properties
819 switch ($fieldName) {
820 case 'financial_type_id':
341a4d71
MW
821 // @fixme - this is going to ignore context, better to get conditions, add params, and call PseudoConstant::get
822 // @fixme - https://lab.civicrm.org/dev/core/issues/547 if CiviContribute not enabled this causes an invalid query
823 // because $relationTypeId is not set in CRM_Financial_BAO_FinancialType::getIncomeFinancialType()
824 if (array_key_exists('CiviContribute', CRM_Core_Component::getEnabledComponents())) {
825 return CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
826 }
827 return [];
e1462487
CW
828 }
829 return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
830 }
ba60f73e 831
832 /**
e63910c5 833 * Get or Set honor/on_behalf params for processing module_data or setting default values.
ba60f73e 834 *
e63910c5 835 * @param array $params :
353ffa53 836 * @param bool $setDefault : If yes then returns array to used for setting default value afterward
e63910c5 837 * @param string $module : processing module_data for which module? e.g. soft_credit, on_behalf
ba60f73e 838 *
839 * @return array|string
840 */
e63910c5 841 public static function formatModuleData($params, $setDefault = FALSE, $module) {
98466ff9 842 $tsLocale = CRM_Core_I18n::getLocale();
ba60f73e 843 $config = CRM_Core_Config::singleton();
e63910c5 844 $json = $jsonDecode = NULL;
ba60f73e 845 $domain = new CRM_Core_DAO_Domain();
846 $domain->find(TRUE);
847
be2fb01f
CW
848 $moduleDataFormat = [
849 'soft_credit' => [
f23093b2 850 1 => 'soft_credit_types',
be2fb01f 851 'multilingual' => [
e63910c5 852 'honor_block_title',
cb804cd9 853 'honor_block_text',
be2fb01f
CW
854 ],
855 ],
856 'on_behalf' => [
f23093b2 857 1 => 'is_for_organization',
be2fb01f 858 'multilingual' => [
f23093b2 859 'for_organization',
be2fb01f
CW
860 ],
861 ],
862 ];
e63910c5 863
12792976 864 //When we are fetching the honor params respecting both multi and mono lingual state
ba60f73e 865 //and setting it to default param of Contribution Page's Main and Setting form
866 if ($setDefault) {
e63910c5 867 $jsonDecode = json_decode($params);
868 $jsonDecode = (array) $jsonDecode->$module;
869 if (!$domain->locales && !empty($jsonDecode['default'])) {
12792976 870 //monolingual state
e63910c5 871 $jsonDecode += (array) $jsonDecode['default'];
872 unset($jsonDecode['default']);
12792976 873 }
d50cdfb5 874 elseif (!empty($jsonDecode[$tsLocale])) {
12792976 875 //multilingual state
d50cdfb5 876 foreach ($jsonDecode[$tsLocale] as $column => $value) {
e63910c5 877 $jsonDecode[$column] = $value;
ba60f73e 878 }
d50cdfb5 879 unset($jsonDecode[$tsLocale]);
ba60f73e 880 }
e63910c5 881 return $jsonDecode;
ba60f73e 882 }
883
884 //check and handle multilingual honoree params
885 if (!$domain->locales) {
886 //if in singlelingual state simply return the array format
be2fb01f 887 $json = [$module => NULL];
e63910c5 888 foreach ($moduleDataFormat[$module] as $key => $attribute) {
889 if ($key === 'multilingual') {
be2fb01f 890 $json[$module]['default'] = [];
e63910c5 891 foreach ($attribute as $attr) {
892 $json[$module]['default'][$attr] = $params[$attr];
893 }
894 }
895 else {
896 $json[$module][$attribute] = $params[$attribute];
897 }
898 }
899 $json = json_encode($json);
ba60f73e 900 }
901 else {
902 //if in multilingual state then retrieve the module_data against this contribution and
903 //merge with earlier module_data json data to current so not to lose earlier multilingual module_data information
be2fb01f 904 $json = [$module => NULL];
e63910c5 905 foreach ($moduleDataFormat[$module] as $key => $attribute) {
906 if ($key === 'multilingual') {
be2fb01f 907 $json[$module][$config->lcMessages] = [];
e63910c5 908 foreach ($attribute as $attr) {
909 $json[$module][$config->lcMessages][$attr] = $params[$attr];
910 }
911 }
912 else {
913 $json[$module][$attribute] = $params[$attribute];
d1f8e278 914 }
d1f8e278 915 }
d1f8e278 916
917 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
e63910c5 918 $ufJoinDAO->module = $module;
d1f8e278 919 $ufJoinDAO->entity_id = $params['id'];
920 $ufJoinDAO->find(TRUE);
921 $jsonData = json_decode($ufJoinDAO->module_data);
922 if ($jsonData) {
e63910c5 923 $json[$module] = array_merge((array) $jsonData->$module, $json[$module]);
d1f8e278 924 }
e63910c5 925 $json = json_encode($json);
d1f8e278 926 }
e63910c5 927 return $json;
d1f8e278 928 }
929
930 /**
931 * Generate html for pdf in confirmation receipt email attachment.
014c4014
TO
932 * @param int $contributionId
933 * Contribution Page Id.
934 * @param int $userID
935 * Contact id for contributor.
a6c01b45 936 * @return array
9161952c 937 */
00be9182 938 public static function addInvoicePdfToEmail($contributionId, $userID) {
be2fb01f
CW
939 $contributionID = [$contributionId];
940 $contactId = [$userID];
941 $pdfParams = [
9161952c 942 'output' => 'pdf_invoice',
21dfd5f5 943 'forPage' => 'confirmpage',
be2fb01f 944 ];
1273d77c 945 $pdfHtml = CRM_Contribute_Form_Task_Invoice::printPDF($contributionID, $pdfParams, $contactId);
9161952c
PD
946 return $pdfHtml;
947 }
f64a217a
EM
948
949 /**
7fe37828
EM
950 * Helper to determine if the page supports separate membership payments.
951 *
79d7553f 952 * @param int $id form id
f64a217a 953 *
a6c01b45
CW
954 * @return bool
955 * isSeparateMembershipPayment
f64a217a 956 */
00be9182 957 public static function getIsMembershipPayment($id) {
be2fb01f 958 $membershipBlocks = civicrm_api3('membership_block', 'get', [
1330f57a
SL
959 'entity_table' => 'civicrm_contribution_page',
960 'entity_id' => $id,
961 'sequential' => TRUE,
962 ]);
22e263ad 963 if (!$membershipBlocks['count']) {
f64a217a
EM
964 return FALSE;
965 }
966 return $membershipBlocks['values'][0]['is_separate_payment'];
967 }
96025800 968
6a488035 969}