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