Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Contribute / BAO / ContributionPage.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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) {
51 $dao = new CRM_Contribute_DAO_ContributionPage();
52 $dao->copyValues($params);
53 $dao->save();
54 return $dao;
55 }
56
57 /**
58 * update the is_active flag in the db
59 *
60 * @param int $id id of the database record
61 * @param boolean $is_active value we want to set the is_active field
62 *
63 * @return Object DAO object on sucess, null otherwise
64 * @static
65 */
66 static function setIsActive($id, $is_active) {
67 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'is_active', $is_active);
68 }
69
70 static function setValues($id, &$values) {
71 $params = array(
72 'id' => $id,
73 );
74
75 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $values);
76
77 // get the profile ids
78 $ufJoinParams = array(
79 'module' => 'CiviContribute',
80 'entity_table' => 'civicrm_contribution_page',
81 'entity_id' => $id,
82 );
83 list($values['custom_pre_id'], $customPostIds) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
84
85 if (!empty($customPostIds)) {
86 $values['custom_post_id'] = $customPostIds[0];
87 }
88 else {
89 $values['custom_post_id'] = '';
90 }
91 // // add an accounting code also
92 // if ($values ['financial_type_id']) {
93 // $values ['accountingCode'] = CRM_Core_DAO::getFieldValue( 'CRM_Financial_DAO_FinancialType', $values ['financial_type_id'], 'accounting_code' );
94 // }
95 }
96
97 /**
98 * Function to send the emails
99 *
100 * @param int $contactID contact id
101 * @param array $values associated array of fields
102 * @param boolean $isTest if in test mode
103 * @param boolean $returnMessageText return the message text instead of sending the mail
104 *
105 * @return void
106 * @access public
107 * @static
108 */
109 static function sendMail($contactID, &$values, $isTest = FALSE, $returnMessageText = FALSE, $fieldTypes = NULL) {
110 $gIds = $params = array();
111 $email = NULL;
112 if (isset($values['custom_pre_id'])) {
113 $preProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_pre_id']);
114 if ($preProfileType == 'Membership' && CRM_Utils_Array::value('membership_id', $values)) {
115 $params['custom_pre_id'] = array(
116 array(
117 'member_id',
118 '=',
119 $values['membership_id'],
120 0,
121 0,
122 ),
123 );
124 }
125 elseif ($preProfileType == 'Contribution' && CRM_Utils_Array::value('contribution_id', $values)) {
126 $params['custom_pre_id'] = array(
127 array(
128 'contribution_id',
129 '=',
130 $values['contribution_id'],
131 0,
132 0,
133 ),
134 );
135 }
136
137 $gIds['custom_pre_id'] = $values['custom_pre_id'];
138 }
139
140 if (isset($values['custom_post_id'])) {
141 $postProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_post_id']);
142 if ($postProfileType == 'Membership' && CRM_Utils_Array::value('membership_id', $values)) {
143 $params['custom_post_id'] = array(
144 array(
145 'member_id',
146 '=',
147 $values['membership_id'],
148 0,
149 0,
150 ),
151 );
152 }
153 elseif ($postProfileType == 'Contribution' && CRM_Utils_Array::value('contribution_id', $values)) {
154 $params['custom_post_id'] = array(
155 array(
156 'contribution_id',
157 '=',
158 $values['contribution_id'],
159 0,
160 0,
161 ),
162 );
163 }
164
165 $gIds['custom_post_id'] = $values['custom_post_id'];
166 }
167
168 if (CRM_Utils_Array::value('is_for_organization', $values)) {
169 if (CRM_Utils_Array::value('membership_id', $values)) {
170 $params['onbehalf_profile'] = array(
171 array(
172 'member_id',
173 '=',
174 $values['membership_id'],
175 0,
176 0,
177 ),
178 );
179 }
180 elseif (CRM_Utils_Array::value('contribution_id', $values)) {
181 $params['onbehalf_profile'] = array(
182 array(
183 'contribution_id',
184 '=',
185 $values['contribution_id'],
186 0,
187 0,
188 ),
189 );
190 }
191 }
192
193 //check whether it is a test drive
194 if ($isTest && !empty($params['custom_pre_id'])) {
195 $params['custom_pre_id'][] = array(
196 'contribution_test',
197 '=',
198 1,
199 0,
200 0,
201 );
202 }
203
204 if ($isTest && !empty($params['custom_post_id'])) {
205 $params['custom_post_id'][] = array(
206 'contribution_test',
207 '=',
208 1,
209 0,
210 0,
211 );
212 }
213
214 if (!$returnMessageText && !empty($gIds)) {
215 //send notification email if field values are set (CRM-1941)
216 foreach ($gIds as $key => $gId) {
217 if ($gId) {
218 $email = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify');
219 if ($email) {
220 $val = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $contactID, CRM_Utils_Array::value($key, $params), true );
221 CRM_Core_BAO_UFGroup::commonSendMail($contactID, $val);
222 }
223 }
224 }
225 }
226
227 if ( CRM_Utils_Array::value('is_email_receipt', $values) ||
228 CRM_Utils_Array::value('onbehalf_dupe_alert', $values) ||
229 $returnMessageText
230 ) {
231 $template = CRM_Core_Smarty::singleton();
232
233 // get the billing location type
234 if (!array_key_exists('related_contact', $values)) {
235 $locationTypes = CRM_Core_PseudoConstant::locationType();
236 $billingLocationTypeId = array_search('Billing', $locationTypes);
237 }
238 else {
239 // presence of related contact implies onbehalf of org case,
240 // where location type is set to default.
241 $locType = CRM_Core_BAO_LocationType::getDefault();
242 $billingLocationTypeId = $locType->id;
243 }
244
245 if (!array_key_exists('related_contact', $values)) {
246 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE, $billingLocationTypeId);
247 }
248 // get primary location email if no email exist( for billing location).
249 if (!$email) {
250 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
251 }
252 if (empty($displayName)) {
253 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
254 }
255
256 //for display profile need to get individual contact id,
257 //hence get it from related_contact if on behalf of org true CRM-3767
258 //CRM-5001 Contribution/Membership:: On Behalf of Organization,
259 //If profile GROUP contain the Individual type then consider the
260 //profile is of Individual ( including the custom data of membership/contribution )
261 //IF Individual type not present in profile then it is consider as Organization data.
262 $userID = $contactID;
263 if ($preID = CRM_Utils_Array::value('custom_pre_id', $values)) {
264 if (CRM_Utils_Array::value('related_contact', $values)) {
265 $preProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($preID);
266 if (in_array('Individual', $preProfileTypes) || in_array('Contact', $postProfileTypes)) {
267 //Take Individual contact ID
268 $userID = CRM_Utils_Array::value('related_contact', $values);
269 }
270 }
271 self::buildCustomDisplay($preID, 'customPre', $userID, $template, $params['custom_pre_id']);
272 }
273 $userID = $contactID;
274 if ($postID = CRM_Utils_Array::value('custom_post_id', $values)) {
275 if (CRM_Utils_Array::value('related_contact', $values)) {
276 $postProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($postID);
277 if (in_array('Individual', $postProfileTypes) || in_array('Contact', $postProfileTypes)) {
278 //Take Individual contact ID
279 $userID = CRM_Utils_Array::value('related_contact', $values);
280 }
281 }
282 self::buildCustomDisplay($postID, 'customPost', $userID, $template, $params['custom_post_id']);
283 }
284
285 $title = isset($values['title']) ? $values['title'] : CRM_Contribute_PseudoConstant::contributionPage($values['contribution_page_id']);
286
287 // set email in the template here
288 $tplParams = array(
289 'email' => $email,
290 'receiptFromEmail' => CRM_Utils_Array::value('receipt_from_email', $values),
291 'contactID' => $contactID,
292 'displayName' => $displayName,
293 'contributionID' => CRM_Utils_Array::value('contribution_id', $values),
294 'contributionOtherID' => CRM_Utils_Array::value('contribution_other_id', $values),
295 'membershipID' => CRM_Utils_Array::value('membership_id', $values),
296 // CRM-5095
297 'lineItem' => CRM_Utils_Array::value('lineItem', $values),
298 // CRM-5095
299 'priceSetID' => CRM_Utils_Array::value('priceSetID', $values),
300 'title' => $title,
301 'isShare' => CRM_Utils_Array::value('is_share', $values),
302 );
303
304 if ( $contributionTypeId = CRM_Utils_Array::value('financial_type_id', $values ) ) {
305 $tplParams['contributionTypeId'] = $contributionTypeId;
306 $tplParams['contributionTypeName'] = CRM_Core_DAO::getFieldValue( 'CRM_Financial_DAO_FinancialType',
307 $contributionTypeId );
308 }
309
310 if ($contributionPageId = CRM_Utils_Array::value('id', $values)) {
311 $tplParams['contributionPageId'] = $contributionPageId;
312 }
313
314 // address required during receipt processing (pdf and email receipt)
315 if ($displayAddress = CRM_Utils_Array::value('address', $values)) {
316 $tplParams['address'] = $displayAddress;
317 }
318
319 // CRM-6976
320 $originalCCReceipt = CRM_Utils_Array::value('cc_receipt', $values);
321
322 // cc to related contacts of contributor OR the one who
323 // signs up. Is used for cases like - on behalf of
324 // contribution / signup ..etc
325 if (array_key_exists('related_contact', $values)) {
326 list($ccDisplayName, $ccEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($values['related_contact']);
327 $ccMailId = "{$ccDisplayName} <{$ccEmail}>";
328
329 $values['cc_receipt'] = CRM_Utils_Array::value('cc_receipt', $values) ? ($values['cc_receipt'] . ',' . $ccMailId) : $ccMailId;
330
331 // reset primary-email in the template
332 $tplParams['email'] = $ccEmail;
333
334 $tplParams['onBehalfName'] = $displayName;
335 $tplParams['onBehalfEmail'] = $email;
336
337 $ufJoinParams = array(
338 'module' => 'onBehalf',
339 'entity_table' => 'civicrm_contribution_page',
340 'entity_id' => $values['id'],
341 );
342 $OnBehalfProfile = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
343 $profileId = $OnBehalfProfile[0];
344 $userID = $contactID;
345 self::buildCustomDisplay($profileId, 'onBehalfProfile', $userID, $template, $params['onbehalf_profile'], $fieldTypes);
346 }
347
348 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
349 $sendTemplateParams = array(
350 'groupName' => $tplParams['membershipID'] ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
351 'valueName' => $tplParams['membershipID'] ? 'membership_online_receipt' : 'contribution_online_receipt',
352 'contactId' => $contactID,
353 'tplParams' => $tplParams,
354 'isTest' => $isTest,
355 'PDFFilename' => 'receipt.pdf',
356 );
357
358 if ($returnMessageText) {
359 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate($sendTemplateParams);
360 return array(
361 'subject' => $subject,
362 'body' => $message,
363 'to' => $displayName,
364 'html' => $html,
365 );
366 }
367
368 if ($values['is_email_receipt']) {
369 $sendTemplateParams['from'] = CRM_Utils_Array::value('receipt_from_name', $values) . ' <' . $values['receipt_from_email'] . '>';
370 $sendTemplateParams['toName'] = $displayName;
371 $sendTemplateParams['toEmail'] = $email;
372 $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_receipt', $values);
373 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $values);
374 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate($sendTemplateParams);
375 }
376
377 // send duplicate alert, if dupe match found during on-behalf-of processing.
378 if (CRM_Utils_Array::value('onbehalf_dupe_alert', $values)) {
379 $sendTemplateParams['groupName'] = 'msg_tpl_workflow_contribution';
380 $sendTemplateParams['valueName'] = 'contribution_dupalert';
381 $sendTemplateParams['from'] = ts('Automatically Generated') . " <{$values['receipt_from_email']}>";
382 $sendTemplateParams['toName'] = CRM_Utils_Array::value('receipt_from_name', $values);
383 $sendTemplateParams['toEmail'] = CRM_Utils_Array::value('receipt_from_email', $values);
384 $sendTemplateParams['tplParams']['onBehalfID'] = $contactID;
385 $sendTemplateParams['tplParams']['receiptMessage'] = $message;
386
387 // fix cc and reset back to original, CRM-6976
388 $sendTemplateParams['cc'] = $originalCCReceipt;
389
390 CRM_Core_BAO_MessageTemplates::sendTemplate($sendTemplateParams);
391 }
392 }
393 }
394
395 /*
396 * Construct the message to be sent by the send function
397 *
398 */
399 function composeMessage($tplParams, $contactID, $isTest) {
400 $sendTemplateParams = array(
401 'groupName' => $tplParams['membershipID'] ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
402 'valueName' => $tplParams['membershipID'] ? 'membership_online_receipt' : 'contribution_online_receipt',
403 'contactId' => $contactID,
404 'tplParams' => $tplParams,
405 'isTest' => $isTest,
406 'PDFFilename' => 'receipt.pdf',
407 );
408 if ($returnMessageText) {
409 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate($sendTemplateParams);
410 return array(
411 'subject' => $subject,
412 'body' => $message,
413 'to' => $displayName,
414 'html' => $html,
415 );
416 }
417 }
418
419 /**
420 * Function to send the emails for Recurring Contribution Notication
421 *
422 * @param string $type txnType
423 * @param int $contactID contact id for contributor
424 * @param int $pageID contribution page id
425 * @param object $recur object of recurring contribution table
426 * @param object $autoRenewMembership is it a auto renew membership.
427 *
428 * @return void
429 * @access public
430 * @static
431 */
432 static function recurringNotify($type, $contactID, $pageID, $recur, $autoRenewMembership = FALSE) {
433 $value = array();
434 if ($pageID) {
435 CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', $pageID, $value, array(
436 'title',
437 'is_email_receipt',
438 'receipt_from_name',
439 'receipt_from_email',
440 'cc_receipt',
441 'bcc_receipt',
442 ));
443 }
444
445 $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]);
446 $isOfflineRecur = FALSE;
447 if (!$pageID && $recur->id) {
448 $isOfflineRecur = TRUE;
449 }
450 if ($isEmailReceipt || $isOfflineRecur) {
451 if ($pageID) {
452 $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>';
453
454 $receiptFromName = $value[$pageID]['receipt_from_name'];
455 $receiptFromEmail = $value[$pageID]['receipt_from_email'];
456 }
457 else {
458 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
459 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
460 $receiptFromName = $domainValues[0];
461 $receiptFromEmail = $domainValues[1];
462 }
463
464 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE);
465 $templatesParams = array(
466 'groupName' => 'msg_tpl_workflow_contribution',
467 'valueName' => 'contribution_recurring_notify',
468 'contactId' => $contactID,
469 'tplParams' => array(
470 'recur_frequency_interval' => $recur->frequency_interval,
471 'recur_frequency_unit' => $recur->frequency_unit,
472 'recur_installments' => $recur->installments,
473 'recur_start_date' => $recur->start_date,
474 'recur_end_date' => $recur->end_date,
475 'recur_amount' => $recur->amount,
476 'recur_txnType' => $type,
477 'displayName' => $displayName,
478 'receipt_from_name' => $receiptFromName,
479 'receipt_from_email' => $receiptFromEmail,
480 'auto_renew_membership' => $autoRenewMembership,
481 ),
482 'from' => $receiptFrom,
483 'toName' => $displayName,
484 'toEmail' => $email,
485 );
486
487 if ($recur->id) {
488 // in some cases its just recurringNotify() thats called for the first time and these urls don't get set.
489 // like in PaypalPro, & therefore we set it here additionally.
490 $template = CRM_Core_Smarty::singleton();
491 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recur->id, 'recur', 'obj');
492 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur');
493 $template->assign('cancelSubscriptionUrl', $url);
494
495 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'billing');
496 $template->assign('updateSubscriptionBillingUrl', $url);
497
498 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'update');
499 $template->assign('updateSubscriptionUrl', $url);
500 }
501
502 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate($templatesParams);
503
504 if ($sent) {
505 CRM_Core_Error::debug_log_message('Success: mail sent for recurring notification.');
506 }
507 else {
508 CRM_Core_Error::debug_log_message('Failure: mail not sent for recurring notification.');
509 }
510 }
511 }
512
513 /**
514 * Function to add the custom fields for contribution page (ie profile)
515 *
516 * @param int $gid uf group id
517 * @param string $name
518 * @param int $cid contact id
519 * @param array $params params to build component whereclause
520 *
521 * @return void
522 * @access public
523 * @static
524 */
525 function buildCustomDisplay($gid, $name, $cid, &$template, &$params, $fieldTypes = NULL) {
526 if ($gid) {
527 if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $cid)) {
528 $values = array();
529 $groupTitle = NULL;
530 $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL);
531 foreach ($fields as $k => $v) {
532 if (!$groupTitle) {
533 $groupTitle = $v["groupTitle"];
534 }
535 // suppress all file fields from display and formatting fields
536 if (
537 CRM_Utils_Array::value('data_type', $v, '') == 'File' ||
538 CRM_Utils_Array::value('name', $v, '') == 'image_URL' ||
539 CRM_Utils_Array::value('field_type', $v) == 'Formatting'
540 ) {
541 unset($fields[$k]);
542 }
543
544 if (!empty($fieldTypes) && (!in_array($v['field_type'], $fieldTypes))) {
545 unset($fields[$k]);
546 }
547 }
548
549 if ($groupTitle) {
550 $template->assign($name . "_grouptitle", $groupTitle);
551 }
552
553 CRM_Core_BAO_UFGroup::getValues($cid, $fields, $values, FALSE, $params);
554
555 if (count($values)) {
556 $template->assign($name, $values);
557 }
558 }
559 }
560 }
561
562 /**
563 * This function is to make a copy of a contribution page, including
564 * all the blocks in the page
565 *
566 * @param int $id the contribution page id to copy
567 *
568 * @return the copy object
569 * @access public
570 * @static
571 */
572 static function copy($id) {
573 $fieldsFix = array(
574 'prefix' => array(
575 'title' => ts('Copy of') . ' ',
576 ),
577 );
578 $copy = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', array(
579 'id' => $id,
580 ), NULL, $fieldsFix);
581
582 //copying all the blocks pertaining to the contribution page
583 $copyPledgeBlock = &CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', array(
584 'entity_id' => $id,
585 'entity_table' => 'civicrm_contribution_page',
586 ), array(
587 'entity_id' => $copy->id,
588 ));
589
590 $copyMembershipBlock = &CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', array(
591 'entity_id' => $id,
592 'entity_table' => 'civicrm_contribution_page',
593 ), array(
594 'entity_id' => $copy->id,
595 ));
596
597 $copyUFJoin = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', array(
598 'entity_id' => $id,
599 'entity_table' => 'civicrm_contribution_page',
600 ), array(
601 'entity_id' => $copy->id,
602 ));
603
604 $copyWidget = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', array(
605 'contribution_page_id' => $id,
606 ), array(
607 'contribution_page_id' => $copy->id,
608 ));
609
610 //copy option group and values
611 $copy->default_amount_id = CRM_Core_BAO_OptionGroup::copyValue('contribution', $id, $copy->id, CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'default_amount_id'));
612 $copyTellFriend = &CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', array(
613 'entity_id' => $id,
614 'entity_table' => 'civicrm_contribution_page',
615 ), array(
616 'entity_id' => $copy->id,
617 ));
618
619 $copyPersonalCampaignPages = &CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', array(
620 'entity_id' => $id,
621 'entity_table' => 'civicrm_contribution_page',
622 ), array(
623 'entity_id' => $copy->id,
624 ));
625
626 $copyPremium = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', array(
627 'entity_id' => $id,
628 'entity_table' => 'civicrm_contribution_page',
629 ), array(
630 'entity_id' => $copy->id,
631 ));
632 $premiumQuery = "
633SELECT id
634FROM civicrm_premiums
635WHERE entity_table = 'civicrm_contribution_page'
636 AND entity_id ={$id}";
637
638 $premiumDao = CRM_Core_DAO::executeQuery($premiumQuery, CRM_Core_DAO::$_nullArray);
639 while ($premiumDao->fetch()) {
640 if ($premiumDao->id) {
641 $copyPremiumProduct = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_PremiumsProduct', array(
642 'premiums_id' => $premiumDao->id,
643 ), array(
644 'premiums_id' => $copyPremium->id,
645 ));
646 }
647 }
648
649 $copy->save();
650
651 CRM_Utils_Hook::copy('ContributionPage', $copy);
652
653 return $copy;
654 }
655
656 /**
657 * Function to check if contribution page contains payment
658 * processor that supports recurring payment
659 *
660 * @param int $contributionPageId Contribution Page Id
661 *
662 * @return boolean true if payment processor supports recurring
663 * else false
664 *
665 * @access public
666 * @static
667 */
668 static function checkRecurPaymentProcessor($contributionPageId) {
669 //FIXME
670 $sql = "
671 SELECT pp.is_recur
672 FROM civicrm_contribution_page cp,
673 civicrm_payment_processor pp
674 WHERE cp.payment_processor = pp.id
675 AND cp.id = {$contributionPageId}
676";
677
678 if ($recurring = &CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray)) {
679 return TRUE;
680 }
681 return FALSE;
682 }
683
684 /**
685 * Function to get info for all sections enable/disable.
686 *
687 * @return array $info info regarding all sections.
688 * @access public
689 * @static
690 */
691 static function getSectionInfo($contribPageIds = array(
692 )) {
693 $info = array();
694 $whereClause = NULL;
695 if (is_array($contribPageIds) && !empty($contribPageIds)) {
696 $whereClause = 'WHERE civicrm_contribution_page.id IN ( ' . implode(', ', $contribPageIds) . ' )';
697 }
698
699 $sections = array(
700 'settings',
701 'amount',
702 'membership',
703 'custom',
704 'thankyou',
705 'friend',
706 'pcp',
707 'widget',
708 'premium',
709 );
710 $query = "
711 SELECT civicrm_contribution_page.id as id,
712 civicrm_contribution_page.financial_type_id as settings,
713 amount_block_is_active as amount,
714 civicrm_membership_block.id as membership,
715 civicrm_uf_join.id as custom,
716 civicrm_contribution_page.thankyou_title as thankyou,
717 civicrm_tell_friend.id as friend,
718 civicrm_pcp_block.id as pcp,
719 civicrm_contribution_widget.id as widget,
720 civicrm_premiums.id as premium
721 FROM civicrm_contribution_page
722LEFT JOIN civicrm_membership_block ON ( civicrm_membership_block.entity_id = civicrm_contribution_page.id
723 AND civicrm_membership_block.entity_table = 'civicrm_contribution_page'
724 AND civicrm_membership_block.is_active = 1 )
725LEFT JOIN civicrm_uf_join ON ( civicrm_uf_join.entity_id = civicrm_contribution_page.id
726 AND civicrm_uf_join.entity_table = 'civicrm_contribution_page'
727 AND module = 'CiviContribute'
728 AND civicrm_uf_join.is_active = 1 )
729LEFT JOIN civicrm_tell_friend ON ( civicrm_tell_friend.entity_id = civicrm_contribution_page.id
730 AND civicrm_tell_friend.entity_table = 'civicrm_contribution_page'
731 AND civicrm_tell_friend.is_active = 1)
732LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_contribution_page.id
733 AND civicrm_pcp_block.entity_table = 'civicrm_contribution_page'
734 AND civicrm_pcp_block.is_active = 1 )
735LEFT JOIN civicrm_contribution_widget ON ( civicrm_contribution_widget.contribution_page_id = civicrm_contribution_page.id
736 AND civicrm_contribution_widget.is_active = 1 )
737LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm_contribution_page.id
738 AND civicrm_premiums.entity_table = 'civicrm_contribution_page'
739 AND civicrm_premiums.premiums_active = 1 )
740 $whereClause";
741
742 $contributionPage = CRM_Core_DAO::executeQuery($query);
743 while ($contributionPage->fetch()) {
744 if (!isset($info[$contributionPage->id]) || !is_array($info[$contributionPage->id])) {
745 $info[$contributionPage->id] = array_fill_keys(array_values($sections), FALSE);
746 }
747 foreach ($sections as $section) {
748 if ($contributionPage->$section) {
749 $info[$contributionPage->id][$section] = TRUE;
750 }
751 }
752 }
753
754 return $info;
755 }
756}
757