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