Merge pull request #5210 from eileenmcnaughton/CRM-15996
[civicrm-core.git] / CRM / Contribute / BAO / ContributionPage.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class contains Contribution Page related functions.
38 */
39class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_ContributionPage {
40
41 /**
fe482240 42 * Takes an associative array and creates a contribution page object.
6a488035 43 *
014c4014
TO
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
6a488035 46 *
16b10e64 47 * @return CRM_Contribute_DAO_ContributionPage
6a488035
TO
48 */
49 public static function &create(&$params) {
157b21d8 50 $financialTypeId = NULL;
a7488080 51 if (!empty($params['id']) && !CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $params['id'], NULL, 1)) {
157b21d8
PN
52 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $params['id'], 'financial_type_id');
53 }
d0a0de8d
EM
54 $hook = empty($params['id']) ? 'create' : 'edit';
55 CRM_Utils_Hook::pre($hook, 'ContributionPage', CRM_Utils_Array::value('id', $params), $params);
6a488035
TO
56 $dao = new CRM_Contribute_DAO_ContributionPage();
57 $dao->copyValues($params);
58 $dao->save();
8cc574cf 59 if ($financialTypeId && !empty($params['financial_type_id']) && $financialTypeId != $params['financial_type_id']) {
e1c77163 60 CRM_Price_BAO_PriceFieldValue::updateFinancialType($params['id'], 'civicrm_contribution_page', $params['financial_type_id']);
157b21d8 61 }
d0a0de8d 62 CRM_Utils_Hook::post($hook, 'ContributionPage', $dao->id, $dao);
6a488035
TO
63 return $dao;
64 }
65
66 /**
fe482240 67 * Update the is_active flag in the db.
6a488035 68 *
014c4014
TO
69 * @param int $id
70 * Id of the database record.
71 * @param bool $is_active
72 * Value we want to set the is_active field.
6a488035 73 *
a6c01b45
CW
74 * @return Object
75 * DAO object on success, null otherwise
6a488035 76 */
00be9182 77 public static function setIsActive($id, $is_active) {
6a488035
TO
78 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'is_active', $is_active);
79 }
80
186c9c17 81 /**
100fef9d 82 * @param int $id
186c9c17
EM
83 * @param $values
84 */
00be9182 85 public static function setValues($id, &$values) {
6a488035
TO
86 $params = array(
87 'id' => $id,
88 );
89
90 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $values);
8ef12e64 91
6a488035
TO
92 // get the profile ids
93 $ufJoinParams = array(
94 'module' => 'CiviContribute',
95 'entity_table' => 'civicrm_contribution_page',
96 'entity_id' => $id,
97 );
98 list($values['custom_pre_id'], $customPostIds) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
99
100 if (!empty($customPostIds)) {
101 $values['custom_post_id'] = $customPostIds[0];
102 }
103 else {
104 $values['custom_post_id'] = '';
105 }
106 // // add an accounting code also
107 // if ($values ['financial_type_id']) {
108 // $values ['accountingCode'] = CRM_Core_DAO::getFieldValue( 'CRM_Financial_DAO_FinancialType', $values ['financial_type_id'], 'accounting_code' );
109 // }
874c9be7 110 }
6a488035
TO
111
112 /**
fe482240 113 * Send the emails.
6a488035 114 *
014c4014
TO
115 * @param int $contactID
116 * Contact id.
117 * @param array $values
118 * Associated array of fields.
119 * @param bool $isTest
120 * If in test mode.
121 * @param bool $returnMessageText
122 * Return the message text instead of sending the mail.
6a488035 123 *
e2133660
EM
124 * @param null $fieldTypes
125 *
6a488035 126 * @return void
6a488035 127 */
00be9182 128 public static function sendMail($contactID, $values, $isTest = FALSE, $returnMessageText = FALSE, $fieldTypes = NULL) {
6a488035
TO
129 $gIds = $params = array();
130 $email = NULL;
131 if (isset($values['custom_pre_id'])) {
132 $preProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_pre_id']);
8cc574cf 133 if ($preProfileType == 'Membership' && !empty($values['membership_id'])) {
6a488035
TO
134 $params['custom_pre_id'] = array(
135 array(
136 'member_id',
137 '=',
138 $values['membership_id'],
139 0,
140 0,
141 ),
142 );
143 }
8cc574cf 144 elseif ($preProfileType == 'Contribution' && !empty($values['contribution_id'])) {
6a488035
TO
145 $params['custom_pre_id'] = array(
146 array(
147 'contribution_id',
148 '=',
149 $values['contribution_id'],
150 0,
151 0,
152 ),
153 );
154 }
155
156 $gIds['custom_pre_id'] = $values['custom_pre_id'];
157 }
158
159 if (isset($values['custom_post_id'])) {
160 $postProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_post_id']);
8cc574cf 161 if ($postProfileType == 'Membership' && !empty($values['membership_id'])) {
6a488035
TO
162 $params['custom_post_id'] = array(
163 array(
164 'member_id',
165 '=',
166 $values['membership_id'],
167 0,
168 0,
169 ),
170 );
171 }
8cc574cf 172 elseif ($postProfileType == 'Contribution' && !empty($values['contribution_id'])) {
6a488035
TO
173 $params['custom_post_id'] = array(
174 array(
175 'contribution_id',
176 '=',
177 $values['contribution_id'],
178 0,
179 0,
180 ),
181 );
182 }
183
184 $gIds['custom_post_id'] = $values['custom_post_id'];
185 }
186
a7488080
CW
187 if (!empty($values['is_for_organization'])) {
188 if (!empty($values['membership_id'])) {
6a488035
TO
189 $params['onbehalf_profile'] = array(
190 array(
191 'member_id',
192 '=',
193 $values['membership_id'],
194 0,
195 0,
196 ),
197 );
198 }
a7488080 199 elseif (!empty($values['contribution_id'])) {
6a488035
TO
200 $params['onbehalf_profile'] = array(
201 array(
202 'contribution_id',
203 '=',
204 $values['contribution_id'],
205 0,
206 0,
207 ),
208 );
209 }
210 }
211
212 //check whether it is a test drive
213 if ($isTest && !empty($params['custom_pre_id'])) {
214 $params['custom_pre_id'][] = array(
215 'contribution_test',
216 '=',
217 1,
218 0,
219 0,
220 );
221 }
222
223 if ($isTest && !empty($params['custom_post_id'])) {
224 $params['custom_post_id'][] = array(
225 'contribution_test',
226 '=',
227 1,
228 0,
229 0,
230 );
231 }
8ef12e64 232
6a488035
TO
233 if (!$returnMessageText && !empty($gIds)) {
234 //send notification email if field values are set (CRM-1941)
235 foreach ($gIds as $key => $gId) {
236 if ($gId) {
237 $email = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify');
238 if ($email) {
481a74f4 239 $val = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $contactID, CRM_Utils_Array::value($key, $params), TRUE);
6a488035
TO
240 CRM_Core_BAO_UFGroup::commonSendMail($contactID, $val);
241 }
242 }
243 }
244 }
245
8cc574cf 246 if (!empty($values['is_email_receipt']) || !empty($values['onbehalf_dupe_alert']) ||
6a488035
TO
247 $returnMessageText
248 ) {
249 $template = CRM_Core_Smarty::singleton();
250
251 // get the billing location type
252 if (!array_key_exists('related_contact', $values)) {
180409a4 253 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
6a488035
TO
254 $billingLocationTypeId = array_search('Billing', $locationTypes);
255 }
256 else {
257 // presence of related contact implies onbehalf of org case,
258 // where location type is set to default.
259 $locType = CRM_Core_BAO_LocationType::getDefault();
260 $billingLocationTypeId = $locType->id;
261 }
262
263 if (!array_key_exists('related_contact', $values)) {
264 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE, $billingLocationTypeId);
265 }
266 // get primary location email if no email exist( for billing location).
267 if (!$email) {
268 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
269 }
270 if (empty($displayName)) {
271 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
272 }
273
274 //for display profile need to get individual contact id,
275 //hence get it from related_contact if on behalf of org true CRM-3767
276 //CRM-5001 Contribution/Membership:: On Behalf of Organization,
277 //If profile GROUP contain the Individual type then consider the
278 //profile is of Individual ( including the custom data of membership/contribution )
279 //IF Individual type not present in profile then it is consider as Organization data.
280 $userID = $contactID;
281 if ($preID = CRM_Utils_Array::value('custom_pre_id', $values)) {
a7488080 282 if (!empty($values['related_contact'])) {
6a488035 283 $preProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($preID);
b8950542 284 //@todo - following line should not refer to undefined $postProfileTypes? figure out way to test
6a488035
TO
285 if (in_array('Individual', $preProfileTypes) || in_array('Contact', $postProfileTypes)) {
286 //Take Individual contact ID
287 $userID = CRM_Utils_Array::value('related_contact', $values);
288 }
289 }
290 self::buildCustomDisplay($preID, 'customPre', $userID, $template, $params['custom_pre_id']);
291 }
292 $userID = $contactID;
293 if ($postID = CRM_Utils_Array::value('custom_post_id', $values)) {
a7488080 294 if (!empty($values['related_contact'])) {
6a488035
TO
295 $postProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($postID);
296 if (in_array('Individual', $postProfileTypes) || in_array('Contact', $postProfileTypes)) {
297 //Take Individual contact ID
298 $userID = CRM_Utils_Array::value('related_contact', $values);
299 }
300 }
301 self::buildCustomDisplay($postID, 'customPost', $userID, $template, $params['custom_post_id']);
302 }
1421174e 303 if (isset($values['honor'])) {
304 $honorValues = $values['honor'];
305 CRM_Contribute_BAO_ContributionSoft::formatHonoreeProfileFields(
306 $template,
307 $honorValues['honor_profile_values'],
308 $honorValues['honor_profile_id'],
309 $honorValues['honor_id']
310 );
311 }
6a488035
TO
312
313 $title = isset($values['title']) ? $values['title'] : CRM_Contribute_PseudoConstant::contributionPage($values['contribution_page_id']);
314
315 // set email in the template here
316 $tplParams = array(
317 'email' => $email,
318 'receiptFromEmail' => CRM_Utils_Array::value('receipt_from_email', $values),
319 'contactID' => $contactID,
320 'displayName' => $displayName,
321 'contributionID' => CRM_Utils_Array::value('contribution_id', $values),
322 'contributionOtherID' => CRM_Utils_Array::value('contribution_other_id', $values),
6a488035
TO
323 // CRM-5095
324 'lineItem' => CRM_Utils_Array::value('lineItem', $values),
325 // CRM-5095
326 'priceSetID' => CRM_Utils_Array::value('priceSetID', $values),
327 'title' => $title,
328 'isShare' => CRM_Utils_Array::value('is_share', $values),
8b067691 329 'thankyou_title' => CRM_Utils_Array::value('thankyou_title', $values),
6a488035
TO
330 );
331
91bb24a7 332 if ($contributionTypeId = CRM_Utils_Array::value('financial_type_id', $values)) {
6a488035 333 $tplParams['contributionTypeId'] = $contributionTypeId;
91bb24a7 334 $tplParams['contributionTypeName'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
335 $contributionTypeId);
6a488035
TO
336 }
337
338 if ($contributionPageId = CRM_Utils_Array::value('id', $values)) {
339 $tplParams['contributionPageId'] = $contributionPageId;
340 }
341
342 // address required during receipt processing (pdf and email receipt)
343 if ($displayAddress = CRM_Utils_Array::value('address', $values)) {
344 $tplParams['address'] = $displayAddress;
345 }
346
347 // CRM-6976
348 $originalCCReceipt = CRM_Utils_Array::value('cc_receipt', $values);
349
350 // cc to related contacts of contributor OR the one who
351 // signs up. Is used for cases like - on behalf of
352 // contribution / signup ..etc
353 if (array_key_exists('related_contact', $values)) {
354 list($ccDisplayName, $ccEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($values['related_contact']);
355 $ccMailId = "{$ccDisplayName} <{$ccEmail}>";
356
fdd2a6db 357 //@todo - this is the only place in this function where $values is altered - but I can't find any evidence it is used
0d8afee2 358 $values['cc_receipt'] = !empty($values['cc_receipt']) ? ($values['cc_receipt'] . ',' . $ccMailId) : $ccMailId;
6a488035
TO
359
360 // reset primary-email in the template
361 $tplParams['email'] = $ccEmail;
362
363 $tplParams['onBehalfName'] = $displayName;
364 $tplParams['onBehalfEmail'] = $email;
365
366 $ufJoinParams = array(
367 'module' => 'onBehalf',
368 'entity_table' => 'civicrm_contribution_page',
369 'entity_id' => $values['id'],
370 );
371 $OnBehalfProfile = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
91bb24a7 372 $profileId = $OnBehalfProfile[0];
373 $userID = $contactID;
6a488035
TO
374 self::buildCustomDisplay($profileId, 'onBehalfProfile', $userID, $template, $params['onbehalf_profile'], $fieldTypes);
375 }
376
377 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
378 $sendTemplateParams = array(
12dac866 379 'groupName' => !empty($values['isMembership']) ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
380 'valueName' => !empty($values['isMembership']) ? 'membership_online_receipt' : 'contribution_online_receipt',
6a488035
TO
381 'contactId' => $contactID,
382 'tplParams' => $tplParams,
383 'isTest' => $isTest,
384 'PDFFilename' => 'receipt.pdf',
385 );
386
387 if ($returnMessageText) {
c6327d7d 388 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
389 return array(
390 'subject' => $subject,
391 'body' => $message,
392 'to' => $displayName,
393 'html' => $html,
394 );
395 }
396
397 if ($values['is_email_receipt']) {
398 $sendTemplateParams['from'] = CRM_Utils_Array::value('receipt_from_name', $values) . ' <' . $values['receipt_from_email'] . '>';
399 $sendTemplateParams['toName'] = $displayName;
400 $sendTemplateParams['toEmail'] = $email;
401 $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_receipt', $values);
402 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $values);
9161952c 403 //send email with pdf invoice
481a74f4 404 $template = CRM_Core_Smarty::singleton();
9161952c 405 $taxAmt = $template->get_template_vars('dataArray');
bb600eae 406 $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
9161952c
PD
407 $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue);
408 if (count($taxAmt) > 0 && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) {
874c9be7 409 $sendTemplateParams['isEmailPdf'] = TRUE;
9161952c
PD
410 $sendTemplateParams['contributionId'] = $values['contribution_id'];
411 }
c6327d7d 412 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
413 }
414
415 // send duplicate alert, if dupe match found during on-behalf-of processing.
a7488080 416 if (!empty($values['onbehalf_dupe_alert'])) {
6a488035
TO
417 $sendTemplateParams['groupName'] = 'msg_tpl_workflow_contribution';
418 $sendTemplateParams['valueName'] = 'contribution_dupalert';
419 $sendTemplateParams['from'] = ts('Automatically Generated') . " <{$values['receipt_from_email']}>";
420 $sendTemplateParams['toName'] = CRM_Utils_Array::value('receipt_from_name', $values);
421 $sendTemplateParams['toEmail'] = CRM_Utils_Array::value('receipt_from_email', $values);
422 $sendTemplateParams['tplParams']['onBehalfID'] = $contactID;
423 $sendTemplateParams['tplParams']['receiptMessage'] = $message;
424
425 // fix cc and reset back to original, CRM-6976
426 $sendTemplateParams['cc'] = $originalCCReceipt;
427
c6327d7d 428 CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
429 }
430 }
431 }
8ef12e64 432
186c9c17 433 /**
fe482240 434 * Construct the message to be sent by the send function.
c490a46a 435 *
100fef9d
CW
436 * @param array $tplParams
437 * @param int $contactID
186c9c17
EM
438 * @param $isTest
439 *
440 * @return array
441 */
00be9182 442 public function composeMessage($tplParams, $contactID, $isTest) {
6a488035
TO
443 $sendTemplateParams = array(
444 'groupName' => $tplParams['membershipID'] ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
445 'valueName' => $tplParams['membershipID'] ? 'membership_online_receipt' : 'contribution_online_receipt',
446 'contactId' => $contactID,
447 'tplParams' => $tplParams,
448 'isTest' => $isTest,
449 'PDFFilename' => 'receipt.pdf',
450 );
451 if ($returnMessageText) {
c6327d7d 452 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
453 return array(
454 'subject' => $subject,
455 'body' => $message,
456 'to' => $displayName,
457 'html' => $html,
458 );
459 }
460 }
461
462 /**
fe482240 463 * Send the emails for Recurring Contribution Notication.
6a488035 464 *
014c4014
TO
465 * @param string $type
466 * TxnType.
467 * @param int $contactID
468 * Contact id for contributor.
469 * @param int $pageID
470 * Contribution page id.
471 * @param object $recur
472 * Object of recurring contribution table.
2a6da8d7 473 * @param bool|object $autoRenewMembership is it a auto renew membership.
6a488035
TO
474 *
475 * @return void
6a488035 476 */
00be9182 477 public static function recurringNotify($type, $contactID, $pageID, $recur, $autoRenewMembership = FALSE) {
6a488035
TO
478 $value = array();
479 if ($pageID) {
480 CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', $pageID, $value, array(
91bb24a7 481 'title',
482 'is_email_receipt',
483 'receipt_from_name',
484 'receipt_from_email',
485 'cc_receipt',
486 'bcc_receipt',
487 ));
6a488035
TO
488 }
489
490 $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]);
491 $isOfflineRecur = FALSE;
492 if (!$pageID && $recur->id) {
493 $isOfflineRecur = TRUE;
494 }
495 if ($isEmailReceipt || $isOfflineRecur) {
496 if ($pageID) {
497 $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>';
498
499 $receiptFromName = $value[$pageID]['receipt_from_name'];
500 $receiptFromEmail = $value[$pageID]['receipt_from_email'];
501 }
502 else {
91bb24a7 503 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
504 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
505 $receiptFromName = $domainValues[0];
6a488035
TO
506 $receiptFromEmail = $domainValues[1];
507 }
508
509 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE);
510 $templatesParams = array(
511 'groupName' => 'msg_tpl_workflow_contribution',
512 'valueName' => 'contribution_recurring_notify',
513 'contactId' => $contactID,
514 'tplParams' => array(
515 'recur_frequency_interval' => $recur->frequency_interval,
516 'recur_frequency_unit' => $recur->frequency_unit,
517 'recur_installments' => $recur->installments,
518 'recur_start_date' => $recur->start_date,
519 'recur_end_date' => $recur->end_date,
520 'recur_amount' => $recur->amount,
521 'recur_txnType' => $type,
522 'displayName' => $displayName,
523 'receipt_from_name' => $receiptFromName,
524 'receipt_from_email' => $receiptFromEmail,
525 'auto_renew_membership' => $autoRenewMembership,
526 ),
527 'from' => $receiptFrom,
528 'toName' => $displayName,
529 'toEmail' => $email,
530 );
1421174e 531 //CRM-13811
039134ba 532 if ($pageID) {
533 $templatesParams['cc'] = CRM_Utils_Array::value('cc_receipt', $value[$pageID]);
534 $templatesParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $value[$pageID]);
535 }
6a488035
TO
536 if ($recur->id) {
537 // in some cases its just recurringNotify() thats called for the first time and these urls don't get set.
538 // like in PaypalPro, & therefore we set it here additionally.
91bb24a7 539 $template = CRM_Core_Smarty::singleton();
6a488035 540 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recur->id, 'recur', 'obj');
91bb24a7 541 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur');
6a488035
TO
542 $template->assign('cancelSubscriptionUrl', $url);
543
544 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'billing');
545 $template->assign('updateSubscriptionBillingUrl', $url);
546
547 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'update');
548 $template->assign('updateSubscriptionUrl', $url);
549 }
550
c6327d7d 551 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($templatesParams);
6a488035
TO
552
553 if ($sent) {
554 CRM_Core_Error::debug_log_message('Success: mail sent for recurring notification.');
555 }
556 else {
557 CRM_Core_Error::debug_log_message('Failure: mail not sent for recurring notification.');
558 }
559 }
560 }
561
562 /**
100fef9d 563 * Add the custom fields for contribution page (ie profile)
6a488035 564 *
014c4014
TO
565 * @param int $gid
566 * Uf group id.
6a488035 567 * @param string $name
014c4014
TO
568 * @param int $cid
569 * Contact id.
2a6da8d7 570 * @param $template
014c4014
TO
571 * @param array $params
572 * Params to build component whereclause.
2a6da8d7
EM
573 *
574 * @param null $fieldTypes
6a488035
TO
575 *
576 * @return void
6a488035 577 */
20222c10 578 public static function buildCustomDisplay($gid, $name, $cid, &$template, &$params, $fieldTypes = NULL) {
6a488035
TO
579 if ($gid) {
580 if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $cid)) {
91bb24a7 581 $values = array();
6a488035 582 $groupTitle = NULL;
91bb24a7 583 $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL);
6a488035
TO
584 foreach ($fields as $k => $v) {
585 if (!$groupTitle) {
586 $groupTitle = $v["groupTitle"];
587 }
588 // suppress all file fields from display and formatting fields
589 if (
590 CRM_Utils_Array::value('data_type', $v, '') == 'File' ||
591 CRM_Utils_Array::value('name', $v, '') == 'image_URL' ||
592 CRM_Utils_Array::value('field_type', $v) == 'Formatting'
593 ) {
594 unset($fields[$k]);
595 }
596
597 if (!empty($fieldTypes) && (!in_array($v['field_type'], $fieldTypes))) {
598 unset($fields[$k]);
599 }
600 }
601
602 if ($groupTitle) {
603 $template->assign($name . "_grouptitle", $groupTitle);
604 }
605
606 CRM_Core_BAO_UFGroup::getValues($cid, $fields, $values, FALSE, $params);
607
608 if (count($values)) {
609 $template->assign($name, $values);
610 }
611 }
612 }
613 }
614
615 /**
dc195289 616 * make a copy of a contribution page, including
6a488035
TO
617 * all the blocks in the page
618 *
014c4014
TO
619 * @param int $id
620 * The contribution page id to copy.
6a488035 621 *
72b3a70c 622 * @return CRM_Contribute_DAO_ContributionPage
6a488035 623 */
00be9182 624 public static function copy($id) {
6a488035
TO
625 $fieldsFix = array(
626 'prefix' => array(
627 'title' => ts('Copy of') . ' ',
628 ),
629 );
630 $copy = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', array(
353ffa53
TO
631 'id' => $id,
632 ), NULL, $fieldsFix);
6a488035
TO
633
634 //copying all the blocks pertaining to the contribution page
635 $copyPledgeBlock = &CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', array(
353ffa53
TO
636 'entity_id' => $id,
637 'entity_table' => 'civicrm_contribution_page',
638 ), array(
639 'entity_id' => $copy->id,
640 ));
6a488035
TO
641
642 $copyMembershipBlock = &CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', array(
353ffa53
TO
643 'entity_id' => $id,
644 'entity_table' => 'civicrm_contribution_page',
645 ), array(
646 'entity_id' => $copy->id,
647 ));
6a488035
TO
648
649 $copyUFJoin = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', array(
353ffa53
TO
650 'entity_id' => $id,
651 'entity_table' => 'civicrm_contribution_page',
652 ), array(
653 'entity_id' => $copy->id,
654 ));
6a488035
TO
655
656 $copyWidget = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', array(
353ffa53
TO
657 'contribution_page_id' => $id,
658 ), array(
659 'contribution_page_id' => $copy->id,
660 ));
6a488035 661
c6914066 662 //copy price sets
9da8dc8c 663 CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_contribution_page', $id, $copy->id);
c6914066 664
6a488035 665 $copyTellFriend = &CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', array(
353ffa53
TO
666 'entity_id' => $id,
667 'entity_table' => 'civicrm_contribution_page',
668 ), array(
669 'entity_id' => $copy->id,
670 ));
6a488035
TO
671
672 $copyPersonalCampaignPages = &CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', array(
353ffa53
TO
673 'entity_id' => $id,
674 'entity_table' => 'civicrm_contribution_page',
675 ), array(
676 'entity_id' => $copy->id,
b7ff0a0a 677 'target_entity_id' => $copy->id,
353ffa53 678 ));
6a488035
TO
679
680 $copyPremium = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', array(
353ffa53
TO
681 'entity_id' => $id,
682 'entity_table' => 'civicrm_contribution_page',
683 ), array(
684 'entity_id' => $copy->id,
685 ));
6a488035
TO
686 $premiumQuery = "
687SELECT id
688FROM civicrm_premiums
689WHERE entity_table = 'civicrm_contribution_page'
690 AND entity_id ={$id}";
691
692 $premiumDao = CRM_Core_DAO::executeQuery($premiumQuery, CRM_Core_DAO::$_nullArray);
693 while ($premiumDao->fetch()) {
694 if ($premiumDao->id) {
353ffa53 695 $copyPremiumProduct = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_PremiumsProduct', array(
91bb24a7 696 'premiums_id' => $premiumDao->id,
697 ), array(
698 'premiums_id' => $copyPremium->id,
699 ));
6a488035
TO
700 }
701 }
702
703 $copy->save();
704
705 CRM_Utils_Hook::copy('ContributionPage', $copy);
706
707 return $copy;
708 }
709
710 /**
100fef9d 711 * Check if contribution page contains payment
6a488035
TO
712 * processor that supports recurring payment
713 *
014c4014
TO
714 * @param int $contributionPageId
715 * Contribution Page Id.
6a488035 716 *
79d7553f 717 * @return bool
a6c01b45 718 * true if payment processor supports recurring
16b10e64 719 * else false
6a488035 720 *
6a488035 721 */
00be9182 722 public static function checkRecurPaymentProcessor($contributionPageId) {
6a488035
TO
723 //FIXME
724 $sql = "
725 SELECT pp.is_recur
726 FROM civicrm_contribution_page cp,
727 civicrm_payment_processor pp
728 WHERE cp.payment_processor = pp.id
729 AND cp.id = {$contributionPageId}
730";
731
732 if ($recurring = &CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray)) {
733 return TRUE;
734 }
735 return FALSE;
736 }
737
738 /**
100fef9d 739 * Get info for all sections enable/disable.
6a488035 740 *
2a6da8d7 741 * @param array $contribPageIds
a6c01b45
CW
742 * @return array
743 * info regarding all sections.
6a488035 744 */
00be9182 745 public static function getSectionInfo($contribPageIds = array()) {
6a488035
TO
746 $info = array();
747 $whereClause = NULL;
748 if (is_array($contribPageIds) && !empty($contribPageIds)) {
749 $whereClause = 'WHERE civicrm_contribution_page.id IN ( ' . implode(', ', $contribPageIds) . ' )';
750 }
751
752 $sections = array(
753 'settings',
754 'amount',
755 'membership',
756 'custom',
757 'thankyou',
758 'friend',
759 'pcp',
760 'widget',
761 'premium',
762 );
763 $query = "
764 SELECT civicrm_contribution_page.id as id,
8ef12e64 765 civicrm_contribution_page.financial_type_id as settings,
6a488035
TO
766 amount_block_is_active as amount,
767 civicrm_membership_block.id as membership,
768 civicrm_uf_join.id as custom,
769 civicrm_contribution_page.thankyou_title as thankyou,
770 civicrm_tell_friend.id as friend,
771 civicrm_pcp_block.id as pcp,
772 civicrm_contribution_widget.id as widget,
773 civicrm_premiums.id as premium
774 FROM civicrm_contribution_page
775LEFT JOIN civicrm_membership_block ON ( civicrm_membership_block.entity_id = civicrm_contribution_page.id
776 AND civicrm_membership_block.entity_table = 'civicrm_contribution_page'
777 AND civicrm_membership_block.is_active = 1 )
778LEFT JOIN civicrm_uf_join ON ( civicrm_uf_join.entity_id = civicrm_contribution_page.id
779 AND civicrm_uf_join.entity_table = 'civicrm_contribution_page'
780 AND module = 'CiviContribute'
781 AND civicrm_uf_join.is_active = 1 )
782LEFT JOIN civicrm_tell_friend ON ( civicrm_tell_friend.entity_id = civicrm_contribution_page.id
783 AND civicrm_tell_friend.entity_table = 'civicrm_contribution_page'
784 AND civicrm_tell_friend.is_active = 1)
785LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_contribution_page.id
786 AND civicrm_pcp_block.entity_table = 'civicrm_contribution_page'
787 AND civicrm_pcp_block.is_active = 1 )
788LEFT JOIN civicrm_contribution_widget ON ( civicrm_contribution_widget.contribution_page_id = civicrm_contribution_page.id
789 AND civicrm_contribution_widget.is_active = 1 )
790LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm_contribution_page.id
791 AND civicrm_premiums.entity_table = 'civicrm_contribution_page'
792 AND civicrm_premiums.premiums_active = 1 )
793 $whereClause";
794
795 $contributionPage = CRM_Core_DAO::executeQuery($query);
796 while ($contributionPage->fetch()) {
797 if (!isset($info[$contributionPage->id]) || !is_array($info[$contributionPage->id])) {
798 $info[$contributionPage->id] = array_fill_keys(array_values($sections), FALSE);
799 }
800 foreach ($sections as $section) {
801 if ($contributionPage->$section) {
802 $info[$contributionPage->id][$section] = TRUE;
803 }
804 }
805 }
806
807 return $info;
808 }
e1462487
CW
809
810 /**
811 * Get options for a given field.
812 * @see CRM_Core_DAO::buildOptions
813 *
014c4014 814 * @param string $fieldName
353ffa53
TO
815 * @param string $context : @see CRM_Core_DAO::buildOptionsContext
816 * @param array $props : whatever is known about this dao object
e1462487
CW
817 *
818 * @return array|bool
819 */
820 public static function buildOptions($fieldName, $context = NULL, $props = array()) {
821 $params = array();
822 // Special logic for fields whose options depend on context or properties
823 switch ($fieldName) {
824 case 'financial_type_id':
825 // Fixme - this is going to ignore context, better to get conditions, add params, and call PseudoConstant::get
826 return CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
874c9be7 827
79d7553f 828 break;
e1462487
CW
829 }
830 return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
831 }
ba60f73e 832
833 /**
834 * Get or Set multilingually affected honor params for processing module_data or setting default values.
835 *
353ffa53
TO
836 * @param string $params :
837 * @param bool $setDefault : If yes then returns array to used for setting default value afterward
ba60f73e 838 *
839 * @return array|string
840 */
841 public static function formatMultilingualHonorParams($params, $setDefault = FALSE) {
842 $config = CRM_Core_Config::singleton();
843 $sctJson = $sctJsonDecode = NULL;
844 $domain = new CRM_Core_DAO_Domain();
845 $domain->find(TRUE);
846
12792976 847 //When we are fetching the honor params respecting both multi and mono lingual state
ba60f73e 848 //and setting it to default param of Contribution Page's Main and Setting form
849 if ($setDefault) {
850 $sctJsonDecode = json_decode($params);
851 $sctJsonDecode = (array) $sctJsonDecode->soft_credit;
12792976 852 if (!$domain->locales && !empty($sctJsonDecode['default'])) {
853 //monolingual state
854 $sctJsonDecode += (array) $sctJsonDecode['default'];
855 }
856 elseif (!empty($sctJsonDecode[$config->lcMessages])) {
857 //multilingual state
ba60f73e 858 foreach ($sctJsonDecode[$config->lcMessages] as $column => $value) {
859 $sctJsonDecode[$column] = $value;
860 }
861 unset($sctJsonDecode[$config->lcMessages]);
862 }
863 return $sctJsonDecode;
864 }
865
866 //check and handle multilingual honoree params
867 if (!$domain->locales) {
868 //if in singlelingual state simply return the array format
869 $sctJson = json_encode(
870 array(
871 'soft_credit' => array(
872 'soft_credit_types' => $params['soft_credit_types'],
12792976 873 'default' => array(
874 'honor_block_title' => $params['honor_block_title'],
21dfd5f5
TO
875 'honor_block_text' => $params['honor_block_text'],
876 ),
877 ),
ba60f73e 878 )
879 );
880 }
881 else {
882 //if in multilingual state then retrieve the module_data against this contribution and
883 //merge with earlier module_data json data to current so not to lose earlier multilingual module_data information
874c9be7 884 $sctJson = array(
ba60f73e 885 'soft_credit' => array(
886 'soft_credit_types' => $params['soft_credit_types'],
874c9be7 887 $config->lcMessages => array(
ba60f73e 888 'honor_block_title' => $params['honor_block_title'],
21dfd5f5
TO
889 'honor_block_text' => $params['honor_block_text'],
890 ),
891 ),
ba60f73e 892 );
893
894 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
895 $ufJoinDAO->module = 'soft_credit';
896 $ufJoinDAO->entity_id = $params['id'];
897 $ufJoinDAO->find(TRUE);
898 $jsonData = json_decode($ufJoinDAO->module_data);
899 if ($jsonData) {
874c9be7 900 $sctJson['soft_credit'] = array_merge((array) $jsonData->soft_credit, $sctJson['soft_credit']);
ba60f73e 901 }
902 $sctJson = json_encode($sctJson);
903 }
904 return $sctJson;
905 }
9161952c 906
874c9be7 907 /**
fe482240 908 * Generate html for pdf in confirmation receipt email attachment.
014c4014
TO
909 * @param int $contributionId
910 * Contribution Page Id.
911 * @param int $userID
912 * Contact id for contributor.
a6c01b45 913 * @return array
9161952c 914 */
00be9182 915 public static function addInvoicePdfToEmail($contributionId, $userID) {
9161952c
PD
916 $contributionID = array($contributionId);
917 $contactId = array($userID);
918 $pdfParams = array(
919 'output' => 'pdf_invoice',
21dfd5f5 920 'forPage' => 'confirmpage',
9161952c 921 );
62179e22 922 $pdfHtml = CRM_Contribute_Form_Task_Invoice::printPDF($contributionID,
923 $pdfParams, $contactId, CRM_Core_DAO::$_nullObject);
9161952c
PD
924 return $pdfHtml;
925 }
f64a217a
EM
926
927 /**
7fe37828
EM
928 * Helper to determine if the page supports separate membership payments.
929 *
79d7553f 930 * @param int $id form id
f64a217a 931 *
a6c01b45
CW
932 * @return bool
933 * isSeparateMembershipPayment
f64a217a 934 */
00be9182 935 public static function getIsMembershipPayment($id) {
353ffa53
TO
936 $membershipBlocks = civicrm_api3('membership_block', 'get', array(
937 'entity_table' => 'civicrm_contribution_page',
938 'entity_id' => $id,
79d7553f 939 'sequential' => TRUE,
353ffa53 940 ));
22e263ad 941 if (!$membershipBlocks['count']) {
f64a217a
EM
942 return FALSE;
943 }
944 return $membershipBlocks['values'][0]['is_separate_payment'];
945 }
96025800 946
6a488035 947}