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