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