Merge pull request #15442 from artfulrobot/fix-select2-height-followup-from-15403
[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_PseudoConstant::contributionPage($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 $fieldsFix = [
670 'prefix' => [
671 'title' => ts('Copy of') . ' ',
672 ],
673 ];
674 $copy = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', [
675 'id' => $id,
676 ], NULL, $fieldsFix);
677
678 //copying all the blocks pertaining to the contribution page
679 $copyPledgeBlock = CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', [
680 'entity_id' => $id,
681 'entity_table' => 'civicrm_contribution_page',
682 ], [
683 'entity_id' => $copy->id,
684 ]);
685
686 $copyMembershipBlock = CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', [
687 'entity_id' => $id,
688 'entity_table' => 'civicrm_contribution_page',
689 ], [
690 'entity_id' => $copy->id,
691 ]);
692
693 $copyUFJoin = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', [
694 'entity_id' => $id,
695 'entity_table' => 'civicrm_contribution_page',
696 ], [
697 'entity_id' => $copy->id,
698 ]);
699
700 $copyWidget = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', [
701 'contribution_page_id' => $id,
702 ], [
703 'contribution_page_id' => $copy->id,
704 ]);
705
706 //copy price sets
707 CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_contribution_page', $id, $copy->id);
708
709 $copyTellFriend = CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', [
710 'entity_id' => $id,
711 'entity_table' => 'civicrm_contribution_page',
712 ], [
713 'entity_id' => $copy->id,
714 ]);
715
716 $copyPersonalCampaignPages = CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', [
717 'entity_id' => $id,
718 'entity_table' => 'civicrm_contribution_page',
719 ], [
720 'entity_id' => $copy->id,
721 'target_entity_id' => $copy->id,
722 ]);
723
724 $copyPremium = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', [
725 'entity_id' => $id,
726 'entity_table' => 'civicrm_contribution_page',
727 ], [
728 'entity_id' => $copy->id,
729 ]);
730 $premiumQuery = "
731 SELECT id
732 FROM civicrm_premiums
733 WHERE entity_table = 'civicrm_contribution_page'
734 AND entity_id ={$id}";
735
736 $premiumDao = CRM_Core_DAO::executeQuery($premiumQuery);
737 while ($premiumDao->fetch()) {
738 if ($premiumDao->id) {
739 CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_PremiumsProduct', [
740 'premiums_id' => $premiumDao->id,
741 ], [
742 'premiums_id' => $copyPremium->id,
743 ]);
744 }
745 }
746
747 $copy->save();
748
749 CRM_Utils_Hook::copy('ContributionPage', $copy);
750
751 return $copy;
752 }
753
754 /**
755 * Get info for all sections enable/disable.
756 *
757 * @param array $contribPageIds
758 * @return array
759 * info regarding all sections.
760 */
761 public static function getSectionInfo($contribPageIds = []) {
762 $info = [];
763 $whereClause = NULL;
764 if (is_array($contribPageIds) && !empty($contribPageIds)) {
765 $whereClause = 'WHERE civicrm_contribution_page.id IN ( ' . implode(', ', $contribPageIds) . ' )';
766 }
767
768 $sections = [
769 'settings',
770 'amount',
771 'membership',
772 'custom',
773 'thankyou',
774 'friend',
775 'pcp',
776 'widget',
777 'premium',
778 ];
779 $query = "
780 SELECT civicrm_contribution_page.id as id,
781 civicrm_contribution_page.financial_type_id as settings,
782 amount_block_is_active as amount,
783 civicrm_membership_block.id as membership,
784 civicrm_uf_join.id as custom,
785 civicrm_contribution_page.thankyou_title as thankyou,
786 civicrm_tell_friend.id as friend,
787 civicrm_pcp_block.id as pcp,
788 civicrm_contribution_widget.id as widget,
789 civicrm_premiums.id as premium
790 FROM civicrm_contribution_page
791 LEFT JOIN civicrm_membership_block ON ( civicrm_membership_block.entity_id = civicrm_contribution_page.id
792 AND civicrm_membership_block.entity_table = 'civicrm_contribution_page'
793 AND civicrm_membership_block.is_active = 1 )
794 LEFT JOIN civicrm_uf_join ON ( civicrm_uf_join.entity_id = civicrm_contribution_page.id
795 AND civicrm_uf_join.entity_table = 'civicrm_contribution_page'
796 AND module = 'CiviContribute'
797 AND civicrm_uf_join.is_active = 1 )
798 LEFT JOIN civicrm_tell_friend ON ( civicrm_tell_friend.entity_id = civicrm_contribution_page.id
799 AND civicrm_tell_friend.entity_table = 'civicrm_contribution_page'
800 AND civicrm_tell_friend.is_active = 1)
801 LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_contribution_page.id
802 AND civicrm_pcp_block.entity_table = 'civicrm_contribution_page'
803 AND civicrm_pcp_block.is_active = 1 )
804 LEFT JOIN civicrm_contribution_widget ON ( civicrm_contribution_widget.contribution_page_id = civicrm_contribution_page.id
805 AND civicrm_contribution_widget.is_active = 1 )
806 LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm_contribution_page.id
807 AND civicrm_premiums.entity_table = 'civicrm_contribution_page'
808 AND civicrm_premiums.premiums_active = 1 )
809 $whereClause";
810
811 $contributionPage = CRM_Core_DAO::executeQuery($query);
812 while ($contributionPage->fetch()) {
813 if (!isset($info[$contributionPage->id]) || !is_array($info[$contributionPage->id])) {
814 $info[$contributionPage->id] = array_fill_keys(array_values($sections), FALSE);
815 }
816 foreach ($sections as $section) {
817 if ($contributionPage->$section) {
818 $info[$contributionPage->id][$section] = TRUE;
819 }
820 }
821 }
822
823 return $info;
824 }
825
826 /**
827 * Get options for a given field.
828 * @see CRM_Core_DAO::buildOptions
829 *
830 * @param string $fieldName
831 * @param string $context : @see CRM_Core_DAO::buildOptionsContext
832 * @param array $props : whatever is known about this dao object
833 *
834 * @return array|bool
835 */
836 public static function buildOptions($fieldName, $context = NULL, $props = []) {
837 $params = [];
838 // Special logic for fields whose options depend on context or properties
839 switch ($fieldName) {
840 case 'financial_type_id':
841 // Fixme - this is going to ignore context, better to get conditions, add params, and call PseudoConstant::get
842 return CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
843
844 break;
845 }
846 return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
847 }
848
849 /**
850 * Get or Set honor/on_behalf params for processing module_data or setting default values.
851 *
852 * @param array $params :
853 * @param bool $setDefault : If yes then returns array to used for setting default value afterward
854 * @param string $module : processing module_data for which module? e.g. soft_credit, on_behalf
855 *
856 * @return array|string
857 */
858 public static function formatModuleData($params, $setDefault = FALSE, $module) {
859 $tsLocale = CRM_Core_I18n::getLocale();
860 $config = CRM_Core_Config::singleton();
861 $json = $jsonDecode = NULL;
862 $domain = new CRM_Core_DAO_Domain();
863 $domain->find(TRUE);
864
865 $moduleDataFormat = [
866 'soft_credit' => [
867 1 => 'soft_credit_types',
868 'multilingual' => [
869 'honor_block_title',
870 'honor_block_text',
871 ],
872 ],
873 'on_behalf' => [
874 1 => 'is_for_organization',
875 'multilingual' => [
876 'for_organization',
877 ],
878 ],
879 ];
880
881 //When we are fetching the honor params respecting both multi and mono lingual state
882 //and setting it to default param of Contribution Page's Main and Setting form
883 if ($setDefault) {
884 $jsonDecode = json_decode($params);
885 $jsonDecode = (array) $jsonDecode->$module;
886 if (!$domain->locales && !empty($jsonDecode['default'])) {
887 //monolingual state
888 $jsonDecode += (array) $jsonDecode['default'];
889 unset($jsonDecode['default']);
890 }
891 elseif (!empty($jsonDecode[$tsLocale])) {
892 //multilingual state
893 foreach ($jsonDecode[$tsLocale] as $column => $value) {
894 $jsonDecode[$column] = $value;
895 }
896 unset($jsonDecode[$tsLocale]);
897 }
898 return $jsonDecode;
899 }
900
901 //check and handle multilingual honoree params
902 if (!$domain->locales) {
903 //if in singlelingual state simply return the array format
904 $json = [$module => NULL];
905 foreach ($moduleDataFormat[$module] as $key => $attribute) {
906 if ($key === 'multilingual') {
907 $json[$module]['default'] = [];
908 foreach ($attribute as $attr) {
909 $json[$module]['default'][$attr] = $params[$attr];
910 }
911 }
912 else {
913 $json[$module][$attribute] = $params[$attribute];
914 }
915 }
916 $json = json_encode($json);
917 }
918 else {
919 //if in multilingual state then retrieve the module_data against this contribution and
920 //merge with earlier module_data json data to current so not to lose earlier multilingual module_data information
921 $json = [$module => NULL];
922 foreach ($moduleDataFormat[$module] as $key => $attribute) {
923 if ($key === 'multilingual') {
924 $json[$module][$config->lcMessages] = [];
925 foreach ($attribute as $attr) {
926 $json[$module][$config->lcMessages][$attr] = $params[$attr];
927 }
928 }
929 else {
930 $json[$module][$attribute] = $params[$attribute];
931 }
932 }
933
934 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
935 $ufJoinDAO->module = $module;
936 $ufJoinDAO->entity_id = $params['id'];
937 $ufJoinDAO->find(TRUE);
938 $jsonData = json_decode($ufJoinDAO->module_data);
939 if ($jsonData) {
940 $json[$module] = array_merge((array) $jsonData->$module, $json[$module]);
941 }
942 $json = json_encode($json);
943 }
944 return $json;
945 }
946
947 /**
948 * Generate html for pdf in confirmation receipt email attachment.
949 * @param int $contributionId
950 * Contribution Page Id.
951 * @param int $userID
952 * Contact id for contributor.
953 * @return array
954 */
955 public static function addInvoicePdfToEmail($contributionId, $userID) {
956 $contributionID = [$contributionId];
957 $contactId = [$userID];
958 $pdfParams = [
959 'output' => 'pdf_invoice',
960 'forPage' => 'confirmpage',
961 ];
962 $pdfHtml = CRM_Contribute_Form_Task_Invoice::printPDF($contributionID, $pdfParams, $contactId);
963 return $pdfHtml;
964 }
965
966 /**
967 * Helper to determine if the page supports separate membership payments.
968 *
969 * @param int $id form id
970 *
971 * @return bool
972 * isSeparateMembershipPayment
973 */
974 public static function getIsMembershipPayment($id) {
975 $membershipBlocks = civicrm_api3('membership_block', 'get', [
976 'entity_table' => 'civicrm_contribution_page',
977 'entity_id' => $id,
978 'sequential' => TRUE,
979 ]);
980 if (!$membershipBlocks['count']) {
981 return FALSE;
982 }
983 return $membershipBlocks['values'][0]['is_separate_payment'];
984 }
985
986 }