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