CRM-15895: fix copy contribution page which links PCP to original contrib page
[civicrm-core.git] / CRM / Contribute / BAO / ContributionPage.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class contains Contribution Page related functions.
38 */
39 class 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 $financialTypeId = NULL;
52 if (!empty($params['id']) && !CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $params['id'], NULL, 1)) {
53 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $params['id'], 'financial_type_id');
54 }
55 $dao = new CRM_Contribute_DAO_ContributionPage();
56 $dao->copyValues($params);
57 $dao->save();
58 if ($financialTypeId && !empty($params['financial_type_id']) && $financialTypeId != $params['financial_type_id']) {
59 CRM_Price_BAO_PriceFieldValue::updateFinancialType($params['id'], 'civicrm_contribution_page', $params['financial_type_id']);
60 }
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 *
70 * @return Object DAO object on success, null otherwise
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
77 /**
78 * @param $id
79 * @param $values
80 */
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);
87
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 *
111 * @param int $contactID contact id
112 * @param array $values associated array of fields
113 * @param boolean $isTest if in test mode
114 * @param boolean $returnMessageText return the message text instead of sending the mail
115 *
116 * @param null $fieldTypes
117 *
118 * @return void
119 * @access public
120 * @static
121 */
122 static function sendMail($contactID, $values, $isTest = FALSE, $returnMessageText = FALSE, $fieldTypes = NULL) {
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']);
127 if ($preProfileType == 'Membership' && !empty($values['membership_id'])) {
128 $params['custom_pre_id'] = array(
129 array(
130 'member_id',
131 '=',
132 $values['membership_id'],
133 0,
134 0,
135 ),
136 );
137 }
138 elseif ($preProfileType == 'Contribution' && !empty($values['contribution_id'])) {
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']);
155 if ($postProfileType == 'Membership' && !empty($values['membership_id'])) {
156 $params['custom_post_id'] = array(
157 array(
158 'member_id',
159 '=',
160 $values['membership_id'],
161 0,
162 0,
163 ),
164 );
165 }
166 elseif ($postProfileType == 'Contribution' && !empty($values['contribution_id'])) {
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
181 if (!empty($values['is_for_organization'])) {
182 if (!empty($values['membership_id'])) {
183 $params['onbehalf_profile'] = array(
184 array(
185 'member_id',
186 '=',
187 $values['membership_id'],
188 0,
189 0,
190 ),
191 );
192 }
193 elseif (!empty($values['contribution_id'])) {
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 }
226
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
240 if (!empty($values['is_email_receipt']) || !empty($values['onbehalf_dupe_alert']) ||
241 $returnMessageText
242 ) {
243 $template = CRM_Core_Smarty::singleton();
244
245 // get the billing location type
246 if (!array_key_exists('related_contact', $values)) {
247 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
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)) {
276 if (!empty($values['related_contact'])) {
277 $preProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($preID);
278 //@todo - following line should not refer to undefined $postProfileTypes? figure out way to test
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)) {
288 if (!empty($values['related_contact'])) {
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 }
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 }
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),
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
325 if ($contributionTypeId = CRM_Utils_Array::value('financial_type_id', $values)) {
326 $tplParams['contributionTypeId'] = $contributionTypeId;
327 $tplParams['contributionTypeName'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
328 $contributionTypeId);
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
350 //@todo - this is the only place in this function where $values is altered - but I can't find any evidence it is used
351 $values['cc_receipt'] = !empty($values['cc_receipt']) ? ($values['cc_receipt'] . ',' . $ccMailId) : $ccMailId;
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);
365 $profileId = $OnBehalfProfile[0];
366 $userID = $contactID;
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(
372 'groupName' => !empty($values['isMembership']) ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
373 'valueName' => !empty($values['isMembership']) ? 'membership_online_receipt' : 'contribution_online_receipt',
374 'contactId' => $contactID,
375 'tplParams' => $tplParams,
376 'isTest' => $isTest,
377 'PDFFilename' => 'receipt.pdf',
378 );
379
380 if ($returnMessageText) {
381 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
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);
396 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
397 }
398
399 // send duplicate alert, if dupe match found during on-behalf-of processing.
400 if (!empty($values['onbehalf_dupe_alert'])) {
401 $sendTemplateParams['groupName'] = 'msg_tpl_workflow_contribution';
402 $sendTemplateParams['valueName'] = 'contribution_dupalert';
403 $sendTemplateParams['from'] = ts('Automatically Generated') . " <{$values['receipt_from_email']}>";
404 $sendTemplateParams['toName'] = CRM_Utils_Array::value('receipt_from_name', $values);
405 $sendTemplateParams['toEmail'] = CRM_Utils_Array::value('receipt_from_email', $values);
406 $sendTemplateParams['tplParams']['onBehalfID'] = $contactID;
407 $sendTemplateParams['tplParams']['receiptMessage'] = $message;
408
409 // fix cc and reset back to original, CRM-6976
410 $sendTemplateParams['cc'] = $originalCCReceipt;
411
412 CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
413 }
414 }
415 }
416
417 /*
418 * Construct the message to be sent by the send function
419 *
420 */
421 /**
422 * @param $tplParams
423 * @param $contactID
424 * @param $isTest
425 *
426 * @return array
427 */
428 function composeMessage($tplParams, $contactID, $isTest) {
429 $sendTemplateParams = array(
430 'groupName' => $tplParams['membershipID'] ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
431 'valueName' => $tplParams['membershipID'] ? 'membership_online_receipt' : 'contribution_online_receipt',
432 'contactId' => $contactID,
433 'tplParams' => $tplParams,
434 'isTest' => $isTest,
435 'PDFFilename' => 'receipt.pdf',
436 );
437 if ($returnMessageText) {
438 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
439 return array(
440 'subject' => $subject,
441 'body' => $message,
442 'to' => $displayName,
443 'html' => $html,
444 );
445 }
446 }
447
448 /**
449 * Function to send the emails for Recurring Contribution Notication
450 *
451 * @param string $type txnType
452 * @param int $contactID contact id for contributor
453 * @param int $pageID contribution page id
454 * @param object $recur object of recurring contribution table
455 * @param bool|object $autoRenewMembership is it a auto renew membership.
456 *
457 * @return void
458 * @access public
459 * @static
460 */
461 static function recurringNotify($type, $contactID, $pageID, $recur, $autoRenewMembership = FALSE) {
462 $value = array();
463 if ($pageID) {
464 CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', $pageID, $value, array(
465 'title',
466 'is_email_receipt',
467 'receipt_from_name',
468 'receipt_from_email',
469 'cc_receipt',
470 'bcc_receipt',
471 ));
472 }
473
474 $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]);
475 $isOfflineRecur = FALSE;
476 if (!$pageID && $recur->id) {
477 $isOfflineRecur = TRUE;
478 }
479 if ($isEmailReceipt || $isOfflineRecur) {
480 if ($pageID) {
481 $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>';
482
483 $receiptFromName = $value[$pageID]['receipt_from_name'];
484 $receiptFromEmail = $value[$pageID]['receipt_from_email'];
485 }
486 else {
487 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
488 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
489 $receiptFromName = $domainValues[0];
490 $receiptFromEmail = $domainValues[1];
491 }
492
493 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE);
494 $templatesParams = array(
495 'groupName' => 'msg_tpl_workflow_contribution',
496 'valueName' => 'contribution_recurring_notify',
497 'contactId' => $contactID,
498 'tplParams' => array(
499 'recur_frequency_interval' => $recur->frequency_interval,
500 'recur_frequency_unit' => $recur->frequency_unit,
501 'recur_installments' => $recur->installments,
502 'recur_start_date' => $recur->start_date,
503 'recur_end_date' => $recur->end_date,
504 'recur_amount' => $recur->amount,
505 'recur_txnType' => $type,
506 'displayName' => $displayName,
507 'receipt_from_name' => $receiptFromName,
508 'receipt_from_email' => $receiptFromEmail,
509 'auto_renew_membership' => $autoRenewMembership,
510 ),
511 'from' => $receiptFrom,
512 'toName' => $displayName,
513 'toEmail' => $email,
514 );
515 //CRM-13811
516 if ($pageID) {
517 $templatesParams['cc'] = CRM_Utils_Array::value('cc_receipt', $value[$pageID]);
518 $templatesParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $value[$pageID]);
519 }
520 if ($recur->id) {
521 // in some cases its just recurringNotify() thats called for the first time and these urls don't get set.
522 // like in PaypalPro, & therefore we set it here additionally.
523 $template = CRM_Core_Smarty::singleton();
524 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recur->id, 'recur', 'obj');
525 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur');
526 $template->assign('cancelSubscriptionUrl', $url);
527
528 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'billing');
529 $template->assign('updateSubscriptionBillingUrl', $url);
530
531 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'update');
532 $template->assign('updateSubscriptionUrl', $url);
533 }
534
535 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($templatesParams);
536
537 if ($sent) {
538 CRM_Core_Error::debug_log_message('Success: mail sent for recurring notification.');
539 }
540 else {
541 CRM_Core_Error::debug_log_message('Failure: mail not sent for recurring notification.');
542 }
543 }
544 }
545
546 /**
547 * Function to add the custom fields for contribution page (ie profile)
548 *
549 * @param int $gid uf group id
550 * @param string $name
551 * @param int $cid contact id
552 * @param $template
553 * @param array $params params to build component whereclause
554 *
555 * @param null $fieldTypes
556 *
557 * @return void
558 * @access public
559 * @static
560 */
561 public static function buildCustomDisplay($gid, $name, $cid, &$template, &$params, $fieldTypes = NULL) {
562 if ($gid) {
563 if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $cid)) {
564 $values = array();
565 $groupTitle = NULL;
566 $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL);
567 foreach ($fields as $k => $v) {
568 if (!$groupTitle) {
569 $groupTitle = $v["groupTitle"];
570 }
571 // suppress all file fields from display and formatting fields
572 if (
573 CRM_Utils_Array::value('data_type', $v, '') == 'File' ||
574 CRM_Utils_Array::value('name', $v, '') == 'image_URL' ||
575 CRM_Utils_Array::value('field_type', $v) == 'Formatting'
576 ) {
577 unset($fields[$k]);
578 }
579
580 if (!empty($fieldTypes) && (!in_array($v['field_type'], $fieldTypes))) {
581 unset($fields[$k]);
582 }
583 }
584
585 if ($groupTitle) {
586 $template->assign($name . "_grouptitle", $groupTitle);
587 }
588
589 CRM_Core_BAO_UFGroup::getValues($cid, $fields, $values, FALSE, $params);
590
591 if (count($values)) {
592 $template->assign($name, $values);
593 }
594 }
595 }
596 }
597
598 /**
599 * This function is to make a copy of a contribution page, including
600 * all the blocks in the page
601 *
602 * @param int $id the contribution page id to copy
603 *
604 * @return the copy object
605 * @access public
606 * @static
607 */
608 static function copy($id) {
609 $fieldsFix = array(
610 'prefix' => array(
611 'title' => ts('Copy of') . ' ',
612 ),
613 );
614 $copy = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', array(
615 'id' => $id,
616 ), NULL, $fieldsFix);
617
618 //copying all the blocks pertaining to the contribution page
619 $copyPledgeBlock = &CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', array(
620 'entity_id' => $id,
621 'entity_table' => 'civicrm_contribution_page',
622 ), array(
623 'entity_id' => $copy->id,
624 ));
625
626 $copyMembershipBlock = &CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', array(
627 'entity_id' => $id,
628 'entity_table' => 'civicrm_contribution_page',
629 ), array(
630 'entity_id' => $copy->id,
631 ));
632
633 $copyUFJoin = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', array(
634 'entity_id' => $id,
635 'entity_table' => 'civicrm_contribution_page',
636 ), array(
637 'entity_id' => $copy->id,
638 ));
639
640 $copyWidget = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', array(
641 'contribution_page_id' => $id,
642 ), array(
643 'contribution_page_id' => $copy->id,
644 ));
645
646 //copy price sets
647 CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_contribution_page', $id, $copy->id);
648
649 $copyTellFriend = &CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', array(
650 'entity_id' => $id,
651 'entity_table' => 'civicrm_contribution_page',
652 ), array(
653 'entity_id' => $copy->id,
654 ));
655
656 $copyPersonalCampaignPages = &CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', array(
657 'entity_id' => $id,
658 'entity_table' => 'civicrm_contribution_page',
659 ), array(
660 'entity_id' => $copy->id,
661 'target_entity_id' => $copy->id,
662 ));
663
664 $copyPremium = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', array(
665 'entity_id' => $id,
666 'entity_table' => 'civicrm_contribution_page',
667 ), array(
668 'entity_id' => $copy->id,
669 ));
670 $premiumQuery = "
671 SELECT id
672 FROM civicrm_premiums
673 WHERE entity_table = 'civicrm_contribution_page'
674 AND entity_id ={$id}";
675
676 $premiumDao = CRM_Core_DAO::executeQuery($premiumQuery, CRM_Core_DAO::$_nullArray);
677 while ($premiumDao->fetch()) {
678 if ($premiumDao->id) {
679 $copyPremiumProduct = & CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_PremiumsProduct', array(
680 'premiums_id' => $premiumDao->id,
681 ), array(
682 'premiums_id' => $copyPremium->id,
683 ));
684 }
685 }
686
687 $copy->save();
688
689 CRM_Utils_Hook::copy('ContributionPage', $copy);
690
691 return $copy;
692 }
693
694 /**
695 * Function to check if contribution page contains payment
696 * processor that supports recurring payment
697 *
698 * @param int $contributionPageId Contribution Page Id
699 *
700 * @return boolean true if payment processor supports recurring
701 * else false
702 *
703 * @access public
704 * @static
705 */
706 static function checkRecurPaymentProcessor($contributionPageId) {
707 //FIXME
708 $sql = "
709 SELECT pp.is_recur
710 FROM civicrm_contribution_page cp,
711 civicrm_payment_processor pp
712 WHERE cp.payment_processor = pp.id
713 AND cp.id = {$contributionPageId}
714 ";
715
716 if ($recurring = &CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray)) {
717 return TRUE;
718 }
719 return FALSE;
720 }
721
722 /**
723 * Function to get info for all sections enable/disable.
724 *
725 * @param array $contribPageIds
726 * @return array $info info regarding all sections.
727 * @access public
728 * @static
729 */
730 static function getSectionInfo($contribPageIds = array()) {
731 $info = array();
732 $whereClause = NULL;
733 if (is_array($contribPageIds) && !empty($contribPageIds)) {
734 $whereClause = 'WHERE civicrm_contribution_page.id IN ( ' . implode(', ', $contribPageIds) . ' )';
735 }
736
737 $sections = array(
738 'settings',
739 'amount',
740 'membership',
741 'custom',
742 'thankyou',
743 'friend',
744 'pcp',
745 'widget',
746 'premium',
747 );
748 $query = "
749 SELECT civicrm_contribution_page.id as id,
750 civicrm_contribution_page.financial_type_id as settings,
751 amount_block_is_active as amount,
752 civicrm_membership_block.id as membership,
753 civicrm_uf_join.id as custom,
754 civicrm_contribution_page.thankyou_title as thankyou,
755 civicrm_tell_friend.id as friend,
756 civicrm_pcp_block.id as pcp,
757 civicrm_contribution_widget.id as widget,
758 civicrm_premiums.id as premium
759 FROM civicrm_contribution_page
760 LEFT JOIN civicrm_membership_block ON ( civicrm_membership_block.entity_id = civicrm_contribution_page.id
761 AND civicrm_membership_block.entity_table = 'civicrm_contribution_page'
762 AND civicrm_membership_block.is_active = 1 )
763 LEFT JOIN civicrm_uf_join ON ( civicrm_uf_join.entity_id = civicrm_contribution_page.id
764 AND civicrm_uf_join.entity_table = 'civicrm_contribution_page'
765 AND module = 'CiviContribute'
766 AND civicrm_uf_join.is_active = 1 )
767 LEFT JOIN civicrm_tell_friend ON ( civicrm_tell_friend.entity_id = civicrm_contribution_page.id
768 AND civicrm_tell_friend.entity_table = 'civicrm_contribution_page'
769 AND civicrm_tell_friend.is_active = 1)
770 LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_contribution_page.id
771 AND civicrm_pcp_block.entity_table = 'civicrm_contribution_page'
772 AND civicrm_pcp_block.is_active = 1 )
773 LEFT JOIN civicrm_contribution_widget ON ( civicrm_contribution_widget.contribution_page_id = civicrm_contribution_page.id
774 AND civicrm_contribution_widget.is_active = 1 )
775 LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm_contribution_page.id
776 AND civicrm_premiums.entity_table = 'civicrm_contribution_page'
777 AND civicrm_premiums.premiums_active = 1 )
778 $whereClause";
779
780 $contributionPage = CRM_Core_DAO::executeQuery($query);
781 while ($contributionPage->fetch()) {
782 if (!isset($info[$contributionPage->id]) || !is_array($info[$contributionPage->id])) {
783 $info[$contributionPage->id] = array_fill_keys(array_values($sections), FALSE);
784 }
785 foreach ($sections as $section) {
786 if ($contributionPage->$section) {
787 $info[$contributionPage->id][$section] = TRUE;
788 }
789 }
790 }
791
792 return $info;
793 }
794
795 /**
796 * Get options for a given field.
797 * @see CRM_Core_DAO::buildOptions
798 *
799 * @param String $fieldName
800 * @param String $context: @see CRM_Core_DAO::buildOptionsContext
801 * @param Array $props: whatever is known about this dao object
802 *
803 * @return array|bool
804 */
805 public static function buildOptions($fieldName, $context = NULL, $props = array()) {
806 $params = array();
807 // Special logic for fields whose options depend on context or properties
808 switch ($fieldName) {
809 case 'financial_type_id':
810 // Fixme - this is going to ignore context, better to get conditions, add params, and call PseudoConstant::get
811 return CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
812 break;
813 }
814 return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
815 }
816
817 /**
818 * Get or Set multilingually affected honor params for processing module_data or setting default values.
819 *
820 * @param Array|String $params: Array when we need to format it according to language state or String as a json encode
821 * @param Boolean $setDefault: If yes then returns array to used for setting default value afterward
822 *
823 * @return array|string
824 */
825 public static function formatMultilingualHonorParams($params, $setDefault = FALSE) {
826 $config = CRM_Core_Config::singleton();
827 $sctJson = $sctJsonDecode = NULL;
828 $domain = new CRM_Core_DAO_Domain();
829 $domain->find(TRUE);
830
831 //When we are fetching the honor params respecting both multi and mono lingual state
832 //and setting it to default param of Contribution Page's Main and Setting form
833 if ($setDefault) {
834 $sctJsonDecode = json_decode($params);
835 $sctJsonDecode = (array) $sctJsonDecode->soft_credit;
836 if (!$domain->locales && !empty($sctJsonDecode['default'])) {
837 //monolingual state
838 $sctJsonDecode += (array) $sctJsonDecode['default'];
839 }
840 elseif (!empty($sctJsonDecode[$config->lcMessages])) {
841 //multilingual state
842 foreach ($sctJsonDecode[$config->lcMessages] as $column => $value) {
843 $sctJsonDecode[$column] = $value;
844 }
845 unset($sctJsonDecode[$config->lcMessages]);
846 }
847 return $sctJsonDecode;
848 }
849
850 //check and handle multilingual honoree params
851 if (!$domain->locales) {
852 //if in singlelingual state simply return the array format
853 $sctJson = json_encode(
854 array(
855 'soft_credit' => array(
856 'soft_credit_types' => $params['soft_credit_types'],
857 'default' => array(
858 'honor_block_title' => $params['honor_block_title'],
859 'honor_block_text' => $params['honor_block_text']
860 )
861 )
862 )
863 );
864 }
865 else {
866 //if in multilingual state then retrieve the module_data against this contribution and
867 //merge with earlier module_data json data to current so not to lose earlier multilingual module_data information
868 $sctJson = array(
869 'soft_credit' => array(
870 'soft_credit_types' => $params['soft_credit_types'],
871 $config->lcMessages => array (
872 'honor_block_title' => $params['honor_block_title'],
873 'honor_block_text' => $params['honor_block_text']
874 )
875 )
876 );
877
878 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
879 $ufJoinDAO->module = 'soft_credit';
880 $ufJoinDAO->entity_id = $params['id'];
881 $ufJoinDAO->find(TRUE);
882 $jsonData = json_decode($ufJoinDAO->module_data);
883 if ($jsonData) {
884 $sctJson['soft_credit'] = array_merge((array)$jsonData->soft_credit, $sctJson['soft_credit']);
885 }
886 $sctJson = json_encode($sctJson);
887 }
888 return $sctJson;
889 }
890
891
892 /**
893 * helper to determine if the page supports separate membership payments
894 * @param integer id form id
895 *
896 * @return bool isSeparateMembershipPayment
897 */
898 static function getIsMembershipPayment($id) {
899 $membershipBlocks = civicrm_api3('membership_block', 'get', array('entity_table' => 'civicrm_contribution_page', 'entity_id' => $id, 'sequential' => TRUE));
900 if(!$membershipBlocks['count']) {
901 return FALSE;
902 }
903 return $membershipBlocks['values'][0]['is_separate_payment'];
904 }
905 }
906