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