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