Merge pull request #6332 from colemanw/CRM-11856
[civicrm-core.git] / CRM / Contribute / BAO / ContributionPage.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class contains Contribution Page related functions.
38 */
39class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_ContributionPage {
40
41 /**
fe482240 42 * Takes an associative array and creates a contribution page object.
6a488035 43 *
014c4014
TO
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
6a488035 46 *
16b10e64 47 * @return CRM_Contribute_DAO_ContributionPage
6a488035
TO
48 */
49 public static function &create(&$params) {
157b21d8 50 $financialTypeId = NULL;
a7488080 51 if (!empty($params['id']) && !CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $params['id'], NULL, 1)) {
157b21d8
PN
52 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $params['id'], 'financial_type_id');
53 }
d0a0de8d
EM
54 $hook = empty($params['id']) ? 'create' : 'edit';
55 CRM_Utils_Hook::pre($hook, 'ContributionPage', CRM_Utils_Array::value('id', $params), $params);
6a488035
TO
56 $dao = new CRM_Contribute_DAO_ContributionPage();
57 $dao->copyValues($params);
58 $dao->save();
8cc574cf 59 if ($financialTypeId && !empty($params['financial_type_id']) && $financialTypeId != $params['financial_type_id']) {
e1c77163 60 CRM_Price_BAO_PriceFieldValue::updateFinancialType($params['id'], 'civicrm_contribution_page', $params['financial_type_id']);
157b21d8 61 }
d0a0de8d 62 CRM_Utils_Hook::post($hook, 'ContributionPage', $dao->id, $dao);
6a488035
TO
63 return $dao;
64 }
65
66 /**
fe482240 67 * Update the is_active flag in the db.
6a488035 68 *
014c4014
TO
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.
6a488035 73 *
a6c01b45
CW
74 * @return Object
75 * DAO object on success, null otherwise
6a488035 76 */
00be9182 77 public static function setIsActive($id, $is_active) {
6a488035
TO
78 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'is_active', $is_active);
79 }
80
186c9c17 81 /**
100fef9d 82 * @param int $id
186c9c17
EM
83 * @param $values
84 */
00be9182 85 public static function setValues($id, &$values) {
6a488035
TO
86 $params = array(
87 'id' => $id,
88 );
89
90 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $values);
8ef12e64 91
6a488035
TO
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 // }
874c9be7 110 }
6a488035
TO
111
112 /**
fe482240 113 * Send the emails.
6a488035 114 *
014c4014
TO
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.
6a488035 123 *
e2133660
EM
124 * @param null $fieldTypes
125 *
6a488035 126 * @return void
6a488035 127 */
00be9182 128 public static function sendMail($contactID, $values, $isTest = FALSE, $returnMessageText = FALSE, $fieldTypes = NULL) {
6a488035
TO
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']);
8cc574cf 133 if ($preProfileType == 'Membership' && !empty($values['membership_id'])) {
6a488035
TO
134 $params['custom_pre_id'] = array(
135 array(
136 'member_id',
137 '=',
138 $values['membership_id'],
139 0,
140 0,
141 ),
142 );
143 }
8cc574cf 144 elseif ($preProfileType == 'Contribution' && !empty($values['contribution_id'])) {
6a488035
TO
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']);
8cc574cf 161 if ($postProfileType == 'Membership' && !empty($values['membership_id'])) {
6a488035
TO
162 $params['custom_post_id'] = array(
163 array(
164 'member_id',
165 '=',
166 $values['membership_id'],
167 0,
168 0,
169 ),
170 );
171 }
8cc574cf 172 elseif ($postProfileType == 'Contribution' && !empty($values['contribution_id'])) {
6a488035
TO
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
a7488080
CW
187 if (!empty($values['is_for_organization'])) {
188 if (!empty($values['membership_id'])) {
6a488035
TO
189 $params['onbehalf_profile'] = array(
190 array(
191 'member_id',
192 '=',
193 $values['membership_id'],
194 0,
195 0,
196 ),
197 );
198 }
a7488080 199 elseif (!empty($values['contribution_id'])) {
6a488035
TO
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 }
8ef12e64 232
6a488035
TO
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) {
481a74f4 239 $val = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $contactID, CRM_Utils_Array::value($key, $params), TRUE);
6a488035
TO
240 CRM_Core_BAO_UFGroup::commonSendMail($contactID, $val);
241 }
242 }
243 }
244 }
245
8cc574cf 246 if (!empty($values['is_email_receipt']) || !empty($values['onbehalf_dupe_alert']) ||
6a488035
TO
247 $returnMessageText
248 ) {
249 $template = CRM_Core_Smarty::singleton();
250
251 // get the billing location type
252 if (!array_key_exists('related_contact', $values)) {
180409a4 253 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
6a488035
TO
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)) {
a7488080 282 if (!empty($values['related_contact'])) {
6a488035 283 $preProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($preID);
b8950542 284 //@todo - following line should not refer to undefined $postProfileTypes? figure out way to test
6a488035
TO
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)) {
a7488080 294 if (!empty($values['related_contact'])) {
6a488035
TO
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 }
1421174e 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 }
6a488035
TO
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),
6a488035
TO
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),
8b067691 329 'thankyou_title' => CRM_Utils_Array::value('thankyou_title', $values),
6a488035
TO
330 );
331
91bb24a7 332 if ($contributionTypeId = CRM_Utils_Array::value('financial_type_id', $values)) {
6a488035 333 $tplParams['contributionTypeId'] = $contributionTypeId;
91bb24a7 334 $tplParams['contributionTypeName'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
335 $contributionTypeId);
6a488035
TO
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
fdd2a6db 357 //@todo - this is the only place in this function where $values is altered - but I can't find any evidence it is used
0d8afee2 358 $values['cc_receipt'] = !empty($values['cc_receipt']) ? ($values['cc_receipt'] . ',' . $ccMailId) : $ccMailId;
6a488035
TO
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);
91bb24a7 372 $profileId = $OnBehalfProfile[0];
373 $userID = $contactID;
6a488035
TO
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(
12dac866 379 'groupName' => !empty($values['isMembership']) ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
380 'valueName' => !empty($values['isMembership']) ? 'membership_online_receipt' : 'contribution_online_receipt',
6a488035
TO
381 'contactId' => $contactID,
382 'tplParams' => $tplParams,
383 'isTest' => $isTest,
384 'PDFFilename' => 'receipt.pdf',
385 );
386
387 if ($returnMessageText) {
c6327d7d 388 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
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);
9161952c 403 //send email with pdf invoice
481a74f4 404 $template = CRM_Core_Smarty::singleton();
9161952c 405 $taxAmt = $template->get_template_vars('dataArray');
bb600eae 406 $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
9161952c 407 $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue);
121bf42c 408 if (isset($invoicing) && isset($prefixValue['is_email_pdf'])) {
874c9be7 409 $sendTemplateParams['isEmailPdf'] = TRUE;
9161952c
PD
410 $sendTemplateParams['contributionId'] = $values['contribution_id'];
411 }
c6327d7d 412 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
413 }
414
415 // send duplicate alert, if dupe match found during on-behalf-of processing.
a7488080 416 if (!empty($values['onbehalf_dupe_alert'])) {
6a488035
TO
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
c6327d7d 428 CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
6a488035
TO
429 }
430 }
431 }
8ef12e64 432
186c9c17 433 /**
84f0ed41 434 * Send the emails for Recurring Contribution Notification.
6a488035 435 *
014c4014
TO
436 * @param string $type
437 * TxnType.
438 * @param int $contactID
439 * Contact id for contributor.
440 * @param int $pageID
441 * Contribution page id.
442 * @param object $recur
443 * Object of recurring contribution table.
2a6da8d7 444 * @param bool|object $autoRenewMembership is it a auto renew membership.
6a488035 445 */
00be9182 446 public static function recurringNotify($type, $contactID, $pageID, $recur, $autoRenewMembership = FALSE) {
6a488035
TO
447 $value = array();
448 if ($pageID) {
449 CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', $pageID, $value, array(
91bb24a7 450 'title',
451 'is_email_receipt',
452 'receipt_from_name',
453 'receipt_from_email',
454 'cc_receipt',
455 'bcc_receipt',
456 ));
ab30e033 457 $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]);
6a488035 458 }
ab30e033
EM
459 elseif ($recur->id) {
460 // This means we are coming from back-office - ie. no page ID, but recurring.
461 // Ideally this information would be passed into the function clearly rather than guessing by convention.
462 $isEmailReceipt = TRUE;
6a488035 463 }
ab30e033
EM
464
465 if ($isEmailReceipt) {
6a488035
TO
466 if ($pageID) {
467 $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>';
468
469 $receiptFromName = $value[$pageID]['receipt_from_name'];
470 $receiptFromEmail = $value[$pageID]['receipt_from_email'];
471 }
472 else {
91bb24a7 473 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
474 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
475 $receiptFromName = $domainValues[0];
6a488035
TO
476 $receiptFromEmail = $domainValues[1];
477 }
478
479 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE);
480 $templatesParams = array(
481 'groupName' => 'msg_tpl_workflow_contribution',
482 'valueName' => 'contribution_recurring_notify',
483 'contactId' => $contactID,
484 'tplParams' => array(
485 'recur_frequency_interval' => $recur->frequency_interval,
486 'recur_frequency_unit' => $recur->frequency_unit,
487 'recur_installments' => $recur->installments,
488 'recur_start_date' => $recur->start_date,
489 'recur_end_date' => $recur->end_date,
490 'recur_amount' => $recur->amount,
491 'recur_txnType' => $type,
492 'displayName' => $displayName,
493 'receipt_from_name' => $receiptFromName,
494 'receipt_from_email' => $receiptFromEmail,
495 'auto_renew_membership' => $autoRenewMembership,
496 ),
497 'from' => $receiptFrom,
498 'toName' => $displayName,
499 'toEmail' => $email,
500 );
1421174e 501 //CRM-13811
039134ba 502 if ($pageID) {
503 $templatesParams['cc'] = CRM_Utils_Array::value('cc_receipt', $value[$pageID]);
504 $templatesParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $value[$pageID]);
505 }
6a488035
TO
506 if ($recur->id) {
507 // in some cases its just recurringNotify() thats called for the first time and these urls don't get set.
508 // like in PaypalPro, & therefore we set it here additionally.
91bb24a7 509 $template = CRM_Core_Smarty::singleton();
6a488035 510 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recur->id, 'recur', 'obj');
91bb24a7 511 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur');
6a488035
TO
512 $template->assign('cancelSubscriptionUrl', $url);
513
514 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'billing');
515 $template->assign('updateSubscriptionBillingUrl', $url);
516
517 $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'update');
518 $template->assign('updateSubscriptionUrl', $url);
519 }
520
84f0ed41 521 list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($templatesParams);
6a488035
TO
522
523 if ($sent) {
524 CRM_Core_Error::debug_log_message('Success: mail sent for recurring notification.');
525 }
526 else {
527 CRM_Core_Error::debug_log_message('Failure: mail not sent for recurring notification.');
528 }
529 }
530 }
531
532 /**
100fef9d 533 * Add the custom fields for contribution page (ie profile)
6a488035 534 *
014c4014
TO
535 * @param int $gid
536 * Uf group id.
6a488035 537 * @param string $name
014c4014
TO
538 * @param int $cid
539 * Contact id.
2a6da8d7 540 * @param $template
014c4014
TO
541 * @param array $params
542 * Params to build component whereclause.
2a6da8d7
EM
543 *
544 * @param null $fieldTypes
6a488035
TO
545 *
546 * @return void
6a488035 547 */
20222c10 548 public static function buildCustomDisplay($gid, $name, $cid, &$template, &$params, $fieldTypes = NULL) {
6a488035
TO
549 if ($gid) {
550 if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $cid)) {
91bb24a7 551 $values = array();
6a488035 552 $groupTitle = NULL;
91bb24a7 553 $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL);
6a488035
TO
554 foreach ($fields as $k => $v) {
555 if (!$groupTitle) {
556 $groupTitle = $v["groupTitle"];
557 }
558 // suppress all file fields from display and formatting fields
559 if (
560 CRM_Utils_Array::value('data_type', $v, '') == 'File' ||
561 CRM_Utils_Array::value('name', $v, '') == 'image_URL' ||
562 CRM_Utils_Array::value('field_type', $v) == 'Formatting'
563 ) {
564 unset($fields[$k]);
565 }
566
567 if (!empty($fieldTypes) && (!in_array($v['field_type'], $fieldTypes))) {
568 unset($fields[$k]);
569 }
570 }
571
572 if ($groupTitle) {
573 $template->assign($name . "_grouptitle", $groupTitle);
574 }
575
576 CRM_Core_BAO_UFGroup::getValues($cid, $fields, $values, FALSE, $params);
577
578 if (count($values)) {
579 $template->assign($name, $values);
580 }
581 }
582 }
583 }
584
585 /**
dc195289 586 * make a copy of a contribution page, including
6a488035
TO
587 * all the blocks in the page
588 *
014c4014
TO
589 * @param int $id
590 * The contribution page id to copy.
6a488035 591 *
72b3a70c 592 * @return CRM_Contribute_DAO_ContributionPage
6a488035 593 */
00be9182 594 public static function copy($id) {
6a488035
TO
595 $fieldsFix = array(
596 'prefix' => array(
597 'title' => ts('Copy of') . ' ',
598 ),
599 );
600 $copy = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', array(
353ffa53
TO
601 'id' => $id,
602 ), NULL, $fieldsFix);
6a488035
TO
603
604 //copying all the blocks pertaining to the contribution page
605 $copyPledgeBlock = &CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', array(
353ffa53
TO
606 'entity_id' => $id,
607 'entity_table' => 'civicrm_contribution_page',
608 ), array(
609 'entity_id' => $copy->id,
610 ));
6a488035
TO
611
612 $copyMembershipBlock = &CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', array(
353ffa53
TO
613 'entity_id' => $id,
614 'entity_table' => 'civicrm_contribution_page',
615 ), array(
616 'entity_id' => $copy->id,
617 ));
6a488035
TO
618
619 $copyUFJoin = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', array(
353ffa53
TO
620 'entity_id' => $id,
621 'entity_table' => 'civicrm_contribution_page',
622 ), array(
623 'entity_id' => $copy->id,
624 ));
6a488035
TO
625
626 $copyWidget = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', array(
353ffa53
TO
627 'contribution_page_id' => $id,
628 ), array(
629 'contribution_page_id' => $copy->id,
630 ));
6a488035 631
c6914066 632 //copy price sets
9da8dc8c 633 CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_contribution_page', $id, $copy->id);
c6914066 634
6a488035 635 $copyTellFriend = &CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', array(
353ffa53
TO
636 'entity_id' => $id,
637 'entity_table' => 'civicrm_contribution_page',
638 ), array(
639 'entity_id' => $copy->id,
640 ));
6a488035
TO
641
642 $copyPersonalCampaignPages = &CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', array(
353ffa53
TO
643 'entity_id' => $id,
644 'entity_table' => 'civicrm_contribution_page',
645 ), array(
646 'entity_id' => $copy->id,
b7ff0a0a 647 'target_entity_id' => $copy->id,
353ffa53 648 ));
6a488035
TO
649
650 $copyPremium = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', array(
353ffa53
TO
651 'entity_id' => $id,
652 'entity_table' => 'civicrm_contribution_page',
653 ), array(
654 'entity_id' => $copy->id,
655 ));
6a488035
TO
656 $premiumQuery = "
657SELECT id
658FROM civicrm_premiums
659WHERE entity_table = 'civicrm_contribution_page'
660 AND entity_id ={$id}";
661
662 $premiumDao = CRM_Core_DAO::executeQuery($premiumQuery, CRM_Core_DAO::$_nullArray);
663 while ($premiumDao->fetch()) {
664 if ($premiumDao->id) {
353ffa53 665 $copyPremiumProduct = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_PremiumsProduct', array(
91bb24a7 666 'premiums_id' => $premiumDao->id,
667 ), array(
668 'premiums_id' => $copyPremium->id,
669 ));
6a488035
TO
670 }
671 }
672
673 $copy->save();
674
675 CRM_Utils_Hook::copy('ContributionPage', $copy);
676
677 return $copy;
678 }
679
680 /**
100fef9d 681 * Check if contribution page contains payment
6a488035
TO
682 * processor that supports recurring payment
683 *
014c4014
TO
684 * @param int $contributionPageId
685 * Contribution Page Id.
6a488035 686 *
79d7553f 687 * @return bool
a6c01b45 688 * true if payment processor supports recurring
16b10e64 689 * else false
6a488035 690 *
6a488035 691 */
00be9182 692 public static function checkRecurPaymentProcessor($contributionPageId) {
6a488035
TO
693 //FIXME
694 $sql = "
695 SELECT pp.is_recur
696 FROM civicrm_contribution_page cp,
697 civicrm_payment_processor pp
698 WHERE cp.payment_processor = pp.id
699 AND cp.id = {$contributionPageId}
700";
701
702 if ($recurring = &CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray)) {
703 return TRUE;
704 }
705 return FALSE;
706 }
707
708 /**
100fef9d 709 * Get info for all sections enable/disable.
6a488035 710 *
2a6da8d7 711 * @param array $contribPageIds
a6c01b45
CW
712 * @return array
713 * info regarding all sections.
6a488035 714 */
00be9182 715 public static function getSectionInfo($contribPageIds = array()) {
6a488035
TO
716 $info = array();
717 $whereClause = NULL;
718 if (is_array($contribPageIds) && !empty($contribPageIds)) {
719 $whereClause = 'WHERE civicrm_contribution_page.id IN ( ' . implode(', ', $contribPageIds) . ' )';
720 }
721
722 $sections = array(
723 'settings',
724 'amount',
725 'membership',
726 'custom',
727 'thankyou',
728 'friend',
729 'pcp',
730 'widget',
731 'premium',
732 );
733 $query = "
734 SELECT civicrm_contribution_page.id as id,
8ef12e64 735 civicrm_contribution_page.financial_type_id as settings,
6a488035
TO
736 amount_block_is_active as amount,
737 civicrm_membership_block.id as membership,
738 civicrm_uf_join.id as custom,
739 civicrm_contribution_page.thankyou_title as thankyou,
740 civicrm_tell_friend.id as friend,
741 civicrm_pcp_block.id as pcp,
742 civicrm_contribution_widget.id as widget,
743 civicrm_premiums.id as premium
744 FROM civicrm_contribution_page
745LEFT JOIN civicrm_membership_block ON ( civicrm_membership_block.entity_id = civicrm_contribution_page.id
746 AND civicrm_membership_block.entity_table = 'civicrm_contribution_page'
747 AND civicrm_membership_block.is_active = 1 )
748LEFT JOIN civicrm_uf_join ON ( civicrm_uf_join.entity_id = civicrm_contribution_page.id
749 AND civicrm_uf_join.entity_table = 'civicrm_contribution_page'
750 AND module = 'CiviContribute'
751 AND civicrm_uf_join.is_active = 1 )
752LEFT JOIN civicrm_tell_friend ON ( civicrm_tell_friend.entity_id = civicrm_contribution_page.id
753 AND civicrm_tell_friend.entity_table = 'civicrm_contribution_page'
754 AND civicrm_tell_friend.is_active = 1)
755LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_contribution_page.id
756 AND civicrm_pcp_block.entity_table = 'civicrm_contribution_page'
757 AND civicrm_pcp_block.is_active = 1 )
758LEFT JOIN civicrm_contribution_widget ON ( civicrm_contribution_widget.contribution_page_id = civicrm_contribution_page.id
759 AND civicrm_contribution_widget.is_active = 1 )
760LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm_contribution_page.id
761 AND civicrm_premiums.entity_table = 'civicrm_contribution_page'
762 AND civicrm_premiums.premiums_active = 1 )
763 $whereClause";
764
765 $contributionPage = CRM_Core_DAO::executeQuery($query);
766 while ($contributionPage->fetch()) {
767 if (!isset($info[$contributionPage->id]) || !is_array($info[$contributionPage->id])) {
768 $info[$contributionPage->id] = array_fill_keys(array_values($sections), FALSE);
769 }
770 foreach ($sections as $section) {
771 if ($contributionPage->$section) {
772 $info[$contributionPage->id][$section] = TRUE;
773 }
774 }
775 }
776
777 return $info;
778 }
e1462487
CW
779
780 /**
781 * Get options for a given field.
782 * @see CRM_Core_DAO::buildOptions
783 *
014c4014 784 * @param string $fieldName
353ffa53
TO
785 * @param string $context : @see CRM_Core_DAO::buildOptionsContext
786 * @param array $props : whatever is known about this dao object
e1462487
CW
787 *
788 * @return array|bool
789 */
790 public static function buildOptions($fieldName, $context = NULL, $props = array()) {
791 $params = array();
792 // Special logic for fields whose options depend on context or properties
793 switch ($fieldName) {
794 case 'financial_type_id':
795 // Fixme - this is going to ignore context, better to get conditions, add params, and call PseudoConstant::get
796 return CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
874c9be7 797
79d7553f 798 break;
e1462487
CW
799 }
800 return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
801 }
ba60f73e 802
803 /**
804 * Get or Set multilingually affected honor params for processing module_data or setting default values.
805 *
353ffa53
TO
806 * @param string $params :
807 * @param bool $setDefault : If yes then returns array to used for setting default value afterward
ba60f73e 808 *
809 * @return array|string
810 */
811 public static function formatMultilingualHonorParams($params, $setDefault = FALSE) {
812 $config = CRM_Core_Config::singleton();
813 $sctJson = $sctJsonDecode = NULL;
814 $domain = new CRM_Core_DAO_Domain();
815 $domain->find(TRUE);
816
12792976 817 //When we are fetching the honor params respecting both multi and mono lingual state
ba60f73e 818 //and setting it to default param of Contribution Page's Main and Setting form
819 if ($setDefault) {
820 $sctJsonDecode = json_decode($params);
821 $sctJsonDecode = (array) $sctJsonDecode->soft_credit;
12792976 822 if (!$domain->locales && !empty($sctJsonDecode['default'])) {
823 //monolingual state
824 $sctJsonDecode += (array) $sctJsonDecode['default'];
825 }
826 elseif (!empty($sctJsonDecode[$config->lcMessages])) {
827 //multilingual state
ba60f73e 828 foreach ($sctJsonDecode[$config->lcMessages] as $column => $value) {
829 $sctJsonDecode[$column] = $value;
830 }
831 unset($sctJsonDecode[$config->lcMessages]);
832 }
833 return $sctJsonDecode;
834 }
835
836 //check and handle multilingual honoree params
837 if (!$domain->locales) {
838 //if in singlelingual state simply return the array format
839 $sctJson = json_encode(
840 array(
841 'soft_credit' => array(
842 'soft_credit_types' => $params['soft_credit_types'],
12792976 843 'default' => array(
844 'honor_block_title' => $params['honor_block_title'],
21dfd5f5
TO
845 'honor_block_text' => $params['honor_block_text'],
846 ),
847 ),
ba60f73e 848 )
849 );
850 }
851 else {
852 //if in multilingual state then retrieve the module_data against this contribution and
853 //merge with earlier module_data json data to current so not to lose earlier multilingual module_data information
874c9be7 854 $sctJson = array(
ba60f73e 855 'soft_credit' => array(
856 'soft_credit_types' => $params['soft_credit_types'],
874c9be7 857 $config->lcMessages => array(
ba60f73e 858 'honor_block_title' => $params['honor_block_title'],
21dfd5f5
TO
859 'honor_block_text' => $params['honor_block_text'],
860 ),
861 ),
ba60f73e 862 );
863
864 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
865 $ufJoinDAO->module = 'soft_credit';
866 $ufJoinDAO->entity_id = $params['id'];
867 $ufJoinDAO->find(TRUE);
868 $jsonData = json_decode($ufJoinDAO->module_data);
869 if ($jsonData) {
874c9be7 870 $sctJson['soft_credit'] = array_merge((array) $jsonData->soft_credit, $sctJson['soft_credit']);
ba60f73e 871 }
872 $sctJson = json_encode($sctJson);
873 }
874 return $sctJson;
875 }
9161952c 876
874c9be7 877 /**
fe482240 878 * Generate html for pdf in confirmation receipt email attachment.
014c4014
TO
879 * @param int $contributionId
880 * Contribution Page Id.
881 * @param int $userID
882 * Contact id for contributor.
a6c01b45 883 * @return array
9161952c 884 */
00be9182 885 public static function addInvoicePdfToEmail($contributionId, $userID) {
9161952c
PD
886 $contributionID = array($contributionId);
887 $contactId = array($userID);
888 $pdfParams = array(
889 'output' => 'pdf_invoice',
21dfd5f5 890 'forPage' => 'confirmpage',
9161952c 891 );
62179e22 892 $pdfHtml = CRM_Contribute_Form_Task_Invoice::printPDF($contributionID,
893 $pdfParams, $contactId, CRM_Core_DAO::$_nullObject);
9161952c
PD
894 return $pdfHtml;
895 }
f64a217a
EM
896
897 /**
7fe37828
EM
898 * Helper to determine if the page supports separate membership payments.
899 *
79d7553f 900 * @param int $id form id
f64a217a 901 *
a6c01b45
CW
902 * @return bool
903 * isSeparateMembershipPayment
f64a217a 904 */
00be9182 905 public static function getIsMembershipPayment($id) {
353ffa53
TO
906 $membershipBlocks = civicrm_api3('membership_block', 'get', array(
907 'entity_table' => 'civicrm_contribution_page',
908 'entity_id' => $id,
79d7553f 909 'sequential' => TRUE,
353ffa53 910 ));
22e263ad 911 if (!$membershipBlocks['count']) {
f64a217a
EM
912 return FALSE;
913 }
914 return $membershipBlocks['values'][0]['is_separate_payment'];
915 }
96025800 916
6a488035 917}