Fix wrong variable in check for profiles
[civicrm-core.git] / CRM / Contribute / BAO / ContributionPage.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
35 * This class contains Contribution Page related functions.
36 */
37class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_ContributionPage {
38
39 /**
7bc789a5 40 * Creates a contribution page.
6a488035 41 *
014c4014 42 * @param array $params
6a488035 43 *
16b10e64 44 * @return CRM_Contribute_DAO_ContributionPage
6a488035 45 */
7bc789a5 46 public static function create($params) {
157b21d8 47 $financialTypeId = NULL;
a7488080 48 if (!empty($params['id']) && !CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $params['id'], NULL, 1)) {
157b21d8
PN
49 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $params['id'], 'financial_type_id');
50 }
7bc789a5 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 }
d0a0de8d
EM
55 $hook = empty($params['id']) ? 'create' : 'edit';
56 CRM_Utils_Hook::pre($hook, 'ContributionPage', CRM_Utils_Array::value('id', $params), $params);
6a488035
TO
57 $dao = new CRM_Contribute_DAO_ContributionPage();
58 $dao->copyValues($params);
59 $dao->save();
8cc574cf 60 if ($financialTypeId && !empty($params['financial_type_id']) && $financialTypeId != $params['financial_type_id']) {
e1c77163 61 CRM_Price_BAO_PriceFieldValue::updateFinancialType($params['id'], 'civicrm_contribution_page', $params['financial_type_id']);
157b21d8 62 }
d0a0de8d 63 CRM_Utils_Hook::post($hook, 'ContributionPage', $dao->id, $dao);
6a488035
TO
64 return $dao;
65 }
66
67 /**
fe482240 68 * Update the is_active flag in the db.
6a488035 69 *
2c4442ca 70 * @deprecated - this bypasses hooks.
71 *
014c4014
TO
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.
6a488035 76 *
8a4fede3 77 * @return bool
78 * true if we found and updated the object, else false
6a488035 79 */
00be9182 80 public static function setIsActive($id, $is_active) {
6a488035
TO
81 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'is_active', $is_active);
82 }
83
186c9c17 84 /**
2c4442ca 85 * Load values for a contribution page.
86 *
100fef9d 87 * @param int $id
2c4442ca 88 * @param array $values
186c9c17 89 */
00be9182 90 public static function setValues($id, &$values) {
be2fb01f 91 $modules = ['CiviContribute', 'soft_credit', 'on_behalf'];
4779abb3 92 $values['custom_pre_id'] = $values['custom_post_id'] = NULL;
6a488035 93
be2fb01f 94 $params = ['id' => $id];
6a488035 95 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $values);
8ef12e64 96
6a488035 97 // get the profile ids
be2fb01f 98 $ufJoinParams = [
6a488035
TO
99 'entity_table' => 'civicrm_contribution_page',
100 'entity_id' => $id,
be2fb01f 101 ];
6a488035 102
4779abb3 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 }
6a488035 135 }
874c9be7 136 }
6a488035
TO
137
138 /**
fe482240 139 * Send the emails.
6a488035 140 *
014c4014
TO
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.
6a488035 149 *
2c4442ca 150 * @param array $fieldTypes
6a488035 151 */
00be9182 152 public static function sendMail($contactID, $values, $isTest = FALSE, $returnMessageText = FALSE, $fieldTypes = NULL) {
be2fb01f
CW
153 $gIds = [];
154 $params = ['custom_pre_id' => [], 'custom_post_id' => []];
6a488035 155 $email = NULL;
6e10d414 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.
858f7096 160 // Next stage is to remove this & ensure there are no e-notices - ie. all are set before they hit this fn.
be2fb01f 161 $valuesRequiredForTemplate = [
858f7096 162 'customPre',
163 'customPost',
164 'customPre_grouptitle',
165 'customPost_grouptitle',
166 'useForMember',
167 'membership_assign',
168 'amount',
d9924163 169 'receipt_date',
c2358f41 170 'is_pay_later',
be2fb01f 171 ];
858f7096 172
6e10d414 173 foreach ($valuesRequiredForTemplate as $valueRequiredForTemplate) {
174 if (!isset($values[$valueRequiredForTemplate])) {
175 $values[$valueRequiredForTemplate] = NULL;
176 }
177 }
178
6a488035
TO
179 if (isset($values['custom_pre_id'])) {
180 $preProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_pre_id']);
8cc574cf 181 if ($preProfileType == 'Membership' && !empty($values['membership_id'])) {
be2fb01f
CW
182 $params['custom_pre_id'] = [
183 [
6a488035
TO
184 'member_id',
185 '=',
186 $values['membership_id'],
187 0,
188 0,
be2fb01f
CW
189 ],
190 ];
6a488035 191 }
8cc574cf 192 elseif ($preProfileType == 'Contribution' && !empty($values['contribution_id'])) {
be2fb01f
CW
193 $params['custom_pre_id'] = [
194 [
6a488035
TO
195 'contribution_id',
196 '=',
197 $values['contribution_id'],
198 0,
199 0,
be2fb01f
CW
200 ],
201 ];
6a488035
TO
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']);
8cc574cf 209 if ($postProfileType == 'Membership' && !empty($values['membership_id'])) {
be2fb01f
CW
210 $params['custom_post_id'] = [
211 [
6a488035
TO
212 'member_id',
213 '=',
214 $values['membership_id'],
215 0,
216 0,
be2fb01f
CW
217 ],
218 ];
6a488035 219 }
8cc574cf 220 elseif ($postProfileType == 'Contribution' && !empty($values['contribution_id'])) {
be2fb01f
CW
221 $params['custom_post_id'] = [
222 [
6a488035
TO
223 'contribution_id',
224 '=',
225 $values['contribution_id'],
226 0,
227 0,
be2fb01f
CW
228 ],
229 ];
6a488035
TO
230 }
231
232 $gIds['custom_post_id'] = $values['custom_post_id'];
233 }
234
a7488080
CW
235 if (!empty($values['is_for_organization'])) {
236 if (!empty($values['membership_id'])) {
be2fb01f
CW
237 $params['onbehalf_profile'] = [
238 [
6a488035
TO
239 'member_id',
240 '=',
241 $values['membership_id'],
242 0,
243 0,
be2fb01f
CW
244 ],
245 ];
6a488035 246 }
a7488080 247 elseif (!empty($values['contribution_id'])) {
be2fb01f
CW
248 $params['onbehalf_profile'] = [
249 [
6a488035
TO
250 'contribution_id',
251 '=',
252 $values['contribution_id'],
253 0,
254 0,
be2fb01f
CW
255 ],
256 ];
6a488035
TO
257 }
258 }
259
260 //check whether it is a test drive
261 if ($isTest && !empty($params['custom_pre_id'])) {
be2fb01f 262 $params['custom_pre_id'][] = [
6a488035
TO
263 'contribution_test',
264 '=',
265 1,
266 0,
267 0,
be2fb01f 268 ];
6a488035
TO
269 }
270
271 if ($isTest && !empty($params['custom_post_id'])) {
be2fb01f 272 $params['custom_post_id'][] = [
6a488035
TO
273 'contribution_test',
274 '=',
275 1,
276 0,
277 0,
be2fb01f 278 ];
6a488035 279 }
8ef12e64 280
6a488035
TO
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) {
481a74f4 287 $val = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $contactID, CRM_Utils_Array::value($key, $params), TRUE);
6a488035
TO
288 CRM_Core_BAO_UFGroup::commonSendMail($contactID, $val);
289 }
290 }
291 }
292 }
293
8cc574cf 294 if (!empty($values['is_email_receipt']) || !empty($values['onbehalf_dupe_alert']) ||
6a488035
TO
295 $returnMessageText
296 ) {
297 $template = CRM_Core_Smarty::singleton();
298
299 // get the billing location type
300 if (!array_key_exists('related_contact', $values)) {
b576d770 301 $billingLocationTypeId = CRM_Core_BAO_LocationType::getBilling();
6a488035
TO
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)) {
a7488080 329 if (!empty($values['related_contact'])) {
6a488035 330 $preProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($preID);
da5d91d5 331 if (in_array('Individual', $preProfileTypes) || in_array('Contact', $preProfileTypes)) {
6a488035
TO
332 //Take Individual contact ID
333 $userID = CRM_Utils_Array::value('related_contact', $values);
334 }
335 }
6e10d414 336 list($values['customPre_grouptitle'], $values['customPre']) = self::getProfileNameAndFields($preID, $userID, $params['custom_pre_id']);
6a488035
TO
337 }
338 $userID = $contactID;
339 if ($postID = CRM_Utils_Array::value('custom_post_id', $values)) {
a7488080 340 if (!empty($values['related_contact'])) {
6a488035
TO
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 }
6e10d414 347 list($values['customPost_grouptitle'], $values['customPost']) = self::getProfileNameAndFields($postID, $userID, $params['custom_post_id']);
6a488035 348 }
1421174e 349 if (isset($values['honor'])) {
350 $honorValues = $values['honor'];
be2fb01f 351 $template->_values = ['honoree_profile_id' => $values['honoree_profile_id']];
1421174e 352 CRM_Contribute_BAO_ContributionSoft::formatHonoreeProfileFields(
353 $template,
354 $honorValues['honor_profile_values'],
1421174e 355 $honorValues['honor_id']
356 );
357 }
6a488035
TO
358
359 $title = isset($values['title']) ? $values['title'] : CRM_Contribute_PseudoConstant::contributionPage($values['contribution_page_id']);
360
557e8899 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.
be2fb01f 363 $tplParams = [
6a488035
TO
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),
6a488035
TO
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),
8b067691 376 'thankyou_title' => CRM_Utils_Array::value('thankyou_title', $values),
6e10d414 377 'customPre' => $values['customPre'],
378 'customPre_grouptitle' => $values['customPre_grouptitle'],
379 'customPost' => $values['customPost'],
380 'customPost_grouptitle' => $values['customPost_grouptitle'],
557e8899 381 'useForMember' => $values['useForMember'],
858f7096 382 'membership_assign' => $values['membership_assign'],
383 'amount' => $values['amount'],
c2358f41 384 'is_pay_later' => $values['is_pay_later'],
76e8d9c4 385 'receipt_date' => !$values['receipt_date'] ? NULL : date('YmdHis', strtotime($values['receipt_date'])),
e05d2e11 386 'pay_later_receipt' => CRM_Utils_Array::value('pay_later_receipt', $values),
399cd461 387 'honor_block_is_active' => CRM_Utils_Array::value('honor_block_is_active', $values),
3b28799d 388 'contributionStatus' => CRM_Utils_Array::value('contribution_status', $values),
be2fb01f 389 ];
6a488035 390
91bb24a7 391 if ($contributionTypeId = CRM_Utils_Array::value('financial_type_id', $values)) {
b956b310 392 $tplParams['financialTypeId'] = $contributionTypeId;
393 $tplParams['financialTypeName'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
91bb24a7 394 $contributionTypeId);
b956b310 395 // Legacy support
396 $tplParams['contributionTypeName'] = $tplParams['financialTypeName'];
397 $tplParams['contributionTypeId'] = $contributionTypeId;
6a488035
TO
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
fdd2a6db 419 //@todo - this is the only place in this function where $values is altered - but I can't find any evidence it is used
0d8afee2 420 $values['cc_receipt'] = !empty($values['cc_receipt']) ? ($values['cc_receipt'] . ',' . $ccMailId) : $ccMailId;
6a488035
TO
421
422 // reset primary-email in the template
423 $tplParams['email'] = $ccEmail;
424
425 $tplParams['onBehalfName'] = $displayName;
426 $tplParams['onBehalfEmail'] = $email;
427
4779abb3 428 if (!empty($values['onbehalf_profile_id'])) {
429 self::buildCustomDisplay($values['onbehalf_profile_id'], 'onBehalfProfile', $contactID, $template, $params['onbehalf_profile'], $fieldTypes);
430 }
6a488035
TO
431 }
432
433 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
be2fb01f 434 $sendTemplateParams = [
12dac866 435 'groupName' => !empty($values['isMembership']) ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
436 'valueName' => !empty($values['isMembership']) ? 'membership_online_receipt' : 'contribution_online_receipt',
6a488035
TO
437 'contactId' => $contactID,
438 'tplParams' => $tplParams,
439 'isTest' => $isTest,
440 'PDFFilename' => 'receipt.pdf',
be2fb01f 441 ];
6a488035
TO
442
443 if ($returnMessageText) {
c6327d7d 444 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
be2fb01f 445 return [
6a488035
TO
446 'subject' => $subject,
447 'body' => $message,
448 'to' => $displayName,
449 'html' => $html,
be2fb01f 450 ];
6a488035
TO
451 }
452
ec7e3954
E
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
6a488035
TO
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);
9161952c 463 //send email with pdf invoice
481a74f4 464 $template = CRM_Core_Smarty::singleton();
9161952c 465 $taxAmt = $template->get_template_vars('dataArray');
aaffa79f 466 $prefixValue = Civi::settings()->get('contribution_invoice_settings');
9161952c 467 $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue);
121bf42c 468 if (isset($invoicing) && isset($prefixValue['is_email_pdf'])) {
874c9be7 469 $sendTemplateParams['isEmailPdf'] = TRUE;
9161952c
PD
470 $sendTemplateParams['contributionId'] = $values['contribution_id'];
471 }
c6327d7d 472 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
473 }
474
475 // send duplicate alert, if dupe match found during on-behalf-of processing.
a7488080 476 if (!empty($values['onbehalf_dupe_alert'])) {
6a488035
TO
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
c6327d7d 488 CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
489 }
490 }
491 }
8ef12e64 492
6e10d414 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 */
be2fb01f 503 protected static function getProfileNameAndFields($gid, $cid, &$params, $fieldTypes = []) {
6e10d414 504 $groupTitle = NULL;
be2fb01f 505 $values = [];
6e10d414 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 }
be2fb01f 530 return [$groupTitle, $values];
6e10d414 531 }
532
186c9c17 533 /**
84f0ed41 534 * Send the emails for Recurring Contribution Notification.
6a488035 535 *
014c4014
TO
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.
2a6da8d7 544 * @param bool|object $autoRenewMembership is it a auto renew membership.
6a488035 545 */
00be9182 546 public static function recurringNotify($type, $contactID, $pageID, $recur, $autoRenewMembership = FALSE) {
be2fb01f 547 $value = [];
ddf80fb9 548 $isEmailReceipt = FALSE;
6a488035 549 if ($pageID) {
be2fb01f 550 CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', $pageID, $value, [
91bb24a7 551 'title',
552 'is_email_receipt',
553 'receipt_from_name',
554 'receipt_from_email',
555 'cc_receipt',
556 'bcc_receipt',
be2fb01f 557 ]);
ab30e033 558 $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]);
6a488035 559 }
ab30e033
EM
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;
6a488035 564 }
ab30e033
EM
565
566 if ($isEmailReceipt) {
6a488035
TO
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 {
91bb24a7 574 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
575 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
576 $receiptFromName = $domainValues[0];
6a488035
TO
577 $receiptFromEmail = $domainValues[1];
578 }
579
580 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE);
be2fb01f 581 $templatesParams = [
6a488035
TO
582 'groupName' => 'msg_tpl_workflow_contribution',
583 'valueName' => 'contribution_recurring_notify',
584 'contactId' => $contactID,
be2fb01f 585 'tplParams' => [
6a488035
TO
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,
be2fb01f 597 ],
6a488035
TO
598 'from' => $receiptFrom,
599 'toName' => $displayName,
600 'toEmail' => $email,
be2fb01f 601 ];
1421174e 602 //CRM-13811
039134ba 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 }
6a488035
TO
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.
91bb24a7 610 $template = CRM_Core_Smarty::singleton();
6a488035 611 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recur->id, 'recur', 'obj');
3e473c0b 612 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'cancel');
6a488035
TO
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
84f0ed41 622 list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($templatesParams);
6a488035
TO
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 /**
c7e7164f 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.
6a488035 640 *
014c4014
TO
641 * @param int $gid
642 * Uf group id.
6a488035 643 * @param string $name
014c4014
TO
644 * @param int $cid
645 * Contact id.
2a6da8d7 646 * @param $template
014c4014
TO
647 * @param array $params
648 * Params to build component whereclause.
2a6da8d7 649 *
6e10d414 650 * @param array|null $fieldTypes
6a488035 651 */
6e10d414 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);
6a488035 656 }
6e10d414 657 $template->assign($name . "_grouptitle", $groupTitle);
6a488035
TO
658 }
659
660 /**
2c4442ca 661 * Make a copy of a contribution page, including all the blocks in the page.
6a488035 662 *
014c4014
TO
663 * @param int $id
664 * The contribution page id to copy.
6a488035 665 *
72b3a70c 666 * @return CRM_Contribute_DAO_ContributionPage
6a488035 667 */
00be9182 668 public static function copy($id) {
be2fb01f
CW
669 $fieldsFix = [
670 'prefix' => [
6a488035 671 'title' => ts('Copy of') . ' ',
be2fb01f
CW
672 ],
673 ];
3fec1adc 674 $copy = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', [
353ffa53 675 'id' => $id,
be2fb01f 676 ], NULL, $fieldsFix);
6a488035
TO
677
678 //copying all the blocks pertaining to the contribution page
3fec1adc 679 $copyPledgeBlock = CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', [
353ffa53
TO
680 'entity_id' => $id,
681 'entity_table' => 'civicrm_contribution_page',
be2fb01f 682 ], [
353ffa53 683 'entity_id' => $copy->id,
be2fb01f 684 ]);
6a488035 685
3fec1adc 686 $copyMembershipBlock = CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', [
353ffa53
TO
687 'entity_id' => $id,
688 'entity_table' => 'civicrm_contribution_page',
be2fb01f 689 ], [
353ffa53 690 'entity_id' => $copy->id,
be2fb01f 691 ]);
6a488035 692
3fec1adc 693 $copyUFJoin = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', [
353ffa53
TO
694 'entity_id' => $id,
695 'entity_table' => 'civicrm_contribution_page',
be2fb01f 696 ], [
353ffa53 697 'entity_id' => $copy->id,
be2fb01f 698 ]);
6a488035 699
3fec1adc 700 $copyWidget = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', [
353ffa53 701 'contribution_page_id' => $id,
be2fb01f 702 ], [
353ffa53 703 'contribution_page_id' => $copy->id,
be2fb01f 704 ]);
6a488035 705
c6914066 706 //copy price sets
9da8dc8c 707 CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_contribution_page', $id, $copy->id);
c6914066 708
3fec1adc 709 $copyTellFriend = CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', [
353ffa53
TO
710 'entity_id' => $id,
711 'entity_table' => 'civicrm_contribution_page',
be2fb01f 712 ], [
353ffa53 713 'entity_id' => $copy->id,
be2fb01f 714 ]);
6a488035 715
3fec1adc 716 $copyPersonalCampaignPages = CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', [
353ffa53
TO
717 'entity_id' => $id,
718 'entity_table' => 'civicrm_contribution_page',
be2fb01f 719 ], [
353ffa53 720 'entity_id' => $copy->id,
b7ff0a0a 721 'target_entity_id' => $copy->id,
be2fb01f 722 ]);
6a488035 723
3fec1adc 724 $copyPremium = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', [
353ffa53
TO
725 'entity_id' => $id,
726 'entity_table' => 'civicrm_contribution_page',
be2fb01f 727 ], [
353ffa53 728 'entity_id' => $copy->id,
be2fb01f 729 ]);
6a488035
TO
730 $premiumQuery = "
731SELECT id
732FROM civicrm_premiums
733WHERE entity_table = 'civicrm_contribution_page'
734 AND entity_id ={$id}";
735
e03e1641 736 $premiumDao = CRM_Core_DAO::executeQuery($premiumQuery);
6a488035
TO
737 while ($premiumDao->fetch()) {
738 if ($premiumDao->id) {
be2fb01f 739 CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_PremiumsProduct', [
91bb24a7 740 'premiums_id' => $premiumDao->id,
be2fb01f 741 ], [
91bb24a7 742 'premiums_id' => $copyPremium->id,
be2fb01f 743 ]);
6a488035
TO
744 }
745 }
746
747 $copy->save();
748
749 CRM_Utils_Hook::copy('ContributionPage', $copy);
750
751 return $copy;
752 }
753
6a488035 754 /**
100fef9d 755 * Get info for all sections enable/disable.
6a488035 756 *
2a6da8d7 757 * @param array $contribPageIds
a6c01b45
CW
758 * @return array
759 * info regarding all sections.
6a488035 760 */
be2fb01f
CW
761 public static function getSectionInfo($contribPageIds = []) {
762 $info = [];
6a488035
TO
763 $whereClause = NULL;
764 if (is_array($contribPageIds) && !empty($contribPageIds)) {
765 $whereClause = 'WHERE civicrm_contribution_page.id IN ( ' . implode(', ', $contribPageIds) . ' )';
766 }
767
be2fb01f 768 $sections = [
6a488035
TO
769 'settings',
770 'amount',
771 'membership',
772 'custom',
773 'thankyou',
774 'friend',
775 'pcp',
776 'widget',
777 'premium',
be2fb01f 778 ];
6a488035
TO
779 $query = "
780 SELECT civicrm_contribution_page.id as id,
8ef12e64 781 civicrm_contribution_page.financial_type_id as settings,
6a488035
TO
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
791LEFT 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 )
794LEFT 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 )
798LEFT 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)
801LEFT 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 )
804LEFT JOIN civicrm_contribution_widget ON ( civicrm_contribution_widget.contribution_page_id = civicrm_contribution_page.id
805 AND civicrm_contribution_widget.is_active = 1 )
806LEFT 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 }
e1462487
CW
825
826 /**
827 * Get options for a given field.
828 * @see CRM_Core_DAO::buildOptions
829 *
014c4014 830 * @param string $fieldName
353ffa53
TO
831 * @param string $context : @see CRM_Core_DAO::buildOptionsContext
832 * @param array $props : whatever is known about this dao object
e1462487
CW
833 *
834 * @return array|bool
835 */
be2fb01f
CW
836 public static function buildOptions($fieldName, $context = NULL, $props = []) {
837 $params = [];
e1462487
CW
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();
874c9be7 843
79d7553f 844 break;
e1462487
CW
845 }
846 return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
847 }
ba60f73e 848
849 /**
e63910c5 850 * Get or Set honor/on_behalf params for processing module_data or setting default values.
ba60f73e 851 *
e63910c5 852 * @param array $params :
353ffa53 853 * @param bool $setDefault : If yes then returns array to used for setting default value afterward
e63910c5 854 * @param string $module : processing module_data for which module? e.g. soft_credit, on_behalf
ba60f73e 855 *
856 * @return array|string
857 */
e63910c5 858 public static function formatModuleData($params, $setDefault = FALSE, $module) {
98466ff9 859 $tsLocale = CRM_Core_I18n::getLocale();
ba60f73e 860 $config = CRM_Core_Config::singleton();
e63910c5 861 $json = $jsonDecode = NULL;
ba60f73e 862 $domain = new CRM_Core_DAO_Domain();
863 $domain->find(TRUE);
864
be2fb01f
CW
865 $moduleDataFormat = [
866 'soft_credit' => [
f23093b2 867 1 => 'soft_credit_types',
be2fb01f 868 'multilingual' => [
e63910c5 869 'honor_block_title',
cb804cd9 870 'honor_block_text',
be2fb01f
CW
871 ],
872 ],
873 'on_behalf' => [
f23093b2 874 1 => 'is_for_organization',
be2fb01f 875 'multilingual' => [
f23093b2 876 'for_organization',
be2fb01f
CW
877 ],
878 ],
879 ];
e63910c5 880
12792976 881 //When we are fetching the honor params respecting both multi and mono lingual state
ba60f73e 882 //and setting it to default param of Contribution Page's Main and Setting form
883 if ($setDefault) {
e63910c5 884 $jsonDecode = json_decode($params);
885 $jsonDecode = (array) $jsonDecode->$module;
886 if (!$domain->locales && !empty($jsonDecode['default'])) {
12792976 887 //monolingual state
e63910c5 888 $jsonDecode += (array) $jsonDecode['default'];
889 unset($jsonDecode['default']);
12792976 890 }
d50cdfb5 891 elseif (!empty($jsonDecode[$tsLocale])) {
12792976 892 //multilingual state
d50cdfb5 893 foreach ($jsonDecode[$tsLocale] as $column => $value) {
e63910c5 894 $jsonDecode[$column] = $value;
ba60f73e 895 }
d50cdfb5 896 unset($jsonDecode[$tsLocale]);
ba60f73e 897 }
e63910c5 898 return $jsonDecode;
ba60f73e 899 }
900
901 //check and handle multilingual honoree params
902 if (!$domain->locales) {
903 //if in singlelingual state simply return the array format
be2fb01f 904 $json = [$module => NULL];
e63910c5 905 foreach ($moduleDataFormat[$module] as $key => $attribute) {
906 if ($key === 'multilingual') {
be2fb01f 907 $json[$module]['default'] = [];
e63910c5 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);
ba60f73e 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
be2fb01f 921 $json = [$module => NULL];
e63910c5 922 foreach ($moduleDataFormat[$module] as $key => $attribute) {
923 if ($key === 'multilingual') {
be2fb01f 924 $json[$module][$config->lcMessages] = [];
e63910c5 925 foreach ($attribute as $attr) {
926 $json[$module][$config->lcMessages][$attr] = $params[$attr];
927 }
928 }
929 else {
930 $json[$module][$attribute] = $params[$attribute];
d1f8e278 931 }
d1f8e278 932 }
d1f8e278 933
934 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
e63910c5 935 $ufJoinDAO->module = $module;
d1f8e278 936 $ufJoinDAO->entity_id = $params['id'];
937 $ufJoinDAO->find(TRUE);
938 $jsonData = json_decode($ufJoinDAO->module_data);
939 if ($jsonData) {
e63910c5 940 $json[$module] = array_merge((array) $jsonData->$module, $json[$module]);
d1f8e278 941 }
e63910c5 942 $json = json_encode($json);
d1f8e278 943 }
e63910c5 944 return $json;
d1f8e278 945 }
946
947 /**
948 * Generate html for pdf in confirmation receipt email attachment.
014c4014
TO
949 * @param int $contributionId
950 * Contribution Page Id.
951 * @param int $userID
952 * Contact id for contributor.
a6c01b45 953 * @return array
9161952c 954 */
00be9182 955 public static function addInvoicePdfToEmail($contributionId, $userID) {
be2fb01f
CW
956 $contributionID = [$contributionId];
957 $contactId = [$userID];
958 $pdfParams = [
9161952c 959 'output' => 'pdf_invoice',
21dfd5f5 960 'forPage' => 'confirmpage',
be2fb01f 961 ];
1273d77c 962 $pdfHtml = CRM_Contribute_Form_Task_Invoice::printPDF($contributionID, $pdfParams, $contactId);
9161952c
PD
963 return $pdfHtml;
964 }
f64a217a
EM
965
966 /**
7fe37828
EM
967 * Helper to determine if the page supports separate membership payments.
968 *
79d7553f 969 * @param int $id form id
f64a217a 970 *
a6c01b45
CW
971 * @return bool
972 * isSeparateMembershipPayment
f64a217a 973 */
00be9182 974 public static function getIsMembershipPayment($id) {
be2fb01f 975 $membershipBlocks = civicrm_api3('membership_block', 'get', [
1330f57a
SL
976 'entity_table' => 'civicrm_contribution_page',
977 'entity_id' => $id,
978 'sequential' => TRUE,
979 ]);
22e263ad 980 if (!$membershipBlocks['count']) {
f64a217a
EM
981 return FALSE;
982 }
983 return $membershipBlocks['values'][0]['is_separate_payment'];
984 }
96025800 985
6a488035 986}