Merge pull request #7978 from eileenmcnaughton/CRM-18240
[civicrm-core.git] / CRM / Contribute / BAO / ContributionPage.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 *
a6c01b45
CW
77 * @return Object
78 * DAO object on success, null otherwise
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) {
4779abb3 91 $modules = array('CiviContribute', 'soft_credit', 'on_behalf');
92 $values['custom_pre_id'] = $values['custom_post_id'] = NULL;
6a488035 93
4779abb3 94 $params = array('id' => $id);
6a488035 95 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $values);
8ef12e64 96
6a488035
TO
97 // get the profile ids
98 $ufJoinParams = array(
6a488035
TO
99 'entity_table' => 'civicrm_contribution_page',
100 'entity_id' => $id,
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) {
6e10d414 153 $gIds = array();
154 $params = array('custom_pre_id' => array(), 'custom_post_id' => array());
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.
161 $valuesRequiredForTemplate = array(
162 'customPre',
163 'customPost',
164 'customPre_grouptitle',
165 'customPost_grouptitle',
166 'useForMember',
167 'membership_assign',
168 'amount',
d9924163 169 'receipt_date',
858f7096 170 );
171
6e10d414 172 foreach ($valuesRequiredForTemplate as $valueRequiredForTemplate) {
173 if (!isset($values[$valueRequiredForTemplate])) {
174 $values[$valueRequiredForTemplate] = NULL;
175 }
176 }
177
6a488035
TO
178 if (isset($values['custom_pre_id'])) {
179 $preProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_pre_id']);
8cc574cf 180 if ($preProfileType == 'Membership' && !empty($values['membership_id'])) {
6a488035
TO
181 $params['custom_pre_id'] = array(
182 array(
183 'member_id',
184 '=',
185 $values['membership_id'],
186 0,
187 0,
188 ),
189 );
190 }
8cc574cf 191 elseif ($preProfileType == 'Contribution' && !empty($values['contribution_id'])) {
6a488035
TO
192 $params['custom_pre_id'] = array(
193 array(
194 'contribution_id',
195 '=',
196 $values['contribution_id'],
197 0,
198 0,
199 ),
200 );
201 }
202
203 $gIds['custom_pre_id'] = $values['custom_pre_id'];
204 }
205
206 if (isset($values['custom_post_id'])) {
207 $postProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_post_id']);
8cc574cf 208 if ($postProfileType == 'Membership' && !empty($values['membership_id'])) {
6a488035
TO
209 $params['custom_post_id'] = array(
210 array(
211 'member_id',
212 '=',
213 $values['membership_id'],
214 0,
215 0,
216 ),
217 );
218 }
8cc574cf 219 elseif ($postProfileType == 'Contribution' && !empty($values['contribution_id'])) {
6a488035
TO
220 $params['custom_post_id'] = array(
221 array(
222 'contribution_id',
223 '=',
224 $values['contribution_id'],
225 0,
226 0,
227 ),
228 );
229 }
230
231 $gIds['custom_post_id'] = $values['custom_post_id'];
232 }
233
a7488080
CW
234 if (!empty($values['is_for_organization'])) {
235 if (!empty($values['membership_id'])) {
6a488035
TO
236 $params['onbehalf_profile'] = array(
237 array(
238 'member_id',
239 '=',
240 $values['membership_id'],
241 0,
242 0,
243 ),
244 );
245 }
a7488080 246 elseif (!empty($values['contribution_id'])) {
6a488035
TO
247 $params['onbehalf_profile'] = array(
248 array(
249 'contribution_id',
250 '=',
251 $values['contribution_id'],
252 0,
253 0,
254 ),
255 );
256 }
257 }
258
259 //check whether it is a test drive
260 if ($isTest && !empty($params['custom_pre_id'])) {
261 $params['custom_pre_id'][] = array(
262 'contribution_test',
263 '=',
264 1,
265 0,
266 0,
267 );
268 }
269
270 if ($isTest && !empty($params['custom_post_id'])) {
271 $params['custom_post_id'][] = array(
272 'contribution_test',
273 '=',
274 1,
275 0,
276 0,
277 );
278 }
8ef12e64 279
6a488035
TO
280 if (!$returnMessageText && !empty($gIds)) {
281 //send notification email if field values are set (CRM-1941)
282 foreach ($gIds as $key => $gId) {
283 if ($gId) {
284 $email = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify');
285 if ($email) {
481a74f4 286 $val = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $contactID, CRM_Utils_Array::value($key, $params), TRUE);
6a488035
TO
287 CRM_Core_BAO_UFGroup::commonSendMail($contactID, $val);
288 }
289 }
290 }
291 }
292
8cc574cf 293 if (!empty($values['is_email_receipt']) || !empty($values['onbehalf_dupe_alert']) ||
6a488035
TO
294 $returnMessageText
295 ) {
296 $template = CRM_Core_Smarty::singleton();
297
298 // get the billing location type
299 if (!array_key_exists('related_contact', $values)) {
b576d770 300 $billingLocationTypeId = CRM_Core_BAO_LocationType::getBilling();
6a488035
TO
301 }
302 else {
303 // presence of related contact implies onbehalf of org case,
304 // where location type is set to default.
305 $locType = CRM_Core_BAO_LocationType::getDefault();
306 $billingLocationTypeId = $locType->id;
307 }
308
309 if (!array_key_exists('related_contact', $values)) {
310 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE, $billingLocationTypeId);
311 }
312 // get primary location email if no email exist( for billing location).
313 if (!$email) {
314 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
315 }
316 if (empty($displayName)) {
317 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
318 }
319
320 //for display profile need to get individual contact id,
321 //hence get it from related_contact if on behalf of org true CRM-3767
322 //CRM-5001 Contribution/Membership:: On Behalf of Organization,
323 //If profile GROUP contain the Individual type then consider the
324 //profile is of Individual ( including the custom data of membership/contribution )
325 //IF Individual type not present in profile then it is consider as Organization data.
326 $userID = $contactID;
327 if ($preID = CRM_Utils_Array::value('custom_pre_id', $values)) {
a7488080 328 if (!empty($values['related_contact'])) {
6a488035 329 $preProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($preID);
b8950542 330 //@todo - following line should not refer to undefined $postProfileTypes? figure out way to test
6a488035
TO
331 if (in_array('Individual', $preProfileTypes) || in_array('Contact', $postProfileTypes)) {
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'];
4779abb3 351 $template->_values = array('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.
6a488035
TO
363 $tplParams = array(
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'],
6a488035
TO
384 );
385
91bb24a7 386 if ($contributionTypeId = CRM_Utils_Array::value('financial_type_id', $values)) {
b956b310 387 $tplParams['financialTypeId'] = $contributionTypeId;
388 $tplParams['financialTypeName'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
91bb24a7 389 $contributionTypeId);
b956b310 390 // Legacy support
391 $tplParams['contributionTypeName'] = $tplParams['financialTypeName'];
392 $tplParams['contributionTypeId'] = $contributionTypeId;
6a488035
TO
393 }
394
395 if ($contributionPageId = CRM_Utils_Array::value('id', $values)) {
396 $tplParams['contributionPageId'] = $contributionPageId;
397 }
398
399 // address required during receipt processing (pdf and email receipt)
400 if ($displayAddress = CRM_Utils_Array::value('address', $values)) {
401 $tplParams['address'] = $displayAddress;
402 }
403
404 // CRM-6976
405 $originalCCReceipt = CRM_Utils_Array::value('cc_receipt', $values);
406
407 // cc to related contacts of contributor OR the one who
408 // signs up. Is used for cases like - on behalf of
409 // contribution / signup ..etc
410 if (array_key_exists('related_contact', $values)) {
411 list($ccDisplayName, $ccEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($values['related_contact']);
412 $ccMailId = "{$ccDisplayName} <{$ccEmail}>";
413
fdd2a6db 414 //@todo - this is the only place in this function where $values is altered - but I can't find any evidence it is used
0d8afee2 415 $values['cc_receipt'] = !empty($values['cc_receipt']) ? ($values['cc_receipt'] . ',' . $ccMailId) : $ccMailId;
6a488035
TO
416
417 // reset primary-email in the template
418 $tplParams['email'] = $ccEmail;
419
420 $tplParams['onBehalfName'] = $displayName;
421 $tplParams['onBehalfEmail'] = $email;
422
4779abb3 423 if (!empty($values['onbehalf_profile_id'])) {
424 self::buildCustomDisplay($values['onbehalf_profile_id'], 'onBehalfProfile', $contactID, $template, $params['onbehalf_profile'], $fieldTypes);
425 }
6a488035
TO
426 }
427
428 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
429 $sendTemplateParams = array(
12dac866 430 'groupName' => !empty($values['isMembership']) ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
431 'valueName' => !empty($values['isMembership']) ? 'membership_online_receipt' : 'contribution_online_receipt',
6a488035
TO
432 'contactId' => $contactID,
433 'tplParams' => $tplParams,
434 'isTest' => $isTest,
435 'PDFFilename' => 'receipt.pdf',
436 );
437
438 if ($returnMessageText) {
c6327d7d 439 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
440 return array(
441 'subject' => $subject,
442 'body' => $message,
443 'to' => $displayName,
444 'html' => $html,
445 );
446 }
447
448 if ($values['is_email_receipt']) {
449 $sendTemplateParams['from'] = CRM_Utils_Array::value('receipt_from_name', $values) . ' <' . $values['receipt_from_email'] . '>';
450 $sendTemplateParams['toName'] = $displayName;
451 $sendTemplateParams['toEmail'] = $email;
452 $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_receipt', $values);
453 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $values);
9161952c 454 //send email with pdf invoice
481a74f4 455 $template = CRM_Core_Smarty::singleton();
9161952c 456 $taxAmt = $template->get_template_vars('dataArray');
aaffa79f 457 $prefixValue = Civi::settings()->get('contribution_invoice_settings');
9161952c 458 $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue);
121bf42c 459 if (isset($invoicing) && isset($prefixValue['is_email_pdf'])) {
874c9be7 460 $sendTemplateParams['isEmailPdf'] = TRUE;
9161952c
PD
461 $sendTemplateParams['contributionId'] = $values['contribution_id'];
462 }
c6327d7d 463 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
464 }
465
466 // send duplicate alert, if dupe match found during on-behalf-of processing.
a7488080 467 if (!empty($values['onbehalf_dupe_alert'])) {
6a488035
TO
468 $sendTemplateParams['groupName'] = 'msg_tpl_workflow_contribution';
469 $sendTemplateParams['valueName'] = 'contribution_dupalert';
470 $sendTemplateParams['from'] = ts('Automatically Generated') . " <{$values['receipt_from_email']}>";
471 $sendTemplateParams['toName'] = CRM_Utils_Array::value('receipt_from_name', $values);
472 $sendTemplateParams['toEmail'] = CRM_Utils_Array::value('receipt_from_email', $values);
473 $sendTemplateParams['tplParams']['onBehalfID'] = $contactID;
474 $sendTemplateParams['tplParams']['receiptMessage'] = $message;
475
476 // fix cc and reset back to original, CRM-6976
477 $sendTemplateParams['cc'] = $originalCCReceipt;
478
c6327d7d 479 CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
480 }
481 }
482 }
8ef12e64 483
6e10d414 484 /**
485 * Get the profile title and fields.
486 *
487 * @param int $gid
488 * @param int $cid
489 * @param array $params
490 * @param array $fieldTypes
491 *
492 * @return array
493 */
494 protected static function getProfileNameAndFields($gid, $cid, &$params, $fieldTypes = array()) {
495 $groupTitle = NULL;
496 $values = array();
497 if ($gid) {
498 if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $cid)) {
499 $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL);
500 foreach ($fields as $k => $v) {
501 if (!$groupTitle) {
502 $groupTitle = $v["groupTitle"];
503 }
504 // suppress all file fields from display and formatting fields
505 if (
506 CRM_Utils_Array::value('data_type', $v, '') == 'File' ||
507 CRM_Utils_Array::value('name', $v, '') == 'image_URL' ||
508 CRM_Utils_Array::value('field_type', $v) == 'Formatting'
509 ) {
510 unset($fields[$k]);
511 }
512
513 if (!empty($fieldTypes) && (!in_array($v['field_type'], $fieldTypes))) {
514 unset($fields[$k]);
515 }
516 }
517
518 CRM_Core_BAO_UFGroup::getValues($cid, $fields, $values, FALSE, $params);
519 }
520 }
521 return array($groupTitle, $values);
522 }
523
186c9c17 524 /**
84f0ed41 525 * Send the emails for Recurring Contribution Notification.
6a488035 526 *
014c4014
TO
527 * @param string $type
528 * TxnType.
529 * @param int $contactID
530 * Contact id for contributor.
531 * @param int $pageID
532 * Contribution page id.
533 * @param object $recur
534 * Object of recurring contribution table.
2a6da8d7 535 * @param bool|object $autoRenewMembership is it a auto renew membership.
6a488035 536 */
00be9182 537 public static function recurringNotify($type, $contactID, $pageID, $recur, $autoRenewMembership = FALSE) {
6a488035 538 $value = array();
ddf80fb9 539 $isEmailReceipt = FALSE;
6a488035
TO
540 if ($pageID) {
541 CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', $pageID, $value, array(
91bb24a7 542 'title',
543 'is_email_receipt',
544 'receipt_from_name',
545 'receipt_from_email',
546 'cc_receipt',
547 'bcc_receipt',
548 ));
ab30e033 549 $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]);
6a488035 550 }
ab30e033
EM
551 elseif ($recur->id) {
552 // This means we are coming from back-office - ie. no page ID, but recurring.
553 // Ideally this information would be passed into the function clearly rather than guessing by convention.
554 $isEmailReceipt = TRUE;
6a488035 555 }
ab30e033
EM
556
557 if ($isEmailReceipt) {
6a488035
TO
558 if ($pageID) {
559 $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>';
560
561 $receiptFromName = $value[$pageID]['receipt_from_name'];
562 $receiptFromEmail = $value[$pageID]['receipt_from_email'];
563 }
564 else {
91bb24a7 565 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
566 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
567 $receiptFromName = $domainValues[0];
6a488035
TO
568 $receiptFromEmail = $domainValues[1];
569 }
570
571 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE);
572 $templatesParams = array(
573 'groupName' => 'msg_tpl_workflow_contribution',
574 'valueName' => 'contribution_recurring_notify',
575 'contactId' => $contactID,
576 'tplParams' => array(
577 'recur_frequency_interval' => $recur->frequency_interval,
578 'recur_frequency_unit' => $recur->frequency_unit,
579 'recur_installments' => $recur->installments,
580 'recur_start_date' => $recur->start_date,
581 'recur_end_date' => $recur->end_date,
582 'recur_amount' => $recur->amount,
583 'recur_txnType' => $type,
584 'displayName' => $displayName,
585 'receipt_from_name' => $receiptFromName,
586 'receipt_from_email' => $receiptFromEmail,
587 'auto_renew_membership' => $autoRenewMembership,
588 ),
589 'from' => $receiptFrom,
590 'toName' => $displayName,
591 'toEmail' => $email,
592 );
1421174e 593 //CRM-13811
039134ba 594 if ($pageID) {
595 $templatesParams['cc'] = CRM_Utils_Array::value('cc_receipt', $value[$pageID]);
596 $templatesParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $value[$pageID]);
597 }
6a488035
TO
598 if ($recur->id) {
599 // in some cases its just recurringNotify() thats called for the first time and these urls don't get set.
600 // like in PaypalPro, & therefore we set it here additionally.
91bb24a7 601 $template = CRM_Core_Smarty::singleton();
6a488035 602 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recur->id, 'recur', 'obj');
3e473c0b 603 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'cancel');
6a488035
TO
604 $template->assign('cancelSubscriptionUrl', $url);
605
606 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'billing');
607 $template->assign('updateSubscriptionBillingUrl', $url);
608
609 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'update');
610 $template->assign('updateSubscriptionUrl', $url);
611 }
612
84f0ed41 613 list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($templatesParams);
6a488035
TO
614
615 if ($sent) {
616 CRM_Core_Error::debug_log_message('Success: mail sent for recurring notification.');
617 }
618 else {
619 CRM_Core_Error::debug_log_message('Failure: mail not sent for recurring notification.');
620 }
621 }
622 }
623
624 /**
c7e7164f 625 * Add the custom fields for contribution page (ie profile).
626 *
627 * @deprecated assigning values to smarty like this is risky because
628 * - it is hard to debug since $name is used in the assign
629 * - it is potentially 'leaky' - it's better to do this on the form
630 * or close to where it is used / required. See CRM-17519 for leakage e.g.
6a488035 631 *
014c4014
TO
632 * @param int $gid
633 * Uf group id.
6a488035 634 * @param string $name
014c4014
TO
635 * @param int $cid
636 * Contact id.
2a6da8d7 637 * @param $template
014c4014
TO
638 * @param array $params
639 * Params to build component whereclause.
2a6da8d7 640 *
6e10d414 641 * @param array|null $fieldTypes
6a488035 642 */
6e10d414 643 public static function buildCustomDisplay($gid, $name, $cid, &$template, &$params, $fieldTypes = NULL) {
644 list($groupTitle, $values) = self::getProfileNameAndFields($gid, $cid, $params, $fieldTypes);
645 if (!empty($values)) {
646 $template->assign($name, $values);
6a488035 647 }
6e10d414 648 $template->assign($name . "_grouptitle", $groupTitle);
6a488035
TO
649 }
650
651 /**
2c4442ca 652 * Make a copy of a contribution page, including all the blocks in the page.
6a488035 653 *
014c4014
TO
654 * @param int $id
655 * The contribution page id to copy.
6a488035 656 *
72b3a70c 657 * @return CRM_Contribute_DAO_ContributionPage
6a488035 658 */
00be9182 659 public static function copy($id) {
6a488035
TO
660 $fieldsFix = array(
661 'prefix' => array(
662 'title' => ts('Copy of') . ' ',
663 ),
664 );
665 $copy = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', array(
353ffa53
TO
666 'id' => $id,
667 ), NULL, $fieldsFix);
6a488035
TO
668
669 //copying all the blocks pertaining to the contribution page
670 $copyPledgeBlock = &CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', array(
353ffa53
TO
671 'entity_id' => $id,
672 'entity_table' => 'civicrm_contribution_page',
673 ), array(
674 'entity_id' => $copy->id,
675 ));
6a488035
TO
676
677 $copyMembershipBlock = &CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', array(
353ffa53
TO
678 'entity_id' => $id,
679 'entity_table' => 'civicrm_contribution_page',
680 ), array(
681 'entity_id' => $copy->id,
682 ));
6a488035
TO
683
684 $copyUFJoin = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', array(
353ffa53
TO
685 'entity_id' => $id,
686 'entity_table' => 'civicrm_contribution_page',
687 ), array(
688 'entity_id' => $copy->id,
689 ));
6a488035
TO
690
691 $copyWidget = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', array(
353ffa53
TO
692 'contribution_page_id' => $id,
693 ), array(
694 'contribution_page_id' => $copy->id,
695 ));
6a488035 696
c6914066 697 //copy price sets
9da8dc8c 698 CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_contribution_page', $id, $copy->id);
c6914066 699
6a488035 700 $copyTellFriend = &CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', array(
353ffa53
TO
701 'entity_id' => $id,
702 'entity_table' => 'civicrm_contribution_page',
703 ), array(
704 'entity_id' => $copy->id,
705 ));
6a488035
TO
706
707 $copyPersonalCampaignPages = &CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', array(
353ffa53
TO
708 'entity_id' => $id,
709 'entity_table' => 'civicrm_contribution_page',
710 ), array(
711 'entity_id' => $copy->id,
b7ff0a0a 712 'target_entity_id' => $copy->id,
353ffa53 713 ));
6a488035
TO
714
715 $copyPremium = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', array(
353ffa53
TO
716 'entity_id' => $id,
717 'entity_table' => 'civicrm_contribution_page',
718 ), array(
719 'entity_id' => $copy->id,
720 ));
6a488035
TO
721 $premiumQuery = "
722SELECT id
723FROM civicrm_premiums
724WHERE entity_table = 'civicrm_contribution_page'
725 AND entity_id ={$id}";
726
727 $premiumDao = CRM_Core_DAO::executeQuery($premiumQuery, CRM_Core_DAO::$_nullArray);
728 while ($premiumDao->fetch()) {
729 if ($premiumDao->id) {
2c4442ca 730 CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_PremiumsProduct', array(
91bb24a7 731 'premiums_id' => $premiumDao->id,
732 ), array(
733 'premiums_id' => $copyPremium->id,
734 ));
6a488035
TO
735 }
736 }
737
738 $copy->save();
739
740 CRM_Utils_Hook::copy('ContributionPage', $copy);
741
742 return $copy;
743 }
744
6a488035 745 /**
100fef9d 746 * Get info for all sections enable/disable.
6a488035 747 *
2a6da8d7 748 * @param array $contribPageIds
a6c01b45
CW
749 * @return array
750 * info regarding all sections.
6a488035 751 */
00be9182 752 public static function getSectionInfo($contribPageIds = array()) {
6a488035
TO
753 $info = array();
754 $whereClause = NULL;
755 if (is_array($contribPageIds) && !empty($contribPageIds)) {
756 $whereClause = 'WHERE civicrm_contribution_page.id IN ( ' . implode(', ', $contribPageIds) . ' )';
757 }
758
759 $sections = array(
760 'settings',
761 'amount',
762 'membership',
763 'custom',
764 'thankyou',
765 'friend',
766 'pcp',
767 'widget',
768 'premium',
769 );
770 $query = "
771 SELECT civicrm_contribution_page.id as id,
8ef12e64 772 civicrm_contribution_page.financial_type_id as settings,
6a488035
TO
773 amount_block_is_active as amount,
774 civicrm_membership_block.id as membership,
775 civicrm_uf_join.id as custom,
776 civicrm_contribution_page.thankyou_title as thankyou,
777 civicrm_tell_friend.id as friend,
778 civicrm_pcp_block.id as pcp,
779 civicrm_contribution_widget.id as widget,
780 civicrm_premiums.id as premium
781 FROM civicrm_contribution_page
782LEFT JOIN civicrm_membership_block ON ( civicrm_membership_block.entity_id = civicrm_contribution_page.id
783 AND civicrm_membership_block.entity_table = 'civicrm_contribution_page'
784 AND civicrm_membership_block.is_active = 1 )
785LEFT JOIN civicrm_uf_join ON ( civicrm_uf_join.entity_id = civicrm_contribution_page.id
786 AND civicrm_uf_join.entity_table = 'civicrm_contribution_page'
787 AND module = 'CiviContribute'
788 AND civicrm_uf_join.is_active = 1 )
789LEFT JOIN civicrm_tell_friend ON ( civicrm_tell_friend.entity_id = civicrm_contribution_page.id
790 AND civicrm_tell_friend.entity_table = 'civicrm_contribution_page'
791 AND civicrm_tell_friend.is_active = 1)
792LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_contribution_page.id
793 AND civicrm_pcp_block.entity_table = 'civicrm_contribution_page'
794 AND civicrm_pcp_block.is_active = 1 )
795LEFT JOIN civicrm_contribution_widget ON ( civicrm_contribution_widget.contribution_page_id = civicrm_contribution_page.id
796 AND civicrm_contribution_widget.is_active = 1 )
797LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm_contribution_page.id
798 AND civicrm_premiums.entity_table = 'civicrm_contribution_page'
799 AND civicrm_premiums.premiums_active = 1 )
800 $whereClause";
801
802 $contributionPage = CRM_Core_DAO::executeQuery($query);
803 while ($contributionPage->fetch()) {
804 if (!isset($info[$contributionPage->id]) || !is_array($info[$contributionPage->id])) {
805 $info[$contributionPage->id] = array_fill_keys(array_values($sections), FALSE);
806 }
807 foreach ($sections as $section) {
808 if ($contributionPage->$section) {
809 $info[$contributionPage->id][$section] = TRUE;
810 }
811 }
812 }
813
814 return $info;
815 }
e1462487
CW
816
817 /**
818 * Get options for a given field.
819 * @see CRM_Core_DAO::buildOptions
820 *
014c4014 821 * @param string $fieldName
353ffa53
TO
822 * @param string $context : @see CRM_Core_DAO::buildOptionsContext
823 * @param array $props : whatever is known about this dao object
e1462487
CW
824 *
825 * @return array|bool
826 */
827 public static function buildOptions($fieldName, $context = NULL, $props = array()) {
828 $params = array();
829 // Special logic for fields whose options depend on context or properties
830 switch ($fieldName) {
831 case 'financial_type_id':
832 // Fixme - this is going to ignore context, better to get conditions, add params, and call PseudoConstant::get
833 return CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
874c9be7 834
79d7553f 835 break;
e1462487
CW
836 }
837 return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
838 }
ba60f73e 839
840 /**
e63910c5 841 * Get or Set honor/on_behalf params for processing module_data or setting default values.
ba60f73e 842 *
e63910c5 843 * @param array $params :
353ffa53 844 * @param bool $setDefault : If yes then returns array to used for setting default value afterward
e63910c5 845 * @param string $module : processing module_data for which module? e.g. soft_credit, on_behalf
ba60f73e 846 *
847 * @return array|string
848 */
e63910c5 849 public static function formatModuleData($params, $setDefault = FALSE, $module) {
1a6a026f 850 global $tsLocale;
ba60f73e 851 $config = CRM_Core_Config::singleton();
e63910c5 852 $json = $jsonDecode = NULL;
ba60f73e 853 $domain = new CRM_Core_DAO_Domain();
854 $domain->find(TRUE);
855
e63910c5 856 $moduleDataFormat = array(
857 'soft_credit' => array(
f23093b2 858 1 => 'soft_credit_types',
e63910c5 859 'multilingual' => array(
860 'honor_block_title',
cb804cd9 861 'honor_block_text',
e63910c5 862 ),
863 ),
864 'on_behalf' => array(
f23093b2 865 1 => 'is_for_organization',
e63910c5 866 'multilingual' => array(
f23093b2 867 'for_organization',
e63910c5 868 ),
869 ),
870 );
871
12792976 872 //When we are fetching the honor params respecting both multi and mono lingual state
ba60f73e 873 //and setting it to default param of Contribution Page's Main and Setting form
874 if ($setDefault) {
e63910c5 875 $jsonDecode = json_decode($params);
876 $jsonDecode = (array) $jsonDecode->$module;
877 if (!$domain->locales && !empty($jsonDecode['default'])) {
12792976 878 //monolingual state
e63910c5 879 $jsonDecode += (array) $jsonDecode['default'];
880 unset($jsonDecode['default']);
12792976 881 }
d50cdfb5 882 elseif (!empty($jsonDecode[$tsLocale])) {
12792976 883 //multilingual state
d50cdfb5 884 foreach ($jsonDecode[$tsLocale] as $column => $value) {
e63910c5 885 $jsonDecode[$column] = $value;
ba60f73e 886 }
d50cdfb5 887 unset($jsonDecode[$tsLocale]);
ba60f73e 888 }
e63910c5 889 return $jsonDecode;
ba60f73e 890 }
891
892 //check and handle multilingual honoree params
893 if (!$domain->locales) {
894 //if in singlelingual state simply return the array format
e63910c5 895 $json = array($module => NULL);
896 foreach ($moduleDataFormat[$module] as $key => $attribute) {
897 if ($key === 'multilingual') {
898 $json[$module]['default'] = array();
899 foreach ($attribute as $attr) {
900 $json[$module]['default'][$attr] = $params[$attr];
901 }
902 }
903 else {
904 $json[$module][$attribute] = $params[$attribute];
905 }
906 }
907 $json = json_encode($json);
ba60f73e 908 }
909 else {
910 //if in multilingual state then retrieve the module_data against this contribution and
911 //merge with earlier module_data json data to current so not to lose earlier multilingual module_data information
e63910c5 912 $json = array($module => NULL);
913 foreach ($moduleDataFormat[$module] as $key => $attribute) {
914 if ($key === 'multilingual') {
915 $json[$module][$config->lcMessages] = array();
916 foreach ($attribute as $attr) {
917 $json[$module][$config->lcMessages][$attr] = $params[$attr];
918 }
919 }
920 else {
921 $json[$module][$attribute] = $params[$attribute];
d1f8e278 922 }
d1f8e278 923 }
d1f8e278 924
925 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
e63910c5 926 $ufJoinDAO->module = $module;
d1f8e278 927 $ufJoinDAO->entity_id = $params['id'];
928 $ufJoinDAO->find(TRUE);
929 $jsonData = json_decode($ufJoinDAO->module_data);
930 if ($jsonData) {
e63910c5 931 $json[$module] = array_merge((array) $jsonData->$module, $json[$module]);
d1f8e278 932 }
e63910c5 933 $json = json_encode($json);
d1f8e278 934 }
e63910c5 935 return $json;
d1f8e278 936 }
937
938 /**
939 * Generate html for pdf in confirmation receipt email attachment.
014c4014
TO
940 * @param int $contributionId
941 * Contribution Page Id.
942 * @param int $userID
943 * Contact id for contributor.
a6c01b45 944 * @return array
9161952c 945 */
00be9182 946 public static function addInvoicePdfToEmail($contributionId, $userID) {
9161952c
PD
947 $contributionID = array($contributionId);
948 $contactId = array($userID);
949 $pdfParams = array(
950 'output' => 'pdf_invoice',
21dfd5f5 951 'forPage' => 'confirmpage',
9161952c 952 );
62179e22 953 $pdfHtml = CRM_Contribute_Form_Task_Invoice::printPDF($contributionID,
954 $pdfParams, $contactId, CRM_Core_DAO::$_nullObject);
9161952c
PD
955 return $pdfHtml;
956 }
f64a217a
EM
957
958 /**
7fe37828
EM
959 * Helper to determine if the page supports separate membership payments.
960 *
79d7553f 961 * @param int $id form id
f64a217a 962 *
a6c01b45
CW
963 * @return bool
964 * isSeparateMembershipPayment
f64a217a 965 */
00be9182 966 public static function getIsMembershipPayment($id) {
353ffa53
TO
967 $membershipBlocks = civicrm_api3('membership_block', 'get', array(
968 'entity_table' => 'civicrm_contribution_page',
969 'entity_id' => $id,
79d7553f 970 'sequential' => TRUE,
353ffa53 971 ));
22e263ad 972 if (!$membershipBlocks['count']) {
f64a217a
EM
973 return FALSE;
974 }
975 return $membershipBlocks['values'][0]['is_separate_payment'];
976 }
96025800 977
6a488035 978}