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