CRM-15844
[civicrm-core.git] / CRM / Contribute / BAO / ContributionPage.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class contains Contribution Page related functions.
38 */
39 class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_ContributionPage {
40
41 /**
42 * Takes an associative array and creates a contribution page object
43 *
44 * @param array $params
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 (count($taxAmt) > 0 && (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 * @return void
476 */
477 public static function recurringNotify($type, $contactID, $pageID, $recur, $autoRenewMembership = FALSE) {
478 $value = array();
479 if ($pageID) {
480 CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', $pageID, $value, array(
481 'title',
482 'is_email_receipt',
483 'receipt_from_name',
484 'receipt_from_email',
485 'cc_receipt',
486 'bcc_receipt',
487 ));
488 }
489
490 $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]);
491 $isOfflineRecur = FALSE;
492 if (!$pageID && $recur->id) {
493 $isOfflineRecur = TRUE;
494 }
495 if ($isEmailReceipt || $isOfflineRecur) {
496 if ($pageID) {
497 $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>';
498
499 $receiptFromName = $value[$pageID]['receipt_from_name'];
500 $receiptFromEmail = $value[$pageID]['receipt_from_email'];
501 }
502 else {
503 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
504 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
505 $receiptFromName = $domainValues[0];
506 $receiptFromEmail = $domainValues[1];
507 }
508
509 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE);
510 $templatesParams = array(
511 'groupName' => 'msg_tpl_workflow_contribution',
512 'valueName' => 'contribution_recurring_notify',
513 'contactId' => $contactID,
514 'tplParams' => array(
515 'recur_frequency_interval' => $recur->frequency_interval,
516 'recur_frequency_unit' => $recur->frequency_unit,
517 'recur_installments' => $recur->installments,
518 'recur_start_date' => $recur->start_date,
519 'recur_end_date' => $recur->end_date,
520 'recur_amount' => $recur->amount,
521 'recur_txnType' => $type,
522 'displayName' => $displayName,
523 'receipt_from_name' => $receiptFromName,
524 'receipt_from_email' => $receiptFromEmail,
525 'auto_renew_membership' => $autoRenewMembership,
526 ),
527 'from' => $receiptFrom,
528 'toName' => $displayName,
529 'toEmail' => $email,
530 );
531 //CRM-13811
532 if ($pageID) {
533 $templatesParams['cc'] = CRM_Utils_Array::value('cc_receipt', $value[$pageID]);
534 $templatesParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $value[$pageID]);
535 }
536 if ($recur->id) {
537 // in some cases its just recurringNotify() thats called for the first time and these urls don't get set.
538 // like in PaypalPro, & therefore we set it here additionally.
539 $template = CRM_Core_Smarty::singleton();
540 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recur->id, 'recur', 'obj');
541 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur');
542 $template->assign('cancelSubscriptionUrl', $url);
543
544 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'billing');
545 $template->assign('updateSubscriptionBillingUrl', $url);
546
547 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'update');
548 $template->assign('updateSubscriptionUrl', $url);
549 }
550
551 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($templatesParams);
552
553 if ($sent) {
554 CRM_Core_Error::debug_log_message('Success: mail sent for recurring notification.');
555 }
556 else {
557 CRM_Core_Error::debug_log_message('Failure: mail not sent for recurring notification.');
558 }
559 }
560 }
561
562 /**
563 * Add the custom fields for contribution page (ie profile)
564 *
565 * @param int $gid
566 * Uf group id.
567 * @param string $name
568 * @param int $cid
569 * Contact id.
570 * @param $template
571 * @param array $params
572 * Params to build component whereclause.
573 *
574 * @param null $fieldTypes
575 *
576 * @return void
577 */
578 public static function buildCustomDisplay($gid, $name, $cid, &$template, &$params, $fieldTypes = NULL) {
579 if ($gid) {
580 if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $cid)) {
581 $values = array();
582 $groupTitle = NULL;
583 $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL);
584 foreach ($fields as $k => $v) {
585 if (!$groupTitle) {
586 $groupTitle = $v["groupTitle"];
587 }
588 // suppress all file fields from display and formatting fields
589 if (
590 CRM_Utils_Array::value('data_type', $v, '') == 'File' ||
591 CRM_Utils_Array::value('name', $v, '') == 'image_URL' ||
592 CRM_Utils_Array::value('field_type', $v) == 'Formatting'
593 ) {
594 unset($fields[$k]);
595 }
596
597 if (!empty($fieldTypes) && (!in_array($v['field_type'], $fieldTypes))) {
598 unset($fields[$k]);
599 }
600 }
601
602 if ($groupTitle) {
603 $template->assign($name . "_grouptitle", $groupTitle);
604 }
605
606 CRM_Core_BAO_UFGroup::getValues($cid, $fields, $values, FALSE, $params);
607
608 if (count($values)) {
609 $template->assign($name, $values);
610 }
611 }
612 }
613 }
614
615 /**
616 * make a copy of a contribution page, including
617 * all the blocks in the page
618 *
619 * @param int $id
620 * The contribution page id to copy.
621 *
622 * @return CRM_Contribute_DAO_ContributionPage
623 */
624 public static function copy($id) {
625 $fieldsFix = array(
626 'prefix' => array(
627 'title' => ts('Copy of') . ' ',
628 ),
629 );
630 $copy = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', array(
631 'id' => $id,
632 ), NULL, $fieldsFix);
633
634 //copying all the blocks pertaining to the contribution page
635 $copyPledgeBlock = &CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', array(
636 'entity_id' => $id,
637 'entity_table' => 'civicrm_contribution_page',
638 ), array(
639 'entity_id' => $copy->id,
640 ));
641
642 $copyMembershipBlock = &CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', array(
643 'entity_id' => $id,
644 'entity_table' => 'civicrm_contribution_page',
645 ), array(
646 'entity_id' => $copy->id,
647 ));
648
649 $copyUFJoin = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', array(
650 'entity_id' => $id,
651 'entity_table' => 'civicrm_contribution_page',
652 ), array(
653 'entity_id' => $copy->id,
654 ));
655
656 $copyWidget = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', array(
657 'contribution_page_id' => $id,
658 ), array(
659 'contribution_page_id' => $copy->id,
660 ));
661
662 //copy price sets
663 CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_contribution_page', $id, $copy->id);
664
665 $copyTellFriend = &CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', array(
666 'entity_id' => $id,
667 'entity_table' => 'civicrm_contribution_page',
668 ), array(
669 'entity_id' => $copy->id,
670 ));
671
672 $copyPersonalCampaignPages = &CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', array(
673 'entity_id' => $id,
674 'entity_table' => 'civicrm_contribution_page',
675 ), array(
676 '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 $config = CRM_Core_Config::singleton();
842 $sctJson = $sctJsonDecode = NULL;
843 $domain = new CRM_Core_DAO_Domain();
844 $domain->find(TRUE);
845
846 //When we are fetching the honor params respecting both multi and mono lingual state
847 //and setting it to default param of Contribution Page's Main and Setting form
848 if ($setDefault) {
849 $sctJsonDecode = json_decode($params);
850 $sctJsonDecode = (array) $sctJsonDecode->soft_credit;
851 if (!$domain->locales && !empty($sctJsonDecode['default'])) {
852 //monolingual state
853 $sctJsonDecode += (array) $sctJsonDecode['default'];
854 }
855 elseif (!empty($sctJsonDecode[$config->lcMessages])) {
856 //multilingual state
857 foreach ($sctJsonDecode[$config->lcMessages] as $column => $value) {
858 $sctJsonDecode[$column] = $value;
859 }
860 unset($sctJsonDecode[$config->lcMessages]);
861 }
862 return $sctJsonDecode;
863 }
864
865 //check and handle multilingual honoree params
866 if (!$domain->locales) {
867 //if in singlelingual state simply return the array format
868 $sctJson = json_encode(
869 array(
870 'soft_credit' => array(
871 'soft_credit_types' => $params['soft_credit_types'],
872 'default' => array(
873 'honor_block_title' => $params['honor_block_title'],
874 'honor_block_text' => $params['honor_block_text'],
875 ),
876 ),
877 )
878 );
879 }
880 else {
881 //if in multilingual state then retrieve the module_data against this contribution and
882 //merge with earlier module_data json data to current so not to lose earlier multilingual module_data information
883 $sctJson = array(
884 'soft_credit' => array(
885 'soft_credit_types' => $params['soft_credit_types'],
886 $config->lcMessages => array(
887 'honor_block_title' => $params['honor_block_title'],
888 'honor_block_text' => $params['honor_block_text'],
889 ),
890 ),
891 );
892
893 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
894 $ufJoinDAO->module = 'soft_credit';
895 $ufJoinDAO->entity_id = $params['id'];
896 $ufJoinDAO->find(TRUE);
897 $jsonData = json_decode($ufJoinDAO->module_data);
898 if ($jsonData) {
899 $sctJson['soft_credit'] = array_merge((array) $jsonData->soft_credit, $sctJson['soft_credit']);
900 }
901 $sctJson = json_encode($sctJson);
902 }
903 return $sctJson;
904 }
905
906 /**
907 * Generate html for pdf in confirmation receipt email attachment
908 * @param int $contributionId
909 * Contribution Page Id.
910 * @param int $userID
911 * Contact id for contributor.
912 * @return array
913 */
914 public static function addInvoicePdfToEmail($contributionId, $userID) {
915 $contributionID = array($contributionId);
916 $contactId = array($userID);
917 $pdfParams = array(
918 'output' => 'pdf_invoice',
919 'forPage' => 'confirmpage',
920 );
921 $pdfHtml = CRM_Contribute_Form_Task_Invoice::printPDF($contributionID,
922 $pdfParams, $contactId, CRM_Core_DAO::$_nullObject);
923 return $pdfHtml;
924 }
925
926 /**
927 * Helper to determine if the page supports separate membership payments
928 * @param int $id form id
929 *
930 * @return bool
931 * isSeparateMembershipPayment
932 */
933 public static function getIsMembershipPayment($id) {
934 $membershipBlocks = civicrm_api3('membership_block', 'get', array(
935 'entity_table' => 'civicrm_contribution_page',
936 'entity_id' => $id,
937 'sequential' => TRUE,
938 ));
939 if (!$membershipBlocks['count']) {
940 return FALSE;
941 }
942 return $membershipBlocks['values'][0]['is_separate_payment'];
943 }
944 }