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